Search in sources :

Example 1 with TimeSeries

use of org.ta4j.core.TimeSeries 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 TimeSeries

use of org.ta4j.core.TimeSeries in project crypto-bot by jnidzwetzki.

the class DonchianBot method barDoneCallback.

/**
 * Handle the next bar
 * @param symbol
 * @param tick
 */
private synchronized void barDoneCallback(final BitfinexCurrencyPair symbol, final Bar bar) {
    final TimeSeries symbolTimeSeries = timeSeries.get(symbol);
    try {
        symbolTimeSeries.addBar(bar);
    } catch (Throwable e) {
        logger.error("Unable to add {} to symbol {}, last bar is {}", bar, symbol, symbolTimeSeries.getLastBar());
    }
    logger.info("Newest bar is {}", bar);
    // Notify portfolio manager about bar done
    if (tickerLatch != null) {
        tickerLatch.countDown();
    }
}
Also used : TimeSeries(org.ta4j.core.TimeSeries)

Example 3 with TimeSeries

use of org.ta4j.core.TimeSeries 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 TimeSeries

use of org.ta4j.core.TimeSeries in project crypto-bot by jnidzwetzki.

the class Chart method buildChartTimeSeries.

private static org.jfree.data.time.TimeSeries buildChartTimeSeries(TimeSeries tickSeries, Indicator<Decimal> indicator, String name) {
    org.jfree.data.time.TimeSeries chartTimeSeries = new org.jfree.data.time.TimeSeries(name);
    for (int i = 0; i < tickSeries.getBarCount(); i++) {
        final Bar tick = tickSeries.getBar(i);
        chartTimeSeries.add(new Minute(Date.from(tick.getEndTime().toInstant())), indicator.getValue(i).doubleValue());
    }
    return chartTimeSeries;
}
Also used : Bar(org.ta4j.core.Bar) TimeSeries(org.ta4j.core.TimeSeries) Minute(org.jfree.data.time.Minute)

Example 5 with TimeSeries

use of org.ta4j.core.TimeSeries in project ta4j by ta4j.

the class CachedIndicator method getValue.

@Override
public T getValue(int index) {
    TimeSeries series = getTimeSeries();
    if (series == null) {
        // --> Calculating the value
        return calculate(index);
    }
    // Series is not null
    final int removedBarsCount = series.getRemovedBarsCount();
    final int maximumResultCount = series.getMaximumBarCount();
    T result;
    if (index < removedBarsCount) {
        // Result already removed from cache
        log.trace("{}: result from bar {} already removed from cache, use {}-th instead", getClass().getSimpleName(), index, removedBarsCount);
        increaseLengthTo(removedBarsCount, maximumResultCount);
        highestResultIndex = removedBarsCount;
        result = results.get(0);
        if (result == null) {
            // It should be "result = calculate(removedBarsCount);".
            // We use "result = calculate(0);" as a workaround
            // to fix issue #120 (https://github.com/mdeverdelhan/ta4j/issues/120).
            result = calculate(0);
            results.set(0, result);
        }
    } else {
        increaseLengthTo(index, maximumResultCount);
        if (index > highestResultIndex) {
            // Result not calculated yet
            highestResultIndex = index;
            result = calculate(index);
            results.set(results.size() - 1, result);
        } else {
            // Result covered by current cache
            int resultInnerIndex = results.size() - 1 - (highestResultIndex - index);
            result = results.get(resultInnerIndex);
            if (result == null) {
                result = calculate(index);
                results.set(resultInnerIndex, result);
            }
        }
    }
    return result;
}
Also used : TimeSeries(org.ta4j.core.TimeSeries)

Aggregations

TimeSeries (org.ta4j.core.TimeSeries)48 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)30 Test (org.junit.Test)24 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)16 BaseTradingRecord (org.ta4j.core.BaseTradingRecord)12 TradingRecord (org.ta4j.core.TradingRecord)11 Bar (org.ta4j.core.Bar)9 Before (org.junit.Before)7 ArrayList (java.util.ArrayList)5 MockBar (org.ta4j.core.mocks.MockBar)5 BaseTimeSeries (org.ta4j.core.BaseTimeSeries)4 ZonedDateTime (java.time.ZonedDateTime)3 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)3 Strategy (org.ta4j.core.Strategy)3 BitfinexApiBroker (com.github.jnidzwetzki.bitfinex.v2.BitfinexApiBroker)2 BitfinexCandlestickSymbol (com.github.jnidzwetzki.bitfinex.v2.entity.symbol.BitfinexCandlestickSymbol)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JFreeChart (org.jfree.chart.JFreeChart)2 XYPlot (org.jfree.chart.plot.XYPlot)2 TimeSeriesManager (org.ta4j.core.TimeSeriesManager)2