Chiến lược này sử dụng độ dài sóng thực trung bình (ATR) để nắm bắt xu hướng giá và đặt điểm dừng để theo dõi xu hướng.
Tính toán ATR
Định mức dừng lỗ dựa trên giá trị ATR
Khi giá phá vỡ ngưỡng dừng lỗ, hãy thực hiện thêm lệnh nhượng quyền.
Khóa lợi nhuận bằng cách điều chỉnh động mức dừng lỗ.
Chiến lược này sử dụng ATR để nắm bắt xu hướng một cách hiệu quả và điều chỉnh động để dừng lỗ để thực hiện khóa lợi nhuận. Thiết lập tham số tối ưu hóa có thể giúp cải thiện hiệu suất của chiến lược. Tuy nhiên, vấn đề ATR bị tụt hậu không thể hoàn toàn tránh được.
/*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)