複数期間指数移動平均クロスオーバー戦略

EMA SL TP TF
作成日: 2024-07-30 12:02:23 最終変更日: 2024-07-30 12:02:23
コピー: 3 クリック数: 618
1
フォロー
1617
フォロワー

複数期間指数移動平均クロスオーバー戦略

概要

この多期指数移動平均クロスストラテジーは,EMAのクロスシグナルに基づく自動取引システムである.異なる時間周期のEMAを利用して取引シグナルを生成し,リスク管理のためにストップ・ロズ・アンド・リベリー・クローズ・メカニズムを組み合わせている.この戦略は,潜在的取引機会を識別するために,主に急速なEMAと遅いEMA,およびより高い時間周期のEMAとのクロスに依存している.

戦略原則

この戦略の核心原則は,市場動向を識別し,取引シグナルを生成するために,複数の時間周期の指数移動平均 (EMA) を利用することです.具体的には:

  1. 9周期EMAを高速線として,50周期EMAを遅い線として,そして15分時間周期の100周期EMAをより高い時間周期基準線として使用する.

  2. 購入条件:

    • 急速EMAの上に遅いEMAを横断し,急速EMAはより高い時間周期EMAの上に位置する;または
    • スピードEMA上ではより高い時間周期EMAを穿います.
  3. 信号の販売条件:

    • 急速EMAの下を通過して,急速EMAはより高い時間周期EMAの下にある;または
    • 急速EMAの下より高い時間周期EMAを穿越する.
  4. 取引管理:

    • 固定ストップ損失 (SL) と利益目標 (TP) を設定する.
    • 価格が最初の利益目標 (TP1) に到達すると,25%のポジションを平置し,ストップロスは保本位置に移動する.
    • 余剰ポジションは,第2の利益目標 ((TP2)) または止損まで進みます.
  5. 取引時間制御:

    • 特定の取引時間帯と取引日を設定できます.

戦略的優位性

  1. 多時間周期分析:異なる時間周期のEMAを組み合わせて,偽信号を減らすのに役立ち,取引の質を向上させる.

  2. トレンド追跡: EMAの交差と位置関係により,市場トレンドを効果的に捉える.

  3. リスク管理: 固定ストップと分期利益戦略により,潜在的損失を制限し,利益の増加を可能にします.

  4. 柔軟性:異なる市場と取引スタイルに応じてEMAのパラメータ,ストップ・ロズとリターンのレベルを調整できます.

  5. 自動化: 戦略は,TradingViewプラットフォームとPineConnectorを通じて完全に自動化された取引を実現できます.

  6. 時間管理:特定の取引時間と取引日を設定して,不利な市場環境で取引を避ける.

戦略リスク

  1. 遅滞性:EMAは本質的に遅滞の指標であり,激しい波動の市場では反応が遅れる可能性がある.

  2. 偽信号:横断市場では,EMA交差が頻繁に偽信号を生じ,過剰取引を引き起こす可能性がある.

  3. 固定ストップ:固定ポイントを使用するストップは,すべての市場条件に適さない場合があり,時には過大または過小である可能性があります.

  4. 歴史的データに依存する:戦略の有効性は,反省期間の市場行動に大きく依存し,将来のパフォーマンスは異なる可能性があります.

  5. 市場適応性: 戦略は,ある通貨ペアではうまく機能するが,他の通貨ペアではうまく機能しない可能性がある.

戦略最適化の方向性

  1. 動態パラメータ調整:市場の変動動態に応じてEMA周期,ストップ・ロズ・アンド・リターン・レベルを調整することを考慮する.

  2. フィルタリング条件の追加:取引信号をフィルタリングするために追加の技術指標または市場情緒指標を導入し,偽信号を減らす.

  3. ストップ戦略の改善:市場の波動に適したトラッキングストップまたはATRベースのダイナミックストップを実現する.

  4. 取引時間を最適化: 詳細な時間分析を行い,最適な取引時間や日付を特定します.

  5. 取引量管理の強化:市場の変動と口座のリスクに応じてポジションサイズを調整する.

  6. 多通貨関連性分析:複数の通貨ペアの関連性を考慮し,類似の市場リスクに過剰な曝露を避ける.

  7. 機械学習統合:機械学習アルゴリズムを利用してパラメータ選択と信号生成プロセスを最適化する.

要約する

多周期指数移動平均クロスストラテジーは,トレンド追跡とリスク管理を組み合わせた自動取引システムである.異なる周期のEMAクロスシグナルを利用することで,市場トレンドを捉え,適切なタイミングで取引することを目的としている.戦略は,特定の市場条件で良好なパフォーマンスを発揮するものの,いくつかの固有のリスクと限界がある.戦略の安定性と適応性をさらに向上させるために,ダイナミックパラメータ調整,追加のフィルタリング条件の調整,より複雑なリスク管理技術の導入を考慮することができます.

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

//@version=5
strategy("Miles Multi TF EMA Strategy v 1", overlay=true)

Fast = input.int(9, "Fast EMA")
Xslow = input.int(50, "Slow EMA")

var bool inTrade = false // Ensure inTrade is declared and initialized
var int tradeDirection = 0
var float buy_slPrice = na
var float buy_tp1Price = na
var float buy_tp2Price = na
var float sell_slPrice = na
var float sell_tp1Price = na
var float sell_tp2Price = na
var bool tp1Hit = false
var bool buytp1Hit = false
var bool selltp1Hit = false
var float entryPrice = na
var float lastSignalBar = na
fastEMA = ta.ema(close, Fast)
XslowEMA = ta.ema(close, Xslow)
var int step = 0

// Example SL and TP settings (adjust according to your strategy)
slPips = input.int(150, "Stop Loss")
tp1Pips = input.int(75, "Take Profit 1")
tp2Pips = input.int(150, "Take Profit 2")
beoff = input.int(25, "Breakeven Offset")

// Define the higher time frame
higherTimeFrame = input.timeframe("15", "Higher Timeframe EMA")

// Fetch the EMA from the higher time frame
higherTimeFrameEMA = request.security(syminfo.tickerid, higherTimeFrame, ta.ema(close, 100))

// Input for trading start and end times, allowing end time to extend beyond midnight
startHour = input.int(1, "Start Hour", minval=0, maxval=23)
endHour = input.int(25, "End Hour", minval=0, maxval=47) // Extend maxval to 47 to allow specifying times into the next day

// Adjust endHour to be within 24-hour format using modulo operation
adjustedEndHour = endHour % 24

// Function to determine if the current time is within the trading hours
isTradingTime(currentHour) =>
    if startHour < adjustedEndHour
        currentHour >= startHour and currentHour < adjustedEndHour
    else
        currentHour >= startHour or currentHour < adjustedEndHour

// Get the current hour in the exchange's timezone
currentHour = hour(time, "Australia/Sydney")

// Check if the current time is within the trading hours
trading = isTradingTime(currentHour)

// Plot background color if within trading hours
bgcolor(trading ? color.new(color.blue, 90) : na)

// Inputs for trading days
tradeOnMonday = input.bool(true, "Trade on Monday")
tradeOnTuesday = input.bool(true, "Trade on Tuesday")
tradeOnWednesday = input.bool(true, "Trade on Wednesday")
tradeOnThursday = input.bool(true, "Trade on Thursday")
tradeOnFriday = input.bool(true, "Trade on Friday")

// Current time checks
currentDayOfWeek = dayofweek(time, "Australia/Sydney")

// Check if current time is within trading hours
isTradingHour = (currentHour >= startHour and currentHour < endHour)

// Check if trading is enabled for the current day of the week
isTradingDay = (currentDayOfWeek == dayofweek.monday and tradeOnMonday) or 
               (currentDayOfWeek == dayofweek.tuesday and tradeOnTuesday) or 
               (currentDayOfWeek == dayofweek.wednesday and tradeOnWednesday) or 
               (currentDayOfWeek == dayofweek.thursday and tradeOnThursday) or 
               (currentDayOfWeek == dayofweek.friday and tradeOnFriday)

// Combined check for trading time and day
isTradingTime = isTradingHour and isTradingDay

buySignal = false
sellSignal = false

// Conditions
if (step == 0 or step == 4) and ta.crossover(fastEMA, XslowEMA) and fastEMA > higherTimeFrameEMA
    step := 1

if (step == 0 or step == 4) and ta.crossover(fastEMA, higherTimeFrameEMA)
    step := 1

if step == 3 and ta.crossover(fastEMA, XslowEMA) and fastEMA > higherTimeFrameEMA
    step := 3

if step == 2 and ta.crossover(fastEMA, XslowEMA) and fastEMA > higherTimeFrameEMA
    step := 1

if (step == 0 or step == 3) and ta.crossunder(fastEMA, XslowEMA) and fastEMA < higherTimeFrameEMA
    step := 2

if (step == 0 or step == 3) and ta.crossunder(fastEMA, higherTimeFrameEMA)
    step := 2

if step == 4 and ta.crossunder(fastEMA, XslowEMA) and fastEMA < higherTimeFrameEMA
    step := 4

if step == 1 and ta.crossunder(fastEMA, XslowEMA) and fastEMA < higherTimeFrameEMA
    step := 2

// For buy signals
if step == 1 and isTradingTime and fastEMA > ta.ema(close, Xslow) and fastEMA > higherTimeFrameEMA
    buySignal := true
    inTrade := true
    entryPrice := close
    tradeDirection := 1
    buytp1Hit := false
    lastSignalBar := bar_index
    buy_slPrice := entryPrice - slPips * syminfo.mintick
    buy_tp1Price := entryPrice + tp1Pips * syminfo.mintick // Set TP1
    buy_tp2Price := entryPrice + tp2Pips * syminfo.mintick // Set TP2
    tp1Hit := false
    step := 3
    strategy.entry("Buy", strategy.long, stop=buy_slPrice, limit=buy_tp1Price)

if step == 2 and isTradingTime and fastEMA < ta.ema(close, Xslow) and fastEMA < higherTimeFrameEMA
    sellSignal := true
    inTrade := true
    entryPrice := close
    tradeDirection := -1
    lastSignalBar := bar_index
    selltp1Hit := false
    sell_slPrice := entryPrice + slPips * syminfo.mintick
    sell_tp1Price := entryPrice - tp1Pips * syminfo.mintick // Set TP1
    sell_tp2Price := entryPrice - tp2Pips * syminfo.mintick // Set TP2
    tp1Hit := false
    step := 4
    strategy.entry("Sell", strategy.short, stop=sell_slPrice, limit=sell_tp1Price)

// Move SL to breakeven once TP1 is hit and close 25% of the trade
if (ta.valuewhen(strategy.position_size != 0, strategy.position_size, 0) > 0)
    if high >= buy_tp1Price and not tp1Hit
        tp1Hit := true
        buy_slPrice := entryPrice + beoff * syminfo.mintick
        strategy.close("Buy", qty_percent = 25, comment = "TP1 Hit")
        strategy.exit("Close", from_entry="Buy", stop=buy_slPrice, limit=buy_tp2Price)
        
if (ta.valuewhen(strategy.position_size != 0, strategy.position_size, 0) < 0)
    if low <= sell_tp1Price and not tp1Hit
        tp1Hit := true
        sell_slPrice := entryPrice - beoff * syminfo.mintick
        strategy.close("Sell", qty_percent = 25, comment = "TP1 Hit")
        strategy.exit("Close", from_entry="Sell", stop=sell_slPrice, limit=sell_tp2Price)

// Managing the trade after it's initiated
if inTrade and tradeDirection == 1 and sellSignal
    inTrade := false
    tradeDirection := 0
    buy_slPrice := na
    buy_tp1Price := na
    buy_tp2Price := na
    tp1Hit := false
    step := 2

if inTrade and tradeDirection == -1 and buySignal
    inTrade := false
    tradeDirection := 0
    sell_slPrice := na
    sell_slPrice := na
    sell_tp2Price := na
    tp1Hit := false
    step := 1

if inTrade and tradeDirection == 1 and step == 1
    step := 0

if inTrade and tradeDirection == -1 and step == 2
    step := 0

if inTrade and tradeDirection == 1 and (bar_index - lastSignalBar) >= 1
    if high >= buy_tp1Price and not tp1Hit
        tp1Hit := true
        buytp1Hit := true
        lastSignalBar := bar_index
        buy_slPrice := entryPrice + beoff * syminfo.mintick
        step := 3

    if low <= buy_slPrice and not tp1Hit and (bar_index - lastSignalBar) >= 1
        strategy.close("Buy", qty_percent = 100, comment = "SL Hit")
        inTrade := false
        tradeDirection := 0
        buy_slPrice := na
        buy_tp1Price := na
        buy_tp2Price := na
        tp1Hit := false
        buytp1Hit := false
        step := 0

if inTrade and tradeDirection == 1 and tp1Hit and (bar_index - lastSignalBar) >= 1
    if low <= buy_slPrice
        inTrade := false
        tradeDirection := 0
        buy_slPrice := na
        buy_tp1Price := na
        buy_tp2Price := na
        tp1Hit := false
        buytp1Hit := false
        step := 0

    if high >= buy_tp2Price and (bar_index - lastSignalBar) >= 1
        inTrade := false
        tradeDirection := 0
        buy_slPrice := na
        buy_tp1Price := na
        buy_tp2Price := na
        tp1Hit := false
        buytp1Hit := false
        step := 0

if inTrade and tradeDirection == -1 and (bar_index - lastSignalBar) >= 1
    if low <= sell_tp1Price and not tp1Hit
        tp1Hit := true
        lastSignalBar := bar_index
        selltp1Hit := true
        sell_slPrice := entryPrice - beoff * syminfo.mintick
        step := 4

    if high >= sell_slPrice and not tp1Hit and (bar_index - lastSignalBar) >= 1
        strategy.close("Sell", qty_percent = 100, comment = "SL Hit")
        inTrade := false
        tradeDirection := 0
        sell_slPrice := na
        sell_tp1Price := na
        sell_tp2Price := na
        tp1Hit := false
        selltp1Hit := false
        step := 0

if inTrade and tradeDirection == -1 and tp1Hit  and (bar_index - lastSignalBar) >= 1
    if high >= sell_slPrice
        inTrade := false
        tradeDirection := 0
        sell_slPrice := na
        sell_tp1Price := na
        sell_tp2Price := na
        tp1Hit := false
        selltp1Hit := false
        step := 0
    if low <= sell_tp2Price
        inTrade := false
        tradeDirection := 0
        sell_slPrice := na
        sell_tp1Price := na
        sell_tp2Price := na
        tp1Hit := false
        selltp1Hit := false
        step := 0