2つの移動平均のクロスオーバーモメント戦略

作者: リン・ハーンチャオチャン,日付: 2023-11-16 17:25:13
タグ:

img

概要

この戦略は,二重移動平均クロスオーバー原理を利用し,トレンド判断とクロスバックグラウンドハイライトのためのMACD指標を組み合わせ,市場の中期トレンドを把握することを目的としたパターンドットでエントリを確認する.主な論理は,移動平均の方向が変化するときにエントリし,MACD背景色でクロスポイントを突出させ,MACDヒストグラム色の変化によってモメンタムを決定することです.

戦略の論理

この戦略は,速いEMAと遅いEMAを使用して二重移動平均を構築し,速いラインと遅いラインのクロスオーバーに基づいてトレンド方向を決定します.また,MACDとシグナルを計算し,その差をヒストグラムとしてプロットします.

コードによると,高速線長は12で,スローライン長は26で,短期的および長期的トレンドを表しています.シグナル長は9で,追加のスムージメントです.

クロスオーバー論理:

  • trend_up = macd > 信号:速い線がスローラインを横切って,短期上昇傾向を示す

  • trend_dn = macd < 信号:速い線がスローラインを下に横断し,短期的なダウントレンドを示します

交差点検出:

  • cross_UP = signal [1] >= macd [1] と signal < macd: 速い線は下からゆっくりとした線を横断する

  • cross_DN = signal [1] <= macd [1] と signal > macd:高速線は,上から遅い線を横断する

ヒストグラムの色の変化は運動強度を決定します

  • histA_IsUp = ヒストグラム列が増加し,0を超えると,上昇傾向の勢いが強まる

  • histA_IsDown = ヒストグラム列が下がっているが,それでも0より大きい.上昇傾向の勢いが弱まっている.

  • 0以下でも同じ論理です

利点

  1. 二重移動平均は中期トレンドを決定し,短期間の騒音を避ける

  2. MACD は,短期的な傾向と,収益性の向上のための勢いを判断するのに役立ちます.

  3. ヒストグラムの色変更により,よりよいエントリータイミングを特定できます.

  4. クロスオーバーの背景色は信号を強調する

  5. 異なる市場環境に適した調整可能な移動平均期

  6. 調整可能なMACDパラメータは,指標を最適化します.

  7. 複数のエントリー確認を提供します:トレンド,クロスオーバー,パターンブレイク

リスク

  1. 短期変動に敏感でない双重MAsは,短期的な機会を逃す可能性があります.

  2. パラメータの設定が不適切で,MACD効果が悪ければ,誤った信号が生じる可能性があります.

  3. MAs と MACD にのみ基づくエントリには,いくつかの盲点があります.

  4. ストップ・ロスのメカニズムがない場合,損失拡大リスクが生じます.

  5. 厳格な資金管理とポジションサイズ管理の欠如

可能な解決策:

  1. 他の指標を組み合わせて短期間のスウィング範囲とコントロールリスクを定義する

  2. MACD パラメータを最適化し,異なる市場でテストする

  3. パターン,インパクトなどを追加して信号を確認します.

  4. 損失の大きさを制限するためのストップ・ロスのメカニズムを確立する

  5. 資本に基づくポジションサイズにマネーマネジメントモジュールを追加する

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

  1. 市場適応性を高めるために,MAパラメータの組み合わせをテストし最適化します.

  2. VWAP,ボリンガーミッドラインなど

  3. 偽のブレイクを避けるために取引量を考慮してください.

  4. RSI など を 含め て 買い過ぎ/売過ぎ を 確認 する

  5. トレイリングストップや波動性ストップなど 強力なストップ・ロスのメカニズムを構築します

  6. 口座サイズに基づいてポジションのサイズを組み込む

  7. パラメータ最適化のために機械学習を検討する

  8. 強化されたポートフォリオアプローチのための戦略の宇宙を拡張する

結論

この戦略は,二重移動平均トレンドフィルタリングとMACDモメンタムを統合し,パターンの特徴を追加し,比較的安定した中期取引システムを構築する.主要な利点は,短期的なノイズを回避しながら主要なトレンドを捕捉することにある.しかし,ストップ損失メカニズムとリスク管理などの改善が可能な分野もあります.全体的にこれは貴重な概念的な例として機能しますが,ライブ取引のための包括的なテストと精製が必要です.


/*backtest
start: 2022-11-15 00:00:00
end: 2023-11-15 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy(title="Histogram MacD MVP_V2.1", shorttitle="Histogram MacD MVP_2.1")
//Plot Inputs
res           = input.timeframe("",  "Indicator TimeFrame")
fast_length   = input.int(title="Fast Length", defval=12)
slow_length   = input.int(title="Slow Length", defval=26)
src           = input.source(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 999, defval = 9)
sma_source    = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal    = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Show Plots T/F
show_macd     = input.bool(true, title="Show MACD Lines", group="Show Plots?", inline="SP10")
show_macd_LW  = input.int(3, minval=0, maxval=5, title = "MACD Width", group="Show Plots?", inline="SP11")
show_signal_LW= input.int(2, minval=0, maxval=5, title = "Signal Width", group="Show Plots?", inline="SP11")
show_Hist     = input.bool(true, title="Show Histogram", group="Show Plots?", inline="SP20")
show_hist_LW  = input.int(5, minval=0, maxval=5, title = "-- Width", group="Show Plots?", inline="SP20")
show_trend    = input.bool(true, title = "Show MACD Lines w/ Trend Color", group="Show Plots?", inline="SP30")
show_HB       = input.bool(false, title="Show Highlight Price Bars", group="Show Plots?", inline="SP40")
show_cross    = input.bool(false, title = "Show BackGround on Cross", group="Show Plots?", inline="SP50")
show_dots     = input.bool(true, title = "Show Circle on Cross", group="Show Plots?", inline="SP60")
show_dots_LW  = input.int(5, minval=0, maxval=5, title = "-- Width", group="Show Plots?", inline="SP60")

//show_trend    = input(true, title = "Colors MACD Lines w/ Trend Color", group="Show Plots?", inline="SP5")
// MACD Lines colors
col_macd      = input.color(#FF6D00, "MACD Line  ",  group="Color Settings", inline="CS1")
col_signal    = input.color(#2962FF, "Signal Line  ",  group="Color Settings", inline="CS1")
col_trnd_Up   = input.color(#4BAF4F, "Trend Up      ",  group="Color Settings", inline="CS2")
col_trnd_Dn   = input.color(#B71D1C, "Trend Down    ",  group="Color Settings", inline="CS2")
// Histogram Colors
col_grow_above = input.color(#26A69A, "Above   Grow",  group="Histogram Colors", inline="Hist10")
col_fall_above = input.color(#FF5252, "Fall",  group="Histogram Colors", inline="Hist10")
col_grow_below = input.color(#FF5252, "Below Grow",  group="Histogram Colors", inline="Hist20")
col_fall_below = input.color(#f8f524, "Fall",  group="Histogram Colors", inline="Hist20")
// Alerts T/F Inputs
alert_Long    = input.bool(true, title = "MACD Cross Up", group = "Alerts", inline="Alert10")
alert_Short   = input.bool(true, title = "MACD Cross Dn", group = "Alerts", inline="Alert10")
alert_Long_A  = input.bool(false, title = "MACD Cross Up & > 0", group = "Alerts", inline="Alert20")
alert_Short_B = input.bool(false, title = "MACD Cross Dn & < 0", group = "Alerts", inline="Alert20")
// Calculating
fast_ma = request.security(syminfo.tickerid, res, sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length))
slow_ma = request.security(syminfo.tickerid, res, sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length))
macd = fast_ma - slow_ma
signal = request.security(syminfo.tickerid, res, sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length))
hist = macd - signal
// MACD Trend and Cross Up/Down conditions
trend_up   = macd > signal
trend_dn   = macd < signal
cross_UP   = signal[1] >= macd[1] and signal < macd
cross_DN   = signal[1] <= macd[1] and signal > macd
cross_UP_A = (signal[1] >= macd[1] and signal < macd) and macd > 0
cross_DN_B = (signal[1] <= macd[1] and signal > macd) and macd < 0
// Condition that changes Color of MACD Line if Show Trend is turned on..
trend_col = show_trend  and trend_up ? col_trnd_Up : trend_up ? col_macd : show_trend  and trend_dn ? col_trnd_Dn: trend_dn ? col_macd : na 

//Var Statements for Histogram Color Change
var bool histA_IsUp = false
var bool histA_IsDown = false
var bool histB_IsDown = false
var bool histB_IsUp = false
histA_IsUp   := hist == hist[1] ? histA_IsUp[1] : hist > hist[1] and hist > 0
histA_IsDown := hist == hist[1] ? histA_IsDown[1] : hist < hist[1] and hist > 0
histB_IsDown := hist == hist[1] ? histB_IsDown[1] : hist < hist[1] and hist <= 0
histB_IsUp   := hist == hist[1] ? histB_IsUp[1] : hist > hist[1] and hist <= 0

hist_col =  histA_IsUp ? col_grow_above : histA_IsDown ? col_fall_above : histB_IsDown ? col_grow_below : histB_IsUp ? col_fall_below :color.silver 

// Plot Statements
//Background Color
bgcolor(show_cross and cross_UP ? col_trnd_Up : na, editable=false)
bgcolor(show_cross and cross_DN ? col_trnd_Dn : na, editable=false)
//Highlight Price Bars
barcolor(show_HB and trend_up ? col_trnd_Up : na, title="Trend Up", offset = 0, editable=false)
barcolor(show_HB and trend_dn ? col_trnd_Dn : na, title="Trend Dn", offset = 0, editable=false)
//Regular Plots
plot(show_Hist and hist ? hist : na, title="Histogram", style=plot.style_columns, color=color.new(hist_col ,0),linewidth=show_hist_LW)
plot(show_macd  and signal ? signal : na, title="Signal", color=color.new(col_signal, 0),  style=plot.style_line ,linewidth=show_signal_LW)
plot(show_macd  and macd ? macd : na, title="MACD", color=color.new(trend_col, 0),  style=plot.style_line ,linewidth=show_macd_LW)
hline(0, title="0 Line", color=color.new(color.gray, 0), linestyle=hline.style_dashed, linewidth=1, editable=false)
plot(show_dots and cross_UP ? macd : na, title="Dots", color=color.new(trend_col ,0), style=plot.style_circles, linewidth=show_dots_LW, editable=false)
plot(show_dots and cross_DN ? macd : na, title="Dots", color=color.new(trend_col ,0), style=plot.style_circles, linewidth=show_dots_LW, editable=false)

//Alerts
if alert_Long and cross_UP
    alert("Symbol = (" + syminfo.tickerid + ") TimeFrame = (" + timeframe.period + ") Current Price (" + str.tostring(close) + ") MACD Crosses Up.", alert.freq_once_per_bar_close)

if alert_Short and cross_DN
    alert("Symbol = (" + syminfo.tickerid + ") TimeFrame = (" + timeframe.period + ") Current Price (" + str.tostring(close) + ") MACD Crosses Down.", alert.freq_once_per_bar_close)
//Alerts - Stricter Condition - Only Alerts When MACD Crosses UP & MACD > 0 -- Crosses Down & MACD < 0
if alert_Long_A and cross_UP_A
    alert("Symbol = (" + syminfo.tickerid + ") TimeFrame = (" + timeframe.period + ") Current Price (" + str.tostring(close) + ") MACD > 0 And Crosses Up.", alert.freq_once_per_bar_close)

if alert_Short_B and cross_DN_B
    alert("Symbol = (" + syminfo.tickerid + ") TimeFrame = (" + timeframe.period + ") Current Price (" + str.tostring(close) + ") MACD < 0 And Crosses Down.", alert.freq_once_per_bar_close)


if (histA_IsUp)
	strategy.entry("buy", strategy.long, comment="buy")
if (histA_IsDown)
	strategy.entry("sell", strategy.short, comment="sell")


もっと