use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol 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();
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol 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();
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class IntegrationTest method testExecutedTradesStream.
/**
* Test executed trades stream
*/
@Test(timeout = 60000)
public void testExecutedTradesStream() {
final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
// Await at least 2 callbacks
final CountDownLatch latch = new CountDownLatch(2);
try {
bitfinexClient.connect();
final BitfinexExecutedTradeSymbol symbol = new BitfinexExecutedTradeSymbol(BitfinexCurrencyPair.BTC_USD);
final QuoteManager executedTradeManager = bitfinexClient.getQuoteManager();
final BiConsumer<BitfinexExecutedTradeSymbol, ExecutedTrade> callback = (c, o) -> {
latch.countDown();
};
executedTradeManager.registerExecutedTradeCallback(symbol, callback);
executedTradeManager.subscribeExecutedTrades(symbol);
latch.await();
executedTradeManager.unsubscribeExecutedTrades(symbol);
Assert.assertTrue(executedTradeManager.removeExecutedTradeCallback(symbol, callback));
Assert.assertFalse(executedTradeManager.removeExecutedTradeCallback(symbol, callback));
} catch (Exception e) {
// Should not happen
e.printStackTrace();
Assert.assertTrue(false);
} finally {
bitfinexClient.close();
}
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol in project bitfinex-v2-wss-api-java by jnidzwetzki.
the class QuoteManager method unsubscribeExecutedTrades.
/**
* Unsubscribe a executed trades channel
* @param currencyPair
* @param orderBookPrecision
* @param orderBookFrequency
* @param pricePoints
*/
public void unsubscribeExecutedTrades(final BitfinexExecutedTradeSymbol tradeSymbol) {
final int channel = bitfinexApiBroker.getChannelForSymbol(tradeSymbol);
if (channel == -1) {
throw new IllegalArgumentException("Unknown symbol: " + tradeSymbol);
}
final UnsubscribeChannelCommand command = new UnsubscribeChannelCommand(channel);
bitfinexApiBroker.sendCommand(command);
bitfinexApiBroker.removeChannelForSymbol(tradeSymbol);
}
use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol 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);
}
Aggregations