ダブルMACDに基づく定量取引戦略


作成日: 2024-01-30 16:43:29 最終変更日: 2024-01-30 16:43:29
コピー: 0 クリック数: 900
1
フォロー
1617
フォロワー

ダブルMACDに基づく定量取引戦略

概要

双MACD量化取引戦略は,二時間枠MACD指標を利用して実現される量化取引戦略である.この戦略は,周回線MACD指標が金叉を形成する時に多仓を打つ,日線MACD指標が死叉を形成する時に平仓を打つ.ポジションが空いているとき,日線MACD指標が再び金叉を形成する場合は,再び多仓を打つことができる.

戦略原則

双MACD量化取引戦略は,週間のMACD指数と日間のMACD指数の組み合わせを使用して,入場と出場シグナルを判断する.

まず,週MACDのMACD線が信号線を横切るときに買入シグナルが生じ,その時に多めにポジションを開く.そして,その日のMACDのMACD線が信号線を横切るときに売り出シグナルが生じ,その時に平仓する.

ポジションが空いているとき,日MACD指数のMACD線が再び信号線を貫くならば,再びポジションを開く.すなわち,日MACD指数の金叉が再びポジション開く条件となる.

注目すべきは,日MACD指標のデッドフォークが平定するだけだが,週MACD指標のMACD線が信号線より高い取引ウィンドウ内で再びポジションを開くことが許される.

戦略的優位性

双MACD定量取引戦略は,二時間枠分析を組み合わせて,偽信号を効果的にフィルターし,信号の質を向上させる.具体的には,以下のいくつかの利点があります.

  1. 週間の時間枠は,主要トレンドの方向を判断し,逆転取引を避けるのに役立ちます.

  2. 日時枠は,入場と出場のタイミングを判断し,短期間の取引機会を間に合うように捉えます.

  3. 取引窓口のメカニズムは,短期的な調整による過度に頻繁なポジション開設や平定を回避する.

  4. MACD指標のパラメータは調整可能で,異なる品種と市場環境に応じて最適化することができます.

  5. 停止,停止,移動停止機能を統合して,リスクを効果的に制御できます.

戦略リスク

双MACD量化取引戦略にはいくつかのリスクがあります.

  1. MACD指標は偽信号と頻繁に交差する傾向があり,他の指標の組み合わせで確認する必要がある.

  2. 週月時間枠の判断の主なトレンドは逆転する可能性があり,時効的に停止する必要がある.

  3. パラメータは,異なる品種と市場環境に応じて,常に最適化および調整する必要があります.

  4. 測量結果に過度に依存しないこと,測量結果と異なることがある.

対応方法:

  1. 他の指標の組み合わせで,論理的に最適化された戦略体系を構築する.

  2. 合理的な止損幅を設定し,最大許容される損失を超えないようにする.

  3. パラメータを常に最適化して,最適なパラメータの組み合わせを探します.

  4. 戦略の安定性を検証するために,最小の資金から実物投資を開始します.

最適化の方向

双 MACD 量化取引戦略は,さらに最適化できる余地があります.

  1. ブリンライン,KDJなどの他の指標を導入して,多指標組合せ戦略を構築し,信号品質を向上させることができる.

  2. 取引量指数と組み合わせることで,価格が上昇しても取引量が不足する偽の突破を回避できます.

  3. 機械学習の方法を使用してパラメータを自動的に最適化し,パラメータの動的調整を実現することができる.

  4. 戦略に適したさらなるリスク調整は可能である.例えば,損失比率などの高度な止損方法の追加である.

  5. 戦略の適合性テストと最適化調整,過適合の問題を回避する.

要約する

双MACD量化取引戦略は,双時間枠分析を統合して主副トレンドを判断し,それぞれの指標の優位性を発揮する.戦略の最適化の余地はまだ十分であり,他の指標を導入し,機械学習を利用してパラメータの最適化などの方法によって戦略の効果をさらに向上させる見込みがある.実態検証は必須のステップであり,さらに戦略を完善するための重要な基礎でもある.

ストラテジーソースコード
/*backtest
start: 2023-01-29 00:00:00
end: 2024-01-11 05:20:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © maxits

// Long Position: Weekly Macd line crosses above Signal line   
// [Trading Window Macd Line > Signal Line] (Weekly)
// Close Position: Daily Macd Line crosses above Daily Signal line.  
// Re Entry Condition: Macd line crosses above Signal line only if [Trading Window MacdLine > Sgnal Line] (Weekly)

//@version=4
strategy("Dual MACD Strategy",
         shorttitle="Dual Macd Tester",
         overlay=false,
         initial_capital=1000,
         default_qty_value=20,
         default_qty_type=strategy.percent_of_equity,
         commission_value=0.1,
         pyramiding=0)



// Define user inputs
i_time     = input(defval = timestamp("01 May 2018 13:30 +0000"), title = "Start Time", type = input.time) // Starting  time for Backtesting
f_time     = input(defval = timestamp("9 Sep 2021 13:30 +0000"), title = "Finish Time", type = input.time) // Finishing time for Backtesting

sep1          = input(false, title="------ Profit & Loss ------")

enable_TP     = input(true, title="Enable Just a Profit Level?")
enable_SL     = input(false, title="Enable Just a S.Loss Level?")
enable_TS     = input(true, title=" Enable Only Trailing Stop")
long_TP_Input = input(30.0,   title='Take Profit %',      type=input.float, minval=0)/100
long_SL_Input = input(1.0,   title='Stop Loss %',        type=input.float, minval=0)/100
long_TS_Input = input(5.0,   title='Trailing Stop %',    type=input.float, minval=0)/100
cl_low_Input  = input(low,   title="Trailing Stop Source")
long_TP       = strategy.position_avg_price * (1 + long_TP_Input)
long_SL       = strategy.position_avg_price * (1 - long_SL_Input)
long_TS       = cl_low_Input * (1 - long_TS_Input)

sep2       = input(false, title="------ Macd Properties ------")

d_res      = input(title="Short Term TimeFrame", type=input.resolution, defval="D") // Daily Time Frame
w_res      = input(title="Long Term TimeFrame", type=input.resolution, defval="W")  // Weekly Time Frame
src        = input(close, title="Source")                                           // Indicator Price Source
fast_len   = input(title="Fast Length", type=input.integer, defval=12)              // Fast MA Length
slow_len   = input(title="Slow Length", type=input.integer, defval=26)              // Slow MA Length
sign_len   = input(title="Sign Length", type=input.integer, defval=9)               // Sign MA Length
d_w        = input(title="Daily or Weekly?", type=input.bool, defval=true)          // Plot Daily or Weekly MACD

// Color Plot for Macd

col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350

// BG Color

bg_color = color.rgb(127, 232, 34, 75)

// Daily Macd

[d_macdLine, d_singleLine, d_histLine] = security(syminfo.tickerid, d_res, macd(src, fast_len, slow_len, sign_len)) // Funcion Security para poder usar correcta resolución

plot(d_w ? d_macdLine   : na, color=color.blue)
plot(d_w ? d_singleLine : na, color=color.orange)
plot(d_w ? d_histLine   : na, style=plot.style_columns,
     color=(d_histLine>=0 ? (d_histLine[1] < d_histLine ? col_grow_above : col_fall_above) : 
     (d_histLine[1] < d_histLine ? col_grow_below : col_fall_below)))
    
// Weekly Macd

[w_macdLine, w_singleLine, w_histLine] = security(syminfo.tickerid, w_res, macd(src, fast_len, slow_len, sign_len)) // Funcion Security para poder usar correcta resolución

plot(d_w ? na : w_macdLine,   color=color.blue)
plot(d_w ? na : w_singleLine, color=color.orange)
plot(d_w ? na : w_histLine,   style=plot.style_columns,
     color=(w_histLine>=0 ? (w_histLine[1] < w_histLine ? col_grow_above : col_fall_above) : 
     (w_histLine[1] < w_histLine ? col_grow_below : col_fall_below)))

///////////////////////////////// Entry Conditions
inTrade    = strategy.position_size != 0       // Posición abierta
notInTrade = strategy.position_size == 0       // Posición Cerrada
start_time = true

trading_window = w_macdLine > w_singleLine   // Weekly Macd Signal enables a trading window 
bgcolor(trading_window ? bg_color : na)
buy_cond       = crossover (w_macdLine, w_singleLine)
sell_cond      = crossunder(d_macdLine, d_singleLine)
re_entry_cond  = crossover (d_macdLine, d_singleLine) and trading_window

// Entry Exit Conditions

trailing_stop  = 0.0        // Code for calculating Long Positions Trailing Stop Loss
trailing_stop := if (strategy.position_size != 0)
    stopValue = long_TS
    max(trailing_stop[1], stopValue)
else 
    0

if (buy_cond and notInTrade and start_time)
    strategy.entry(id="First Entry", long=strategy.long, comment="First Long")

if (sell_cond and inTrade)
    strategy.close(id="First Entry", comment="Close First Long")
    
if (re_entry_cond and notInTrade and start_time)
    strategy.entry(id="Further Entry", long=strategy.long, comment="Further Entry")

if (sell_cond and inTrade)
    strategy.close(id="Further Entry", comment="Close First Long")

if enable_TP
    if (enable_TS and not enable_SL)
        strategy.exit("Long TP & TS FiEn", "First Entry",   limit = long_TP, stop = trailing_stop)
        strategy.exit("Long TP & TS FuEn", "Further Entry", limit = long_TP, stop = trailing_stop)
    else
        if (enable_SL and not enable_TS)
            strategy.exit("Long TP & TS FiEn", "First Entry",   limit = long_TP, stop = long_SL)
            strategy.exit("Long TP & TS FuEn", "Further Entry", limit = long_TP, stop = long_SL)
        else 
            strategy.exit("Long TP & TS FiEn", "First Entry",   limit = long_TP)
            strategy.exit("Long TP & TS FuEn", "Further Entry", limit = long_TP)
else
    if not enable_TP 
        if (enable_TS and not enable_SL)
            strategy.exit("Long TP & TS FiEn", "First Entry",   stop = trailing_stop)
            strategy.exit("Long TP & TS FuEn", "Further Entry", stop = trailing_stop)
        else
            if (enable_SL and not enable_TS)
                strategy.exit("Long TP & TS FiEn", "First Entry",   stop = long_SL)
                strategy.exit("Long TP & TS FuEn", "Further Entry", stop = long_SL)

plot(enable_TP ? long_TP : na, title="TP Level", color=color.green, style=plot.style_linebr, linewidth=2)
plot(enable_SL ? long_SL : na, title="SL Level", color=color.red,   style=plot.style_linebr, linewidth=2)
plot(enable_TS and trailing_stop ? trailing_stop : na, title="TS Level", color=color.red, style=plot.style_linebr, linewidth=2)