구성 가능한 이동 평균 교차 전략

MA-X EMA MA CROSSOVER trading strategy risk management
생성 날짜: 2025-04-03 11:31:33 마지막으로 수정됨: 2025-04-03 11:31:33
복사: 9 클릭수: 300
avatar of ianzeng123 ianzeng123
2
집중하다
319
수행원

구성 가능한 이동 평균 교차 전략 구성 가능한 이동 평균 교차 전략

개요

이 문서에서는 유연하고 강력한 이동 평균 크로스 트레이딩 전략에 대해 소개합니다. 이 전략은 트레이더가 다른 시장 조건에 따라 이동 평균의 파라미터와 유형을 사용자 정의 할 수 있습니다. 이 전략의 핵심은 다양한 주기 및 유형의 이동 평균을 사용하여 트렌드 추적과 신호 생성입니다.

전략 원칙

전략은 세 개의 다른 주기들의 이동 평균을 계산하여 거래 신호를 생성합니다. 주요 원칙은 다음과 같습니다.

  1. 이동 평균 유형 선택: 지원 간단한 이동 평균 ((SMA), 지수 이동 평균 ((EMA), 가중 이동 평균 ((WMA) 및 헐 이동 평균 ((HMA)
  2. 입장 조건:
    • 다중 엔트리: 클로즈값이 빠른 라인보다 높고, 빠른 라인은 느린 라인보다 높고, 클로즈값이 퇴출 라인보다 높다
    • 공백 입구: 닫기 값이 빠른 선보다 낮고, 빠른 선은 느린 선보다 낮고, 닫기 값은 퇴출 선보다 낮다
  3. 출전 조건:
    • 다수 출전: 최소 2개의 K 라인에 진입한 후, 출전 라인보다 낮은 가격으로 종료
    • 공짜 출전: 적어도 두 개의 K 라인에 진입한 후, 출전 라인보다 높은 매각 가격

전략적 이점

  1. 고도로 구성 가능: 거래자는 이동 평균 주기와 유형을 조정할 수 있습니다.
  2. 다시장 적응성: 변수를 조정하여 유동성이 다른 거래 품종에 적용할 수 있다
  3. 트렌드 추적 능력: 여러 이동 평균을 사용하여 가짜 신호를 필터링합니다.
  4. 리스크 제어: 계정 지분 10%의 위치 관리로 기본 설정
  5. 유연한 거래 방향: 공허 거래가 가능할 경우 선택

전략적 위험

  1. 매개 변수 민감성: 다른 시장에는 다른 이동 평균 매개 변수가 필요할 수 있습니다.
  2. 트렌딩 시장의 성능이 더 좋아: 흔들리는 시장에서 더 많은 무효 신호가 발생할 수 있습니다.
  3. 거래 비용: 전략의 기본 설정은 0.06%의 거래 수수료이며 실제 거래에서 고려해야 합니다.
  4. 감지 제한: 현재 일부 품종에서만 (BTCUSD 및 NIFTY와 같은) 초기 검증

전략 최적화 방향

  1. 동적 변수 조정: 적응형 이동 평균 주기 도입
  2. 다른 기술 지표와 결합: RSI, MACD와 같은 지표를 추가하여 신호 필터링
  3. 손해 방지 장치: 변동율에 기반한 손해 방지 전략을 추가
  4. 다중 시간 프레임 검증: 다양한 시간 주기에서 전체적인 재검토
  5. 기계 학습 최적화: 알고리즘을 사용하여 최적의 변수 조합을 자동으로 찾습니다.

요약하다

구성 가능한 이동 평균 가로 전략 ((MA-X) 은 유연한 트렌드 추적 프레임워크를 제공합니다. 합리적인 구성과 지속적인 최적화를 통해 이 전략은 양적 거래 툴킷의 강력한 도구가 될 수 있습니다. 거래자는 특정 시장 특성에 따라 개인화 된 조정을 수행하고 충분한 피드백과 검증을 수행해야합니다.

전략 소스 코드
/*backtest
start: 2024-04-03 00:00:00
end: 2025-04-02 00:00:00
period: 2d
basePeriod: 2d
exchanges: [{"eid":"Futures_Binance","currency":"BNB_USDT"}]
*/

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © YetAnotherTA

//@version=6
strategy("Configurable MA Cross (MA-X) Strategy", "MA-X", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type = strategy.commission.percent, commission_value = 0.06)

// === Inputs ===
// Moving Average Periods
maPeriodA = input.int(13, title="Fast MA")
maPeriodB = input.int(55, title="Slow MA")
maPeriodC = input.int(34, title="Exit MA")

// MA Type Selection
maType = input.string("EMA", title="MA Type", options=["SMA", "EMA", "WMA", "HMA"])

// Toggle for Short Trades (Disabled by Default)
enableShorts = input.bool(false, title="Enable Short Trades", tooltip="Enable or disable short positions")

// === Function to Select MA Type ===
getMA(src, length) =>
    maType == "SMA" ? ta.sma(src, length) : maType == "EMA" ? ta.ema(src, length) : maType == "WMA" ? ta.wma(src, length) : ta.hma(src, length)

// === MA Calculation ===
maA = getMA(close, maPeriodA)
maB = getMA(close, maPeriodB)
maC = getMA(close, maPeriodC)

// === Global Variables for Crossover Signals ===
var bool crossAboveA = false
var bool crossBelowA = false

crossAboveA := ta.crossover(close, maA)
crossBelowA := ta.crossunder(close, maA)

// === Bar Counter for Exit Control ===
var int barSinceEntry = na

// Reset the counter on new entries
if (strategy.opentrades == 0)
    barSinceEntry := na

// Increment the counter on each bar
if (strategy.opentrades > 0)
    barSinceEntry := (na(barSinceEntry) ? 1 : barSinceEntry + 1)

// === Entry Conditions ===
goLong = close > maA and maA > maB and close > maC and crossAboveA
goShort = enableShorts and close < maA and maA < maB and close < maC and crossBelowA  // Shorts only when toggle is enabled

// === Exit Conditions (only after 1+ bar since entry) ===
exitLong = (strategy.position_size > 0) and (barSinceEntry >= 2) and (close < maC)
exitShort = enableShorts and (strategy.position_size < 0) and (barSinceEntry >= 2) and (close > maC)

// === Strategy Execution ===
// Long entry logic
if (goLong)
    strategy.close("Short")         // Close any short position
    strategy.entry("Long", strategy.long)
    alert("[MA-X] Go Long")
    barSinceEntry := 1               // Reset the bar counter

// Short entry logic (only if enabled)
if (enableShorts and goShort)
    strategy.close("Long")          // Close any long position
    strategy.entry("Short", strategy.short)
    alert("[MA-X] Go Short")
    barSinceEntry := 1               // Reset the bar counter

// Exit logic (only after at least 1 bar has passed)
if (exitLong)
    strategy.close("Long")
    alert("[MA-X] Exit Long")

if (enableShorts and exitShort)
    strategy.close("Short")
    alert("[MA-X] Exit Short")

// === Plotting ===
plot(maA, color=color.green, linewidth=2, title="Fast MA")
plot(maB, color=color.blue, linewidth=2, title="Slow MA")
plot(maC, color=color.red, linewidth=2, title="Exit MA")