Documentation, examples and further information of the ta4j project
This project is maintained by ta4j Organization
ta4j now provides a full stop-rule toolkit that covers fixed %, fixed amount, trailing variants, and volatility/ATR-adaptive variants for both loss and gain exits.
Use this page as the operational guide for:
StopLossRuleStopGainRuleTrailingStopLossRuleTrailingStopGainRuleThese use percentage distance from entry (or favorable move for trailing variants).
Percent inputs are percentage points (for example, 3 means 3%, not 0.03).
Best when:
FixedAmountStopLossRuleFixedAmountStopGainRuleTrailingFixedAmountStopLossRuleTrailingFixedAmountStopGainRuleThese use an absolute price distance (for example: $10 from reference).
Best when:
VolatilityStopLossRuleVolatilityStopGainRuleVolatilityTrailingStopLossRuleVolatilityTrailingStopGainRuleAverageTrueRangeStopLossRuleAverageTrueRangeStopGainRuleAverageTrueRangeTrailingStopLossRuleAverageTrueRangeTrailingStopGainRuleThese use a dynamic threshold based on volatility indicators (typically ATR multiplied by a coefficient).
From 0.22.3, ATR-based rules support constructors that accept a custom ATRIndicator directly (including trailing variants), so you can share precomputed ATR pipelines instead of rebuilding ATR inside each rule.
Best when:
AverageTrueRangeTrailingStopLossRule for downside protection.TrailingStopGainRule or AverageTrueRangeTrailingStopGainRule for profit capture.StopLossRule / FixedAmountStopLossRule.StopGainRule / FixedAmountStopGainRule at expected reversion targets.ClosePriceIndicator close = new ClosePriceIndicator(series);
ATRIndicator atr = new ATRIndicator(series, 14);
Rule riskExit = new AverageTrueRangeTrailingStopLossRule(close, atr, 2.0)
.or(new AverageTrueRangeTrailingStopGainRule(close, atr, 3.0))
.or(new StopLossRule(close, series.numFactory().numOf(1.5))); // hard fail-safe
Rule signalExit = new CrossedDownIndicatorRule(fast, slow);
Rule exitRule = signalExit.or(riskExit);
Strategy strategy = new BaseStrategy(entryRule, exitRule);
Notes:
TrailingStopGainRule and AverageTrueRangeTrailingStopGainRule are two-stage: they activate only after reaching the initial gain threshold, then trigger on retracement from the favorable extreme.AverageTrueRangeTrailingStopGainRule requires a positive ATR coefficient; invalid values fail fast with IllegalArgumentException.Several rules implement:
StopLossPriceModelStopGainPriceModelThis lets you query stop prices directly (for example in risk budgeting or custom position sizing flows) without duplicating threshold math.
For fixed-percentage and fixed-distance calculations, you can also use helper methods from StopLossRule / StopGainRule directly:
Num stopLoss = StopLossRule.stopLossPrice(entryPrice, numOf(2), true);
Num stopGain = StopGainRule.stopGainPrice(entryPrice, numOf(4), true);
Num trailingGain = StopGainRule.trailingStopGainPrice(highestPrice, numOf(1.5), true);
If you evaluate strategy quality in risk units, pair your stop model with RMultipleCriterion (for example, new RMultipleCriterion(new StopLossPositionRiskModel(5))) so research and stop geometry stay aligned.
ClosePriceIndicator may be acceptable.Pick one model explicitly and keep it consistent between research and production.
Do not rely on a single mechanism.
7-14) reacts quickly,20-50) is smoother and slower.barCount where applicable):
Practical tuning workflow:
Use these with stop rules when you need stronger entry/exit constraints:
RiskRewardRatioRule
price/stop/target tuple satisfies a minimum reward-to-risk threshold.OverOrEqualIndicatorRule
OverIndicatorRule, but inclusive (>=) for threshold boundaries.UnderOrEqualIndicatorRule
UnderIndicatorRule, but inclusive (<=) for threshold boundaries.OrWithThresholdRule
N bars.Rule rrGate = new RiskRewardRatioRule(close, stopPrice, targetPrice, true, 2.0);
Rule inclusiveState = new OverOrEqualIndicatorRule(rsi, 50);
Rule inclusiveOversold = new UnderOrEqualIndicatorRule(rsi, 30);
Rule delayedConfirm = new OrWithThresholdRule(macdCrossUp, breakoutRule, 3);
Rule entryRule = rrGate.and(inclusiveState).and(inclusiveOversold.negation()).and(delayedConfirm);
last, bid/ask, mark/index) matches indicator input.See also:
89cd2271 (BaseVolatility*, fixed-amount, trailing, and stop-price model interfaces).1fa097ef and current AverageTrueRange* constructors that accept ATRIndicator directly.AverageTrueRangeTrailingStopGainRule based on its input validation in requirePositiveAtrCoefficient(...).