EMA ラインに基づく戦略をフォローする傾向

作者: リン・ハーンチャオチャン開催日:2024-02-05 14:21:18
タグ:

img

概要

この戦略は,異なる期間の3つのEMAラインに基づいています.価格はEMAラインの上にあるかどうかによって現在のトレンド方向を判断します. 短期EMAラインが長期EMAラインの上を横切ると,購入信号が生成されます. 短期EMAラインが長期EMAライン下を横切ると,販売信号が生成されます. この戦略は,トレンドが走っていることを追跡し,トレンドが逆転すると,タイミングでポジションを閉じます.

戦略の論理

この戦略は,それぞれ10日,20日および50日間の3つのEMAラインを使用しています.判断ルールは以下の通りです.

  1. 10 日間EMAと 20 日間EMAの両方が50 日間EMAを超えると,上昇傾向として定義される.

  2. 10日間のEMAと20日間のEMAが50日間のEMAを下回る場合,ダウントレンドとして定義されます.

  3. 短期EMA線 (10日線と20日線) が長期EMA線 (50日線) を越えると,買い信号が生成される.

  4. 短期EMA線 (10日線と20日線) が長期EMA線 (50日線) 以下の線を横切ると,セールシグナルが生成される.

  5. 上向きのトレンド中にロングポジションを保持し,下向きのトレンド中にショートポジションを保持する

  6. トレンドが逆転すると現在の指向ポジションを閉じる (短期EMAは長期EMAを横切る).

この戦略は,利益を確保するために,ポジションをタイミングで閉鎖し,ロングとショートポジションを交替して利益を得ます.

利点分析

この戦略の利点は次のとおりです.

  1. 規則は単純で明快で,理解し実行しやすい.
  2. 傾向を決定するためにEMA線を使用することで,短期間の市場変動による干渉を避ける.
  3. トレンド・ランスを追跡するために ポジションを間に合うように閉じることで 損失の拡大を回避できます
  4. トレンドを追跡して 高い収益率を持つ市場を予測する必要はありません

リスク分析

この戦略にはいくつかのリスクもあります:

  1. レンジ・バインド市場では,EMA線が頻繁に交差し,頻繁なオープン・閉鎖の取引コストが高くなります.

  2. EMAによるトレンド決定は 価格格差の後に失敗し 良いエントリー機会を逃す可能性があります

リスクを最適化するために,いくつかの方法が使用できます.

  1. オープンポジション規則は,EMAが接近しているときに,過剰取引を避けるために適切に緩和することができる.

  2. EMAの失敗を避けるために他の指標を組み合わせて傾向を決定する.

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

戦略は以下の側面から最適化できます.

  1. パラメータ最適化.最適なパラメータを見つけるために異なるEMA期間の組み合わせをテストする.

  2. 取引コストの最適化.不要な頻繁な取引を減らすためにオープンポジションルールを適切に最適化する.

  3. ストップ損失戦略の最適化.単一の損失を制御するために合理的なストップ損失レベルを設定する.

  4. 他の指標を組み合わせ,MACD,KDJなどの指標を使用して,最適なエントリータイミングを決定します.

概要

一般的には,この戦略はかなりシンプルで実用的です.リスクを効果的に制御するために適切なストップ損失戦略でトレンド方向を決定するためにEMAを使用します.最適化にも余地があります.パラメータ最適化,ストップ損失戦略および他の指標を組み合わせることで,この戦略のパフォーマンスはさらに改善できます.


/*backtest
start: 2024-01-28 00:00:00
end: 2024-01-31 04:00:00
period: 45m
basePeriod: 5m
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/
// © mattehalen

//@version=4
//study("EMA 10,20 59",overlay=true)
strategy("EMA 10,20 59",overlay=true)
infoBox     = input(true, title="infoBox", type=input.bool)
infoBox2    = input(false, title="infoBox2", type=input.bool)
BuySellSignal_Bool = input(false, title="Buy & SellSignal", type=input.bool)
infoBoxSize = input(title="infoBoxSize", defval=size.large, options=[size.auto, size.tiny, size.small, size.normal, size.large, size.huge])
ema1Value   = input(10)
ema2Value   = input(20)
ema3Value   = input(59)
maxLoss = input(3000)
ema1        = ema(close,ema1Value)
ema2        = ema(close,ema2Value)
ema3        = ema(close,ema3Value)
objcnt      = 0
buyTitle    = tostring(close[1])
myProfit    = float(0)

plot(ema1,title="ema1",color=color.red,linewidth=2)
plot(ema2,title="ema2",color=color.green,linewidth=2)
plot(ema3,title="ema3",color=color.black,linewidth=2)

Buytrend = (ema1 and ema2 > ema3) and (ema1[1] and ema2[1] > ema3[1])
BarssinceBuyTrend = barssince(Buytrend)
BarssinceSellTrend = barssince(not Buytrend)
closeAtBuyTrend = close[1]
bgcolor(Buytrend ? color.green : color.red,transp=70)

BuySignal = Buytrend and not Buytrend[1] and BuySellSignal_Bool

BuySignalOut = Buytrend and (crossunder(ema1,ema2)) and BuySellSignal_Bool
BarssinceBuy = barssince(BuySignal)
bgcolor(BuySignal ? color.green : na , transp=30)
bgcolor(BuySignalOut ? color.black : na , transp=30)
plot(BarssinceBuy,title="BarssinceBuy",display=display.none)


SellSignal = not Buytrend and Buytrend[1] and BuySellSignal_Bool
SellSignalOut = not Buytrend and (crossover(ema1,ema2)) and BuySellSignal_Bool
BarssinceSell = barssince(SellSignal)
bgcolor(SellSignal ? color.red : na , transp=30)
bgcolor(SellSignalOut ? color.black : na , transp=30)
plot(BarssinceSell,title="BarssinceSell",display=display.none)


buyProfit   = float(0)
cntBuy      =0
sellProfit  = float(0)
cntSell     =0
buyProfit   := Buytrend and not Buytrend[1]? nz(buyProfit[1]) + (close[BarssinceBuyTrend[1]]-close) : nz(buyProfit[1])
cntBuy      := Buytrend and not Buytrend[1]? nz(cntBuy[1]) + 1: nz(cntBuy[1])
sellProfit  := not Buytrend and Buytrend[1]? nz(sellProfit[1]) + (close-close[BarssinceSellTrend[1]]) : nz(sellProfit[1])
cntSell     := not Buytrend and Buytrend[1]? nz(cntSell[1]) + 1 : nz(cntSell[1])
totalProfit = buyProfit + sellProfit

// if (Buytrend and not Buytrend[1] and infoBox==true)
//     l = label.new(bar_index - (BarssinceBuyTrend[1]/2), na,text="Close = " + tostring(close) + "\n" + "Start = "+tostring(close[BarssinceBuyTrend[1]]) + "\n" + "Profit = "+tostring(close[BarssinceBuyTrend[1]]-close) ,style=label.style_labelup, yloc=yloc.belowbar,color=color.red,size=infoBoxSize)
// if (not Buytrend and Buytrend[1] and infoBox==true)
//     l = label.new(bar_index - (BarssinceSellTrend[1]/2), na,text="Close = " + tostring(close) + "\n" + "Start = "+tostring(close[BarssinceSellTrend[1]]) + "\n" + "Profit = "+tostring(close-close[BarssinceSellTrend[1]]) ,style=label.style_labeldown, yloc=yloc.abovebar,color=color.green,size=infoBoxSize)

// if (BuySignalOut and not BuySignalOut[1] and infoBox2==true)
// //    l = label.new(bar_index - (BarssinceBuy[0]/2), na,text="Close = " + tostring(close) + "\n" + "Start = "+tostring(close[BarssinceBuy[0]]) + "\n" + "Profit = "+tostring(close-close[BarssinceBuy[0]]) ,style=label.style_labelup, yloc=yloc.belowbar,color=color.purple,size=infoBoxSize
//     l = label.new(bar_index, na,text="Close = " + tostring(close) + "\n" + "Start = "+tostring(close[BarssinceBuy[0]]) + "\n" + "Profit = "+tostring(close-close[BarssinceBuy[0]]) ,style=label.style_labelup, yloc=yloc.belowbar,color=color.lime,size=infoBoxSize)
// if (SellSignalOut and not SellSignalOut[1] and infoBox2==true)
// //    l = label.new(bar_index - (BarssinceSell[0]/2), na,text="Close = " + tostring(close) + "\n" + "Start = "+tostring(close[BarssinceSell[0]]) + "\n" + "Profit = "+tostring(close[BarssinceSell[0]]-close) ,style=label.style_labeldown, yloc=yloc.abovebar,color=color.purple,size=infoBoxSize)
//     l = label.new(bar_index, na,text="Close = " + tostring(close) + "\n" + "Start = "+tostring(close[BarssinceSell[0]]) + "\n" + "Profit = "+tostring(close[BarssinceSell[0]]-close) ,style=label.style_labeldown, yloc=yloc.abovebar,color=color.fuchsia,size=infoBoxSize)


// l2 = label.new(bar_index, na, 'buyProfit in pip = '+tostring(buyProfit)+"\n"+  'cntBuy = '+tostring(cntBuy) +"\n"+  'sellProfit in pip = '+tostring(sellProfit)+"\n"+  'cntSell = '+tostring(cntSell) +"\n"+  'totalProfit in pip = '+tostring(totalProfit)     , 
//   color=totalProfit>0 ? color.green : color.red, 
//   textcolor=color.white,
//   style=label.style_labeldown, yloc=yloc.abovebar,
//   size=size.large)

// label.delete(l2[1])



//--------------------------------------------------
//--------------------------------------------------
if (Buytrend)
    strategy.close("short", comment = "Exit short")
    strategy.entry("long", true)
    strategy.exit("Max Loss", "long", loss = maxLoss)

//if BuySignalOut
   // strategy.close("long", comment = "Exit Long")
if (not Buytrend)
    // Enter trade and issue exit order on max loss.
    strategy.close("long", comment = "Exit Long")
    strategy.entry("short", false)
    strategy.exit("Max Loss", "short", loss = maxLoss)
//if SellSignalOut
    // Force trade exit.
    //strategy.close("short", comment = "Exit short")
    
//--------------------------------------------------
//--------------------------------------------------
//--------------------------------------------------



もっと