Search in sources :

Example 11 with BitfinexCandlestickSymbol

use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol 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 12 with BitfinexCandlestickSymbol

use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol 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)

Example 13 with BitfinexCandlestickSymbol

use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class CandlestickHandler method handleChannelData.

/**
 * Handle a candlestick callback
 * @param channel
 * @param subarray
 */
@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final BitfinexStreamSymbol channelSymbol, final JSONArray jsonArray) throws APIException {
    // channel symbol trade:1m:tLTCUSD
    final List<BitfinexTick> ticksBuffer = new ArrayList<>();
    // Snapshots contain multiple Bars, Updates only one
    if (jsonArray.get(0) instanceof JSONArray) {
        for (int pos = 0; pos < jsonArray.length(); pos++) {
            final JSONArray parts = jsonArray.getJSONArray(pos);
            parseCandlestick(ticksBuffer, parts);
        }
    } else {
        parseCandlestick(ticksBuffer, jsonArray);
    }
    // Use natural ordering
    ticksBuffer.sort(null);
    final BitfinexCandlestickSymbol candlestickSymbol = (BitfinexCandlestickSymbol) channelSymbol;
    bitfinexApiBroker.getQuoteManager().handleCandlestickList(candlestickSymbol, ticksBuffer);
}
Also used : BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray)

Aggregations

BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)11 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)6 Test (org.junit.Test)5 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)4 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)4 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)4 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)4 JSONArray (org.json.JSONArray)4 CandlestickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.CandlestickHandler)3 SubscribeCandlesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand)3 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)3 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 SubscribeOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand)2 SubscribeRawOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)2 SubscribeTickerCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand)2 SubscribeTradesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTradesCommand)2 UnsubscribeChannelCommand (com.github.jnidzwetzki.bitfinex.v2.commands.UnsubscribeChannelCommand)2 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)2 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)2