forked from gazbert/bxbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitfinexExchangeAdapter.java
1068 lines (905 loc) · 36.1 KB
/
BitfinexExchangeAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Gareth Jon Lynch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.gazbert.bxbot.exchanges;
import com.gazbert.bxbot.exchange.api.AuthenticationConfig;
import com.gazbert.bxbot.exchange.api.ExchangeAdapter;
import com.gazbert.bxbot.exchange.api.ExchangeConfig;
import com.gazbert.bxbot.exchanges.trading.api.impl.BalanceInfoImpl;
import com.gazbert.bxbot.exchanges.trading.api.impl.MarketOrderBookImpl;
import com.gazbert.bxbot.exchanges.trading.api.impl.MarketOrderImpl;
import com.gazbert.bxbot.exchanges.trading.api.impl.OpenOrderImpl;
import com.gazbert.bxbot.exchanges.trading.api.impl.TickerImpl;
import com.gazbert.bxbot.trading.api.BalanceInfo;
import com.gazbert.bxbot.trading.api.ExchangeNetworkException;
import com.gazbert.bxbot.trading.api.MarketOrder;
import com.gazbert.bxbot.trading.api.MarketOrderBook;
import com.gazbert.bxbot.trading.api.OpenOrder;
import com.gazbert.bxbot.trading.api.OrderType;
import com.gazbert.bxbot.trading.api.Ticker;
import com.gazbert.bxbot.trading.api.TradingApi;
import com.gazbert.bxbot.trading.api.TradingApiException;
import com.google.common.base.MoreObjects;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Exchange Adapter for integrating with the Bitfinex exchange. The Bitfinex API is documented <a
* href="https://www.bitfinex.com/pages/api">here</a>.
*
* <p><strong> DISCLAIMER: This Exchange Adapter is provided as-is; it might have bugs in it and you
* could lose money. Despite running live on Bitfinex, it has only been unit tested up until the
* point of calling the {@link #sendPublicRequestToExchange(String)} and {@link
* #sendAuthenticatedRequestToExchange(String, Map)} methods. Use it at our own risk!</strong>
*
* <p>The adapter uses v1 of the Bitfinex API - it is limited to 60 API calls per minute. It only
* supports 'exchange' accounts; it does <em>not</em> support 'trading' (margin trading) accounts or
* 'deposit' (liquidity SWAPs) accounts. Furthermore, the adapter does not support sending 'hidden'
* orders.
*
* <p>There are different exchange fees for Takers and Makers - see <a
* href="https://www.bitfinex.com/pages/fees">here.</a> This adapter will use the <em>Taker</em>
* fees to keep things simple for now.
*
* <p>The Exchange Adapter is <em>not</em> thread safe. It expects to be called using a single
* thread in order to preserve trade execution order. The {@link URLConnection} achieves this by
* blocking/waiting on the input stream (response) for each API call.
*
* <p>The {@link TradingApi} calls will throw a {@link ExchangeNetworkException} if a network error
* occurs trying to connect to the exchange. A {@link TradingApiException} is thrown for
* <em>all</em> other failures.
*
* @author gazbert
* @since 1.0
*/
public final class BitfinexExchangeAdapter extends AbstractExchangeAdapter
implements ExchangeAdapter {
private static final Logger LOG = LogManager.getLogger();
private static final String BITFINEX_API_VERSION = "v1";
private static final String PUBLIC_API_BASE_URL =
"https://api.bitfinex.com/" + BITFINEX_API_VERSION + "/";
private static final String AUTHENTICATED_API_URL = PUBLIC_API_BASE_URL;
private static final String UNEXPECTED_ERROR_MSG =
"Unexpected error has occurred in Bitfinex Exchange Adapter. ";
private static final String UNEXPECTED_IO_ERROR_MSG =
"Failed to connect to Exchange due to unexpected IO error.";
private static final String ID = "id";
private static final String EXCHANGE = "exchange";
private static final String SYMBOL = "symbol";
private static final String AMOUNT = "amount";
private static final String PRICE = "price";
private static final String TIMESTAMP = "timestamp";
private static final String AVG_EXECUTION_PRICE = "avgExecutionPrice";
private static final String IS_LIVE = "isLive";
private static final String IS_CANCELLED = "isCancelled";
private static final String IS_HIDDEN = "isHidden";
private static final String WAS_FORCED = "wasForced";
private static final String ORIGINAL_AMOUNT = "originalAmount";
private static final String REMAINING_AMOUNT = "remainingAmount";
private static final String EXECUTED_AMOUNT = "executedAmount";
private static final String KEY_PROPERTY_NAME = "key";
private static final String SECRET_PROPERTY_NAME = "secret";
private String key = "";
private String secret = "";
private Mac mac;
private boolean initializedMacAuthentication = false;
private long nonce = 0;
private Gson gson;
@Override
public void init(ExchangeConfig config) {
LOG.info(() -> "About to initialise Bitfinex ExchangeConfig: " + config);
setAuthenticationConfig(config);
setNetworkConfig(config);
nonce = System.currentTimeMillis() / 1000;
initSecureMessageLayer();
initGson();
}
// --------------------------------------------------------------------------
// Bitfinex API Calls adapted to the Trading API.
// See https://www.bitfinex.com/pages/api
// --------------------------------------------------------------------------
@Override
public MarketOrderBook getMarketOrders(String marketId)
throws TradingApiException, ExchangeNetworkException {
try {
final ExchangeHttpResponse response = sendPublicRequestToExchange("book/" + marketId);
LOG.debug(() -> "Market Orders response: " + response);
final BitfinexOrderBook orderBook =
gson.fromJson(response.getPayload(), BitfinexOrderBook.class);
final List<MarketOrder> buyOrders = new ArrayList<>();
for (BitfinexMarketOrder bitfinexBuyOrder : orderBook.bids) {
final MarketOrder buyOrder =
new MarketOrderImpl(
OrderType.BUY,
bitfinexBuyOrder.price,
bitfinexBuyOrder.amount,
bitfinexBuyOrder.price.multiply(bitfinexBuyOrder.amount));
buyOrders.add(buyOrder);
}
final List<MarketOrder> sellOrders = new ArrayList<>();
for (BitfinexMarketOrder bitfinexSellOrder : orderBook.asks) {
final MarketOrder sellOrder =
new MarketOrderImpl(
OrderType.SELL,
bitfinexSellOrder.price,
bitfinexSellOrder.amount,
bitfinexSellOrder.price.multiply(bitfinexSellOrder.amount));
sellOrders.add(sellOrder);
}
return new MarketOrderBookImpl(marketId, sellOrders, buyOrders);
} catch (ExchangeNetworkException | TradingApiException e) {
throw e;
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
@Override
public List<OpenOrder> getYourOpenOrders(String marketId)
throws TradingApiException, ExchangeNetworkException {
try {
final ExchangeHttpResponse response = sendAuthenticatedRequestToExchange("orders", null);
LOG.debug(() -> "Open Orders response: " + response);
final BitfinexOpenOrders bitfinexOpenOrders =
gson.fromJson(response.getPayload(), BitfinexOpenOrders.class);
final List<OpenOrder> ordersToReturn = new ArrayList<>();
for (final BitfinexOpenOrder bitfinexOpenOrder : bitfinexOpenOrders) {
if (!marketId.equalsIgnoreCase(bitfinexOpenOrder.symbol)) {
continue;
}
OrderType orderType;
switch (bitfinexOpenOrder.side) {
case "buy":
orderType = OrderType.BUY;
break;
case "sell":
orderType = OrderType.SELL;
break;
default:
throw new TradingApiException(
"Unrecognised order type received in getYourOpenOrders(). Value: "
+ bitfinexOpenOrder.type);
}
final OpenOrder order =
new OpenOrderImpl(
Long.toString(bitfinexOpenOrder.id),
// for some reason 'finex adds decimal point to long date value, e.g. "1442073766.0"
// - grrrr!
Date.from(
Instant.ofEpochMilli(
Integer.parseInt(bitfinexOpenOrder.timestamp.split("\\.")[0]))),
marketId,
orderType,
bitfinexOpenOrder.price,
bitfinexOpenOrder.remainingAmount,
bitfinexOpenOrder.originalAmount,
bitfinexOpenOrder.price.multiply(
bitfinexOpenOrder.originalAmount) // total - not provided by finex :-(
);
ordersToReturn.add(order);
}
return ordersToReturn;
} catch (ExchangeNetworkException | TradingApiException e) {
throw e;
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
@Override
public String createOrder(
String marketId, OrderType orderType, BigDecimal quantity, BigDecimal price)
throws TradingApiException, ExchangeNetworkException {
try {
final Map<String, Object> params = createRequestParamMap();
params.put(SYMBOL, marketId);
// note we need to limit amount and price to 8 decimal places else exchange will barf
params.put(
AMOUNT, new DecimalFormat("#.########", getDecimalFormatSymbols()).format(quantity));
params.put(PRICE, new DecimalFormat("#.########", getDecimalFormatSymbols()).format(price));
params.put(EXCHANGE, "bitfinex");
if (orderType == OrderType.BUY) {
params.put("side", "buy");
} else if (orderType == OrderType.SELL) {
params.put("side", "sell");
} else {
final String errorMsg =
"Invalid order type: "
+ orderType
+ " - Can only be "
+ OrderType.BUY.getStringValue()
+ " or "
+ OrderType.SELL.getStringValue();
LOG.error(errorMsg);
throw new IllegalArgumentException(errorMsg);
}
// 'type' is either "market" / "limit" / "stop" / "trailing-stop" / "fill-or-kill" / "exchange
// market" /
// "exchange limit" / "exchange stop" / "exchange trailing-stop" / "exchange fill-or-kill".
// (type starting by "exchange " are exchange orders, others are margin trading orders)
// this adapter only supports 'exchange limit orders'
params.put("type", "exchange limit");
// This adapter does not currently support hidden orders.
// Exchange API notes: "true if the order should be hidden. Default is false."
// If you try and set "is_hidden" to false, the exchange barfs and sends a 401 back. Nice.
// params.put("is_hidden", "false");
final ExchangeHttpResponse response = sendAuthenticatedRequestToExchange("order/new", params);
LOG.debug(() -> "Create Order response: " + response);
final BitfinexNewOrderResponse createOrderResponse =
gson.fromJson(response.getPayload(), BitfinexNewOrderResponse.class);
final long id = createOrderResponse.orderId;
if (id == 0) {
final String errorMsg = "Failed to place order on exchange. Error response: " + response;
LOG.error(errorMsg);
throw new TradingApiException(errorMsg);
} else {
return Long.toString(createOrderResponse.orderId);
}
} catch (ExchangeNetworkException | TradingApiException e) {
throw e;
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
/*
* marketId is not needed for cancelling orders on this exchange.
*/
@Override
public boolean cancelOrder(String orderId, String marketIdNotNeeded)
throws TradingApiException, ExchangeNetworkException {
try {
final Map<String, Object> params = createRequestParamMap();
params.put("order_id", Long.parseLong(orderId));
final ExchangeHttpResponse response =
sendAuthenticatedRequestToExchange("order/cancel", params);
LOG.debug(() -> "Cancel Order response: " + response);
// Exchange returns order id and other details if successful, a 400 HTTP Status if the order
// id was not recognised.
gson.fromJson(response.getPayload(), BitfinexCancelOrderResponse.class);
return true;
} catch (ExchangeNetworkException | TradingApiException e) {
if (e.getCause() != null && e.getCause().getMessage().contains("400")) {
final String errorMsg =
"Failed to cancel order on exchange. Did not recognise Order Id: " + orderId;
LOG.error(errorMsg, e);
return false;
} else {
throw e;
}
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
@Override
public BigDecimal getLatestMarketPrice(String marketId)
throws TradingApiException, ExchangeNetworkException {
try {
final ExchangeHttpResponse response = sendPublicRequestToExchange("pubticker/" + marketId);
LOG.debug(() -> "Latest Market Price response: " + response);
final BitfinexTicker ticker = gson.fromJson(response.getPayload(), BitfinexTicker.class);
return ticker.lastPrice;
} catch (ExchangeNetworkException | TradingApiException e) {
throw e;
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
@Override
public BalanceInfo getBalanceInfo() throws TradingApiException, ExchangeNetworkException {
try {
final ExchangeHttpResponse response = sendAuthenticatedRequestToExchange("balances", null);
LOG.debug(() -> "Balance Info response: " + response);
final BitfinexBalances allAccountBalances =
gson.fromJson(response.getPayload(), BitfinexBalances.class);
final HashMap<String, BigDecimal> balancesAvailable = new HashMap<>();
/*
* The adapter only fetches the 'exchange' account balance details - this is the Bitfinex
* 'exchange' account, i.e. the limit order trading account balance.
*/
if (allAccountBalances != null) {
allAccountBalances.stream()
.filter(accountBalance -> accountBalance.type.equalsIgnoreCase(EXCHANGE))
.forEach(
accountBalance -> {
if (accountBalance.currency.equalsIgnoreCase("usd")) {
balancesAvailable.put("USD", accountBalance.available);
} else if (accountBalance.currency.equalsIgnoreCase("btc")) {
balancesAvailable.put("BTC", accountBalance.available);
}
});
}
// 2nd arg of BalanceInfo constructor for reserved/on-hold balances is not provided by
// exchange.
return new BalanceInfoImpl(balancesAvailable, new HashMap<>());
} catch (ExchangeNetworkException | TradingApiException e) {
throw e;
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
@Override
public BigDecimal getPercentageOfBuyOrderTakenForExchangeFee(String marketId)
throws TradingApiException, ExchangeNetworkException {
try {
final ExchangeHttpResponse response =
sendAuthenticatedRequestToExchange("account_infos", null);
LOG.debug(() -> "Buy Fee response: " + response);
// Nightmare to adapt! Just take the top-level taker fees.
final BitfinexAccountInfos bitfinexAccountInfos =
gson.fromJson(response.getPayload(), BitfinexAccountInfos.class);
final BigDecimal fee = bitfinexAccountInfos.get(0).takerFees;
// adapt the % into BigDecimal format
return fee.divide(new BigDecimal("100"), 8, RoundingMode.HALF_UP);
} catch (ExchangeNetworkException | TradingApiException e) {
throw e;
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
@Override
public BigDecimal getPercentageOfSellOrderTakenForExchangeFee(String marketId)
throws TradingApiException, ExchangeNetworkException {
try {
final ExchangeHttpResponse response =
sendAuthenticatedRequestToExchange("account_infos", null);
LOG.debug(() -> "Sell Fee response: " + response);
// Nightmare to adapt! Just take the top-level taker fees.
final BitfinexAccountInfos bitfinexAccountInfos =
gson.fromJson(response.getPayload(), BitfinexAccountInfos.class);
final BigDecimal fee = bitfinexAccountInfos.get(0).takerFees;
// adapt the % into BigDecimal format
return fee.divide(new BigDecimal("100"), 8, RoundingMode.HALF_UP);
} catch (ExchangeNetworkException | TradingApiException e) {
throw e;
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
@Override
public String getImplName() {
return "Bitfinex API v1";
}
@Override
public Ticker getTicker(String marketId) throws TradingApiException, ExchangeNetworkException {
try {
final ExchangeHttpResponse response = sendPublicRequestToExchange("pubticker/" + marketId);
LOG.debug(() -> "Latest Market Price response: " + response);
final BitfinexTicker ticker = gson.fromJson(response.getPayload(), BitfinexTicker.class);
return new TickerImpl(
ticker.lastPrice,
ticker.bid,
ticker.ask,
ticker.low,
ticker.high,
null, // open not supplied by Bitfinex
ticker.volume,
null, // vwap not supplied by Bitfinex
// for some reason 'finex adds decimal point to long date value, e.g. "1513631756.0798516"
// - grrrr!
Date.from(Instant.ofEpochMilli(Integer.parseInt(ticker.timestamp.split("\\.")[0])))
.getTime());
} catch (ExchangeNetworkException | TradingApiException e) {
throw e;
} catch (Exception e) {
LOG.error(UNEXPECTED_ERROR_MSG, e);
throw new TradingApiException(UNEXPECTED_ERROR_MSG, e);
}
}
// --------------------------------------------------------------------------
// GSON classes for JSON responses.
// See https://www.bitfinex.com/pages/api
// --------------------------------------------------------------------------
/** GSON class for a market Order Book. */
private static class BitfinexOrderBook {
BitfinexMarketOrder[] bids;
BitfinexMarketOrder[] asks;
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("bids", bids).add("asks", asks).toString();
}
}
/** GSON class for a Market Order. */
private static class BitfinexMarketOrder {
BigDecimal price;
BigDecimal amount;
String timestamp;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add(PRICE, price)
.add(AMOUNT, amount)
.add(TIMESTAMP, timestamp)
.toString();
}
}
/** GSON class for receiving your open orders in 'orders' API call response. */
private static class BitfinexOpenOrders extends ArrayList<BitfinexOpenOrder> {
private static final long serialVersionUID = 5516523641153401953L;
}
/** GSON class for mapping returned order from 'orders' API call response. */
private static class BitfinexOpenOrder {
long id;
String symbol;
String exchange;
BigDecimal price;
@SerializedName("avg_execution_price")
BigDecimal avgExecutionPrice;
String side; // e.g. "sell"
String type; // e.g. "exchange limit"
String timestamp;
@SerializedName("is_live")
boolean isLive;
@SerializedName("is_cancelled")
boolean isCancelled;
@SerializedName("is_hidden")
boolean isHidden;
@SerializedName("was_forced")
boolean wasForced;
@SerializedName("original_amount")
BigDecimal originalAmount;
@SerializedName("remaining_amount")
BigDecimal remainingAmount;
@SerializedName("executed_amount")
BigDecimal executedAmount;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add(ID, id)
.add(SYMBOL, symbol)
.add(EXCHANGE, exchange)
.add(PRICE, price)
.add(AVG_EXECUTION_PRICE, avgExecutionPrice)
.add("side", side)
.add("type", type)
.add(TIMESTAMP, timestamp)
.add(IS_LIVE, isLive)
.add(IS_CANCELLED, isCancelled)
.add(IS_HIDDEN, isHidden)
.add(WAS_FORCED, wasForced)
.add(ORIGINAL_AMOUNT, originalAmount)
.add(REMAINING_AMOUNT, remainingAmount)
.add(EXECUTED_AMOUNT, executedAmount)
.toString();
}
}
/** GSON class for a Bitfinex 'pubticker' API call response. */
private static class BitfinexTicker {
BigDecimal mid;
BigDecimal bid;
BigDecimal ask;
@SerializedName("last_price")
BigDecimal lastPrice;
BigDecimal low;
BigDecimal high;
BigDecimal volume;
String timestamp;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("mid", mid)
.add("bid", bid)
.add("ask", ask)
.add("lastPrice", lastPrice)
.add("low", low)
.add("high", high)
.add("volume", volume)
.add(TIMESTAMP, timestamp)
.toString();
}
}
/**
* GSON class for holding Bitfinex response from 'account_infos' API call.
*
* <p>This is a lot of work to just get the exchange fees!
*
* <p>We want the taker fees.
*
* <pre>
* [
* {
* "maker_fees": "0.1",
* "taker_fees": "0.2",
* "fees": [
* {
* "pairs": "BTC",
* "maker_fees": "0.1",
* "taker_fees": "0.2"
* },
* {
* "pairs": "LTC",
* "maker_fees": "0.1",
* "taker_fees": "0.2"
* },
* {
* "pairs": "DRK",
* "maker_fees": "0.1",
* "taker_fees": "0.2"
* }
* ]
* }
* ]
* </pre>
*/
private static class BitfinexAccountInfos extends ArrayList<BitfinexAccountInfo> {
private static final long serialVersionUID = 5516521641453401953L;
}
/** GSON class for holding Bitfinex Account Info. */
private static class BitfinexAccountInfo {
@SerializedName("maker_fees")
BigDecimal makerFees;
@SerializedName("taker_fees")
BigDecimal takerFees;
BitfinexPairFees fees;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("makerFees", makerFees)
.add("takerFees", takerFees)
.add("fees", fees)
.toString();
}
}
/** GSON class for holding Bitfinex Pair Fees. */
private static class BitfinexPairFees extends ArrayList<BitfinexPairFee> {
private static final long serialVersionUID = 1516526641473401953L;
}
/** GSON class for holding Bitfinex Pair Fee. */
private static class BitfinexPairFee {
String pairs;
@SerializedName("maker_fees")
BigDecimal makerFees;
@SerializedName("taker_fees")
BigDecimal takerFees;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("pairs", pairs)
.add("makerFees", makerFees)
.add("takerFees", takerFees)
.toString();
}
}
/**
* GSON class for holding Bitfinex response from 'balances' API call.
*
* <p>Basically an array of BitfinexAccountBalance types.
*
* <pre>
* [
* {"type":"deposit","currency":"btc","amount":"0.12347175","available":"0.001"},
* {"type":"deposit","currency":"usd","amount":"0.0","available":"0.0"},
* {"type":"exchange","currency":"btc","amount":"0.0","available":"0.0"},
* {"type":"exchange","currency":"usd","amount":"0.0","available":"0.0"},
* {"type":"trading","currency":"btc","amount":"0.0","available":"0.0"},
* {"type":"trading","currency":"usd","amount":"0.0","available":"0.0"}
* ]
* </pre>
*/
private static class BitfinexBalances extends ArrayList<BitfinexAccountBalance> {
private static final long serialVersionUID = 5516523641953401953L;
}
/**
* GSON class for holding a Bitfinex account type balance info.
*
* <p>There are 3 types of account: 'deposit' (swaps), 'exchange' (limit orders), 'trading'
* (margin).
*
* <pre>
* [
* {"type":"deposit","currency":"btc","amount":"0.12347175","available":"0.001"},
* {"type":"deposit","currency":"usd","amount":"0.0","available":"0.0"},
* {"type":"exchange","currency":"btc","amount":"0.0","available":"0.0"},
* {"type":"exchange","currency":"usd","amount":"0.0","available":"0.0"},
* {"type":"trading","currency":"btc","amount":"0.0","available":"0.0"},
* {"type":"trading","currency":"usd","amount":"0.0","available":"0.0"}
* ]
* </pre>
*/
private static class BitfinexAccountBalance {
String type;
String currency;
BigDecimal amount;
BigDecimal available;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("type", type)
.add("currency", currency)
.add(AMOUNT, amount)
.add("available", available)
.toString();
}
}
/** GSON class for Bitfinex 'order/new' response. */
private static class BitfinexNewOrderResponse {
long id; // same as order_id
String symbol;
String exchange;
BigDecimal price;
@SerializedName("avg_execution_price")
BigDecimal avgExecutionPrice;
String side; // e.g. "sell"
String type; // e.g. "exchange limit"
String timestamp;
@SerializedName("is_live")
boolean isLive;
@SerializedName("is_cancelled")
boolean isCancelled;
@SerializedName("is_hidden")
boolean isHidden;
@SerializedName("was_forced")
boolean wasForced;
@SerializedName("original_amount")
BigDecimal originalAmount;
@SerializedName("remaining_amount")
BigDecimal remainingAmount;
@SerializedName("executed_amount")
BigDecimal executedAmount;
@SerializedName("order_id")
long orderId; // same as id
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add(ID, id)
.add(SYMBOL, symbol)
.add(EXCHANGE, exchange)
.add(PRICE, price)
.add(AVG_EXECUTION_PRICE, avgExecutionPrice)
.add("side", side)
.add("type", type)
.add(TIMESTAMP, timestamp)
.add(IS_LIVE, isLive)
.add(IS_CANCELLED, isCancelled)
.add(IS_HIDDEN, isHidden)
.add(WAS_FORCED, wasForced)
.add(ORIGINAL_AMOUNT, originalAmount)
.add(REMAINING_AMOUNT, remainingAmount)
.add(EXECUTED_AMOUNT, executedAmount)
.add("orderId", orderId)
.toString();
}
}
/** GSON class for Bitfinex 'order/cancel' response. */
private static class BitfinexCancelOrderResponse {
long id; // only get this param; there is no order_id
String symbol;
String exchange;
BigDecimal price;
@SerializedName("avg_execution_price")
BigDecimal avgExecutionPrice;
String side; // e.g. "sell"
String type; // e.g. "exchange limit"
String timestamp;
@SerializedName("is_live")
boolean isLive;
@SerializedName("is_cancelled")
boolean isCancelled;
@SerializedName("is_hidden")
boolean isHidden;
@SerializedName("was_forced")
boolean wasForced;
@SerializedName("original_amount")
BigDecimal originalAmount;
@SerializedName("remaining_amount")
BigDecimal remainingAmount;
@SerializedName("executed_amount")
BigDecimal executedAmount;
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add(ID, id)
.add(SYMBOL, symbol)
.add(EXCHANGE, exchange)
.add(PRICE, price)
.add(AVG_EXECUTION_PRICE, avgExecutionPrice)
.add("side", side)
.add("type", type)
.add(TIMESTAMP, timestamp)
.add(IS_LIVE, isLive)
.add(IS_CANCELLED, isCancelled)
.add(IS_HIDDEN, isHidden)
.add(WAS_FORCED, wasForced)
.add(ORIGINAL_AMOUNT, originalAmount)
.add(REMAINING_AMOUNT, remainingAmount)
.add(EXECUTED_AMOUNT, executedAmount)
.toString();
}
}
// --------------------------------------------------------------------------
// Transport layer methods
// --------------------------------------------------------------------------
private ExchangeHttpResponse sendPublicRequestToExchange(String apiMethod)
throws ExchangeNetworkException, TradingApiException {
try {
final URL url = new URL(PUBLIC_API_BASE_URL + apiMethod);
return makeNetworkRequest(url, "GET", null, createHeaderParamMap());
} catch (MalformedURLException e) {
final String errorMsg = UNEXPECTED_IO_ERROR_MSG;
LOG.error(errorMsg, e);
throw new TradingApiException(errorMsg, e);
}
}
/*
* Makes an authenticated API call to the Bitfinex exchange.
*
* Bitfinex Example:
*
* POST https://api.bitfinex.com/v1/order/new
*
* With JSON payload of:
* {
* "request": "/v1/<request-type>
* "nonce": "1234",
* "other-params : "for the request if any..."
* }
*
* To authenticate a request, we must calculate the following:
*
* payload = request-parameters-dictionary -> JSON encode -> base64
* signature = HMAC-SHA384(payload, api-secret) as hexadecimal in lowercase (MUST be lowercase)
* send (api-key, payload, signature)
*
* These are sent as HTTP headers named:
*
* X-BFX-APIKEY
* X-BFX-PAYLOAD
* X-BFX-SIGNATURE
*/
private ExchangeHttpResponse sendAuthenticatedRequestToExchange(
String apiMethod, Map<String, Object> params)
throws ExchangeNetworkException, TradingApiException {
if (!initializedMacAuthentication) {
final String errorMsg = "MAC Message security layer has not been initialized.";
LOG.error(errorMsg);
throw new IllegalStateException(errorMsg);
}
try {
if (params == null) {
// create empty map for non param API calls, e.g. "balances"
params = createRequestParamMap();
}
// nonce is required by Bitfinex in every request
params.put("nonce", Long.toString(nonce));
nonce++; // increment ready for next call.
// must include the method in request param too
params.put("request", "/" + BITFINEX_API_VERSION + "/" + apiMethod);
// JSON-ify the param dictionary
final String paramsInJson = gson.toJson(params);
// Need to base64 encode payload as per API
final String base64payload =
DatatypeConverter.printBase64Binary(paramsInJson.getBytes(StandardCharsets.UTF_8));
// Request headers required by Exchange
final Map<String, String> requestHeaders = createHeaderParamMap();
requestHeaders.put("X-BFX-APIKEY", key);
requestHeaders.put("X-BFX-PAYLOAD", base64payload);
// Add the signature
mac.reset(); // force reset
mac.update(base64payload.getBytes(StandardCharsets.UTF_8));
/*
* signature = HMAC-SHA384(payload, api-secret) as hexadecimal - MUST be in LOWERCASE else
* signature fails. See:
* http://bitcoin.stackexchange.com/questions/25835/bitfinex-api-call-returns-400-bad-request
*/
final String signature = toHex(mac.doFinal()).toLowerCase();
requestHeaders.put("X-BFX-SIGNATURE", signature);
// payload is JSON for this exchange
requestHeaders.put("Content-Type", "application/json");
final URL url = new URL(AUTHENTICATED_API_URL + apiMethod);
return makeNetworkRequest(url, "POST", paramsInJson, requestHeaders);
} catch (MalformedURLException e) {
final String errorMsg = UNEXPECTED_IO_ERROR_MSG;
LOG.error(errorMsg, e);
throw new TradingApiException(errorMsg, e);
}
}
private String toHex(byte[] byteArrayToConvert) {
final StringBuilder hexString = new StringBuilder();
for (final byte aByte : byteArrayToConvert) {
hexString.append(String.format("%02x", aByte & 0xff));
}
return hexString.toString();