Search in sources :

Example 1 with CandlestickHandler

use of com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler 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)

Example 2 with CandlestickHandler

use of com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler 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)

Example 3 with CandlestickHandler

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

the class CandlestickHandlerTest method testCandlestickSnapshotUpdateAndNotify.

/**
 * Test the parsing of a candlestick snapshot
 * @throws APIException
 */
@Test
public void testCandlestickSnapshotUpdateAndNotify() throws APIException {
    final String callbackValue = "[[15134900000,15996,15997,16000,15980,318.5139342],[15135100000,15899,15996,16097,15890,1137.180342268]]";
    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) -> {
        Assert.assertEquals(symbol, s);
        final int counterValue = counter.getAndIncrement();
        if (counterValue == 0) {
            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);
        } else if (counterValue == 1) {
            Assert.assertEquals(15899, c.getOpen().doubleValue(), DELTA);
            Assert.assertEquals(15996, c.getClose().doubleValue(), DELTA);
            Assert.assertEquals(16097, c.getHigh().doubleValue(), DELTA);
            Assert.assertEquals(15890, c.getLow().doubleValue(), DELTA);
            Assert.assertEquals(1137.180342268, c.getVolume().doubleValue(), DELTA);
        } else {
            throw new IllegalArgumentException("Illegal call, expected 2 candlesticks");
        }
    });
    final CandlestickHandler candlestickHandler = new CandlestickHandler();
    candlestickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    Assert.assertEquals(2, 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

CandlestickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler)3 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)3 JSONArray (org.json.JSONArray)3 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)2 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 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 TickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)1 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)1 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)1 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)1 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)1