use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class EMABot method registerTicker.
protected void registerTicker(final BitfinexApiBroker bitfinexApiBroker) throws InterruptedException, APIException {
logger.info("Register ticker");
for (final BitfinexCurrencyPair currency : tradedCurrencies) {
tickMerger.put(currency, new BarMerger(currency, TIMEFRAME, (s, t) -> barDoneCallback(s, t)));
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
bitfinexApiBroker.getQuoteManager().subscribeTicker(symbol);
logger.info("Wait for ticker");
while (!bitfinexApiBroker.isTickerActive(symbol)) {
Thread.sleep(100);
}
bitfinexApiBroker.getQuoteManager().registerTickCallback(symbol, (s, c) -> handleTickCallback(s, c));
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair 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.BitfinexCurrencyPair in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class TickHandlerTest method testTickUpdateAndNotify.
/**
* Test the parsing of one tick
* @throws APIException
* @throws InterruptedException
*/
@Test(timeout = 20000)
public void testTickUpdateAndNotify() throws APIException, InterruptedException {
final String callbackValue = "[26123,41.4645776,26129,33.68138507,2931,0.2231,26129,144327.10936387,26149,13139]";
final JSONArray jsonArray = new JSONArray(callbackValue);
final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.BTC_USD;
final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currencyPair);
final ExecutorService executorService = Executors.newFixedThreadPool(10);
final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
final QuoteManager tickerManager = new QuoteManager(bitfinexApiBroker);
Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(tickerManager);
final CountDownLatch latch = new CountDownLatch(1);
tickerManager.registerTickCallback(symbol, (s, c) -> {
try {
Assert.assertEquals(symbol, s);
Assert.assertEquals(26129, c.getOpen().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getClose().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getHigh().doubleValue(), DELTA);
Assert.assertEquals(26129, c.getLow().doubleValue(), DELTA);
Assert.assertEquals(BitfinexTick.INVALID_VOLUME.doubleValue(), c.getVolume().doubleValue(), DELTA);
} catch (Throwable e) {
System.out.println(e);
throw e;
}
latch.countDown();
});
Assert.assertEquals(-1, tickerManager.getHeartbeatForSymbol(symbol));
Assert.assertEquals(null, tickerManager.getLastTick(symbol));
final TickHandler tickHandler = new TickHandler();
final long now = System.currentTimeMillis();
tickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
// Tick callbacks are handled async
latch.await();
Assert.assertTrue(now <= tickerManager.getHeartbeatForSymbol(symbol));
Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class BarMegerTest method testTickMerger2.
/**
* Test two tick merge
* @throws InterruptedException
* @throws IOException
* @throws ParseException
*/
@Test(timeout = 5000)
public void testTickMerger2() throws InterruptedException, IOException, ParseException {
final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
final CountDownLatch latch = new CountDownLatch(1);
final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (s, t) -> {
Assert.assertEquals(10, t.getVolume().doubleValue(), DELTA);
Assert.assertEquals(1.0, t.getMinPrice().doubleValue(), DELTA);
Assert.assertEquals(2.0, t.getMaxPrice().doubleValue(), DELTA);
latch.countDown();
};
final BarMerger tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, tickConsumer);
tickMerger.addNewPrice(parser.parse("01:01:13").getTime(), 1.0, 5.0);
tickMerger.addNewPrice(parser.parse("01:01:23").getTime(), 2.0, 5.0);
tickMerger.close();
latch.await();
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair in project crypto-bot by jnidzwetzki.
the class BarMegerTest method testTickAlignment1.
/**
* Test the alignment of the ticks
* @throws InterruptedException
* @throws IOException
* @throws ParseException
*/
@Test(timeout = 10000)
public void testTickAlignment1() throws InterruptedException, IOException, ParseException {
final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
final CountDownLatch latch = new CountDownLatch(3);
final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (s, t) -> {
Assert.assertTrue(t.getEndTime().getSecond() == 59);
latch.countDown();
};
final BarMerger tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, tickConsumer);
tickMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
tickMerger.addNewPrice(parser.parse("01:02:33").getTime(), 2.0, 5.0);
tickMerger.addNewPrice(parser.parse("02:03:53").getTime(), 2.0, 5.0);
tickMerger.addNewPrice(parser.parse("22:22:53").getTime(), 2.0, 5.0);
tickMerger.close();
latch.await();
}
Aggregations