Strategy vault / Mean reversion
ATR Adjustable Trailing Stop Loss Strategy
The strategy uses the ATR indicator to calculate a dynamic stop loss line. When prices rise, the stop loss line will move up with prices to lock in profits. When prices fall, the stop loss line remains unchanged to…

This strategy uses the ATR indicator to calculate a dynamic stop loss line for risk control.
Overview
The strategy uses the ATR indicator to calculate a dynamic stop loss line. When prices rise, the stop loss line will move up with prices to lock in profits. When prices fall, the stop loss line remains unchanged to avoid being stopped out. The ATR indicator can measure market volatility and risk. Multiplying it by a coefficient generates the stop loss line, thus controlling the risk exposure per trade.
Principles
The strategy uses the ATR indicator and Highest function to calculate the dynamic stop loss line. The specific formula is:
TS=highest(high-Mult*atr(Atr),Hhv)
Where Atr is the ATR period parameter, Hhv is the lookback period parameter of the Highest function, and Mult is the ATR coefficient.
The logic is to first calculate the ATR value, then multiply it by the Mult coefficient to get the range of the stop loss buffer zone. Then use the Highest function to find the highest high in the past Hhv periods, and subtract the stop loss buffer zone to obtain the dynamic stop loss line TS.
When prices rise, the highest high will be constantly updated, driving the stop loss line to move up and locking in profits. When prices fall, the stop loss line will maintain the previous high point to avoid being stopped out.
Advantages
- Dynamic stop loss for timely profit taking
The stop loss line adjusts dynamically to track the highest point after price rises, allowing timely profit taking. This is superior to fixed stop loss.
- Avoid unnecessary stop loss
Fixed stop loss lines can easily be triggered by normal pullbacks or overtight stops. This strategy keeps the stop loss unchanged during price declines to avoid unnecessary stops.
- Adjustable stop loss range
By tuning the ATR period and multiplier parameters, the sensitivity of the stop loss adjustment can be controlled for different degrees of stops.
- Controllable risk
The ATR dynamically calculates the stop loss range, allowing reasonable stop loss ranges according to market volatility for risk control per trade.
Risks
- Stop loss too aggressive during high volatility
When volatility spikes, ATR rises quickly and drives the stop loss line up rapidly, increasing the chance of unnecessary stops. The ATR period can be adjusted to make the line less sensitive.
- Difficult to adapt to sharp reversals
The strategy struggles to adapt to sharp reversals. The stop loss line may lag too much and needs timely position reduction.
- Difficult optimization
Optimizing the ATR period, Highest period and multiplier parameters together can be challenging. Stepped parameter sweep testing is recommended.
Optimization
- Optimize ATR Period
Increase ATR period to reduce overly frequent stop line adjustment, but at the cost of larger loss per stop.
- Optimize Highest Period
Increase Highest period to make the line more stable, but balance tracking speed.
- Test different ATR coefficients
Choose proper ATR multipliers according to instrument characteristics. Larger multipliers widen stops, smaller ones decrease loss per stop.
- Add trend filter
Adding a trend filter reduces the chance of stops being triggered by reversals.
Summary
The strategy has the advantage of dynamic stops and controllable risks. It fits trending markets but watch out for volatility spikes and difficult parameter optimization. With proper settings, optimization and additional techniques, it can be applied for live trading.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 5 | Atr Period |
| v_input_2 | 10 | HHV Period |
| v_input_3 | 2.5 | Multiplier |
| v_input_4 | true | Barcolor |
Source Pine Script
/*backtest
start: 2023-10-17 00:00:00
end: 2023-10-24 00:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ceyhun
//@version=4
strategy("ATR Trailing Stoploss Strategy ",overlay=true)
Atr=input(defval=5,title="Atr Period",minval=1,maxval=500)
Hhv=input(defval=10,title="HHV Period",minval=1,maxval=500)
Mult=input(defval=2.5,title="Multiplier",minval=0.1)
Barcolor=input(true,title="Barcolor")
TS=highest(high-Mult*atr(Atr),Hhv),barssince(close>highest(high-Mult*atr(Atr),Hhv) and close>close)
Color=iff(close>TS,color.green,iff(close<TS,color.red,color.black))
barcolor(Barcolor? Color:na)
plot(TS,color=Color,linewidth=3,title="ATR Trailing Stoploss")
Buy = crossover(close,TS)
Sell = crossunder(close,TS)
if Buy
strategy.entry("Buy", strategy.long, comment="Buy")
if Sell
strategy.entry("Sell", strategy.short, comment="Sell")Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-25. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/ATR-Adjustable-Trailing-Stop-Loss-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- ATR and EMA based Dynamic Take Profit and Stop Loss Adaptive StrategyPine Script · Mean reversion · 1h
- Adaptive Trading Strategy Combining Dynamic Trend Reversal and Stochastic IndicatorPine Script · Mean reversion · 1d
- Advanced Fibonacci Retracement Trend Following and Reversal Trading StrategyPine Script · Mean reversion · 3h
- Advanced Momentum EMA Trend Following with RSI Overbought Oversold Trading StrategyPine Script · Mean reversion · 1d
- Advanced Quantitative Trend Following and Cloud Reversal Composite Trading StrategyPine Script · Mean reversion · 1d
- Advanced Strategy with Volume and Price Pullback Multiple Take ProfitPine Script · Mean reversion · 1h