Search in sources :

Example 1 with Timeframe

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

the class DonchianBot method run.

@Override
public void run() {
    try {
        logger.info("===============================================");
        logger.info("Starting with {} API keys", apiBrokerList.size());
        logger.info("===============================================");
        for (final BitfinexApiBroker bitfinexApiBroker : apiBrokerList) {
            bitfinexApiBroker.connect();
        }
        final Map<BitfinexCandlestickSymbol, TimeSeries> historicalCandles = HistoricalCandlesHelper.requestHistoricalCandles(apiBrokerList.get(0), TIMEFRAME, tradedCurrencies);
        historicalCandles.forEach((k, v) -> timeSeries.put(k.getSymbol(), v));
        registerTicker();
        while (true) {
            if (tickerLatch != null) {
                tickerLatch.await();
            }
            executeSystem();
            tickerLatch = new CountDownLatch(tradedCurrencies.size());
        }
    } catch (Throwable e) {
        logger.error("Got exception", e);
    }
}
Also used : BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) TimeSeries(org.ta4j.core.TimeSeries) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with Timeframe

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

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

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

the class QuoteManager method subscribeCandles.

/**
 * Subscribe candles for a symbol
 * @param currencyPair
 * @param timeframe
 */
public void subscribeCandles(final BitfinexCandlestickSymbol symbol) {
    final SubscribeCandlesCommand command = new SubscribeCandlesCommand(symbol);
    bitfinexApiBroker.sendCommand(command);
}
Also used : SubscribeCandlesCommand(com.github.jnidzwetzki.bitfinex.v2.commands.SubscribeCandlesCommand)

Example 5 with Timeframe

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

the class DonchianBot method registerTicker.

/**
 * Register the ticker
 * @throws InterruptedException
 * @throws APIException
 */
protected void registerTicker() throws InterruptedException, APIException {
    logger.info("Register ticker");
    for (final BitfinexCurrencyPair currency : tradedCurrencies) {
        // Subscribe ticket on all connections (needed for wallet in USD conversion)
        for (final BitfinexApiBroker bitfinexApiBroker : apiBrokerList) {
            final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
            bitfinexApiBroker.getQuoteManager().subscribeTicker(symbol);
            logger.info("Wait for ticker");
            while (!bitfinexApiBroker.isTickerActive(symbol)) {
                Thread.sleep(100);
            }
        }
        // Use only one connection for merging
        tickMerger.put(currency, new BarMerger(currency, TIMEFRAME, (s, t) -> barDoneCallback(s, t)));
        final BitfinexTickerSymbol symbol = new BitfinexTickerSymbol(currency);
        apiBrokerList.get(0).getQuoteManager().registerTickCallback(symbol, (s, c) -> handleBarCallback(s, c));
    }
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) Arrays(java.util.Arrays) BitfinexTick(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick) DonchianChannelUpper(com.github.jnidzwetzki.cryptobot.strategy.indicator.DonchianChannelUpper) Decimal(org.ta4j.core.Decimal) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) BitfinexCandlestickSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol) ArrayList(java.util.ArrayList) MaxPriceIndicator(org.ta4j.core.indicators.helpers.MaxPriceIndicator) APIException(com.github.jnidzwetzki.bitfinex.v2.entity.APIException) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol) PortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.PortfolioManager) Map(java.util.Map) BitfinexClientFactory(com.github.jnidzwetzki.cryptobot.util.BitfinexClientFactory) DonchianChannelLower(com.github.jnidzwetzki.cryptobot.strategy.indicator.DonchianChannelLower) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) Logger(org.slf4j.Logger) BasePortfolioManager(com.github.jnidzwetzki.cryptobot.portfolio.BasePortfolioManager) Timeframe(com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe) Bar(org.ta4j.core.Bar) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) MinPriceIndicator(org.ta4j.core.indicators.helpers.MinPriceIndicator) MathUtil(org.bboxdb.commons.MathUtil) BitfinexApiBroker(com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker) BarMerger(com.github.jnidzwetzki.cryptobot.util.BarMerger) BitfinexCurrencyPair(com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair) BitfinexTickerSymbol(com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)

Aggregations

BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)4 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)4 TimeSeries (org.ta4j.core.TimeSeries)4 APIException (com.github.jnidzwetzki.bitfinex.v2.entity.APIException)3 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)3 BitfinexTick (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexTick)3 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Bar (org.ta4j.core.Bar)3 BitfinexTickerSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexTickerSymbol)2 BarMerger (com.github.jnidzwetzki.cryptobot.util.BarMerger)2 BitfinexClientFactory (com.github.jnidzwetzki.cryptobot.util.BitfinexClientFactory)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 TimeUnit (java.util.concurrent.TimeUnit)2