動的封筒移動平均戦略

作者: リン・ハーンチャオチャン開催日:2024年2月5日 (月) 14時15分40秒
タグ:

img

概要

この戦略は,移動平均線とダイナミックエンベロープラインをベースに,ロングとショートトレードの両方を実装する. ポジションを確立するためにエンベロープラインを超えた価格ブレイクを追跡し,価格がベースライン移動平均線を下回るとポジションを閉鎖する. この戦略は,明らかなトレンドを持つ株式や暗号通貨にうまく機能する.

戦略の論理

まず,この戦略は,ユーザーによって定義された移動平均の種類と長さに基づいてベースライン移動平均を計算します.一般的な移動平均にはSMA,EMAなどがあります.

価格の変動は,価格の変動率を基準値の移動平均値より5%上回る場合,ポジションを設定することを意味します.封筒線の数はカスタマイズできます.

入場ルールを考慮すると,価格が下層封筒線を下回るとロングで,価格が上層封筒線を下回るとショートです.ルールはシンプルで直接的です.

最後に,価格がベースラインの移動平均値を下回るとすべてのポジションを閉じます.これはトレンドを追跡する出口ポイントです.

この戦略は,部分的なポジション設定を実施している.複数の封筒ラインが存在する場合,資本は比例的に割り当てられる.これは一方的な賭けのリスクを防ぐ.

利点分析

この戦略の最大の利点は

  1. 自動的なトレンドフォロー 移動平均を使用してトレンド方向を決定することは確立された方法です

  2. 過剰な敏感な取引を防止する 封筒線でノイズをフィルタリングする. 合理的なパラメータ設定は戦略の収益性を大幅に改善することができます.

  3. 部分的なポジションの確立は戦略の回復力を高めます.一方が失敗しても,もう一方がうまく走ることができるのです.これは全体的なリスク・リターン比を最適化します.

  4. 調整可能な移動平均値と封筒線番号.これは異なる製品に基づくパラメータ調整の柔軟性を高めます.

リスク分析

この戦略の主なリスクは

  1. 移動平均系は黄金十字信号に敏感ではない.明らかなトレンドがない場合,いくつかの機会を逃す可能性があります.

  2. オーバーワイドな封筒ライン設定は,取引頻度と滑り込みリスクを増やす可能性があります.あまりにも狭い設定は,より大きな動きを見逃す可能性があります.バランスを探すには徹底的なテストが必要です.

  3. この戦略は,市場が異なる場合,より多くの失敗に直面する可能性があります.したがって,トレンド製品が好ましいです.

  4. 部分的なポジション設定の利得制限.一方的な賭けを求める場合,さらなる最適化が必要です.

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

この戦略を最適化するための主な方向性:

  1. KDJ などなどの他の入力/出出指示で置き換えるか,複数の指示を持つフィルターを追加します.

  2. ストップ・プロフィット/ロスト・ロジックを追加します.これは利益の一部をロックし,積極的にリスクの一部を軽減します.

  3. 最良の移動平均値と封筒の組み合わせを見つけるためにパラメータを最適化します.広範なバックテストと最適化が必要です.

  4. スマートなパラメータ調整のためにディープラーニングなどを組み込む. 絶えず学習し,時間とともに更新する.

  5. 製品と市場の違いを考慮し,異なる取引環境に適した複数のパラメータセットを設定します.これは戦略の安定性を大幅に向上させます.

結論

結論として,このダイナミックエンベロープ移動平均戦略はトレンド取引に非常にうまく機能します. シンプルで効率的で,理解し最適化しやすい. 基本的な戦略として,それは大きな可塑性と拡張性を持っています. より複雑なシステムと組み合わせると,より高い収益とより良いリスク調整指標のためにさらに強化することができます. したがって,定量的な取引戦略のための優れた基盤として機能します.


/*backtest
start: 2024-01-05 00:00:00
end: 2024-02-04 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Envelope Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=1000, pyramiding = 5, commission_type=strategy.commission.percent, commission_value=0.0)

// CopyRight Crypto Robot

src = input(ohlc4, title="Source", group = "Base MA")
ma_base_window = input.int(5, "Base Mooving Average Window", step = 1, group = "Base MA")
ma_type = input.string(defval='1. SMA', options=['1. SMA', '2. PCMA', '3. EMA', '4. WMA', '5. DEMA', '6. ZLEMA', '7. HMA'], title='MA Type', group = "Base MA")


envelope_1_pct = input.float(0.05, "Envelope 1", step = 0.01, group = "Envelopes")
envelope_2_pct = input.float(0.10, "Envelope 2", step = 0.01, group = "Envelopes")
envelope_3_pct = input.float(0.15, "Envelope 3", step = 0.01, group = "Envelopes")
envelope_4_pct = input.float(0.0, "Envelope 4", step = 0.01, group = "Envelopes")
envelope_5_pct = input.float(0.0, "Envelope 5", step = 0.01, group = "Envelopes")

use_longs = input.bool(true, 'Long Positions') 
use_short = input.bool(true, 'Short Positions')

total_envelope = 0
if envelope_1_pct > 0
    total_envelope := total_envelope + 1
if envelope_2_pct > 0
    total_envelope := total_envelope + 1
if envelope_3_pct > 0
    total_envelope := total_envelope + 1
if envelope_4_pct > 0
    total_envelope := total_envelope + 1
if envelope_5_pct > 0
    total_envelope := total_envelope + 1

// ---------------------------------------------
// -------------- INDICATORS -------------------
ma_function(MA_type, MA_length) =>
    zlema_lag = (MA_length - 1) / 2
    hma_src = MA_type == '7. HMA' ? 2 * ta.wma(src, math.floor(MA_length / 2)) - ta.wma(src, MA_length) : na
    MA_type == '1. SMA' ? ta.sma(src, MA_length) : MA_type == '2. PCMA' ? (ta.highest(high, MA_length) + ta.lowest(low, MA_length)) / 2 : MA_type == '3. EMA' ? ta.ema(src, MA_length) : MA_type == '4. WMA' ? ta.wma(src, MA_length) : MA_type == '5. DEMA' ? 2 * ta.ema(src, MA_length) - ta.ema(ta.ema(src, MA_length), MA_length) : MA_type == '6. ZLEMA' ? ta.ema(src + src - src[zlema_lag], MA_length) : MA_type == '7. HMA' ? ta.wma(hma_src, math.floor(math.sqrt(MA_length))) : na

    
ma_base = ma_function(ma_type, ma_base_window)

ma_high_1 = envelope_1_pct > 0 ? ma_base * (1 + envelope_1_pct) : na
ma_high_2 = envelope_2_pct > 0 ? ma_base * (1 + envelope_2_pct) : na
ma_high_3 = envelope_3_pct > 0 ? ma_base * (1 + envelope_3_pct) : na
ma_high_4 = envelope_4_pct > 0 ? ma_base * (1 + envelope_4_pct) : na
ma_high_5 = envelope_5_pct > 0 ? ma_base * (1 + envelope_5_pct) : na

ma_low_1 = envelope_1_pct > 0 ? ma_base * (1 - envelope_1_pct) : na
ma_low_2 = envelope_2_pct > 0 ? ma_base * (1 - envelope_2_pct) : na
ma_low_3 = envelope_3_pct > 0 ? ma_base * (1 - envelope_3_pct) : na
ma_low_4 = envelope_4_pct > 0 ? ma_base * (1 - envelope_4_pct) : na
ma_low_5 = envelope_5_pct > 0 ? ma_base * (1 - envelope_5_pct) : na

// ---------------------------------------------
// --------------- STRATEGY --------------------
if use_longs
    if envelope_1_pct > 0 and strategy.opentrades < 1
        strategy.entry('long 1', strategy.long, limit=ma_low_1, qty=(strategy.equity / ma_low_1) * (1 / total_envelope))
    if envelope_2_pct > 0 and strategy.opentrades < 2
        strategy.entry('long 2', strategy.long, limit=ma_low_2, qty=(strategy.equity / ma_low_2) * (1 / total_envelope))
    if envelope_3_pct > 0 and strategy.opentrades < 3
        strategy.entry('long 3', strategy.long, limit=ma_low_3, qty=(strategy.equity / ma_low_3) * (1 / total_envelope))
    if envelope_4_pct > 0 and strategy.opentrades < 4
        strategy.entry('long 4', strategy.long, limit=ma_low_4, qty=(strategy.equity / ma_low_4) * (1 / total_envelope))
    if envelope_5_pct > 0 and strategy.opentrades < 5
        strategy.entry('long 5', strategy.long, limit=ma_low_5, qty=(strategy.equity / ma_low_5) * (1 / total_envelope))


  
if use_short
    if envelope_1_pct > 0 and strategy.opentrades < 1
        strategy.entry('short 1', strategy.short, limit=ma_high_1, qty=(strategy.equity / ma_high_1) * (1 / total_envelope))
    if envelope_2_pct > 0 and strategy.opentrades < 2
        strategy.entry('short 2', strategy.short, limit=ma_high_2, qty=(strategy.equity / ma_high_2) * (1 / total_envelope))
    if envelope_3_pct > 0 and strategy.opentrades < 3
        strategy.entry('short 3', strategy.short, limit=ma_high_3, qty=(strategy.equity / ma_high_3) * (1 / total_envelope))
    if envelope_4_pct > 0 and strategy.opentrades < 4
        strategy.entry('short 4', strategy.short, limit=ma_high_4, qty=(strategy.equity / ma_high_4) * (1 / total_envelope))
    if envelope_5_pct > 0 and strategy.opentrades < 5
        strategy.entry('short 5', strategy.short, limit=ma_high_5, qty=(strategy.equity / ma_high_5) * (1 / total_envelope))


strategy.exit('close', limit=ma_base)


// ---------------------------------------------
// ------------------ PLOT ---------------------

ma_base_plot = plot(ma_base, title = "Base MA", color = color.orange, linewidth = 3, offset = 1)

ma_high_1_plot = plot(ma_high_1, title = "MA high 1", color = color.red, offset = 1)
ma_high_2_plot = plot(ma_high_2, title = "MA high 2", color = color.red, offset = 1)
ma_high_3_plot = plot(ma_high_3, title = "MA high 3", color = color.red, offset = 1)
ma_high_4_plot = plot(ma_high_4, title = "MA high 4", color = color.red, offset = 1)
ma_high_5_plot = plot(ma_high_5, title = "MA high 5", color = color.red, offset = 1)

ma_low_1_plot = plot(ma_low_1, title = "MA low 1", color = color.green, offset = 1)
ma_low_2_plot = plot(ma_low_2, title = "MA low 2", color = color.green, offset = 1)
ma_low_3_plot = plot(ma_low_3, title = "MA low 3", color = color.green, offset = 1)
ma_low_4_plot = plot(ma_low_4, title = "MA low 4", color = color.green, offset = 1)
ma_low_5_plot = plot(ma_low_5, title = "MA low 5", color = color.green, offset = 1)

plot(ohlc4, color=color.purple)

// use_period = input.bool(false, "Période spécifique ?", group="periode")
// startDate = input.time(timestamp("01 Jan 2020"), "Date de début", group="periode")
// endDate = input.time(timestamp("01 Jan 2025"), "Date de fin", group="periode")


//------------------------------------------
//-------------Indicateurs------------------

// inDateRange = use_period ? ((time >= startDate) and (time < endDate)) : true

// //--------------Backtest-------------------

// strategy_pnl = strategy.netprofit + strategy.openprofit
// bnh_strategy_pnl_pcnt = (strategy_pnl / strategy.initial_capital) * 100

// float bnh_start_bar = na
// bnh_start_bar := na(bnh_start_bar[1]) or inDateRange != true? close : bnh_start_bar[1]
// float bnl_buy_hold_equity = na
// bnl_buy_hold_equity :=  inDateRange == true ? ((close - bnh_start_bar)/bnh_start_bar) * 100 : bnl_buy_hold_equity[1]

// bnh_vs_diff = bnh_strategy_pnl_pcnt - bnl_buy_hold_equity
// bnh_diff_color = bnh_vs_diff > 0 ? color.new(color.green, inDateRange ? 60 : 100) : color.new(color.red, inDateRange ? 60 : 100)

// var Table = table.new(position.top_right, columns = 2, rows = 4, border_width = 1, bgcolor = color.black, border_color = color.gray)
// table.cell(table_id = Table, column = 0, row = 0, text_color=(bnh_strategy_pnl_pcnt>bnl_buy_hold_equity)?color.gray:color.green, text_size = size.normal, text = "Buy & hold profit")
// table.cell(table_id = Table, column = 1, row = 0, text_color=(bnh_strategy_pnl_pcnt>bnl_buy_hold_equity)?color.gray:color.green, text_size = size.normal, text = str.tostring(bnl_buy_hold_equity, '#.##') + ' %')
// table.cell(table_id = Table, column = 0, row = 1, text_color=(bnh_strategy_pnl_pcnt<bnl_buy_hold_equity)?color.gray:color.green, text_size = size.normal, text = "Strategy profit")
// table.cell(table_id = Table, column = 1, row = 1, text_color=(bnh_strategy_pnl_pcnt<bnl_buy_hold_equity)?color.gray:color.green, text_size = size.normal, text = str.tostring(bnh_strategy_pnl_pcnt, '#.##') + ' %')
// table.cell(table_id = Table, column = 0, row = 2, text_color=color.yellow, text_size = size.normal, text = "Date de début")
// table.cell(table_id = Table, column = 1, row = 2, text_color=color.yellow, text_size = size.normal, text = str.format("{0,date,dd-MM-YYYY}",strategy.closedtrades.entry_time(1)))

もっと