Search in sources :

Example 1 with RawOrderbookConfiguration

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

the class IntegrationTest method testRawOrderbookStream.

/**
 * Test the raw orderbook stream
 */
@Test(timeout = 10000)
public void testRawOrderbookStream() {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    // Await at least 20 callbacks
    final CountDownLatch latch = new CountDownLatch(20);
    try {
        bitfinexClient.connect();
        final RawOrderbookConfiguration orderbookConfiguration = new RawOrderbookConfiguration(BitfinexCurrencyPair.BTC_USD);
        final RawOrderbookManager rawOrderbookManager = bitfinexClient.getRawOrderbookManager();
        final BiConsumer<RawOrderbookConfiguration, RawOrderbookEntry> callback = (c, o) -> {
            Assert.assertTrue(o.getAmount().doubleValue() != 0);
            Assert.assertTrue(o.getPrice().doubleValue() != 0);
            Assert.assertTrue(o.getOrderId() >= 0);
            Assert.assertTrue(o.toString().length() > 0);
            latch.countDown();
        };
        rawOrderbookManager.registerOrderbookCallback(orderbookConfiguration, callback);
        rawOrderbookManager.subscribeOrderbook(orderbookConfiguration);
        latch.await();
        rawOrderbookManager.unsubscribeOrderbook(orderbookConfiguration);
        Assert.assertTrue(rawOrderbookManager.removeOrderbookCallback(orderbookConfiguration, callback));
        Assert.assertFalse(rawOrderbookManager.removeOrderbookCallback(orderbookConfiguration, 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) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) RawOrderbookManager(com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager) RawOrderbookEntry(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry) CountDownLatch(java.util.concurrent.CountDownLatch) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Test(org.junit.Test)

Example 2 with RawOrderbookConfiguration

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

the class RawOrderbookTest method testTradingOrderbookEquals.

/**
 * Test the equals method
 */
@Test
public void testTradingOrderbookEquals() {
    final RawOrderbookConfiguration configuration1 = new RawOrderbookConfiguration(BitfinexCurrencyPair.BCH_USD);
    final RawOrderbookConfiguration configuration2 = new RawOrderbookConfiguration(BitfinexCurrencyPair.BCH_USD);
    final RawOrderbookConfiguration configuration3 = new RawOrderbookConfiguration(BitfinexCurrencyPair.AVT_BTC);
    Assert.assertEquals(configuration1.hashCode(), configuration2.hashCode());
    Assert.assertEquals(configuration1, configuration2);
    Assert.assertFalse(configuration1.equals(configuration3));
}
Also used : RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) Test(org.junit.Test)

Example 3 with RawOrderbookConfiguration

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

the class RawOrderbookManager method subscribeOrderbook.

/**
 * Subscribe a orderbook
 * @param currencyPair
 * @param orderBookPrecision
 * @param orderBookFrequency
 * @param pricePoints
 */
public void subscribeOrderbook(final RawOrderbookConfiguration orderbookConfiguration) {
    final SubscribeRawOrderbookCommand subscribeOrderbookCommand = new SubscribeRawOrderbookCommand(orderbookConfiguration);
    bitfinexApiBroker.sendCommand(subscribeOrderbookCommand);
}
Also used : SubscribeRawOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)

Example 4 with RawOrderbookConfiguration

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

the class BitfinexApiBroker method resubscribeChannels.

/**
 * Resubscribe the old ticker
 * @throws InterruptedException
 * @throws APIException
 */
private void resubscribeChannels() throws InterruptedException, APIException {
    final Map<Integer, BitfinexStreamSymbol> oldChannelIdSymbolMap = new HashMap<>();
    synchronized (channelIdSymbolMap) {
        oldChannelIdSymbolMap.putAll(channelIdSymbolMap);
        channelIdSymbolMap.clear();
        channelIdSymbolMap.notifyAll();
    }
    // Resubscribe channels
    for (BitfinexStreamSymbol symbol : oldChannelIdSymbolMap.values()) {
        if (symbol instanceof BitfinexTickerSymbol) {
            sendCommand(new SubscribeTickerCommand((BitfinexTickerSymbol) symbol));
        } else if (symbol instanceof BitfinexExecutedTradeSymbol) {
            sendCommand(new SubscribeTradesCommand((BitfinexExecutedTradeSymbol) symbol));
        } else if (symbol instanceof BitfinexCandlestickSymbol) {
            sendCommand(new SubscribeCandlesCommand((BitfinexCandlestickSymbol) symbol));
        } else if (symbol instanceof OrderbookConfiguration) {
            sendCommand(new SubscribeOrderbookCommand((OrderbookConfiguration) symbol));
        } else if (symbol instanceof RawOrderbookConfiguration) {
            sendCommand(new SubscribeRawOrderbookCommand((RawOrderbookConfiguration) symbol));
        } else {
            logger.error("Unknown stream symbol: {}", symbol);
        }
    }
    waitForChannelResubscription(oldChannelIdSymbolMap);
}
Also used : BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) HashMap(java.util.HashMap) SubscribeOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) OrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration) SubscribeTradesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTradesCommand) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) SubscribeCandlesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand) SubscribeTickerCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand) RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BitfinexStreamSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexStreamSymbol) SubscribeRawOrderbookCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)

Example 5 with RawOrderbookConfiguration

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

RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)8 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)5 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)4 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)4 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)4 Test (org.junit.Test)4 SubscribeRawOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)3 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)2 SubscribeCandlesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand)2 SubscribeOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand)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 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)2 JSONArray (org.json.JSONArray)2 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