2つの移動平均のクロスオーバー戦略

作者: リン・ハーンチャオチャン,日付: 2024-01-12 14:59:18
タグ:

img

概要

ダブル・ムービング・平均クロスオーバー戦略は,典型的なトレンドフォロー戦略である.異なる期間の2つのEMA線を使用し,短い期間のEMAが長い期間のEMAをクロスすると長くなって,逆のクロスがトレンド逆転を記録するときに短くなってしまう.

原則

この戦略の主な指標は,EMA線が2つで,1つは30期,もう1つは60期である.この2つのEMA線はコード内のカスタム関数で計算される.

emaLen1 = emaFuncOne(close, lenMA1)
emaLen2 = emaFuncTwo(close, lenMA2)  

取引シグナルは,二つのEMA線が交差する時に生成される.

currentState = if emaLen2 > emaLen1
    0
else 
    1

previousState = if emaLastLen2 > emaLastLen1 
    0
else
    1

convergence = if currentState != previousState
    1  
else
    0

短い期間の EMA が長期間の EMA を横切る時,currentState は前期状態に等しくありません.クロスオーバー信号が発信され,ロングになります. 短い期間のEMAが長い期間のEMAを下回ると,currentStateは前のStateに等しくなく,クロスオーバー信号が発信され,ショートになります.

利点分析

この戦略の利点は次のとおりです.

  1. 論理はシンプルで直感的で 分かりやすく実行できます
  2. 価格変動をEMAと調整し,市場の騒音をフィルタリングする
  3. トレンドを自動的にフォローし,見逃した取引を回避します

リスク分析

この戦略にはいくつかのリスクもあります:

  1. クロスオーバー信号は遅れており,反転を間に合うように捉えることができない.
  2. Whipsaw信号は,範囲市場中に頻繁に発生する可能性があります.
  3. パラメータの調節が不十分で過敏度や遅延を引き起こす可能性があります.

EMA期間を調整したりフィルターを追加したりすることで最適化できます.

オプティマイゼーションの方向性

この戦略は,次の側面から最適化できます.

  1. 異なるEMA期間の組み合わせをテストする
  2. 誤った信号を減らすために音量または波動性フィルターを追加します.
  3. 傾向を確認するためにMACDのような他の指標を組み込む
  4. ストップ・ロストと利益を取ることでマネー管理を最適化します

結論

ダブル・ムービング・アベア・クロスオーバー戦略は,全体的にトレンドフォローするシンプルで実践的なシステムです. シンプルで,実装が簡単で,トレンドを自動的に追跡できます. しかし,遅れや誤ったシグナルなどのリスクがあります. パラメータ調節とフィルターを追加することで,さらに改善され,基本的なアルゴリズム取引戦略の1つになります.


/*backtest
start: 2024-01-10 00:00:00
end: 2024-01-11 00:00:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=2
strategy("ParkerMAStrat", overlay=true)

lenMA1=input(title="Length 1", defval=30)
lenMA2=input(title="Length 2",  defval=60)

x = 0

checkLines(current, last) =>

    if current > last
        x = 1
    else
        x = 0
    x
    

//plot ema based on len1
emaFuncOne(src, time_period) =>

    alpha = 2 / (time_period + 1)
    // we have defined the alpha function above
    ema = 0.0
    // this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
    ema := alpha * src + (1 - alpha) * nz(ema[1])
    // this returns the computed ema at the current time
    // notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
    // since the ema was previously declared to 0
    // this is called mutable variale declaration in pine script
    ema
    // return ema from the function

emaLen1 = emaFuncOne(close, lenMA1)

    
plot(emaLen1, color=green, transp=0, linewidth=2)
// now we plot the _10_period_ema

//plot ema based on len2
emaFuncTwo(src, time_period) =>

    alpha = 2 / (time_period + 1)
    // we have defined the alpha function above
    ema = 0.0
    // this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
    ema := alpha * src + (1 - alpha) * nz(ema[1])
    // this returns the computed ema at the current time
    // notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
    // since the ema was previously declared to 0
    // this is called mutable variale declaration in pine script
    ema
    // return ema from the function

//plot ema based on len2
emaFuncOneLast(src, time_period) =>

    alpha = 2 / (time_period + 1)
    // we have defined the alpha function above
    ema = 0.0
    // this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
    ema := alpha * src + (1 - alpha) * nz(ema[0])
    // this returns the computed ema at the current time
    // notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
    // since the ema was previously declared to 0
    // this is called mutable variale declaration in pine script
    ema
    // return ema from the function

//plot ema based on len2
emaFuncTwoLast(src, time_period) =>

    alpha = 2 / (time_period + 1)
    // we have defined the alpha function above
    ema = 0.0
    // this is the initial declaration of ema, since we dont know the first ema we will declare it to 0.0 [as a decimal]
    ema := alpha * src + (1 - alpha) * nz(ema[0])
    // this returns the computed ema at the current time
    // notice the use of : (colon) symbol before =, it symbolises, that we are changing the value of ema,
    // since the ema was previously declared to 0
    // this is called mutable variale declaration in pine script
    ema
    // return ema from the function



emaLastLen1 = emaFuncOneLast(close, lenMA1)
emaLastLen2 = emaFuncTwoLast(close, lenMA2)
emaLen2 = emaFuncTwo(close, lenMA2)

    
plot(emaLen2, color=red, transp=30, linewidth=2)
// now we plot the _10_period_ema

//now we compare the two and when green crosses red we buy/sell (line1 vs line2)

previousState = if emaLastLen2 > emaLastLen1
    0
else
    1

currentState = if emaLen2 > emaLen1
    0
else
    1

convergence = if currentState != previousState
    1
else
    0

    
lineCheck = if convergence == 1 
    checkLines(currentState, previousState)
    
if lineCheck == 1
    strategy.entry("Long", strategy.long)
else
    strategy.entry("Short", strategy.short)


もっと