Stop rules exit an open position when the price moves by a defined amount against (stop-loss) or in favor of (stop-gain) the trade. All stop rules implementDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ta4j/ta4j/llms.txt
Use this file to discover all available pages before exploring further.
Rule and use the tradingRecord to read the entry price of the current position.
All stop rules evaluate against the current open position’s entry price. They return
false when no position is open.Fixed stop rules
StopLossRule
StopLossRule
Exits when the current price drops (for long) or rises (for short) by more than Parameters:
lossPercentage from the entry price.Constructor signatures:| Parameter | Description |
|---|---|
priceIndicator | The price series to compare — typically ClosePriceIndicator |
lossPercentage | Maximum acceptable loss as a percentage (e.g., 2.0 for 2%) |
StopGainRule
StopGainRule
Exits when the current price rises (for long) or falls (for short) by more than Parameters:
gainPercentage from the entry price.Constructor signatures:| Parameter | Description |
|---|---|
priceIndicator | The price series to compare |
gainPercentage | Target gain as a percentage (e.g., 5.0 for 5%) |
FixedAmountStopLossRule
FixedAmountStopLossRule
Exits when the price moves against the position by a fixed absolute amount (not a percentage).
FixedAmountStopGainRule
FixedAmountStopGainRule
Exits when the price moves in favor of the position by a fixed absolute amount.
TrailingStopLossRule
TrailingStopLossRule
A trailing stop that tracks the highest (long) or lowest (short) price seen since entry and exits if the price retraces by Parameters:
lossPercentage from that peak.Constructor signatures:| Parameter | Description |
|---|---|
indicator | Typically ClosePriceIndicator |
lossPercentage | Retracement percentage from the peak/trough |
barCount | (Optional) Maximum lookback window; defaults to all bars since entry |
TrailingFixedAmountStopLossRule
TrailingFixedAmountStopLossRule
Trailing stop based on a fixed absolute distance from the highest (long) or lowest (short) price since entry.
TrailingFixedAmountStopGainRule
TrailingFixedAmountStopGainRule
Exits when the price retraces by a fixed absolute amount from the most favorable price reached since entry.
TrailingStopGainRule
TrailingStopGainRule
Exits when the price retraces by
retracementPercentage from the most favorable price reached since entry — locking in profits as the position moves in your favor.ATR-based stop rules
These rules scale the stop distance dynamically using the Average True Range (ATR), adapting to current market volatility.AverageTrueRangeStopLossRule
AverageTrueRangeStopLossRule
Sets the stop distance as Parameters:
atrCoefficient × ATR(atrBarCount) below the entry price.Constructor signatures:| Parameter | Description |
|---|---|
series | The bar series |
atrBarCount | Period for ATR calculation (e.g., 14) |
atrCoefficient | Multiplier applied to the ATR value (e.g., 2.0) |
AverageTrueRangeStopGainRule
AverageTrueRangeStopGainRule
Takes profit when the price moves
atrCoefficient × ATR(atrBarCount) in favor of the position from the entry price.AverageTrueRangeTrailingStopLossRule
AverageTrueRangeTrailingStopLossRule
A trailing ATR stop: tracks the most favorable price seen since entry and triggers when the price retraces by
atrCoefficient × ATR.AverageTrueRangeTrailingStopGainRule
AverageTrueRangeTrailingStopGainRule
Trailing profit target based on ATR: exits when the price retraces by
atrCoefficient × ATR from the peak favorable price.Volatility stop rules
These rules accept anyIndicator<Num> as the volatility measure (ATR, standard deviation, etc.), giving you full flexibility over the volatility model.
VolatilityStopLossRule
VolatilityStopLossRule
Stop distance =
volatilityIndicator × coefficient. Fires when the price moves more than this distance against the position.Constructor signatures:VolatilityStopGainRule
VolatilityStopGainRule
Takes profit when the price moves
volatilityIndicator × coefficient in favor of the position.VolatilityTrailingStopLossRule
VolatilityTrailingStopLossRule
Trailing volatility stop: trails the most favorable price and exits when the price retraces by
volatility × coefficient.VolatilityTrailingStopGainRule
VolatilityTrailingStopGainRule
Trailing profit target based on a volatility indicator.
Price model rules
These interfaces let stop levels be computed externally and queried programmatically, separate from rule evaluation.StopLossPriceModel
StopLossPriceModel
Interface implemented by
StopLossRule and TrailingStopLossRule. Provides stopPrice(BarSeries, Position) to retrieve the current stop level without needing a full bar index evaluation.StopGainPriceModel
StopGainPriceModel
Interface implemented by
StopGainRule. Provides stopPrice(BarSeries, Position) to retrieve the profit target level.Risk-reward rule
RiskRewardRatioRule
RiskRewardRatioRule
Satisfied when the ratio of potential reward to risk meets a minimum threshold. Used as an entry filter — you only enter a trade if the risk/reward setup is favorable.Constructor signature:Parameters:
| Parameter | Description |
|---|---|
priceIndicator | Current reference price |
stopIndicator | Indicator providing the stop level |
targetIndicator | Indicator providing the profit target level |
bullish | true for long setups, false for short |
minRiskReward | Minimum reward/risk ratio required (e.g., 2.0 for 2:1) |
Compound stop rule example
Combine stop rules with.or() to exit on the first condition that fires: