2
2
3
3
import com .lmax .api .*;
4
4
import com .lmax .api .account .LoginCallback ;
5
- import com .lmax .api .account .LoginRequest ;
6
5
import com .lmax .api .order .*;
7
6
import com .lmax .api .orderbook .OrderBookEvent ;
8
7
import com .lmax .api .orderbook .OrderBookEventListener ;
30
29
* @version $Id: OrderWithStopLoss.java, v 0.1 2/13/16 4:54 PM qiancheng.xq Exp $
31
30
*/
32
31
public class OrderWithStopLoss implements LoginCallback , OrderBookEventListener ,
33
- OrderEventListener , ExecutionEventListener ,
34
- InstructionRejectedEventListener {
32
+ OrderEventListener , ExecutionEventListener ,
33
+ InstructionRejectedEventListener {
35
34
36
35
private static final Logger LOGGER = LoggerFactory
37
36
.getLogger (OrderWithStopLoss .class );
@@ -89,7 +88,7 @@ public void onSuccess() {
89
88
@ Override
90
89
public void onFailure (FailureResponse failureResponse ) {
91
90
LOGGER .error ("Failed to subscribe order event. msg=[{}],description=[{}]" ,
92
- failureResponse .getMessage (), failureResponse .getDescription ());
91
+ failureResponse .getMessage (), failureResponse .getDescription ());
93
92
}
94
93
});
95
94
@@ -102,8 +101,8 @@ public void onSuccess() {
102
101
@ Override
103
102
public void onFailure (FailureResponse failureResponse ) {
104
103
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 ());
107
106
}
108
107
});
109
108
@@ -135,10 +134,10 @@ public void notify(OrderBookEvent orderBookEvent) {
135
134
@ Override
136
135
public void notify (Order order ) {
137
136
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 ());
142
141
}
143
142
144
143
@ Override
@@ -148,45 +147,45 @@ public void notify(Execution execution) {
148
147
PlacedLimitOrder placedLimitOrder = placedOrder .get (order .getInstructionId ());
149
148
if (placedLimitOrder != null ) {
150
149
placedLimitOrder .setFilledQuantity (FixedPointNumber .valueOf (placedLimitOrder
151
- .getFilledQuantity ().longValue () + execution .getQuantity ().longValue ()));
150
+ .getFilledQuantity ().longValue () + execution .getQuantity ().longValue ()));
152
151
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 ()));
157
156
158
157
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 ());
166
165
167
166
if (placedLimitOrder .isComplete ()) {
168
167
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 ()));
176
175
placedOrder .remove (order .getInstructionId ());
177
176
}
178
177
return ;
179
178
}
180
179
}
181
180
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 ());
184
183
}
185
184
186
185
@ Override
187
186
public void notify (InstructionRejectedEvent instructionRejected ) {
188
187
LOGGER .error ("InstructionRejected. instructionId={}, reason={}." ,
189
- instructionRejected .getInstructionId (), instructionRejected .getReason ());
188
+ instructionRejected .getInstructionId (), instructionRejected .getReason ());
190
189
placedOrder .remove (instructionRejected .getInstructionId ());
191
190
}
192
191
@@ -221,55 +220,55 @@ public void run() {
221
220
if (!lastBidPrice .equals (FixedPointNumber .ZERO )) {
222
221
String instructionId = String .valueOf (time );
223
222
FixedPointNumber limitPrice = FixedPointNumbers .toFixedPointNumber (
224
- FixedPointNumbers .doubleValue (lastAskPrice ), offset .negate ());
223
+ FixedPointNumbers .doubleValue (lastAskPrice ), offset .negate ());
225
224
//place buy order
226
225
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 );
229
228
230
229
LOGGER .debug ("Place buy order. lastAskPrice={}, orderSpecification={}" ,
231
- lastAskPrice , limitOrderSpecification );
230
+ lastAskPrice , limitOrderSpecification );
232
231
placedOrder .putIfAbsent (instructionId , PlacedLimitOrder .create (instrumentId ,
233
- quantity , limitPrice , offset , offset , System .currentTimeMillis ()));
232
+ quantity , limitPrice , offset , offset , System .currentTimeMillis ()));
234
233
session .placeLimitOrder (limitOrderSpecification , new PlaceOrderAcceptCallback ());
235
234
236
235
}
237
236
238
237
if (!lastAskPrice .equals (FixedPointNumber .ZERO )) {
239
238
String instructionId = String .valueOf (time + 1 );
240
239
FixedPointNumber limitPrice = FixedPointNumbers .toFixedPointNumber (
241
- FixedPointNumbers .doubleValue (lastBidPrice ), offset );
240
+ FixedPointNumbers .doubleValue (lastBidPrice ), offset );
242
241
//place sell order
243
242
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 );
246
245
LOGGER .debug ("Place sell order. lastBidPrice={}, orderSpecification={}" ,
247
- lastBidPrice , limitOrderSpecification );
246
+ lastBidPrice , limitOrderSpecification );
248
247
placedOrder .putIfAbsent (instructionId , PlacedLimitOrder .create (instrumentId ,
249
- quantity , limitPrice , offset , offset , System .currentTimeMillis ()));
248
+ quantity , limitPrice , offset , offset , System .currentTimeMillis ()));
250
249
session .placeLimitOrder (limitOrderSpecification , new PlaceOrderAcceptCallback ());
251
250
}
252
251
}
253
252
}
254
253
255
254
public static class PlacedLimitOrder {
256
- private long instrumentId ;
255
+ private long instrumentId ;
257
256
258
- private FixedPointNumber limitPrice ;
257
+ private FixedPointNumber limitPrice ;
259
258
260
- private FixedPointNumber stopLossPriceOffset ;
259
+ private FixedPointNumber stopLossPriceOffset ;
261
260
262
- private FixedPointNumber stopProfitPriceOffset ;
261
+ private FixedPointNumber stopProfitPriceOffset ;
263
262
264
- private FixedPointNumber quantity ;
263
+ private FixedPointNumber quantity ;
265
264
266
265
private volatile FixedPointNumber filledQuantity ;
267
266
268
267
private volatile FixedPointNumber pendingQuantity ;
269
268
270
269
private volatile FixedPointNumber canceledQuantity ;
271
270
272
- private long placedTime ;
271
+ private long placedTime ;
273
272
274
273
public static PlacedLimitOrder create (long instrumentId , FixedPointNumber quantity ,
275
274
FixedPointNumber limitPrice ,
@@ -291,7 +290,7 @@ public static PlacedLimitOrder create(long instrumentId, FixedPointNumber quanti
291
290
292
291
public boolean isComplete () {
293
292
return (canceledQuantity .longValue () + filledQuantity .longValue () == quantity
294
- .longValue () && pendingQuantity .longValue () == 0 );
293
+ .longValue () && pendingQuantity .longValue () == 0 );
295
294
}
296
295
297
296
public long getInstrumentId () {
@@ -399,11 +398,11 @@ public boolean equals(Object o) {
399
398
return false ;
400
399
}
401
400
if (stopLossPriceOffset != null ? !stopLossPriceOffset .equals (that .stopLossPriceOffset )
402
- : that .stopLossPriceOffset != null ) {
401
+ : that .stopLossPriceOffset != null ) {
403
402
return false ;
404
403
}
405
404
if (stopProfitPriceOffset != null ? !stopProfitPriceOffset
406
- .equals (that .stopProfitPriceOffset ) : that .stopProfitPriceOffset != null ) {
405
+ .equals (that .stopProfitPriceOffset ) : that .stopProfitPriceOffset != null ) {
407
406
return false ;
408
407
}
409
408
@@ -439,22 +438,25 @@ public static String formate(long time) {
439
438
}
440
439
441
440
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 ());
458
460
459
461
}
460
462
0 commit comments