Search in sources :

Example 1 with QuoteManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project crypto-bot by jnidzwetzki.

the class EMABot method updateScreen.

public synchronized void updateScreen() {
    if (!UPDATE_SCREEN) {
        return;
    }
    final QuoteManager tickerManager = bitfinexApiBroker.getQuoteManager();
    CliTools.clearScreen();
    System.out.println("");
    System.out.println("==========");
    System.out.println("Last ticks");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
        System.out.println(symbol + " " + tickerManager.getLastTick(tickerSymbol));
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("Last bars");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        System.out.println(currency + " " + timeSeries.get(currency).getLastBar());
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("P/L");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final BitfinexTickerSymbol tickerSymbol = new BitfinexTickerSymbol(currency);
        final Trade trade = getOpenTrade(currency);
        if (trade != null) {
            final double priceIn = trade.getExpectedPriceOpen();
            final double currentPrice = tickerManager.getLastTick(tickerSymbol).getClose();
            System.out.println(symbol + ": price in " + priceIn + " / " + (currentPrice - priceIn));
        }
    }
    System.out.println("");
    System.out.println("==========");
    System.out.println("Trades");
    System.out.println("==========");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String symbol = currency.toBitfinexString();
        final List<Trade> lastTrades = trades.get(currency);
        if (lastTrades == null) {
            continue;
        }
        lastTrades.sort((t1, t2) -> Long.compare(t2.getTid(), t1.getTid()));
        final List<Trade> lastTwoTrades = lastTrades.subList(Math.max(lastTrades.size() - 2, 0), lastTrades.size());
        for (final Trade trade : lastTwoTrades) {
            System.out.println(symbol + " " + trade);
        }
    }
    try {
        System.out.println("");
        System.out.println("==========");
        System.out.println("Orders");
        System.out.println("==========");
        final List<ExchangeOrder> orders = new ArrayList<>(bitfinexApiBroker.getOrderManager().getOrders());
        orders.sort((o1, o2) -> Long.compare(o2.getCid(), o1.getCid()));
        final List<ExchangeOrder> lastOrders = orders.subList(Math.max(orders.size() - 3, 0), orders.size());
        for (final ExchangeOrder order : lastOrders) {
            System.out.println(order);
        }
    } catch (APIException e) {
        logger.error("Got eception while reading wallets", e);
    }
}
Also used : Trade(com.github.jnidzwetzki.cryptobot.entity.Trade) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) ArrayList(java.util.ArrayList) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder)

Example 2 with QuoteManager

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

Example 3 with QuoteManager

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

Example 4 with QuoteManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager 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 5 with QuoteManager

use of com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager in project bitfinex-v2-wss-api-java by jnidzwetzki.

the class IntegrationTest method testTickerStream.

/**
 * Test the tick stream
 */
@Test(timeout = 60000)
public void testTickerStream() {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    // Await at least 2 callbacks
    final CountDownLatch latch = new CountDownLatch(2);
    try {
        bitfinexClient.connect();
        final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(BitfinexCurrencyPair.BTC_USD);
        final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
        final BiConsumer<BitfinexTickerSymbol, BitfinexTick> callback = (c, o) -> {
            latch.countDown();
        };
        orderbookManager.registerTickCallback(symbol, callback);
        orderbookManager.subscribeTicker(symbol);
        latch.await();
        Assert.assertTrue(bitfinexClient.isTickerActive(symbol));
        orderbookManager.unsubscribeTicker(symbol);
        Assert.assertFalse(bitfinexClient.isTickerActive(symbol));
        Assert.assertTrue(orderbookManager.removeTickCallback(symbol, callback));
        Assert.assertFalse(orderbookManager.removeTickCallback(symbol, 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) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) CountDownLatch(java.util.concurrent.CountDownLatch) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) Test(org.junit.Test)

Aggregations

QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)11 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)6 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)6 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)6 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)6 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)5 ExecutorService (java.util.concurrent.ExecutorService)5 JSONArray (org.json.JSONArray)5 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)4 ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)4 OrderBookFrequency (com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency)4 OrderBookPrecision (com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision)4 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)4 OrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry)4 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)4 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)4 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)4