이 전략은 평균 실제 파장 (ATR) 을 사용하여 가격 트렌드를 포착하고 ATR을 사용하여 스톱 로스를 설정하여 트렌드 추적을 수행합니다.
ATR값을 계산한다.
ATR 값에 따라 스톱로스를 결정한다.
가격이 스톱로스 라인을 넘으면 더 많은 공백을 다.
동적으로 스톱로스를 조정하여 수익을 잠금합니다.
이 전략은 ATR을 사용하여 트렌드를 효과적으로 포착하고, 동적으로 스톱로스를 조정하여 수익을 고정한다. 최적화 파라미터 설치는 전략의 성능을 향상시킬 수 있다. 그러나 ATR 지연 문제를 완전히 피할 수는 없다. 전체적으로, 이 전략은 간단한 실용적인 트렌드 추적 솔루션이다.
/*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)