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);
}
}
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();
}
}
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;
}
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;
}
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;
}
Aggregations