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

Live trading

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

Ta4j can also be used to build automated trading systems.

About automated trading:

In order to build an automated trading system (a.k.a. trading bot) you have to think about two states of your program: the initialization phase and the trading phase.

Initialization phase

In the same way than for backtesting, you have to initialize the system before running it. It involves having created your time series and built your trading strategy before.

Even if you will keep your series constantly updated with new ticks, you should initialize it with the most recent ticks the exchange can give you. Thus your strategy will have a more predictable/relevant behavior during the first steps of the trading phase. For instance, let's assume:

  • We have 3 ticks: 5, 10, 30 (close prices)
  • Your strategy computes a SMA(3)

If you start your trading phase on the 3rd tick:

  • Without initialization: SMA(3) --> 30 / 1 = 30
  • After your series have been initialized with last ticks: SMA(3) --> (5 + 10 + 30) / 3 = 15
Maximum tick count

Since you will continuously feed your time series with new ticks during the trading phase, it will grow infinitely and you will encounter memory issues soon. To avoid that you have to set a maximum tick count to your series. It represents the maximum number of ticks your trading strategy needs to be run.

For instance: if your strategy relies on a SMA(200) and a RSI(2), then your maximum tick count should be 200. You may want to set it to 400 (it's more an order of magnitude than a strict value, but it has to be larger than the maximum tick count you need); it will ensure that your series will never be more than 400-ticks long (i.e. adding a new tick will be preceded with the deletion of the oldest tick).

You just have to call the TimeSeries#setMaximumTickCount(int) method.

Trading phase

The trading phase itself can be designed as a simple infinite loop in which you wait for a new tick from the broker/exchange before interrogating your strategy.

Tick newTick = // Get the exchange new tick here...;
series.addTick(newTick);

Since you use a moving (see above) time series, you run your strategy on this new tick: the tick index is always series.getEndIndex().

int endIndex = series.getEndIndex();
if (strategy.shouldEnter(endIndex)) {
    // Entering...
    tradingRecord.enter(endIndex, newTick.getClosePrice(), Decimal.TEN);
} else if (strategy.shouldExit(endIndex)) {
    // Exiting...
    tradingRecord.exit(endIndex, newTick.getClosePrice(), Decimal.TEN);
}

Note that the strategy gives you a you-should-enter information, then it's up to you to call the TradingRecord#enter()/TradingRecord#exit() methods with the price you really spent. It's justified by the fact that you may not follow your strategy on any signal; this way you can take external events into account.

This documentation has also live trading engine examples.