Search in sources :

Example 1 with TickHandler

use of com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler 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 2 with TickHandler

use of com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class BitfinexApiBroker method handleChannelDataArray.

/**
 * Handle the channel data with has an array at first position
 * @param jsonArray
 * @param channelSymbol
 * @throws APIException
 */
private void handleChannelDataArray(final JSONArray jsonArray, final BitfinexStreamSymbol channelSymbol) throws APIException {
    final JSONArray subarray = jsonArray.getJSONArray(1);
    if (channelSymbol instanceof BitfinexCandlestickSymbol) {
        final ChannelCallbackHandler handler = new CandlestickHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof RawOrderbookConfiguration) {
        final RawOrderbookHandler handler = new RawOrderbookHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof OrderbookConfiguration) {
        final OrderbookHandler handler = new OrderbookHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof BitfinexTickerSymbol) {
        final ChannelCallbackHandler handler = new TickHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else if (channelSymbol instanceof BitfinexExecutedTradeSymbol) {
        final ChannelCallbackHandler handler = new ExecutedTradeHandler();
        handler.handleChannelData(this, channelSymbol, subarray);
    } else {
        logger.error("Unknown stream type: {}", channelSymbol);
    }
}
Also used : CandlestickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler) RawOrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) ChannelCallbackHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ChannelCallbackHandler) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) JSONArray(org.json.JSONArray) RawOrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler) OrderbookHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.OrderbookHandler) ExecutedTradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) TickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)

Aggregations

TickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)2 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)2 JSONArray (org.json.JSONArray)2 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)1 CandlestickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler)1 ChannelCallbackHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.ChannelCallbackHandler)1 ExecutedTradeHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler)1 OrderbookHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.OrderbookHandler)1 RawOrderbookHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler)1 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)1 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)1 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)1 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)1 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)1 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1