Search in sources :

Example 1 with BitfinexStreamSymbol

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

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

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

the class HeartbeatThread method checkTickerFreshness.

/**
 * Are all ticker uptodate
 * @return
 */
private boolean checkTickerFreshness() {
    final long currentTime = System.currentTimeMillis();
    final QuoteManager tickerManager = bitfinexApiBroker.getQuoteManager();
    final Set<BitfinexStreamSymbol> activeSymbols = tickerManager.getActiveSymbols();
    for (final BitfinexStreamSymbol symbol : activeSymbols) {
        final long lastHeatbeat = tickerManager.getHeartbeatForSymbol(symbol);
        if (lastHeatbeat == -1) {
            continue;
        }
        if (lastHeatbeat + TICKER_TIMEOUT < currentTime) {
            logger.error("Last update for symbol {} is {} current time is {}, the data is outdated", symbol, lastHeatbeat, currentTime);
            return false;
        }
    }
    return true;
}
Also used : BitfinexStreamSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexStreamSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)

Example 4 with BitfinexStreamSymbol

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

the class RawOrderbookHandler method handleChannelData.

@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final BitfinexStreamSymbol channelSymbol, final JSONArray jsonArray) throws APIException {
    final RawOrderbookConfiguration configuration = (RawOrderbookConfiguration) channelSymbol;
    // Example: [13182,1,-0.1]
    try {
        // Snapshots contain multiple Orderbook entries, updates only one
        if (jsonArray.get(0) instanceof JSONArray) {
            for (int pos = 0; pos < jsonArray.length(); pos++) {
                final JSONArray parts = jsonArray.getJSONArray(pos);
                handleEntry(bitfinexApiBroker, configuration, parts);
            }
        } else {
            handleEntry(bitfinexApiBroker, configuration, jsonArray);
        }
    } catch (JSONException e) {
        throw new APIException(e);
    }
}
Also used : RawOrderbookConfiguration(com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 5 with BitfinexStreamSymbol

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

the class TickHandler method handleChannelData.

/**
 * Handle a tick callback
 * @param channel
 * @param subarray
 */
@Override
public void handleChannelData(final BitfinexApiBroker bitfinexApiBroker, final BitfinexStreamSymbol channelSymbol, final JSONArray jsonArray) throws APIException {
    final BitfinexTickerSymbol currencyPair = (BitfinexTickerSymbol) channelSymbol;
    // 0 = BID
    // 2 = ASK
    // 6 = Price
    final BigDecimal price = jsonArray.getBigDecimal(6);
    // Volume is set to 0, because the ticker contains only the daily volume
    final BitfinexTick tick = new BitfinexTick(System.currentTimeMillis(), price, price, price, price);
    bitfinexApiBroker.getQuoteManager().handleNewTick(currencyPair, tick);
}
Also used : BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) BigDecimal(java.math.BigDecimal)

Aggregations

JSONArray (org.json.JSONArray)5 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)4 BitfinexStreamSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexStreamSymbol)4 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)3 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)3 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)3 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)3 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)3 JSONException (org.json.JSONException)3 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)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 RawOrderbookHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.RawOrderbookHandler)1 TickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)1 SubscribeCandlesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand)1 SubscribeOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand)1 SubscribeRawOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)1 SubscribeTickerCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand)1