Skip to content
This repository has been archived by the owner on May 12, 2019. It is now read-only.

Time series and ticks

Marc de Verdelhan edited this page Sep 7, 2017 · 12 revisions

A TimeSeries contains the aggregated data of a security/commodity into fixed intervals. Each interval ending is represented by a Tick.

A Tick contains aggregated data of a security/commodity during a time period. "Aggregated" means that the Tick object does not contain direct exchange data. It merges all the orders operated during the time period and extract:

  • an open price
  • a high price
  • a low price
  • a close price
  • a volume

A Tick is the basic building block of a TimeSeries. Then the time series is used for backtesting or live trading.

The Tick object in TA4J is also commonly referred to as Bar.
For more discussion of this naming difference see issue #139.

Time series for backtesting

In order to backtest a strategy you need to fill a time series with past data. To do that you have to load each tick of your period and give your list of ticks to your time series.

ZonedDateTime endTime = ZonedDateTime.now();
List<Tick> ticks = Arrays.asList(
    new BaseTick(endTime, 105.42, 112.99, 104.01, 111.42, 1337),
    new BaseTick(endTime.plusDays(1), 111.43, 112.83, 107.77, 107.99, 1234),
    new BaseTick(endTime.plusDays(2), 107.90, 117.50, 107.90, 115.42, 4242),
    //...
);
TimeSeries series = new BaseTimeSeries("my_2017_series", ticks);

Those examples show how to load time series in order to backtest strategies over them.

For this use case, the TimeSeries class provides helper methods to split the series into sub-series, run a trading strategy, etc.

Time series for live trading

Live trading involves building a time series for current prices. In this use case you just have to initialize your series.

TimeSeries series = new BaseTimeSeries("my_live_series");

Then for each tick received from the broker/exchange you have to add it to your series.

series.addTick(new BaseTick(ZonedDateTime.now(), 105.42, 112.99, 104.01, 111.42, 1337));

In this mode, I strongly advise you to:

  • initialize your series with the last data from the exchange (as it's described above for backtesting). It ensures your trading strategy will get enough data to be relevant.
  • call the TimeSeries#setMaximumTickCount(int) method on your series. It ensures that your memory consumption won't increase infinitely.

Warning! Setting a maximum tick count to the series will turn it into a moving time series. In this mode trying to get a removed tick will return the first tick found (i.e. the oldest still in memory). It may involve approximations but only for old ticks.