MACDとRSIに基づくロングとショートの双方向取引戦略


作成日: 2023-10-09 15:33:17 最終変更日: 2023-10-09 15:33:17
コピー: 0 クリック数: 701
1
フォロー
1617
フォロワー

概要

この戦略は,MACDとRSIの2つの指標を組み合わせて,トレンドの方向が不明確である場合に実現し,同時に余剰利益を得るために多額の空白取引を行います.

戦略原則

  1. 計算する 急速EMA ((12日線) と 遅いEMA ((26日線)
  2. MACD収束距離を計算する (高速EMAを減算して遅いEMA)
  3. MACDの9日移動平均を信号線として計算する
  4. 14日RSIを計算する
  5. MACD<-0.1,RSI<27で,急速EMAが遅いEMAより低い場合は,さらに実行する
  6. MACD>0.125で,RSI>81で,急速EMAが遅いEMAより高いとき,空白する
  7. ストップ,ストップ,移動ストップを設定してポジションを管理する

優位分析

  1. また,多空取引により,トレンド外での余剰利益を得ることができます.
  2. トレンド指数EMAと逆転指数RSIを組み合わせると,信号の質が向上します.
  3. モバイル・ストップを活用して収益を固定し,損失のリスクを効果的に制御する

リスク分析

  1. 双方向取引は,保証金要求を支えるためにより多くの資金を必要とします.
  2. 市場が急激に逆転すると,余剰空白のポジションを停止する可能性があります.
  3. パラメータの設定を間違えた場合,取引が頻発する可能性があります.

リスク対策:

  1. 十分な資金のサポートとポジションの管理
  2. 合理的な停止距離を設定し,過密の停止を避ける
  3. パラメータの最適化,取引頻度の低下

最適化の方向

  1. 投資のタイミングを最適化するために,変動率の指標と組み合わせて考えられます.
  2. 異なるパラメータの組み合わせをテストして,最適なパラメータを見つけることができます.
  3. 市場条件に応じて最適化できるストップ・ストラトジー,例えば尾行ストップなど
  4. 機械学習アルゴリズムを組み込む自動最適化パラメータ

要約する

この戦略は,MACDとRSIの組み合わせにより,双方向の取引を実現する.移動のストップを利潤にロックするために使用し,非トレンドの状況で余分な利益を得ることができます.この戦略は,パラメータ設定,ストップ戦略などをさらに最適化して,より安定した余分な利益を得ることができます.

ストラテジーソースコード
/*backtest
start: 2023-09-08 00:00:00
end: 2023-10-08 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
// Revision:        290
// Author:          @Hugo_Moriceau
//study("Moriceau_Crypto_strategies_Long_short_indicator_thesis",overlay=true)

// Pyramide 10 order size 100, every tick

strategy("Moriceau_Crypto_strategies_Long_short_indicator",overlay=true)

// === GENERAL INPUTS ===

fast = 12, slow = 26
fastMA = ema(close, fast)
slowMA = ema(close, slow)

macd = fastMA - slowMA
signal = sma(macd, 9)
rsi = rsi(close,14)

dataB = macd < -0.1  and rsi<27 and fastMA < slowMA
// data1 = macd > 0.125  and rsi>81 and fastMA> slowMA
dataS = macd > 0.125 and rsi > 81 and fastMA > slowMA

tradeInvert     = input(defval = false, title = "Invert Trade Direction?")

// === LOGIC ===

// is fast ma above slow ma?
Achat = macd < -0.1  and rsi < 27 and fastMA < slowMA ? true : false
vente = macd > 0.125 and rsi > 81 and fastMA > slowMA ? true : false

// are we inverting our trade direction?
tradeDirection = vente ? Achat ? false : true : Achat ? true : false

// === Plot Setting ===

plot(fastMA,color=red)
plot(slowMA,color=blue)
barcolor(color=iff(fastMA > slowMA, yellow, na))
barcolor(color=iff(fastMA < slowMA, black, na))
//barcolor(color=iff(macd > 0.12*close , fuchsia, na))
//barcolor(color=iff(macd < -0.1*close , lime, na))
plotchar(dataB, char='B',color=black,size = size.auto,location = location.belowbar,transp= 0)  
plotchar(dataS, char='S',color=black,size = size.auto,location = location.abovebar,transp= 0)

//fast = plot(maFast, title = "FastMA", color = yellow, linewidth = 2, style = line, transp = 50)
//slow = plot(maSlow, title = "SlowMA", color = black, linewidth = 2, style = line, transp = 50)

// === BACKTEST RANGE ===
FromMonth = input(defval = 05, title = "From Month", minval = 1)
FromDay   = input(defval = 23, title = "From Day", minval = 1)
FromYear  = input(defval = 2021, title = "From Year", minval = 2017)
ToMonth   = input(defval = 5, title = "To Month", minval = 1)
ToDay     = input(defval = 25, title = "To Day", minval = 1)
ToYear    = input(defval = 2021, title = "To Year", minval = 2017)


// === STRATEGY RELATED INPUTS ===+
// the risk management inputs
inpTakeProfit   = input(defval = 2500, title = "Take Profit", minval = 28)
inpStopLoss     = input(defval = 600, title = "Stop Loss", minval = 15)
inpTrailStop    = input(defval = 300, title = "Trailing Stop Loss", minval = 5)
inpTrailOffset  = input(defval = 50, title = "Trailing Stop Loss Offset", minval = 1)

// === RISK MANAGEMENT VALUE PREP ===

// if an input is less than 1, assuming not wanted so we assign 'na' value to disable it.

useTakeProfit   = inpTakeProfit  >= 1 ? inpTakeProfit  : na
useStopLoss     = inpStopLoss    >= 1 ? inpStopLoss    : na
useTrailStop    = inpTrailStop   >= 1 ? inpTrailStop   : na
useTrailOffset  = inpTrailOffset >= 1 ? inpTrailOffset : na


// === STRATEGY - LONG POSITION EXECUTION ===

enterLong() => not tradeDirection[1] and tradeDirection 
exitLong() => tradeDirection[1] and not tradeDirection
strategy.entry(id = "Achat", long = true, when = enterLong()) // use function or simple condition to decide when to get in
strategy.close(id = "TP 50% Sell", when = exitLong()) // ...and when to get out

// === STRATEGY - SHORT POSITION EXECUTION ===

enterShort() => tradeDirection[1] and not tradeDirection
exitShort() => not tradeDirection[1] and tradeDirection
strategy.entry(id = "Vente", long = false, when = enterShort())
strategy.close(id = "Vente", when = exitShort())

// === STRATEGY RISK MANAGEMENT EXECUTION ===

// finally, make use of all the earlier values we got prepped
strategy.exit("Vente", from_entry = "Vente", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
strategy.exit("Short", from_entry = "Achat", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)