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);
}
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);
}
}
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;
}
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);
}
}
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);
}
Aggregations