Search in sources :

Example 1 with BitfinexTickerSymbol

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

use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol in project crypto-bot by jnidzwetzki.

the class EMABot method registerTicker.

protected void registerTicker(final BitfinexApiBroker bitfinexApiBroker) throws InterruptedException, APIException {
    logger.info("Register ticker");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        tickMerger.put(currency, new BarMerger(currency, TIMEFRAME, (s, t) -> barDoneCallback(s, t)));
        final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
        bitfinexApiBroker.getQuoteManager().subscribeTicker(symbol);
        logger.info("Wait for ticker");
        while (!bitfinexApiBroker.isTickerActive(symbol)) {
            Thread.sleep(100);
        }
        bitfinexApiBroker.getQuoteManager().registerTickCallback(symbol, (s, c) -> handleTickCallback(s, c));
    }
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) Arrays(java.util.Arrays) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) Decimal(org.ta4j.core.Decimal) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) OrderType(org.ta4j.core.Order.OrderType) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) EMAStrategy03(com.github.jnidzwetzki.cryptobot.strategy.EMAStrategy03) ArrayList(java.util.ArrayList) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) ExchangeOrder(com.github.jnidzwetzki.bitfinex.v2.entity.ExchangeOrder) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) Map(java.util.Map) BitfinexClientFactory(com.github.jnidzwetzki.cryptobot.util.BitfinexClientFactory) Strategy(org.ta4j.core.Strategy) TradeState(com.github.jnidzwetzki.cryptobot.entity.TradeState) TradeStrategyFactory(com.github.jnidzwetzki.cryptobot.strategy.TradeStrategyFactory) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) Logger(org.slf4j.Logger) TradeDirection(com.github.jnidzwetzki.cryptobot.entity.TradeDirection) Collectors(java.util.stream.Collectors) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) TimeUnit(java.util.concurrent.TimeUnit) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) List(java.util.List) Trade(com.github.jnidzwetzki.cryptobot.entity.Trade) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)

Example 3 with BitfinexTickerSymbol

use of com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol 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)

Example 4 with BitfinexTickerSymbol

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

the class QuoteManager method unsubscribeTicker.

/**
 * Unsubscribe a ticker
 * @param tickerSymbol
 */
public void unsubscribeTicker(final BitfinexTickerSymbol tickerSymbol) {
    final int channel = bitfinexApiBroker.getChannelForSymbol(tickerSymbol);
    if (channel == -1) {
        throw new IllegalArgumentException("Unknown symbol: " + tickerSymbol);
    }
    final UnsubscribeChannelCommand command = new UnsubscribeChannelCommand(channel);
    bitfinexApiBroker.sendCommand(command);
    bitfinexApiBroker.removeChannelForSymbol(tickerSymbol);
}
Also used : UnsubscribeChannelCommand(com.github.jnidzwetzki.bitfinex.v2.commands.UnsubscribeChannelCommand)

Example 5 with BitfinexTickerSymbol

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

the class TickHandlerTest method testTickUpdateAndNotify.

/**
 * Test the parsing of one tick
 * @throws APIException
 * @throws InterruptedException
 */
@Test(timeout = 20000)
public void testTickUpdateAndNotify() throws APIException, InterruptedException {
    final String callbackValue = "[26123,41.4645776,26129,33.68138507,2931,0.2231,26129,144327.10936387,26149,13139]";
    final JSONArray jsonArray = new JSONArray(callbackValue);
    final BitfinexCurrencyPair currencyPair = BitfinexCurrencyPair.BTC_USD;
    final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currencyPair);
    final ExecutorService executorService = Executors.newFixedThreadPool(10);
    final BitfinexApiBroker bitfinexApiBroker = Mockito.mock(BitfinexApiBroker.class);
    Mockito.when(bitfinexApiBroker.getExecutorService()).thenReturn(executorService);
    final QuoteManager tickerManager = new QuoteManager(bitfinexApiBroker);
    Mockito.when(bitfinexApiBroker.getQuoteManager()).thenReturn(tickerManager);
    final CountDownLatch latch = new CountDownLatch(1);
    tickerManager.registerTickCallback(symbol, (s, c) -> {
        try {
            Assert.assertEquals(symbol, s);
            Assert.assertEquals(26129, c.getOpen().doubleValue(), DELTA);
            Assert.assertEquals(26129, c.getClose().doubleValue(), DELTA);
            Assert.assertEquals(26129, c.getHigh().doubleValue(), DELTA);
            Assert.assertEquals(26129, c.getLow().doubleValue(), DELTA);
            Assert.assertEquals(BitfinexTick.INVALID_VOLUME.doubleValue(), c.getVolume().doubleValue(), DELTA);
        } catch (Throwable e) {
            System.out.println(e);
            throw e;
        }
        latch.countDown();
    });
    Assert.assertEquals(-1, tickerManager.getHeartbeatForSymbol(symbol));
    Assert.assertEquals(null, tickerManager.getLastTick(symbol));
    final TickHandler tickHandler = new TickHandler();
    final long now = System.currentTimeMillis();
    tickHandler.handleChannelData(bitfinexApiBroker, symbol, jsonArray);
    // Tick callbacks are handled async
    latch.await();
    Assert.assertTrue(now <= tickerManager.getHeartbeatForSymbol(symbol));
    Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
    Assert.assertTrue(tickerManager.getLastTick(symbol) != null);
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) JSONArray(org.json.JSONArray) ExecutorService(java.util.concurrent.ExecutorService) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) QuoteManager(com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager) CountDownLatch(java.util.concurrent.CountDownLatch) TickHandler(com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler) Test(org.junit.Test)

Aggregations

BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)13 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)7 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)7 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)7 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)6 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)5 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)5 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)5 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)5 QuoteManager (com.github.jnidzwetzki.bitfinex.v2.manager.QuoteManager)5 Test (org.junit.Test)5 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 SubscribeTickerCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTickerCommand)3 TickHandler (com.github.jnidzwetzki.bitfinex.v2.callback.channel.TickHandler)2 SubscribeCandlesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand)2 SubscribeOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeOrderbookCommand)2 SubscribeRawOrderbookCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeRawOrderbookCommand)2 SubscribeTradesCommand (com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeTradesCommand)2 UnsubscribeChannelCommand (com.github.jnidzwetzki.bitfinex.v2.commands.UnsubscribeChannelCommand)2