use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project crypto-bot by jnidzwetzki.
the class HistoricalCandlesHelper method requestHistoricalCandles.
/**
* Request historical candles
*
* @param bitfinexApiBroker
* @param timeframe
* @param tradedCurrencies
* @return
* @throws InterruptedException
* @throws APIException
*/
public static Map<BitfinexCandlestickSymbol, TimeSeries> requestHistoricalCandles(final BitfinexApiBroker bitfinexApiBroker, final Timeframe timeframe, final List<BitfinexCurrencyPair> tradedCurrencies) throws InterruptedException, APIException {
logger.info("Request historical candles");
final Map<BitfinexCandlestickSymbol, TimeSeries> timeSeries = new HashMap<>();
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
final String bitfinexString = currency.toBitfinexString();
final BaseTimeSeries currencyTimeSeries = new BaseTimeSeries(bitfinexString);
final BitfinexCandlestickSymbol barSymbol = new BitfinexCandlestickSymbol(currency, timeframe);
timeSeries.put(barSymbol, currencyTimeSeries);
final CountDownLatch tickCountdown = new CountDownLatch(100);
// Add bars to timeseries callback
final BiConsumer<BitfinexCandlestickSymbol, BitfinexTick> callback = (channelSymbol, tick) -> {
final TimeSeries timeSeriesToAdd = timeSeries.get(channelSymbol);
final Bar bar = BarConverter.convertBitfinexTick(tick);
try {
timeSeriesToAdd.addBar(bar);
tickCountdown.countDown();
} catch (IllegalArgumentException e) {
logger.error("Unable to add tick {} to time series, last tick is {}", bar, timeSeriesToAdd.getLastBar());
}
};
bitfinexApiBroker.getQuoteManager().registerCandlestickCallback(barSymbol, callback);
bitfinexApiBroker.getQuoteManager().subscribeCandles(barSymbol);
// Wait for 100 bars or 10 seconds. All snapshot ticks are handled in
// a synchronized block, so we receive the full snapshot even if we
// call removeTickCallback.
tickCountdown.await(10, TimeUnit.SECONDS);
bitfinexApiBroker.getQuoteManager().registerCandlestickCallback(barSymbol, callback);
bitfinexApiBroker.getQuoteManager().unsubscribeCandles(barSymbol);
logger.info("Loaded ticks for symbol {} {}", bitfinexString, +timeSeries.get(barSymbol).getEndIndex());
}
return timeSeries;
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class ExecutedTradesHandlerTest method testExecutedTradesSnapshotUpdateAndNotify.
/**
* Test the parsing of a executed trades snapshot
* @throws APIException
* @throws InterruptedException
*/
@Test(timeout = 10000)
public void testExecutedTradesSnapshotUpdateAndNotify() throws APIException, InterruptedException {
final String callbackValue = "[[190631057,1518037080162,0.007,8175.9],[190631052,1518037080110,-0.25,8175.8]]";
final JSONArray jsonArray = new JSONArray(callbackValue);
final BitfinexExecutedTradeSymbol symbol = new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BTC_USD);
final ExecutorService executorService = Executors.newFixedThreadPool(10);
final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
final QuoteManager quoteManager = new QuoteManager(bitfinexApiBroker);
Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(quoteManager);
final CountDownLatch latch = new CountDownLatch(2);
quoteManager.registerExecutedTradeCallback(symbol, (s, c) -> {
try {
Assert.assertEquals(symbol, s);
if (c.getId() == 190631057) {
Assert.assertEquals(190631057, c.getId());
Assert.assertEquals(1518037080162l, c.getTimestamp());
Assert.assertEquals(0.007, c.getAmount().doubleValue(), DELTA);
Assert.assertEquals(8175.9, c.getPrice().doubleValue(), DELTA);
} else if (c.getId() == 190631052) {
Assert.assertEquals(190631052, c.getId());
Assert.assertEquals(1518037080110l, c.getTimestamp());
Assert.assertEquals(-0.25, c.getAmount().doubleValue(), DELTA);
Assert.assertEquals(8175.8, c.getPrice().doubleValue(), DELTA);
} else {
throw new IllegalArgumentException("Illegal call, expected 2 trades");
}
latch.countDown();
} catch (Throwable e) {
e.printStackTrace();
}
});
final ExecutedTradeHandler handler = new ExecutedTradeHandler();
handler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
latch.await();
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class ExecutedTradesHandlerTest method testExecutedTradesUpdateAndNotify.
/**
* Test the parsing of one executed trade
* @throws APIException
* @throws InterruptedException
*/
@Test(timeout = 10000)
public void testExecutedTradesUpdateAndNotify() throws APIException, InterruptedException {
final String callbackValue = "[190631057,1518037080162,0.007,8175.9]";
final JSONArray jsonArray = new JSONArray(callbackValue);
final BitfinexExecutedTradeSymbol symbol = new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BTC_USD);
final ExecutorService executorService = Executors.newFixedThreadPool(10);
final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
final QuoteManager quoteManager = new QuoteManager(bitfinexApiBroker);
Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(quoteManager);
final CountDownLatch latch = new CountDownLatch(1);
quoteManager.registerExecutedTradeCallback(symbol, (s, c) -> {
try {
Assert.assertEquals(symbol, s);
Assert.assertEquals(190631057, c.getId());
Assert.assertEquals(1518037080162l, c.getTimestamp());
Assert.assertEquals(0.007, c.getAmount().doubleValue(), DELTA);
Assert.assertEquals(8175.9, c.getPrice().doubleValue(), DELTA);
latch.countDown();
} catch (Throwable e) {
e.printStackTrace();
}
});
final ExecutedTradeHandler handler = new ExecutedTradeHandler();
handler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
latch.await();
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManagerTest method testCancelOrderUnauth.
/**
* Test the cancelation of an order
* @throws InterruptedException
* @throws APIException
*/
@Test(expected = APIException.class)
public void testCancelOrderUnauth() throws APIException, InterruptedException {
final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
Mockito.when(bitfinexApiBroker.getCapabilities()).thenReturn(ConnectionCapabilities.NO_CAPABILITIES);
final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
orderManager.cancelOrderAndWaitForCompletion(12);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.APIException in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class OrderManagerTest method testOrderChannelHandler2.
/**
* Test the order channel handler - snapshot
* @throws APIException
*/
@Test
public void testOrderChannelHandler2() throws APIException {
final String jsonString = "[0,\"on\",[[6784335053,null,1514956504945000,\"tIOTUSD\",1514956505134,1514956505164,-24.175121,-24.175121,\"EXCHANGE STOP\",null,null,null,0,\"ACTIVE\",null,null,3.84,0,null,null,null,null,null,0,0,0], [67843353243,null,1514956234945000,\"tBTCUSD\",1514956505134,1514956505164,-24.175121,-24.175121,\"EXCHANGE STOP\",null,null,null,0,\"ACTIVE\",null,null,3.84,0,null,null,null,null,null,0,0,0]]]";
final JSONArray jsonArray = new JSONArray(jsonString);
final OrderHandler orderHandler = new OrderHandler();
final BitfinexApiBroker bitfinexApiBroker = buildMockedBitfinexConnection();
final OrderManager orderManager = bitfinexApiBroker.getOrderManager();
Assert.assertTrue(orderManager.getOrders().isEmpty());
orderHandler.handleChannelData(bitfinexApiBroker, jsonArray);
Assert.assertEquals(2, orderManager.getOrders().size());
orderManager.clear();
Assert.assertTrue(orderManager.getOrders().isEmpty());
}
Aggregations