use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol in project crypto-bot by jnidzwetzki.
the class DonchianBot method run.
@Override
public void run() {
try {
logger.info("===============================================");
logger.info("Starting with {} API keys", apiBrokerList.size());
logger.info("===============================================");
for (final BitfinexApiBroker bitfinexApiBroker : apiBrokerList) {
bitfinexApiBroker.connect();
}
final Map<BitfinexCandlestickSymbol, TimeSeries> historicalCandles = HistoricalCandlesHelper.requestHistoricalCandles(apiBrokerList.get(0), TIMEFRAME, tradedCurrencies);
historicalCandles.forEach((k, v) -> timeSeries.put(k.getSymbol(), v));
registerTicker();
while (true) {
if (tickerLatch != null) {
tickerLatch.await();
}
executeSystem();
tickerLatch = new CountDownLatch(tradedCurrencies.size());
}
} catch (Throwable e) {
logger.error("Got exception", e);
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol 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.symbol.BitfinexCandlestickSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class IntegrationTest method testCandleStream.
/**
* Test the candle stream
*/
@Test(timeout = 10000)
public void testCandleStream() {
final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
// Await at least 10 callbacks
final CountDownLatch latch = new CountDownLatch(10);
try {
bitfinexClient.connect();
final BitfinexCandlestickSymbol symbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1);
final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
final BiConsumer<BitfinexCandlestickSymbol, BitfinexTick> callback = (c, o) -> {
latch.countDown();
};
orderbookManager.registerCandlestickCallback(symbol, callback);
orderbookManager.subscribeCandles(symbol);
latch.await();
orderbookManager.unsubscribeCandles(symbol);
Assert.assertTrue(orderbookManager.removeCandlestickCallback(symbol, callback));
Assert.assertFalse(orderbookManager.removeCandlestickCallback(symbol, callback));
} catch (Exception e) {
// Should not happen
e.printStackTrace();
Assert.assertTrue(false);
} finally {
bitfinexClient.close();
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class QuoteManager method subscribeCandles.
/**
* Subscribe candles for a symbol
* @param currencyPair
* @param timeframe
*/
public void subscribeCandles(final BitfinexCandlestickSymbol symbol) {
final SubscribeCandlesCommand command = new SubscribeCandlesCommand(symbol);
bitfinexApiBroker.sendCommand(command);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class CandlestickHandlerTest method testCandlestickSymbolEncoding1.
/**
* Test the symbol encoding and decoding
*/
@Test
public void testCandlestickSymbolEncoding1() {
final BitfinexCandlestickSymbol symbol1 = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BCH_USD, Timeframe.MINUTES_15);
final BitfinexCandlestickSymbol symbol2 = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_15);
Assert.assertFalse(symbol1.equals(symbol2));
final String symbol1String = symbol1.toBifinexCandlestickString();
final String symbol2String = symbol2.toBifinexCandlestickString();
Assert.assertEquals(symbol1, BitfinexCandlestickSymbol.fromBitfinexString(symbol1String));
Assert.assertEquals(symbol2, BitfinexCandlestickSymbol.fromBitfinexString(symbol2String));
}
Aggregations