Ta4j Wiki

Documentation, examples and further information of the ta4j project

View the Wiki On GitHub

This project is maintained by ta4j Organization


In financial analysis, backtesting seeks to estimate the performance of a strategy if it had been employed during a past period.

About backtesting:

Backtesting is the main use case of ta4j.

Running your backtest

Once you constructed your bar series and your trading strategy, you can backtest the strategy by just calling:

BarSeries series = ...
BarSeriesManager seriesManager = new BarSeriesManager(series);
Strategy myStrategy = ...

TradingRecord tradingRecord = seriesManager.run(myStrategy);

That’s it! You get a TradingRecord object which is the record of the resulting trading session (basically a list of trades/orders). By providing different strategies to the BarSeriesManager#run(Strategy) methods, you get different TradingRecord objects and you can compare them according to analysis criteria.

Analyzing strategies

Let’s assume you backtested strategy1 and strategy2 over a series. You get two TradingRecord objects: record1 and record2.

In order to get the profitability ratio of each strategy you have to give those records to an analysis criterion:

AnalysisCriterion criterion = new TotalProfitCriterion();
criterion.calculate(series, record1); // Returns the result for strategy1
criterion.calculate(series, record2); // Returns the result for strategy2

If you just want to get the best strategy according to an analysis criterion you just have to call:

BarSeriesManager seriesManager = new BarSeriesManager(series);
Strategy bestStrategy = criterion.chooseBest(seriesManager, Arrays.asList(strategy1, strategy2));

Ta4j comes with several analysis criteria which are all listed in the Javadoc.

Walk-forward optimization

Ta4j allows you to perform a well-known Walk-forward optimization. An example can be found here.