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

Backtesting

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

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 time series](Time series and ticks) and [your trading strategy](Trading strategies), you can backtest the strategy by just calling:

TimeSeries series = ...
TimeSeriesManager seriesManager = new TimeSeriesManager(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 TimeSeriesManager#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:

TimeSeriesManager seriesManager = new TimeSeriesManager(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.