동적 ATR 트레일링 스톱로스 및 이동평균 교차 조합 전략

EMA ATR RSI SMA VOLUME
생성 날짜: 2025-02-18 15:48:18 마지막으로 수정됨: 2025-02-18 15:48:18
복사: 1 클릭수: 504
avatar of ChaoZhang ChaoZhang
1
집중하다
1617
수행원

동적 ATR 트레일링 스톱로스 및 이동평균 교차 조합 전략

개요

이 전략은 ATR의 동적 추적 스톱 손실과 평행선 교차를 기반으로 한 복합 거래 시스템으로, 거래 필터링과 위험 제어를 위해 여러 가지 기술 지표를 결합합니다. 전략은 15 분 시간 주기에서 작동하며, EMA 평행선, ATR 변동률, RSI 지표 및 거래량과 같은 다차원 지표를 통해 거래 신호를 결정하고, 동적 추적 스톱 손실을 관리합니다.

전략 원칙

전략의 핵심 논리는 다음과 같은 핵심 구성 요소를 포함합니다.

  1. 입시 조건은 다음과 같습니다:
    • 100주기 EMA 상위/하위
    • 1 시간 주기 100 EMA 트렌드 확인
    • 가격과 ATR의 교차
    • RSI 30~70 사이의 중립 영역
    • 현재 거래량은 20주기 평균보다 크다.
  2. 위험 관리 시스템:
    • 3배 ATR 기반의 동적 추적 중지
    • ATR의 2배를 수익 목표로 설정
  3. 출전 메커니즘
    • 15과 17주기 EMA 연속 K선 두 개의 교차 신호
    • 트리거 추적 중지 또는 수익 목표

전략적 이점

  1. 가짜 신호를 줄이기 위한 다중 기술 지표 교차 검증
  2. 다주기 트렌드 필터를 사용하여 거래 방향의 정확성을 향상시킵니다.
  3. 동적 ATR 중지 손실은 시장의 변동에 따라 적응할 수 있습니다.
  4. 수익 목표와 손실을 차단하여 위험과 이익의 동적인 균형을 보장합니다.
  5. EMA 교차로에서 출발 신호를 사용하여 조기 출발을 피하십시오.
  6. 거래량 확인은 거래의 유효성을 증가시킵니다.

전략적 위험

  1. 다중 필터링 조건으로 인해 일부 거래 기회를 놓칠 수 있습니다.
  2. 급격한 변동성 시장에서 ATR 중단이 너무 넓을 수 있습니다.
  3. 연속 EMA 교차 출전이 일부 수익 부출을 초래할 수 있다.
  4. 시장 추세에 의존성이 강하며, 변동성 시장은 좋지 않을 수 있다.
  5. 계산 복잡성이 높기 때문에 실행 지연 위험이 발생할 수 있습니다.

전략 최적화 방향

  1. 그리고, 이러한 변화의 결과로,
    • ATR 곱수는 시장의 변화에 따라 조정할 수 있습니다.
    • EMA 주기는 시장의 변동에 따라 자동으로 최적화됩니다.
  2. 시장 상태 인식:
    • 트렌드 강도 지표 추가
    • 변동률 주기 판단을 도입
  3. 위험 관리 개선:
    • 매장량 축소 및 매장량 축소
    • 최대 보유 시간 제한을 늘립니다.
  4. 경기 출전 메커니즘을 최적화:
    • 동향 강도에 따라 수익 목표 조정
    • 시간 상쇄 메커니즘

요약하다

이 전략은 여러 가지 기술 지표와 위험 제어 수단을 통합하여 비교적 완전한 거래 시스템을 구축한다. 전략의 주요 특징은 시장의 변동에 적응하기 위해 동적 ATR 추적 스톱을 채택하고, 동시에 거래 신호를 확인하기 위해 여러 지표 필터링과 일률적 교차를 이용한다. 약간의 최적화 공간이 있지만, 전체적인 디자인 개념은 현대적인 거래의 요구 사항에 부합하며, 좋은 실무 응용 가치가 있다.

전략 소스 코드
/*backtest
start: 2024-02-19 00:00:00
end: 2025-02-16 08:00:00
period: 2h
basePeriod: 2h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy(title='UT Bot Strategy with 100 EMA Filter, Trailing Stop and EMA Cross Exit', overlay=true)

// Inputs
a = input(1, title='Key Value. \'This changes the sensitivity\'')
c = input(10, title='ATR Period')
h = input(false, title='Signals from Heikin Ashi Candles')

// Higher timeframe trend filter (1-hour)
higherTimeframeEMA = request.security(syminfo.tickerid, "60", ta.ema(close, 100)) // 1-hour EMA

xATR = ta.atr(c)

// Adjusting ATR multiplier for Gold on 15-minute timeframe
atrMultiplier = 3
nLoss = atrMultiplier * xATR  // ATR-based loss calculation

src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=barmerge.lookahead_off) : close

// Trailing Stop Calculation
xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2

pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3

xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue

// Define 100 EMA
ema100 = ta.ema(src, 100)
plot(ema100, title="100 EMA", color=color.black, linewidth=2)

// Define 15 EMA and 17 EMA for exit conditions
ema15 = ta.ema(src, 15)
ema17 = ta.ema(src, 17)
plot(ema15, title="15 EMA", color=color.blue, linewidth=1)
plot(ema17, title="17 EMA", color=color.purple, linewidth=1)

// Define Entry Conditions
longCondition = src > ema100 and src > xATRTrailingStop and close > higherTimeframeEMA and ta.crossover(ta.ema(src, 1), xATRTrailingStop)
shortCondition = src < ema100 and src < xATRTrailingStop and close < higherTimeframeEMA and ta.crossover(xATRTrailingStop, ta.ema(src, 1))

// RSI Filter
rsi = ta.rsi(close, 14)
longCondition := longCondition and rsi > 30 and rsi < 70  // Ensure RSI is not in extreme conditions
shortCondition := shortCondition and rsi > 30 and rsi < 70

// Volume Filter
volumeMA = ta.sma(volume, 20)
longCondition := longCondition and volume > volumeMA
shortCondition := shortCondition and volume > volumeMA

// ** Trailing Stop Setup **
trailOffset = nLoss  // The trailing stop distance is based on ATR, which is already calculated
trailPriceLong = close - trailOffset  // The trailing stop for long trades is below the entry price
trailPriceShort = close + trailOffset  // The trailing stop for short trades is above the entry price

// Define Take Profit (TP) condition
takeProfitMultiplier = 2  // This sets the take profit at 2x the ATR from the entry
takeProfitLong = close + takeProfitMultiplier * nLoss
takeProfitShort = close - takeProfitMultiplier * nLoss

// Strategy Entries
if (longCondition)
    strategy.entry('long', strategy.long)

if (shortCondition)
    strategy.entry('short', strategy.short)

// Exit conditions for 15 and 17 EMA cross (must be consecutive on two bars)
exitLong = ta.crossover(ema15, ema17) and ta.crossover(ema15[1], ema17[1])  // Exit long if both the current and previous bars have 15 EMA crossing above 17 EMA
exitShort = ta.crossunder(ema15, ema17) and ta.crossunder(ema15[1], ema17[1])  // Exit short if both the current and previous bars have 15 EMA crossing below 17 EMA

// Apply trailing stop and take profit along with EMA cross exit
strategy.exit("Exit Long", "long", trail_price=trailPriceLong, trail_offset=trailOffset, limit=takeProfitLong)  // Long exit with trailing stop and TP
strategy.exit("Exit Short", "short", trail_price=trailPriceShort, trail_offset=trailOffset, limit=takeProfitShort)  // Short exit with trailing stop and TP

// Close positions when 15 and 17 EMAs cross consecutively
strategy.close("long", when=exitLong)
strategy.close("short", when=exitShort)

// Alert condition for trade closure
longExitAlert = exitLong  // Only trigger alert for long if both consecutive bars show crossover
shortExitAlert = exitShort  // Only trigger alert for short if both consecutive bars show crossunder

alertcondition(longExitAlert, title="Close Long Trade", message="Long trade closed. 15 EMA crossed above 17 EMA on two consecutive bars.")
alertcondition(shortExitAlert, title="Close Short Trade", message="Short trade closed. 15 EMA crossed below 17 EMA on two consecutive bars.")