MACD と RSI に 基づく 長期 短期 取引 戦略

作者: リン・ハーンチャオチャン,日付: 2023年10月9日 15:33:17
タグ:

概要

この戦略は,MACDとRSI指標を組み合わせて,不透明なトレンド状況で過剰な収益を得るために,長期と短期取引を同時に実施します.

戦略の論理

  1. 速やかなEMA (12日) と遅いEMA (26日) を計算する
  2. MACD 収束差を計算する (速い EMA マイナス遅い EMA)
  3. 信号線としてMACDの9日移動平均を計算する
  4. 14日間のRSIを計算する
  5. MACD<-0.1,RSI<27 と,EMAが低くなると長引く.
  6. MACD>0.125,RSI>81と,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)

もっと