Search in sources :

Example 1 with ExecutedTrade

use of com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade 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();
    }
}
Also used : ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) 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) BitfinexExecutedTradeSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) CountDownLatch(java.util.concurrent.CountDownLatch) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Test(org.junit.Test)

Example 2 with ExecutedTrade

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

the class ExecutedTradeHandler method handleEntry.

/**
 * Handle a new executed trade entry
 * @param bitfinexApiBroker
 * @param symbol
 * @param jsonArray
 */
private void handleEntry(final BitfinexApiBroker bitfinexApiBroker, final BitfinexExecutedTradeSymbol symbol, final JSONArray jsonArray) {
    final ExecutedTrade executedTrade = new ExecutedTrade();
    final long id = jsonArray.getNumber(0).longValue();
    executedTrade.setId(id);
    final long timestamp = jsonArray.getNumber(1).longValue();
    executedTrade.setTimestamp(timestamp);
    final BigDecimal amount = jsonArray.getBigDecimal(2);
    executedTrade.setAmount(amount);
    // Funding or Currency
    if (jsonArray.optNumber(4) != null) {
        final BigDecimal rate = jsonArray.getBigDecimal(3);
        executedTrade.setRate(rate);
        final int period = jsonArray.getNumber(4).intValue();
        executedTrade.setPeriod(period);
    } else {
        final BigDecimal price = jsonArray.getBigDecimal(3);
        executedTrade.setPrice(price);
    }
    bitfinexApiBroker.getQuoteManager().handleExecutedTradeEntry(symbol, executedTrade);
}
Also used : ExecutedTrade(com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade) BigDecimal(java.math.BigDecimal)

Aggregations

ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)2 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)1 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)1 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)1 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)1 OrderBookFrequency (com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency)1 OrderBookPrecision (com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision)1 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)1 OrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry)1 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)1 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)1 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)1 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)1 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)1 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)1 OrderbookManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager)1 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)1 RawOrderbookManager (com.github.jnidzwetzki.bitfinex.v2.manager.RawOrderbookManager)1 BigDecimal (java.math.BigDecimal)1 CountDownLatch (java.util.concurrent.CountDownLatch)1