Skip to content

Commit 0a6ab0f

Browse files
author
qiancheng.xq
committed
integrate order request and execution with db
1 parent 3fb3e4a commit 0a6ab0f

26 files changed

+1573
-278
lines changed

log4j.xml

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424
</layout>
2525
</appender>
2626

27-
<logger name="com.lmax.api.test" additivity="false">
27+
<logger name="com.lmax.trade" additivity="false">
2828
<level value="DEBUG"/>
2929
<appender-ref ref="consoleAppender"/>
30-
<appender-ref ref="activitiesAppender"/>
31-
<appender-ref ref="transactionsAppender"/>
3230
</logger>
3331

3432
</log4j:configuration>

pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@
3434
<artifactId>mysql-connector-java</artifactId>
3535
<version>5.1.14</version>
3636
</dependency>
37+
<dependency>
38+
<groupId>com.google.guava</groupId>
39+
<artifactId>guava</artifactId>
40+
<version>18.0</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.apache.commons</groupId>
44+
<artifactId>commons-collections4</artifactId>
45+
<version>4.0</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.apache.commons</groupId>
49+
<artifactId>commons-lang3</artifactId>
50+
<version>3.3.1</version>
51+
</dependency>
3752
</dependencies>
3853

3954
</project>

src/main/java/com/lmax/trade/OrderWithStopLoss.java

+69-67
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.lmax.api.*;
44
import com.lmax.api.account.LoginCallback;
5-
import com.lmax.api.account.LoginRequest;
65
import com.lmax.api.order.*;
76
import com.lmax.api.orderbook.OrderBookEvent;
87
import com.lmax.api.orderbook.OrderBookEventListener;
@@ -30,8 +29,8 @@
3029
* @version $Id: OrderWithStopLoss.java, v 0.1 2/13/16 4:54 PM qiancheng.xq Exp $
3130
*/
3231
public class OrderWithStopLoss implements LoginCallback, OrderBookEventListener,
33-
OrderEventListener, ExecutionEventListener,
34-
InstructionRejectedEventListener {
32+
OrderEventListener, ExecutionEventListener,
33+
InstructionRejectedEventListener {
3534

3635
private static final Logger LOGGER = LoggerFactory
3736
.getLogger(OrderWithStopLoss.class);
@@ -89,7 +88,7 @@ public void onSuccess() {
8988
@Override
9089
public void onFailure(FailureResponse failureResponse) {
9190
LOGGER.error("Failed to subscribe order event. msg=[{}],description=[{}]",
92-
failureResponse.getMessage(), failureResponse.getDescription());
91+
failureResponse.getMessage(), failureResponse.getDescription());
9392
}
9493
});
9594

@@ -102,8 +101,8 @@ public void onSuccess() {
102101
@Override
103102
public void onFailure(FailureResponse failureResponse) {
104103
LOGGER.error(
105-
"Failed to subscribe order execution event. msg=[{}],description=[{}]",
106-
failureResponse.getMessage(), failureResponse.getDescription());
104+
"Failed to subscribe order execution event. msg=[{}],description=[{}]",
105+
failureResponse.getMessage(), failureResponse.getDescription());
107106
}
108107
});
109108

@@ -135,10 +134,10 @@ public void notify(OrderBookEvent orderBookEvent) {
135134
@Override
136135
public void notify(Order order) {
137136
LOGGER
138-
.debug(
139-
"Order(instructionId={},limitPrice={},quantity={},filledQuantity={},canceledQuantity={}) is executing by Broker",
140-
order.getInstructionId(), order.getLimitPrice(), order.getQuantity(),
141-
order.getFilledQuantity(), order.getCancelledQuantity());
137+
.debug(
138+
"Order(instructionId={},limitPrice={},quantity={},filledQuantity={},canceledQuantity={}) is executing by Broker",
139+
order.getInstructionId(), order.getLimitPrice(), order.getQuantity(),
140+
order.getFilledQuantity(), order.getCancelledQuantity());
142141
}
143142

144143
@Override
@@ -148,45 +147,45 @@ public void notify(Execution execution) {
148147
PlacedLimitOrder placedLimitOrder = placedOrder.get(order.getInstructionId());
149148
if (placedLimitOrder != null) {
150149
placedLimitOrder.setFilledQuantity(FixedPointNumber.valueOf(placedLimitOrder
151-
.getFilledQuantity().longValue() + execution.getQuantity().longValue()));
150+
.getFilledQuantity().longValue() + execution.getQuantity().longValue()));
152151
placedLimitOrder
153-
.setPendingQuantity(FixedPointNumber.valueOf(placedLimitOrder
154-
.getPendingQuantity().longValue()
155-
- placedLimitOrder.getQuantity()
156-
.longValue()));
152+
.setPendingQuantity(FixedPointNumber.valueOf(placedLimitOrder
153+
.getPendingQuantity().longValue()
154+
- placedLimitOrder.getQuantity()
155+
.longValue()));
157156

158157
LOGGER
159-
.info(
160-
"Order(instructionId={},limitPrice={},stopLossOffset={},stopProfitOffset={},placedTime={}) is filled by Broker(price={},quantity={})",
161-
execution.getOrder().getInstructionId(), placedLimitOrder.getLimitPrice(),
162-
placedLimitOrder.getStopLossPriceOffset(),
163-
placedLimitOrder.getStopProfitPriceOffset(),
164-
DateUtils.formate(placedLimitOrder.getPlacedTime()), execution.getPrice(),
165-
execution.getQuantity());
158+
.info(
159+
"Order(instructionId={},limitPrice={},stopLossOffset={},stopProfitOffset={},placedTime={}) is filled by Broker(price={},quantity={})",
160+
execution.getOrder().getInstructionId(), placedLimitOrder.getLimitPrice(),
161+
placedLimitOrder.getStopLossPriceOffset(),
162+
placedLimitOrder.getStopProfitPriceOffset(),
163+
DateUtils.formate(placedLimitOrder.getPlacedTime()), execution.getPrice(),
164+
execution.getQuantity());
166165

167166
if (placedLimitOrder.isComplete()) {
168167
LOGGER
169-
.info(
170-
"Order(instructionId={},limitPrice={},stopLossOffset={},stopProfitOffset={},placedTime={}) is completely finished by Broker",
171-
execution.getOrder().getInstructionId(),
172-
placedLimitOrder.getLimitPrice(),
173-
placedLimitOrder.getStopLossPriceOffset(),
174-
placedLimitOrder.getStopProfitPriceOffset(),
175-
DateUtils.formate(placedLimitOrder.getPlacedTime()));
168+
.info(
169+
"Order(instructionId={},limitPrice={},stopLossOffset={},stopProfitOffset={},placedTime={}) is completely finished by Broker",
170+
execution.getOrder().getInstructionId(),
171+
placedLimitOrder.getLimitPrice(),
172+
placedLimitOrder.getStopLossPriceOffset(),
173+
placedLimitOrder.getStopProfitPriceOffset(),
174+
DateUtils.formate(placedLimitOrder.getPlacedTime()));
176175
placedOrder.remove(order.getInstructionId());
177176
}
178177
return;
179178
}
180179
}
181180
LOGGER.warn(
182-
"Unpredicted Order(instructionId={}) is filled by Broker(price={},quantity={})",
183-
execution.getOrder().getInstructionId(), execution.getPrice(), execution.getQuantity());
181+
"Unpredicted Order(instructionId={}) is filled by Broker(price={},quantity={})",
182+
execution.getOrder().getInstructionId(), execution.getPrice(), execution.getQuantity());
184183
}
185184

186185
@Override
187186
public void notify(InstructionRejectedEvent instructionRejected) {
188187
LOGGER.error("InstructionRejected. instructionId={}, reason={}.",
189-
instructionRejected.getInstructionId(), instructionRejected.getReason());
188+
instructionRejected.getInstructionId(), instructionRejected.getReason());
190189
placedOrder.remove(instructionRejected.getInstructionId());
191190
}
192191

@@ -221,55 +220,55 @@ public void run() {
221220
if (!lastBidPrice.equals(FixedPointNumber.ZERO)) {
222221
String instructionId = String.valueOf(time);
223222
FixedPointNumber limitPrice = FixedPointNumbers.toFixedPointNumber(
224-
FixedPointNumbers.doubleValue(lastAskPrice), offset.negate());
223+
FixedPointNumbers.doubleValue(lastAskPrice), offset.negate());
225224
//place buy order
226225
LimitOrderSpecification limitOrderSpecification = new LimitOrderSpecification(
227-
instrumentId, instructionId, limitPrice, quantity, TimeInForce.GOOD_FOR_DAY,
228-
offset, offset);
226+
instrumentId, instructionId, limitPrice, quantity, TimeInForce.GOOD_FOR_DAY,
227+
offset, offset);
229228

230229
LOGGER.debug("Place buy order. lastAskPrice={}, orderSpecification={}",
231-
lastAskPrice, limitOrderSpecification);
230+
lastAskPrice, limitOrderSpecification);
232231
placedOrder.putIfAbsent(instructionId, PlacedLimitOrder.create(instrumentId,
233-
quantity, limitPrice, offset, offset, System.currentTimeMillis()));
232+
quantity, limitPrice, offset, offset, System.currentTimeMillis()));
234233
session.placeLimitOrder(limitOrderSpecification, new PlaceOrderAcceptCallback());
235234

236235
}
237236

238237
if (!lastAskPrice.equals(FixedPointNumber.ZERO)) {
239238
String instructionId = String.valueOf(time + 1);
240239
FixedPointNumber limitPrice = FixedPointNumbers.toFixedPointNumber(
241-
FixedPointNumbers.doubleValue(lastBidPrice), offset);
240+
FixedPointNumbers.doubleValue(lastBidPrice), offset);
242241
//place sell order
243242
LimitOrderSpecification limitOrderSpecification = new LimitOrderSpecification(
244-
instrumentId, instructionId, limitPrice, quantity.negate(),
245-
TimeInForce.GOOD_FOR_DAY, offset, offset);
243+
instrumentId, instructionId, limitPrice, quantity.negate(),
244+
TimeInForce.GOOD_FOR_DAY, offset, offset);
246245
LOGGER.debug("Place sell order. lastBidPrice={}, orderSpecification={}",
247-
lastBidPrice, limitOrderSpecification);
246+
lastBidPrice, limitOrderSpecification);
248247
placedOrder.putIfAbsent(instructionId, PlacedLimitOrder.create(instrumentId,
249-
quantity, limitPrice, offset, offset, System.currentTimeMillis()));
248+
quantity, limitPrice, offset, offset, System.currentTimeMillis()));
250249
session.placeLimitOrder(limitOrderSpecification, new PlaceOrderAcceptCallback());
251250
}
252251
}
253252
}
254253

255254
public static class PlacedLimitOrder {
256-
private long instrumentId;
255+
private long instrumentId;
257256

258-
private FixedPointNumber limitPrice;
257+
private FixedPointNumber limitPrice;
259258

260-
private FixedPointNumber stopLossPriceOffset;
259+
private FixedPointNumber stopLossPriceOffset;
261260

262-
private FixedPointNumber stopProfitPriceOffset;
261+
private FixedPointNumber stopProfitPriceOffset;
263262

264-
private FixedPointNumber quantity;
263+
private FixedPointNumber quantity;
265264

266265
private volatile FixedPointNumber filledQuantity;
267266

268267
private volatile FixedPointNumber pendingQuantity;
269268

270269
private volatile FixedPointNumber canceledQuantity;
271270

272-
private long placedTime;
271+
private long placedTime;
273272

274273
public static PlacedLimitOrder create(long instrumentId, FixedPointNumber quantity,
275274
FixedPointNumber limitPrice,
@@ -291,7 +290,7 @@ public static PlacedLimitOrder create(long instrumentId, FixedPointNumber quanti
291290

292291
public boolean isComplete() {
293292
return (canceledQuantity.longValue() + filledQuantity.longValue() == quantity
294-
.longValue() && pendingQuantity.longValue() == 0);
293+
.longValue() && pendingQuantity.longValue() == 0);
295294
}
296295

297296
public long getInstrumentId() {
@@ -399,11 +398,11 @@ public boolean equals(Object o) {
399398
return false;
400399
}
401400
if (stopLossPriceOffset != null ? !stopLossPriceOffset.equals(that.stopLossPriceOffset)
402-
: that.stopLossPriceOffset != null) {
401+
: that.stopLossPriceOffset != null) {
403402
return false;
404403
}
405404
if (stopProfitPriceOffset != null ? !stopProfitPriceOffset
406-
.equals(that.stopProfitPriceOffset) : that.stopProfitPriceOffset != null) {
405+
.equals(that.stopProfitPriceOffset) : that.stopProfitPriceOffset != null) {
407406
return false;
408407
}
409408

@@ -439,22 +438,25 @@ public static String formate(long time) {
439438
}
440439

441440
public static void main(String[] args) {
442-
if (args.length != 4) {
443-
System.out.println("Usage " + OrderWithStopLoss.class.getName()
444-
+ " <url> <username> <password> [CFD_DEMO|CFD_LIVE]");
445-
System.exit(-1);
446-
}
447-
448-
String url = args[0];
449-
String username = args[1];
450-
String password = args[2];
451-
LoginRequest.ProductType productType = LoginRequest.ProductType.valueOf(args[3]
452-
.toUpperCase());
453-
454-
LmaxApi lmaxApi = new LmaxApi(url);
455-
456-
OrderWithStopLoss orderWithStopLoss = new OrderWithStopLoss();
457-
lmaxApi.login(new LoginRequest(username, password, productType), orderWithStopLoss);
441+
// if (args.length != 4) {
442+
// System.out.println("Usage " + OrderWithStopLoss.class.getName()
443+
// + " <url> <username> <password> [CFD_DEMO|CFD_LIVE]");
444+
// System.exit(-1);
445+
// }
446+
//
447+
// String url = args[0];
448+
// String username = args[1];
449+
// String password = args[2];
450+
// LoginRequest.ProductType productType = LoginRequest.ProductType.valueOf(args[3]
451+
// .toUpperCase());
452+
//
453+
// LmaxApi lmaxApi = new LmaxApi(url);
454+
//
455+
// OrderWithStopLoss orderWithStopLoss = new OrderWithStopLoss();
456+
// lmaxApi.login(new LoginRequest(username, password, productType), orderWithStopLoss);
457+
458+
FixedPointNumber fixedPointNumber = FixedPointNumber.valueOf(10000);
459+
System.out.println(fixedPointNumber.toString());
458460

459461
}
460462

0 commit comments

Comments
 (0)