동적 이동 평균 교차 추세 추적 조합 전략

EMA SMA Moving Average CROSSOVER TREND FOLLOWING STOP LOSS TAKE PROFIT
생성 날짜: 2025-02-24 09:46:10 마지막으로 수정됨: 2025-02-24 09:46:10
복사: 1 클릭수: 315
avatar of ianzeng123 ianzeng123
2
집중하다
319
수행원

동적 이동 평균 교차 추세 추적 조합 전략 동적 이동 평균 교차 추세 추적 조합 전략

개요

이 전략은 다중 평균선 교차를 기반으로 한 트렌드 추적 시스템으로, SMA와 EMA 지표를 결합하여 시장 트렌드를 포착한다. 전략은 사용자 정의 된 주기의 간단한 이동 평균 ((SMA) 과 두 개의 지수 이동 평균 ((EMA) 의 조합을 사용하여 완전한 트렌드 추적 거래 시스템을 구축한다. 동적 정지 및 수익 목표 관리 장치를 통합하면서 위험을 효과적으로 제어하고 수익을 잠금합니다.

전략 원칙

이 전략은 주로 세 개의 평행선의 동적 관계를 기반으로 거래 결정을 내립니다. 시스템은 가격과 SMA의 상대적인 위치를 모니터링하고, 빠른 EMA와 느린 EMA의 교차를 통해 트렌드 방향을 결정합니다. 진입 신호는 두 가지로 구분됩니다.

전략적 이점

  1. 다중 평균선 조합을 사용하면 트렌드 판단의 정확도가 높아지고, 가짜 돌파구로 인한 손실이 감소합니다.
  2. 이중 입시 조건의 설계는 트렌드의 초기 기회를 포착하고 기존의 트렌드의 지속성을 추적합니다.
  3. 동적 상쇄 메커니즘은 이미 이윤을 보호하면서도 트렌드가 충분히 발전할 수 있는 공간을 제공합니다.
  4. 정지/손실 비율이 합리적으로 설정되어 있으며, 리스크 제어와 수익 공간 사이의 균형이 잘 잡혀 있습니다.
  5. 평행선 교차는 추세 반전의 위험을 적시에 회피하는 데 도움이되는 추가 평평한 조건입니다.

전략적 위험

  1. 불안한 시장에서 자주 거래하는 것이 손실로 이어질 수 있습니다.
  2. 다중평균형 시스템은 빠르게 변동하는 시장에서 지연될 수 있다.
  3. 고정된 스톱 로즈 배수는 모든 시장 환경에 적합하지 않을 수 있습니다.
  4. 변동성이 높은 시장에서, 이동한 중지 (mobile stop loss) 은 수익을 조기 고정시킬 수 있습니다.
  5. 매개 변수를 과도하게 최적화하면 전략이 실 디스크에서 재검토 결과를 못 따라하게 될 수 있다.

전략 최적화 방향

  1. 변동성 지표를 도입하여 스톱 및 스톱 배수를 동적으로 조정하여 전략이 다양한 시장 환경에 더 잘 적응하도록합니다.
  2. 출입 신호의 신뢰성을 높이기 위해 보조 확인으로 출입량 지표를 추가합니다.
  3. 시장 변동 특성에 따라 동적으로 수평 주기를 조정하여 전략 적응력을 향상시킵니다.
  4. 트렌드 강도 필터를 추가하여 약한 트렌드 환경에서 자주 거래하는 것을 피하십시오.
  5. 시장의 변동성에 따라 중단 거리를 조정할 수 있는 적응형 모바일 중지 메커니즘을 개발

요약하다

이 전략은 복수평등선의 조합을 사용하여 전체적인 트렌드 추적 시스템을 구축하고 입점, 출구 및 위험 관리와 같은 세부적인 규칙이 설계되어 있습니다. 전략의 장점은 트렌드를 효과적으로 식별하고 추적 할 수 있으며, 동적 스톱 손실 메커니즘을 통해 수익을 보호 할 수 있습니다.

전략 소스 코드
/*backtest
start: 2025-02-17 17:00:00
end: 2025-02-20 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Binance","currency":"SOL_USDT"}]
*/

//@version=5
strategy("交易策略(自定义EMA/SMA参数)", overlay=true, initial_capital=100000, currency=currency.EUR, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// 输入参数:可调的 SMA 和 EMA 周期
smaLength     = input.int(120, "SMA Length", minval=1, step=1)
emaFastPeriod = input.int(13, "EMA Fast Period", minval=1, step=1)
emaSlowPeriod = input.int(21, "EMA Slow Period", minval=1, step=1)

// 计算均线
smaVal   = ta.sma(close, smaLength)
emaFast  = ta.ema(close, emaFastPeriod)
emaSlow  = ta.ema(close, emaSlowPeriod)

// 绘制均线
plot(smaVal, color=color.orange, title="SMA")
plot(emaFast, color=color.blue, title="EMA Fast")
plot(emaSlow, color=color.red, title="EMA Slow")

// 入场条件 - 做多
// 条件1:收盘价高于SMA 且 EMA Fast 向上穿越 EMA Slow
longTrigger1 = (close > smaVal) and ta.crossover(emaFast, emaSlow)
// 条件2:收盘价上穿SMA 且前5根K线的最低价均高于各自的SMA
longTrigger2 = ta.crossover(close, smaVal) and (low[1] > smaVal[1] and low[2] > smaVal[2] and low[3] > smaVal[3] and low[4] > smaVal[4] and low[5] > smaVal[5])
longCondition = longTrigger1 or longTrigger2

// 入场条件 - 做空
// 条件1:收盘价低于SMA 且 EMA Fast 向下穿越 EMA Slow
shortTrigger1 = (close < smaVal) and ta.crossunder(emaFast, emaSlow)
// 条件2:收盘价下穿SMA 且前5根K线的最高价均低于各自的SMA
shortTrigger2 = ta.crossunder(close, smaVal) and (high[1] < smaVal[1] and high[2] < smaVal[2] and high[3] < smaVal[3] and high[4] < smaVal[4] and high[5] < smaVal[5])
shortCondition = shortTrigger1 or shortTrigger2

// 定义变量记录入场时的价格与EMA Fast值,用于计算止损
var float entryPriceLong      = na
var float entryEMA_Fast_Long   = na
var float entryPriceShort     = na
var float entryEMA_Fast_Short = na

// 入场与初始止盈止损设置 - 做多
// 止损取“开仓时的EMA Fast价格”与“0.2%止损”中较大者;止盈为止损的5倍
if (longCondition and strategy.position_size == 0)
    entryPriceLong      := close
    entryEMA_Fast_Long  := emaFast
    strategy.entry("Long", strategy.long)
    stopPercLong = math.max(0.002, (entryPriceLong - entryEMA_Fast_Long) / entryPriceLong)
    stopLong     = entryPriceLong * (1 - stopPercLong)
    tpLong       = entryPriceLong * (1 + 5 * stopPercLong)
    strategy.exit("LongExit", "Long", stop=stopLong, limit=tpLong)

// 入场与初始止盈止损设置 - 做空
// 止损取“开仓时的EMA Fast价格”与“0.2%止损”中较大者;止盈为止损的5倍
if (shortCondition and strategy.position_size == 0)
    entryPriceShort      := close
    entryEMA_Fast_Short  := emaFast
    strategy.entry("Short", strategy.short)
    stopPercShort = math.max(0.002, (entryEMA_Fast_Short - entryPriceShort) / entryPriceShort)
    stopShort     = entryPriceShort * (1 + stopPercShort)
    tpShort       = entryPriceShort * (1 - 5 * stopPercShort)
    strategy.exit("ShortExit", "Short", stop=stopShort, limit=tpShort)

// 移动止损逻辑
// 当持仓盈利达到0.8%时更新止损和止盈,保持止盈为止损的5倍
var float longHighest = na
if (strategy.position_size > 0)
    longHighest := na(longHighest) ? high : math.max(longHighest, high)
    if (high >= entryPriceLong * 1.008)
        newLongStop = longHighest * (1 - 0.003)
        newPerc     = (entryPriceLong - newLongStop) / entryPriceLong
        newLongTP   = entryPriceLong * (1 + 5 * newPerc)
        strategy.exit("LongExit", "Long", stop=newLongStop, limit=newLongTP)
else
    longHighest := na

var float shortLowest = na
if (strategy.position_size < 0)
    shortLowest := na(shortLowest) ? low : math.min(shortLowest, low)
    if (low <= entryPriceShort * 0.992)
        newShortStop  = shortLowest * (1 + 0.003)
        newPercShort  = (newShortStop - entryPriceShort) / entryPriceShort
        newShortTP    = entryPriceShort * (1 - 5 * newPercShort)
        strategy.exit("ShortExit", "Short", stop=newShortStop, limit=newShortTP)
else
    shortLowest := na

// 额外平仓条件
// 如果持多仓时EMA Fast下穿EMA Slow,则立即平多
if (strategy.position_size > 0 and ta.crossunder(emaFast, emaSlow))
    strategy.close("Long", comment="EMA下穿平多")
// 如果持空仓时EMA Fast上穿EMA Slow,则立即平空
if (strategy.position_size < 0 and ta.crossover(emaFast, emaSlow))
    strategy.close("Short", comment="EMA上穿平空")