Ta4j Wiki

Documentation, examples and further information of the ta4j project

View the Wiki On GitHub

This project is maintained by ta4j Organization

Trading Strategies

A trading strategy is a set of rules designating the conditions that must be met for trade entries and exits to occur.

About trading strategies:

In ta4j a trading strategy is made of trading rules.

Trading rules

Trading rules are designed according to the specification pattern. They can be combined and chained together using boolean logic:

Rule entryRule = new CrossedUpIndicatorRule(shortSma, longSma)
    .or(new CrossedDownIndicatorRule(closePrice, Decimal.valueOf("800")));

Ta4j provides a set of basic rules. They are all implementations of the Rule interface and they can be used to build both entry and exit rules.

Rule exitRule = new CrossedDownIndicatorRule(shortSma, longSma)
    .or(new StopLossRule(closePrice, Decimal.valueOf("3")))
    .or(new StopGainRule(closePrice, Decimal.valueOf("2")));
Checking rule condition

Using ta4j, you can check if an entry/exit condition is met by calling the Rule#isSatisfied(int, TradingRecord) method. You just have to give:

Trading strategies

A trading strategy is just a pair of rules designed to achieve a profitable return over a bar series. It is made of an entry rule and an exit rule.

Strategy myStrategy = new BaseStrategy(entryRule, exitRule);

It can be backtested over a bar series:

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

TradingRecord tradingRecord = seriesManager.run(myStrategy);

Or used for live trading, as it’s done in the bot examples.