use of org.ta4j.core.Bar in project crypto-bot by jnidzwetzki.
the class BarMegerTest method testBarMerger1.
/**
* Test one Bar
* @throws InterruptedException
* @throws IOException
*/
@Test(timeout = 5000)
public void testBarMerger1() throws InterruptedException, IOException {
final CountDownLatch latch = new CountDownLatch(1);
final BiConsumer<BitfinexCurrencyPair, Bar> BarConsumer = (s, t) -> {
latch.countDown();
};
final BarMerger BarMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, BarConsumer);
BarMerger.addNewPrice(1000000, 1.0, 5.0);
BarMerger.close();
latch.await();
}
use of org.ta4j.core.Bar in project crypto-bot by jnidzwetzki.
the class BarMegerTest method testTickMerger4.
/**
* Test three tick merge with other timestamps
* @throws InterruptedException
* @throws IOException
* @throws ParseException
*/
@Test(timeout = 6000)
public void testTickMerger4() throws InterruptedException, IOException, ParseException {
final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
final CountDownLatch latch = new CountDownLatch(2);
final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (s, t) -> {
latch.countDown();
};
final BarMerger tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, tickConsumer);
tickMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
tickMerger.addNewPrice(parser.parse("01:01:33").getTime(), 2.0, 5.0);
tickMerger.addNewPrice(parser.parse("02:02:53").getTime(), 2.0, 5.0);
tickMerger.close();
latch.await();
}
use of org.ta4j.core.Bar in project crypto-bot by jnidzwetzki.
the class BarMegerTest method testTickMerger3.
/**
* Test three tick merge
* @throws InterruptedException
* @throws IOException
* @throws ParseException
*/
@Test(timeout = 6000)
public void testTickMerger3() throws InterruptedException, IOException, ParseException {
final SimpleDateFormat parser = new SimpleDateFormat("HH:mm:ss");
final CountDownLatch latch = new CountDownLatch(2);
final BiConsumer<BitfinexCurrencyPair, Bar> tickConsumer = (s, t) -> {
latch.countDown();
};
final BarMerger tickMerger = new BarMerger(BitfinexCurrencyPair.BTC_USD, Timeframe.MINUTES_1, tickConsumer);
tickMerger.addNewPrice(parser.parse("01:01:23").getTime(), 1.0, 5.0);
tickMerger.addNewPrice(parser.parse("01:01:33").getTime(), 2.0, 5.0);
tickMerger.addNewPrice(parser.parse("01:02:53").getTime(), 2.0, 5.0);
tickMerger.close();
latch.await();
}
use of org.ta4j.core.Bar 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.Bar 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;
}
Aggregations