ATR 지표를 기반으로 한 포물선형 손절매 전략


생성 날짜: 2023-09-13 15:53:00 마지막으로 수정됨: 2023-09-13 15:53:00
복사: 0 클릭수: 1194
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

이 전략의 이름은 ATR 지표 기반의 패러블라인 중지 전략이다. 이 전략은 ATR 지표를 사용하여 패러블라인 중지 곡선의 수축 속도를 조정하여 시장의 변동률 변화에 적응할 수 있도록 한다.

전통적인 패러블라인 상쇄의 가속인수는 고정되어 있어 변동률의 증가에 대처할 수 없다. 이 전략은 패러블라인 수축 속도를 ATR 값이 커질수록 가속화하여 변동이 커질 때, 상쇄 곡선이 가격에 더 빨리 접근하여 위험을 효과적으로 제어할 수 있다.

구체적으로, 전략은 가격의 트렌드 방향을 판단한 후, ATR 값에 따라 적응 가속 인자를 계산하고, 이에 따라 평평한 파로스 라인 중지 곡선을 그립니다. 가격이 스톱 라인을 뚫었을 때, 중지 손해 평정 포지션을 실행합니다.

이 전략의 장점은 전통적인 패러블리 라인 스톱로드가 시장의 변동성에 따라 동적으로 조정할 수 있다는 것입니다. 그러나 ATR 파라미터는 최적화가 필요하며 스톱로드는 너무 민감하여 깨질 수 있습니다.

전체적으로, 자율적인 손해 차단은 수익을 보호하고 위험을 제어하는 데 중요합니다. 거래자는 시장 조건에 따라 적절한 손해 차단 지표를 선택하고, 변수를 테스트하고 최적화하여 손해 차단 전략을 최대한 활용해야합니다.

전략 소스 코드
/*backtest
start: 2023-08-13 00:00:00
end: 2023-09-12 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy(title="ATR Parabolic SAR Strategy [QuantNomad]", shorttitle="ATR PSAR Strategy [QN]", overlay=true, default_qty_type = strategy.percent_of_equity,  default_qty_value = 100)

atr_length = input(14)
start      = input(0.02)
increment  = input(0.02)
maximum    = input(0.2)
entry_bars = input(1, title = "Entry on Nth trend bar") 

atr = atr(atr_length)

atr := na(atr) ? tr : atr

psar        = 0.0 // PSAR
af          = 0.0 // Acceleration Factor
trend_dir   = 0   // Current direction of PSAR
ep          = 0.0 // Extreme point
trend_bars  = 0

sar_long_to_short = trend_dir[1] == 1  and close <= psar[1] // PSAR switches from long to short
sar_short_to_long = trend_dir[1] == -1 and close >= psar[1] // PSAR switches from short to long

trend_change = barstate.isfirst[1] or sar_long_to_short or sar_short_to_long

// Calculate trend direction
trend_dir    :=  barstate.isfirst[1] and close[1]  > open[1] ?  1 : 
                 barstate.isfirst[1] and close[1] <= open[1] ? -1 : 
                 sar_long_to_short ? -1 : 
                 sar_short_to_long ?  1 : nz(trend_dir[1])

trend_bars := sar_long_to_short ? -1 : 
              sar_short_to_long ?  1 : 
              trend_dir ==  1   ? nz(trend_bars[1]) + 1 : 
              trend_dir == -1   ? nz(trend_bars[1]) - 1 : 
              nz(trend_bars[1])

// Calculate  Acceleration Factor
af := trend_change ? start : 
   (trend_dir == 1 and high > ep[1]) or  
   (trend_dir == -1 and low < ep[1]) ? 
   min(maximum, af[1] + increment) : 
   af[1]

// Calculate extreme point
ep := trend_change and trend_dir == 1 ? high :  
   trend_change and trend_dir == -1 ? low : 
   trend_dir == 1 ? max(ep[1], high) : 
   min(ep[1], low)

// Calculate PSAR
psar :=  barstate.isfirst[1] and close[1]  > open[1] ? low[1]  : 
         barstate.isfirst[1] and close[1] <= open[1] ? high[1] : 
         trend_change ? ep[1] :    
         trend_dir == 1 ? psar[1] + af * atr : 
                          psar[1] - af * atr

plot(psar, style=plot.style_cross, color=trend_dir == 1 ? color.green : color.red,  linewidth = 2)


// Strategy 
strategy.entry("Long",  true,  when = trend_bars ==  entry_bars)
strategy.entry("Short", false, when = trend_bars == -entry_bars)