Search in sources :

Example 1 with ExecutedTradeHandler

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

the class ExecutedTradesHandlerTest method testExecutedTradesSnapshotUpdateAndNotify.

/**
 * Test the parsing of a executed trades snapshot
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testExecutedTradesSnapshotUpdateAndNotify() throws APIException, InterruptedException {
    final String callbackValue = "[[190631057,1518037080162,0.007,8175.9],[190631052,1518037080110,-0.25,8175.8]]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexExecutedTradeSymbol symbol = new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BTC_USD);
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    final QuoteManager quoteManager = new QuoteManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(quoteManager);
    final CountDownLatch latch = new CountDownLatch(2);
    quoteManager.registerExecutedTradeCallback(symbol, (s, c) -> {
        try {
            Assert.assertEquals(symbol, s);
            if (c.getId() == 190631057) {
                Assert.assertEquals(190631057, c.getId());
                Assert.assertEquals(1518037080162l, c.getTimestamp());
                Assert.assertEquals(0.007, c.getAmount().doubleValue(), DELTA);
                Assert.assertEquals(8175.9, c.getPrice().doubleValue(), DELTA);
            } else if (c.getId() == 190631052) {
                Assert.assertEquals(190631052, c.getId());
                Assert.assertEquals(1518037080110l, c.getTimestamp());
                Assert.assertEquals(-0.25, c.getAmount().doubleValue(), DELTA);
                Assert.assertEquals(8175.8, c.getPrice().doubleValue(), DELTA);
            } else {
                throw new IllegalArgumentException("Illegal call, expected 2 trades");
            }
            latch.countDown();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    });
    final ExecutedTradeHandler handler = new ExecutedTradeHandler();
    handler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    latch.await();
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) ExecutedTradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with ExecutedTradeHandler

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

the class ExecutedTradesHandlerTest method testExecutedTradesUpdateAndNotify.

/**
 * Test the parsing of one executed trade
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testExecutedTradesUpdateAndNotify() throws APIException, InterruptedException {
    final String callbackValue = "[190631057,1518037080162,0.007,8175.9]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexExecutedTradeSymbol symbol = new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BTC_USD);
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    final QuoteManager quoteManager = new QuoteManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(quoteManager);
    final CountDownLatch latch = new CountDownLatch(1);
    quoteManager.registerExecutedTradeCallback(symbol, (s, c) -> {
        try {
            Assert.assertEquals(symbol, s);
            Assert.assertEquals(190631057, c.getId());
            Assert.assertEquals(1518037080162l, c.getTimestamp());
            Assert.assertEquals(0.007, c.getAmount().doubleValue(), DELTA);
            Assert.assertEquals(8175.9, c.getPrice().doubleValue(), DELTA);
            latch.countDown();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    });
    final ExecutedTradeHandler handler = new ExecutedTradeHandler();
    handler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    latch.await();
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) ExecutedTradeHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.ExecutedTradeHandler) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with ExecutedTradeHandler

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

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