Skip to content

Commit 7532335

Browse files
Merge branch 'BMDEV-2479-Disable-contingent-orders'
2 parents 4ac7127 + 4db0fca commit 7532335

8 files changed

Lines changed: 89 additions & 54 deletions

File tree

bitmex-adapter/.classpath

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/commons-collections4-4.1.jar"/>
2121
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/commons-compress-1.15.jar"/>
2222
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/commons-io-2.6.jar"/>
23-
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/commons-lang3-3.4.jar"/>
2423
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/commons-logging-1.2.jar"/>
2524
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/commons-math3-3.6.jar"/>
2625
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/commons-net-3.4.jar"/>
@@ -58,7 +57,6 @@
5857
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/logback-core-1.1.2.jar"/>
5958
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/mars.jar"/>
6059
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/marytts-lang-en-5.2.jar"/>
61-
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/marytts-runtime-5.2-jar-with-dependencies.jar"/>
6260
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/mbassador-1.2.3.jar"/>
6361
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/mdd-util.jar"/>
6462
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/mina-core-2.0.16.jar"/>
@@ -87,7 +85,7 @@
8785
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/util.jar"/>
8886
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/voice-cmu-bdl-hsmm-5.2.jar"/>
8987
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/webapilayer.jar"/>
90-
<classpathentry kind="lib" path="C:/Program Files/Bookmap/BookMap.jar"/>
88+
<classpathentry kind="lib" path="C:/Program Files/Bookmap/Bookmap.jar"/>
9189
<classpathentry kind="lib" path="C:/Program Files/Bookmap/lib/bm-l1api.jar">
9290
<attributes>
9391
<attribute name="javadoc_location" value="jar:file:/C:/Program%20Files/Bookmap/lib/bm-l1api-javadoc.jar!/"/>

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/Provider.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,15 @@ private void passCancelMessageIfNeededAndClearPendingList(String response) {
280280
}
281281

282282
private boolean isBracketOrder(SimpleOrderSendParameters simpleParams) {
283-
return simpleParams.takeProfitOffset != 0 && simpleParams.stopLossOffset != 0;
283+
/*
284+
* These lines were commented out when BitMEX announced contingent
285+
* orders deprecation
286+
* https://blog.bitmex.com/api_announcement/deprecation-of-contingent-
287+
* orders/
288+
*
289+
* return simpleParams.takeProfitOffset != 0 && simpleParams.stopLossOffset != 0;
290+
*/
291+
return false;
284292
}
285293

286294
private SimpleOrderSendParameters createStopLossFromParameters(SimpleOrderSendParameters simpleParams) {
@@ -1101,8 +1109,15 @@ public Layer1ApiProviderSupportedFeatures getSupportedFeatures() {
11011109
a.setTrading(true);
11021110
}
11031111

1104-
a.setOco(true)
1105-
.setBrackets(true)
1112+
/*
1113+
* OCO and brackets are set to false because BitMEX announced contingent
1114+
* orders deprecation
1115+
* https://blog.bitmex.com/api_announcement/deprecation-of-contingent-
1116+
* orders/
1117+
*/
1118+
1119+
a.setOco(false)
1120+
.setBrackets(false)
11061121
.setSupportedOrderDurations(Arrays.asList(new OrderDuration[] { OrderDuration.GTC }))
11071122
// At the moment of writing this method it was not possible to
11081123
// report limit orders support, but no stop orders support
@@ -1117,7 +1132,6 @@ public Layer1ApiProviderSupportedFeatures getSupportedFeatures() {
11171132
"http://bitmex.historicaldata.bookmap.com:38080/historical-data-server-1.0/"))
11181133
.setKnownInstruments(knownInstruments);
11191134

1120-
// Log.info("PROVIDER getSupportedFeatures INVOKED");
11211135
return a.build();
11221136
}
11231137

@@ -1132,9 +1146,7 @@ public String getSource() {
11321146
public void close() {
11331147
// Stop events generation
11341148
Log.info("[bitmex] Provider close(): ");
1135-
if (connector.getSocket() != null) {
1136-
connector.getSocket().close();
1137-
}
1149+
connector.closeSocket();
11381150
connector.setInterruptionNeeded(true);
11391151
providerThread.interrupt();
11401152
}

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/BmConnector.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.nio.channels.UnresolvedAddressException;
1212
import java.time.ZoneOffset;
1313
import java.time.ZonedDateTime;
14-
import java.util.ArrayList;
1514
import java.util.Collections;
1615
import java.util.HashMap;
1716
import java.util.HashSet;
@@ -60,6 +59,9 @@ public class BmConnector implements Runnable {
6059
private ScheduledExecutorService executionsResetTimer;
6160
private int executionDay = 0;
6261
private boolean isExecutionReset;
62+
63+
private Object socketLock = new Object();
64+
private int timerCount = 0;
6365

6466
public Provider getProvider() {
6567
return provider;
@@ -186,10 +188,10 @@ public void wsConnect() {
186188
isReconnecting = false;
187189
}
188190

189-
Log.info("[bitmex] BmConnector wsConnect subscribed to an instrument ");
190191
// WAITING FOR THE SOCKET TO CLOSE
191-
socket.getClosingLatch().await();
192-
socket = null;
192+
193+
CountDownLatch closingLatch = socket.getClosingLatch();
194+
closingLatch.await();
193195
isReconnecting = true;
194196

195197
} catch (UpgradeException e) {
@@ -223,8 +225,9 @@ public void sendWebsocketMessage(String message) {
223225
throw new RuntimeException();
224226
}
225227
Log.info("[bitmex] BmConnector wsConnect Send websocket message");
226-
if (socket != null) {// this solution still needs to be examined
227-
synchronized (socket) {
228+
229+
synchronized (socketLock) {
230+
if (socket != null) {
228231
socket.sendMessage(message);
229232
}
230233
}
@@ -280,24 +283,39 @@ public HashMap<String, BmInstrument> getActiveInstrumentsMap() {
280283
}
281284

282285
private void launchSnapshotTimer(BmInstrument instr) {
283-
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
284-
286+
int localTimerCount = timerCount;
285287
TimerTask task = new TimerTask() {
286288
@Override
287289
public void run() {
290+
Thread.currentThread().setName("-> BmConnector: snapshotTimer " + localTimerCount + " for" + instr.getSymbol() );
291+
292+
if (socket == null || isReconnecting){
293+
Log.info("[bitmex] Waiting for the socket, timer " + localTimerCount + " shutdown");
294+
return;
295+
}
296+
288297
if (!instr.isOrderBookSnapshotParsed()) {
289-
Log.info("[bitmex] BmConnector launchSnapshotTimer(): resubscribe " + now);
298+
Log.info("[bitmex] BmConnector launchSnapshotTimer " + localTimerCount + " for" + instr.getSymbol() + ": resubscribe " + ZonedDateTime.now(ZoneOffset.UTC));
290299
unSubscribe(instr);
300+
301+
try {
302+
Thread.sleep(5000);
303+
} catch (InterruptedException e) {
304+
e.printStackTrace();
305+
}
306+
291307
subscribe(instr);
308+
} else {
309+
Log.info("[bitmex] BmConnector launchSnapshotTimer " + localTimerCount + " for" + instr.getSymbol() + ": end " + ZonedDateTime.now(ZoneOffset.UTC));
292310
}
293-
Log.info("[bitmex] BmConnector launchSnapshotTimer(): end " + now);
294311
}
295312
};
296313

297314
Timer timer = new Timer();
298315
instr.setSnapshotTimer(timer);
299-
Log.info("[bitmex] BmConnector launchSnapshotTimer(): " + now);
316+
Log.info("[bitmex] BmConnector launchSnapshotTimer " + localTimerCount + " for" + instr.getSymbol() + ": " + ZonedDateTime.now(ZoneOffset.UTC));
300317
timer.schedule(task, 10000);
318+
timerCount++;
301319
}
302320

303321
private void launchExecutionsResetTimer() {
@@ -338,6 +356,8 @@ public void run() {
338356
public void subscribe(BmInstrument instr) {
339357
Log.info("[bitmex] BmConnector subscribe: " + instr.getSymbol());
340358
instr.setSubscribed(true);
359+
Log.info("[bitmex] BmConnector subscribe: set true");
360+
341361
sendWebsocketMessage(instr.getSubscribeReq());
342362
launchSnapshotTimer(instr);
343363

@@ -353,7 +373,7 @@ public void unSubscribe(BmInstrument instr) {
353373
if (timer != null) {
354374
timer.cancel();
355375
Log.info("[bitmex] BmConnector unSubscribe: timer gets cancelled");
356-
}
376+
}
357377
instr.setSubscribed(false);
358378

359379
}
@@ -460,9 +480,16 @@ public void run() {
460480

461481
}
462482
executionsResetTimer.shutdownNow();
463-
if (socket != null) {
464-
socket.close();
465-
}
483+
closeSocket();
484+
466485
Log.info("[bitmex] BmConnector run: closing");
467486
}
487+
488+
public void closeSocket(){
489+
synchronized (socketLock) {
490+
if (socket != null) {
491+
socket.close();
492+
}
493+
}
494+
}
468495
}

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/BmInstrument.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public boolean isOrderBookSnapshotParsed() {
6363
}
6464

6565
public void setOrderBookSnapshotParsed(boolean orderBookSnapshotParsed) {
66+
this.snapshotTimer.cancel();
6667
this.orderBookSnapshotParsed = orderBookSnapshotParsed;
6768
}
6869

@@ -140,14 +141,6 @@ public Integer getPriceFromMap(long id) {
140141
return pricesMap.get(id);
141142
}
142143

143-
public boolean isFirstSnapshotParsed() {
144-
return isFirstSnapshotParsed;
145-
}
146-
147-
public void setFirstSnapshotParsed(boolean isFirstSnapshotParsed) {
148-
this.isFirstSnapshotParsed = isFirstSnapshotParsed;
149-
}
150-
151144
public UnitPosition getValidPosition() {
152145
return validPosition;
153146
}

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/ClientSocket.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ public class ClientSocket {
3131
private JsonParser parser;
3232
private boolean isConnectionPossiblyLost = false;
3333
private long lastMessageTime = System.currentTimeMillis();
34-
ScheduledExecutorService snapshotTimer;
34+
ScheduledExecutorService pingTimer;
3535

3636
@OnWebSocketClose
3737
public void OnClose(int i, String str) {
38-
// Log.info("ClientSocket: CLOSED WITH STATUS " + i + " FOR " + str + "
39-
// REASON");
38+
Log.info("ClientSocket: CLOSED WITH STATUS " + i + " FOR " + str + " REASON");
4039
closingLatch.countDown();
4140
}
4241

@@ -64,10 +63,10 @@ public Thread newThread(Runnable r) {
6463
}
6564

6665
long maxDelay = 5000;
67-
ScheduledExecutorService snapshotTimer = Executors.newSingleThreadScheduledExecutor(new CustomThreadFactory());
68-
this.snapshotTimer = snapshotTimer;
66+
ScheduledExecutorService pingTimer = Executors.newSingleThreadScheduledExecutor(new CustomThreadFactory());
67+
this.pingTimer = pingTimer;
6968

70-
snapshotTimer.scheduleWithFixedDelay(new Runnable() {
69+
pingTimer.scheduleWithFixedDelay(new Runnable() {
7170
@Override
7271
public void run() {
7372
// if the last message was > [5s + time to launch this timer]
@@ -79,7 +78,6 @@ public void run() {
7978
Log.info("[bitmex] ClientSocket launchPingTimer: connection lost UTC="
8079
+ System.currentTimeMillis());
8180
close();
82-
snapshotTimer.shutdown();
8381
} else {// but this did not happen before
8482
sendPing();
8583
setConnectionPossiblyLost(true);
@@ -138,7 +136,9 @@ public void onError(Session session, Throwable error) throws Exception {
138136
}
139137

140138
public void close() {
141-
snapshotTimer.shutdownNow();
139+
if (pingTimer != null) {
140+
pingTimer.shutdownNow();
141+
}
142142

143143
if (session != null) {
144144
try {
@@ -149,6 +149,7 @@ public void close() {
149149
}
150150
}
151151
Log.info("[bitmex] ClientSockeT close(): socket interrupted");
152+
closingLatch.countDown();
152153
}
153154

154155
public void setParser(JsonParser parser) {

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/JsonParser.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.bookmap.plugins.layer0.bitmex.adapter;
22

3-
import java.io.IOException;
43
import java.lang.reflect.Type;
54
import java.util.ArrayList;
65
import java.util.Collections;
@@ -44,10 +43,6 @@ public void setProvider(Provider provider) {
4443
this.provider = provider;
4544
}
4645

47-
public void setNonInstrumentPartialsParsed(Set<String> nonInstrumentPartialsParsed) {
48-
this.nonInstrumentPartialsParsed = nonInstrumentPartialsParsed;
49-
}
50-
5146
public void setActiveInstrumentsMap(Map<String, BmInstrument> activeInstrumentsMap) {
5247
this.activeInstrumentsMap = activeInstrumentsMap;
5348
}
@@ -99,18 +94,17 @@ public void parse(String str) {
9994
}
10095

10196
if (responseWs.getError() != null) {
102-
Log.info("[bitmex] JsonParser parser: errro message " + str);
97+
Log.info("[bitmex] JsonParser parser: error message " + str);
10398
BmErrorMessage error = new Gson().fromJson(str, BmErrorMessage.class);
10499
Log.info(error.getMessage());
105100
return;
106101
}
107102
return;
108103
}
109104

110-
// Options 'No object', 'success' and 'error' are already excluded
111-
// so only 'message' object (that contains 'data', an array of
112-
// objects)
113-
// stays
105+
// 'No object', 'success' and 'error' are excluded already excluded
106+
// so this must be a 'message' object (that contains 'data', an array
107+
// of objects)
114108
Message msg = (Message) gson.fromJson(str, Message.class);
115109

116110
// skip a messages if it contains empty data
@@ -264,6 +258,7 @@ private <T> void preprocessMessage(String str, Topic topic) {
264258
if (topic.equals(Topic.ORDERBOOKL2)) {
265259
BmInstrument instr = activeInstrumentsMap
266260
.get(((MessageGeneric<UnitData>) msg0).getData().get(0).getSymbol());
261+
nonInstrumentPartialsParsed.add(container.name);
267262
instr.setOrderBookSnapshotParsed(true);
268263
Log.info("[bitmex] JsonParser preprocessMessage setOrderBookSnapshotParsed set true for "
269264
+ instr.getSymbol());

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/TradeConnector.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,17 @@ public JsonObject createSendData(SimpleOrderSendParameters params, OrderType ord
126126
json.addProperty("side", side);
127127
json.addProperty("orderQty", orderQty);
128128
json.addProperty("clOrdID", tempOrderId);
129-
json.addProperty("clOrdLinkID", clOrdLinkID);
130-
json.addProperty("contingencyType", contingencyType);
131-
129+
132130
/*
131+
* These lines were commented out when BitMEX announced
132+
* contingent orders deprecation
133+
* https://blog.bitmex.com/api_announcement/deprecation-of-contingent-orders/
134+
*
135+
* json.addProperty("clOrdLinkID", clOrdLinkID);
136+
* json.addProperty("contingencyType", contingencyType);
137+
*/
138+
139+
/**
133140
* https://www.bitmex.com/api/explorer/#!/Order/Order_new Send a
134141
* simpleOrderQty instead of an orderQty to create an order denominated
135142
* in the underlying currency. This is useful for opening up a position

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/WsData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.bookmap.plugins.layer0.bitmex.adapter.ConnectorUtils.WebSocketOperation;
44

55
public class WsData {
6+
@SuppressWarnings("unused")
67
private String op;
8+
@SuppressWarnings("unused")
79
private Object[] args;
810

911
public WsData(WebSocketOperation op, Object... args) {

0 commit comments

Comments
 (0)