Search in sources :

Example 1 with BitfinexTick

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

the class HistoricalCandlesHelper method requestHistoricalCandles.

/**
 * Request historical candles
 *
 * @param bitfinexApiBroker
 * @param timeframe
 * @param tradedCurrencies
 * @return
 * @throws InterruptedException
 * @throws APIException
 */
public static Map<BitfinexCandlestickSymbol, TimeSeries> requestHistoricalCandles(final BitfinexApiBroker bitfinexApiBroker, final Timeframe timeframe, final List<BitfinexCurrencyPair> tradedCurrencies) throws InterruptedException, APIException {
    logger.info("Request historical candles");
    final Map<BitfinexCandlestickSymbol, TimeSeries> timeSeries = new HashMap<>();
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        final String bitfinexString = currency.toBitfinexString();
        final BaseTimeSeries currencyTimeSeries = new BaseTimeSeries(bitfinexString);
        final BitfinexCandlestickSymbol barSymbol = new BitfinexCandlestickSymbol(currency, timeframe);
        timeSeries.put(barSymbol, currencyTimeSeries);
        final CountDownLatch tickCountdown = new CountDownLatch(100);
        // Add bars to timeseries callback
        final BiConsumer<BitfinexCandlestickSymbol, BitfinexTick> callback = (channelSymbol, tick) -> {
            final TimeSeries timeSeriesToAdd = timeSeries.get(channelSymbol);
            final Bar bar = BarConverter.convertBitfinexTick(tick);
            try {
                timeSeriesToAdd.addBar(bar);
                tickCountdown.countDown();
            } catch (IllegalArgumentException e) {
                logger.error("Unable to add tick {} to time series, last tick is {}", bar, timeSeriesToAdd.getLastBar());
            }
        };
        bitfinexApiBroker.getQuoteManager().registerCandlestickCallback(barSymbol, callback);
        bitfinexApiBroker.getQuoteManager().subscribeCandles(barSymbol);
        // Wait for 100 bars or 10 seconds. All snapshot ticks are handled in
        // a synchronized block, so we receive the full snapshot even if we
        // call removeTickCallback.
        tickCountdown.await(10, TimeUnit.SECONDS);
        bitfinexApiBroker.getQuoteManager().registerCandlestickCallback(barSymbol, callback);
        bitfinexApiBroker.getQuoteManager().unsubscribeCandles(barSymbol);
        logger.info("Loaded ticks for symbol {} {}", bitfinexString, +timeSeries.get(barSymbol).getEndIndex());
    }
    return timeSeries;
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) Bar(org.ta4j.core.Bar) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) TimeUnit(java.util.concurrent.TimeUnit) BaseTimeSeries(org.ta4j.core.BaseTimeSeries) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) BarConverter(com.github.jnidzwetzki.cryptobot.util.BarConverter) TimeSeries(org.ta4j.core.TimeSeries) BaseTimeSeries(org.ta4j.core.BaseTimeSeries) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Bar(org.ta4j.core.Bar) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BaseTimeSeries(org.ta4j.core.BaseTimeSeries) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)

Example 2 with BitfinexTick

use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick 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 3 with BitfinexTick

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

the class IntegrationTest method testCandleStream.

/**
 * Test the candle stream
 */
@Test(timeout = 10000)
public void testCandleStream() {
    final BitfinexApiBroker bitfinexClient = new BitfinexApiBroker();
    // Await at least 10 callbacks
    final CountDownLatch latch = new CountDownLatch(10);
    try {
        bitfinexClient.connect();
        final BitfinexCandlestickSymbol symbol = new BitfinexCandlestickSymbol(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1);
        final QuoteManager orderbookManager = bitfinexClient.getQuoteManager();
        final BiConsumer<BitfinexCandlestickSymbol, BitfinexTick> callback = (c, o) -> {
            latch.countDown();
        };
        orderbookManager.registerCandlestickCallback(symbol, callback);
        orderbookManager.subscribeCandles(symbol);
        latch.await();
        orderbookManager.unsubscribeCandles(symbol);
        Assert.assertTrue(orderbookManager.removeCandlestickCallback(symbol, callback));
        Assert.assertFalse(orderbookManager.removeCandlestickCallback(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) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) 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 4 with BitfinexTick

use of com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick 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);
}
Also used : BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) BigDecimal(java.math.BigDecimal)

Example 5 with BitfinexTick

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

the class BasePortfolioManager method getTotalPortfolioValueInUSD.

@Override
protected double getTotalPortfolioValueInUSD() throws APIException {
    final List<Wallet> wallets = getAllWallets();
    double totalValue = 0;
    for (final Wallet wallet : wallets) {
        final String curreny = wallet.getCurreny();
        if (curreny.equals("USD")) {
            totalValue = totalValue + wallet.getBalance();
        } else {
            final String symbol = "t" + curreny + "USD";
            try {
                final BitfinexCurrencyPair bitfinexCurrencyPair = BitfinexCurrencyPair.fromSymbolString(symbol);
                final BitfinexTickerSymbol bitfinexSymbol = new BitfinexTickerSymbol(bitfinexCurrencyPair);
                final BitfinexTick lastTick = bitfinexApiBroker.getQuoteManager().getLastTick(bitfinexSymbol);
                if (lastTick != null) {
                    final double rate = lastTick.getClose();
                    final double value = rate * wallet.getBalance();
                    totalValue = totalValue + value;
                } else {
                    logger.debug("Unable to find tick for {}, appraise wallet with 0 USD", symbol);
                }
            } catch (IllegalArgumentException e) {
                logger.debug("Unkown symbol {}, ignoring wallet", symbol);
            }
        }
    }
    return totalValue;
}
Also used : BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) Wallet(com.github.jnidzwetzki.bitfinex.v2.entity.Wallet) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)

Aggregations

BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)12 Test (org.junit.Test)7 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)5 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)5 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)5 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)4 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)4 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)4 BigDecimal (java.math.BigDecimal)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 BiConsumer (java.util.function.BiConsumer)4 ExecutedTrade (com.github.jnidzwetzki.bitfinex.v2.entity.ExecutedTrade)3 OrderBookFrequency (com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookFrequency)3 OrderBookPrecision (com.github.jnidzwetzki.bitfinex.v2.entity.OrderBookPrecision)3 OrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookConfiguration)3 OrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.OrderbookEntry)3 RawOrderbookConfiguration (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookConfiguration)3 RawOrderbookEntry (com.github.jnidzwetzki.bitfinex.v2.entity.RawOrderbookEntry)3 BitfinexExecutedTradeSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexExecutedTradeSymbol)3 OrderbookManager (com.github.jnidzwetzki.bitfinex.v2.manager.OrderbookManager)3