Search in sources :

Example 6 with QuoteManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager 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();
    }
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) OrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry) OrderBookPrecision(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision) Test(org.junit.Test) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) OrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager) OrderBookFrequency(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) BiConsumer(java.util.function.BiConsumer) ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) Assert(org.junit.Assert) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) CountDownLatch(java.util.concurrent.CountDownLatch) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Test(org.junit.Test)

Example 7 with QuoteManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager 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);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) TickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler) Test(org.junit.Test)

Example 8 with QuoteManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class HeartbeatThread method checkTickerFreshness.

/**
 * Are all ticker uptodate
 * @return
 */
private boolean checkTickerFreshness() {
    final long currentTime = System.currentTimeMillis();
    final QuoteManager tickerManager = bitfinexApiBroker.getQuoteManager();
    final Set<BitfinexStreamSymbol> activeSymbols = tickerManager.getActiveSymbols();
    for (final BitfinexStreamSymbol symbol : activeSymbols) {
        final long lastHeatbeat = tickerManager.getHeartbeatForSymbol(symbol);
        if (lastHeatbeat == -1) {
            continue;
        }
        if (lastHeatbeat + TICKER_TIMEOUT < currentTime) {
            logger.error("Last update for symbol {} is {} current time is {}, the data is outdated", symbol, lastHeatbeat, currentTime);
            return false;
        }
    }
    return true;
}
Also used : BitfinexStreamSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexStreamSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)

Example 9 with QuoteManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class IntegrationTest method testReconnect.

/**
 * Test the session reconnect
 * @throws APIException
 * @throws InterruptedException
 */
@Test
public void testReconnect() throws APIException, InterruptedException {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    bitfinexClient.connect();
    final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(BitfinexCurrencyPair.BTC_USD);
    final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
    orderbookManager.subscribeTicker(symbol);
    Thread.sleep(1000);
    bitfinexClient.reconnect();
    // Await at least 2 callbacks
    final CountDownLatch latch = new CountDownLatch(2);
    final BiConsumer<BitfinexTickerSymbol, BitfinexTick> callback = (c, o) -> {
        latch.countDown();
    };
    orderbookManager.registerTickCallback(symbol, callback);
    latch.await();
    Assert.assertTrue(bitfinexClient.isTickerActive(symbol));
    orderbookManager.unsubscribeTicker(symbol);
    Assert.assertFalse(bitfinexClient.isTickerActive(symbol));
    bitfinexClient.close();
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) OrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry) OrderBookPrecision(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision) Test(org.junit.Test) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) OrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager) OrderBookFrequency(com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) BiConsumer(java.util.function.BiConsumer) ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) Assert(org.junit.Assert) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with QuoteManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class CandlestickHandlerTest method testCandlestickUpdateAndNotify.

/**
 * Test the parsing of one candlestick
 * @throws APIException
 */
@Test
public void testCandlestickUpdateAndNotify() throws APIException {
    final String callbackValue = "[15134900000,15996,15997,16000,15980,318.5139342]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexCandlestickSymbol symbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1);
    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 AtomicInteger counter = new AtomicInteger(0);
    tickerManager.registerCandlestickCallback(symbol, (s, c) -> {
        counter.incrementAndGet();
        Assert.assertEquals(symbol, s);
        Assert.assertEquals(15996, c.getOpen().doubleValue(), DELTA);
        Assert.assertEquals(15997, c.getClose().doubleValue(), DELTA);
        Assert.assertEquals(16000, c.getHigh().doubleValue(), DELTA);
        Assert.assertEquals(15980, c.getLow().doubleValue(), DELTA);
        Assert.assertEquals(318.5139342, c.getVolume().doubleValue(), DELTA);
    });
    final CandlestickHandler candlestickHandler = new CandlestickHandler();
    candlestickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    Assert.assertEquals(1, counter.get());
}
Also used : CandlestickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) Test(org.junit.Test)

Aggregations

QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)11 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)6 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)6 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)6 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)6 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)5 ExecutorService (java.util.concurrent.ExecutorService)5 JSONArray (org.json.JSONArray)5 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)4 ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)4 OrderBookFrequency (com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency)4 OrderBookPrecision (com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision)4 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)4 OrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry)4 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)4 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)4 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)4