Strategi ini menggunakan rata-rata real bandwidth (ATR) untuk menangkap tren harga, dan ATR untuk mengatur stop loss, untuk melacak tren.
Menghitung nilai ATR.
Stop loss ditentukan berdasarkan nilai ATR.
Ketika harga menembus batas stop loss, Anda harus melakukan lebih banyak shorting.
Mengunci keuntungan dengan mengadaptasi stop loss secara dinamis.
Strategi ini memanfaatkan ATR untuk menangkap tren secara efektif dan secara dinamis menyesuaikan stop loss untuk mencapai profit lock. Pengaturan parameter optimasi dapat meningkatkan kinerja strategi. Namun, masalah ATR lag tidak dapat sepenuhnya dihindari. Secara keseluruhan, strategi ini adalah solusi pelacakan tren yang sederhana dan praktis.
/*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)