This strategy uses Average True Range (ATR) to capture price trends and sets stops based on ATR for trend following.
Calculate ATR value.
Determine stop loss level based on ATR.
Enter long/short when price breaks stop level.
Lock in profits by adjusting stops dynamically.
The strategy effectively catches trends using ATR and locks in profits with dynamic stops. Fine tuning parameters can improve performance. But ATR lag cannot be completely eliminated. Overall a simple and practical trend following solution.
/*backtest start: 2022-09-14 00:00:00 end: 2023-09-20 00:00:00 period: 1d basePeriod: 1h exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ strategy(title="ATR Strategy", overlay = true, commission_type=strategy.commission.percent,commission_value=0.075) //credits to HPotter for the orginal code nATRPeriod = input(5) nATRMultip = input(3.5) xATR = ta.atr(nATRPeriod) nLoss = nATRMultip * xATR xATRTrailingStop = iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), math.max(nz(xATRTrailingStop[1]), close - nLoss), iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), math.min(nz(xATRTrailingStop[1]), close + nLoss), iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss))) pos = iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1, iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0))) color = pos == -1 ? color.red: pos == 1 ? color.green : color.blue plot(xATRTrailingStop, color=color, title="ATR Trailing Stop") barbuy = close > xATRTrailingStop barsell = close < xATRTrailingStop strategy.entry("Long", strategy.long, when = barbuy) strategy.entry("Short", strategy.short, when = barsell) barcolor(barbuy? color.green:color.red)