設定可能な移動平均クロスオーバー戦略

MA-X EMA MA CROSSOVER trading strategy risk management
作成日: 2025-04-03 11:31:33 最終変更日: 2025-04-03 11:31:33
コピー: 9 クリック数: 300
2
フォロー
319
フォロワー

設定可能な移動平均クロスオーバー戦略 設定可能な移動平均クロスオーバー戦略

概要

この記事では,柔軟で強力な移動平均クロス取引戦略を紹介し,異なる市場条件に応じて移動平均のパラメータとタイプをカスタマイズできるようにします. 戦略の核心は,異なる周期とタイプの移動平均を利用してトレンド追跡とシグナル生成を行うことです.

戦略原則

戦略は,3つの異なる周期の移動平均を計算して取引シグナルを生成する. (速線,遅線,退出線) 主な原則は以下のとおりである.

  1. 移動平均のタイプ選択: シンプル移動平均 ((SMA),指数移動平均 ((EMA),加重移動平均 ((WMA) とヘル移動平均 ((HMA) をサポートする.
  2. 応募条件:
    • 多頭入場:クローズオフは速線より高く,速線は遅線より高く,クローズオフは退場線より高く
    • 空頭入場:クローズオフは速線より低く,速線は遅線より低く,クローズオフは出場線より低く
  3. 出場条件:
    • 多頭出場:少なくとも2つのKラインに入ると,出場ラインより低い閉店価格
    • 空頭出場:少なくとも2つの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")