이 전략은 평균 실제 파동 (ATR) 지표에 기초하여 동적 스톱 라인을 설정하고, 주식 가격 변화를 추적하며, 스톱 보호를 실현하면서 최대 수익을 잠금화한다.
이 전략은 주로 다음과 같은 단계를 통해 이루어집니다.
ATR 지수를 계산하기 위해, ATR 주기는 nATRPeriod 파라미터에 의해 설정되며, 기본은 5이다.
ATR 값에 따라 계산된 스톱 라인, nATRMultip 변수로 설정된 스톱 폭은 ATR의 3.5배입니다.
주가가 상승할 때, 만약 그 이전 막점선보다 높다면, 그 막점선은 주가가 감소한 막점으로 상승한다; 주가가 감소할 때, 만약 그 이전 막점선보다 낮다면, 그 막점선은 주가가 감소한 막점으로 감소한다.
주가가 스톱로스 라인을 넘었는지 판단하고, 넘으면 구매 또는 판매 신호를 냅니다.
스톱 라인을 깨는 신호에 따라 더 많은 또는 빈 포지션에 들어가서 스톱 라인을 다시 만질 때 평형 포지션.
주가 상승할 때, 스톱 로프는 상향 조정되어 수익을 잠금화한다. 주가 하락할 때, 스톱 로프는 상향 조정되어 스톱 로프는 상향 조정된다. ATR 지표는 주가 변동의 정도를 더 정확하게 반영할 수 있으며, ATR 동력에 따라 스톱 로프를 조정하여 스톱 로프는 너무 급진적이거나 보수적이지는 않을 수 있다.
매개 변수를 최적화하여 ATR 주기의 매개 변수와 중지 폭을 조정하여 중단과 추적을 균형 잡는 최적의 매개 변수 조합을 찾을 수 있습니다. 다른 기술 지표와 결합하여 시장에 진입하는 시간을 필터링하여 불필요한 중지 손실을 줄일 수 있습니다.
이 전략은 ATR 중지 라인을 동적으로 조정하는 방법을 통해 포지션 과정에서 손실과 이익을 잠금하는 것을 구현한다. 고정된 중지 위치에 비해 주가 변동에 더 잘 적응할 수 있으며, 너무 급진적이거나 보수적인 중지 손실을 피한다. ATR 지표는 중지 라인 조정을 더 타겟으로 만든다. 그러나 변수 설정 및 재입장 전략은 불필요한 중지 손실을 줄이고 수익 공간을 확장하기 위해 추가적인 최적화가 필요합니다.
/*backtest
start: 2023-09-08 00:00:00
end: 2023-10-08 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
//@okadoke
////////////////////////////////////////////////////////////
// Based on Average True Range Trailing Stops Strategy by HPotter
// Average True Range Trailing Stops Strategy, by Sylvain Vervoort
// The related article is copyrighted material from Stocks & Commodities Jun 2009
////////////////////////////////////////////////////////////
strategy(title="ATR Trailing Stops Strategy", shorttitle="ATRTSS", overlay = true,
initial_capital=100000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type="percent", commission_value=0.0)
nATRPeriod = input(5, "ATR Period")
nATRMultip = input(3.5, "ATR Multiplier")
useShorts = input(false, "Test w/Shorts?")
daysBackMax = input(defval = 360, title = "Max Days Back to Test", minval = 0)
daysBackMin = input(defval = 0, title = "Min Days Back to Test", minval = 0)
msBackMax = 1000 * 60 * 60 * 24 * daysBackMax
msBackMin = 1000 * 60 * 60 * 24 * daysBackMin
xATR = atr(nATRPeriod)
nLoss = nATRMultip * xATR
xATRTrailingStop = na
xATRTrailingStop :=
iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss),
iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
pos = na
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 ? red: pos == 1 ? green : blue
plot(xATRTrailingStop, color=color, title="ATR Trailing Stop")
isWithinTimeBounds = (msBackMax == 0 or (time > (timenow - msBackMax))) and (msBackMin == 0 or (time < (timenow - msBackMin)))
buy = crossover(close, xATRTrailingStop)
sell = crossunder(close, xATRTrailingStop)
strategy.entry("LONG", long=true, when=buy and isWithinTimeBounds)
strategy.close("LONG", when=sell and isWithinTimeBounds)
strategy.entry("SHORT", long=false, when=useShorts and sell and isWithinTimeBounds)
strategy.close("SHORT", when=useShorts and buy and isWithinTimeBounds)