88import java .util .List ;
99import java .util .Map ;
1010import java .util .Set ;
11+ import java .util .concurrent .CopyOnWriteArrayList ;
1112
1213import com .bookmap .plugins .layer0 .bitmex .adapter .BmConnector ;
1314import com .bookmap .plugins .layer0 .bitmex .adapter .BmInstrument ;
3031import velox .api .layer0 .live .ExternalLiveBaseProvider ;
3132import velox .api .layer1 .Layer1ApiAdminListener ;
3233import velox .api .layer1 .Layer1ApiDataListener ;
34+ import velox .api .layer1 .annotations .Layer1ApiVersion ;
35+ import velox .api .layer1 .annotations .Layer1ApiVersionValue ;
3336import velox .api .layer1 .common .Log ;
3437import velox .api .layer1 .data .BalanceInfo ;
38+ import velox .api .layer1 .data .BmSimpleHistoricalDataInfo ;
3539import velox .api .layer1 .data .DisconnectionReason ;
3640import velox .api .layer1 .data .ExecutionInfo ;
3741import velox .api .layer1 .data .InstrumentInfo ;
5155import velox .api .layer1 .data .OrderUpdateParameters ;
5256import velox .api .layer1 .data .SimpleOrderSendParameters ;
5357import velox .api .layer1 .data .StatusInfo ;
58+ import velox .api .layer1 .data .SubscribeInfo ;
5459import velox .api .layer1 .data .SystemTextMessageType ;
5560import velox .api .layer1 .data .TradeInfo ;
5661import velox .api .layer1 .data .UserPasswordDemoLoginData ;
5762import velox .api .layer1 .layers .utils .OrderBook ;
5863
59- @ Layer0LiveModule
64+ @ Layer1ApiVersion (Layer1ApiVersionValue .VERSION1 )
65+ @ Layer0LiveModule (shortName = "MEX" , fullName = "BitMEX" )
6066public class Provider extends ExternalLiveBaseProvider {
6167
6268 private BmConnector connector ;
@@ -82,6 +88,8 @@ public class Provider extends ExternalLiveBaseProvider {
8288 private List <String > batchCancels = new LinkedList <>();
8389 private Map <String , BalanceInfo .BalanceInCurrency > balanceMap = new HashMap <>();
8490
91+ private CopyOnWriteArrayList <SubscribeInfo > knownInstruments = new CopyOnWriteArrayList <>();
92+
8593 protected class Instrument {
8694 protected final String alias ;
8795 protected final double pips ;
@@ -110,6 +118,14 @@ public BmConnector getConnector() {
110118 return connector ;
111119 }
112120
121+ public List <SubscribeInfo > getKnownInstruments () {
122+ return knownInstruments ;
123+ }
124+
125+ public void setKnownInstruments (CopyOnWriteArrayList <SubscribeInfo > knownInstruments ) {
126+ this .knownInstruments = knownInstruments ;
127+ }
128+
113129 /**
114130 * <p>
115131 * Generates alias from symbol, exchange and type of the instrument. Alias
@@ -135,7 +151,11 @@ public static String testReponseForError(String str) {
135151 }
136152
137153 @ Override
138- public void subscribe (String symbol , String exchange , String type ) {
154+ public void subscribe (SubscribeInfo subscribeInfo ) {
155+ final String symbol = subscribeInfo .symbol ;
156+ final String exchange = subscribeInfo .exchange ;
157+ final String type = subscribeInfo .type ;
158+
139159 Log .info ("[bitmex] Provider subscribe" );
140160 String alias = createAlias (symbol , exchange , type );
141161 // Since instruments also will be accessed from the data generation
@@ -270,7 +290,7 @@ private SimpleOrderSendParameters createStopLossFromParameters(SimpleOrderSendPa
270290 int offsetMultiplier = simpleParams .isBuy ? 1 : -1 ;
271291
272292 double limitPriceChecked = checkLImitPriceForBracket (simpleParams , bmInstrument );
273-
293+
274294 SimpleOrderSendParameters stopLoss = new SimpleOrderSendParameters (
275295 simpleParams .alias ,
276296 !simpleParams .isBuy , // !
@@ -299,13 +319,12 @@ private SimpleOrderSendParameters createTakeProfitFromParameters(SimpleOrderSend
299319 simpleParams .sizeMultiplier );
300320 return takeProfit ;
301321 }
302-
303- private double checkLImitPriceForBracket (SimpleOrderSendParameters simpleParams , BmInstrument bmInstrument ){
322+
323+ private double checkLImitPriceForBracket (SimpleOrderSendParameters simpleParams , BmInstrument bmInstrument ) {
304324 double limitPriceChecked = simpleParams .limitPrice ;
305325 if (Double .isNaN (simpleParams .limitPrice )) {
306326 OrderBook orderBook = bmInstrument .getOrderBook ();
307- limitPriceChecked = simpleParams .isBuy ?
308- orderBook .getBestAskPriceOrNone () * bmInstrument .getTickSize ()
327+ limitPriceChecked = simpleParams .isBuy ? orderBook .getBestAskPriceOrNone () * bmInstrument .getTickSize ()
309328 : orderBook .getBestBidPriceOrNone () * bmInstrument .getTickSize ();
310329 }
311330 return limitPriceChecked ;
@@ -906,23 +925,22 @@ public void listenForMargin(UnitMargin margin) {
906925 long tempMultiplier = 100000000 ;// temp
907926 String currency = margin .getCurrency ();
908927 BalanceInfo .BalanceInCurrency currentBic = balanceMap .get (margin .getCurrency ());
909- BalanceInfo .BalanceInCurrency newBic ;
910928 if (currentBic == null ) {// no current balance balance
911- newBic = new BalanceInfo .BalanceInCurrency (0.0 , 0.0 , 0.0 , 0.0 , 0.0 , margin .getCurrency (), null );
912- } else {
913- newBic = new BalanceInfo .BalanceInCurrency (currentBic .balance ,
914- margin .getRealisedPnl () == null ? currentBic .realizedPnl
915- : (double ) margin .getRealisedPnl () / tempMultiplier ,
916- margin .getUnrealisedPnl () == null ? currentBic .unrealizedPnl
917- : (double ) margin .getUnrealisedPnl () / tempMultiplier ,
918- currentBic .previousDayBalance , margin .getAvailableMargin () == null ? currentBic .netLiquidityValue
919- : (double ) margin .getAvailableMargin () / tempMultiplier ,
920- currency ,
921- currentBic .rateToBase );
929+ currentBic = new BalanceInfo .BalanceInCurrency (0.0 , 0.0 , 0.0 , 0.0 , 0.0 , currency , null );
922930 }
931+ currentBic = new BalanceInfo .BalanceInCurrency (
932+ currentBic .balance ,
933+ margin .getRealisedPnl () == null ? currentBic .realizedPnl
934+ : (double ) margin .getRealisedPnl () / tempMultiplier ,
935+ margin .getUnrealisedPnl () == null ? currentBic .unrealizedPnl
936+ : (double ) margin .getUnrealisedPnl () / tempMultiplier ,
937+ currentBic .previousDayBalance ,
938+ margin .getAvailableMargin () == null ? currentBic .netLiquidityValue
939+ : (double ) margin .getAvailableMargin () / tempMultiplier ,
940+ currency ,
941+ currentBic .rateToBase );
923942
924- balanceMap .remove (currency );
925- balanceMap .put (currency , newBic );
943+ balanceMap .put (currency , currentBic );
926944 BalanceInfo info = new BalanceInfo (new ArrayList <BalanceInfo .BalanceInCurrency >(balanceMap .values ()));
927945 tradingListeners .forEach (l -> l .onBalance (info ));
928946 }
@@ -1073,24 +1091,27 @@ private void checkIfLinkedAndAddToMaps(UnitOrder order) {
10731091 @ Override
10741092 public Layer1ApiProviderSupportedFeatures getSupportedFeatures () {
10751093 // Expanding parent supported features, reporting basic trading support
1076- Layer1ApiProviderSupportedFeaturesBuilder a ;
1094+ Layer1ApiProviderSupportedFeaturesBuilder a = super . getSupportedFeatures (). toBuilder () ;
10771095
1078- if (isCredentialsEmpty ) {
1079- return super . getSupportedFeatures (). toBuilder (). build ( );
1096+ if (! isCredentialsEmpty ) {
1097+ a . setTrading ( true );
10801098 }
10811099
1082- a = super .getSupportedFeatures ().toBuilder ().setTrading (true )
1083- .setOco (true )
1100+ a .setOco (true )
10841101 .setBrackets (true )
10851102 .setSupportedOrderDurations (Arrays .asList (new OrderDuration [] { OrderDuration .GTC }))
10861103 // At the moment of writing this method it was not possible to
10871104 // report limit orders support, but no stop orders support
10881105 // If you actually need it, you can report stop orders support
10891106 // but reject stop orders when those are sent.
1090- .setSupportedStopOrders (Arrays .asList (new OrderType [] { OrderType .LMT , OrderType .MKT }));
1091-
1092- a .setBalanceSupported (true );
1093- a .setTrailingStopsAsIndependentOrders (true );
1107+ .setSupportedStopOrders (Arrays .asList (new OrderType [] { OrderType .LMT , OrderType .MKT }))
1108+ .setBalanceSupported (true )
1109+ .setTrailingStopsAsIndependentOrders (true )
1110+ .setExchangeUsedForSubscription (false )
1111+ .setTypeUsedForSubscription (false )
1112+ .setHistoricalDataInfo (new BmSimpleHistoricalDataInfo (
1113+ "http://bitmex.historicaldata.bookmap.com:38080/historical-data-server-1.0/" ))
1114+ .setKnownInstruments (knownInstruments );
10941115
10951116 // Log.info("PROVIDER getSupportedFeatures INVOKED");
10961117 return a .build ();
0 commit comments