短期取引戦略 MACD と RSI を統合する

作者: リン・ハーンチャオチャン, 日付: 2023-09-13 14:59:32
タグ:

この戦略は"MACDとRSIを統合する短期取引戦略"と呼ばれる.この戦略は,MACDとRSI指標からの信号を組み合わせて,短期間の市場変動を把握して利益を得ています.

MACDは,移動平均収束分岐を表す. 急速線,遅い線,ヒストグラムから構成される. 急速線が遅い線を越えると,短期価格勢力の強化をシグナル化し,購入信号を生成する. 急速線が遅い線を下回ると,勢力の弱まりをシグナル化し,販売信号を生成する.

RSIは,相対強度指数 (Relative Strength Index) を表す.価格の過買い・過売り状態を反映する.RSI20未満は過売り,80を超えるのは過買いである.過買いゾーンは潜在的な価格低下の警告であり,過売りゾーンは潜在的なブランスの警告である.

この戦略の貿易シグナルには2つの側面があります.

まず,MACD線クロスオーバーとヒストグラムの変化.ヒストグラムがマイナスからポジティブに変化すると,短期的には価格の勢いが増加することを示し,購入する機会を示します.ヒストグラムがポジティブからマイナスに変化すると,勢いが衰え,売却を示唆します.

2つ目は,RSIの過剰購入/過剰販売レベルです.RSIを組み合わせると,MACDからのいくつかの誤った信号をフィルターするのに役立ちます.RSIが低いときにのみ購入し,RSIが高いときにのみ販売することで,正確性が向上します.

この戦略の利点は,より正確な取引信号のために2つの指標の強みを組み合わせ,短期変動を敏感に把握することです. しかし,MACDとRSIパラメータは,過剰取引を防ぐために最適化する必要があります. ストップ損失レベルも,単一の取引損失を制御するために合理的である必要があります.

要するに,この戦略は,短期の逆転から利益の機会を捕獲し,迅速な短期取引に適しています.しかし,適切なタイミングでパラメータを調整するには,積極的なリスク管理と緊密な市場監視が必要です.


/*backtest
start: 2022-09-06 00:00:00
end: 2023-09-12 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("Uraynium V3", overlay=false, pyramiding = 0, calc_on_every_tick=true, precision=1, currency="USD", default_qty_value=10, default_qty_type=strategy.cash,initial_capital=100,commission_type=strategy.commission.percent,commission_value=0.1) 
// === INPUT BACKTEST RANGE ===
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2019, title = "From Year", minval = 2017)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 2020, title = "To Year", minval = 2017)

// === FUNCTION EXAMPLE ===
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)        // backtest finish window
inTimeframe()  => true

overSold      = input( 20 , minval = 1, title = "RSI Oversold")
overBought    = input( 80 , minval = 1, title = "RSI Overbought")
rsiLength     = input(14, minval = 1, title = "RSI Length")
fastLength    = input(12, minval = 1, title = "MACD fast")
slowlength    = input(26, minval = 1, title = "MACD slow")
MACDLength    = input( 9, minval = 1, title = "MACD length")
stopLoss      = input(   10, minval = 1, title = "Stop Loss (price %)")
takeProfit    = input(   50, minval = 1, title = "Take Profit (price %)")
triggerPosLvl = input(    2, minval = 1 ,title ="Take Position Threshold", type=input.float)
src = close

// === CALC ===

stopLossValue        = close*(stopLoss/100)/syminfo.mintick
takeProfitValue      = close*(takeProfit/100)/syminfo.mintick

vrsi = rsi(src, rsiLength)
//avgRSI = vrsi*0.5 + vrsi[1]*0.25 + vrsi[2]*0.125 + vrsi[3]*0.0625
avgRSI = (4*vrsi + 3*vrsi + 2*vrsi[2] + vrsi[3])/10
[macdLine, signalLine, histLine] = macd(src, fastLength, slowlength, MACDLength)


MACDdelta         = signalLine - macdLine
isMACDRunLong     = signalLine > macdLine
isMACDRunShort    = macdLine < signalLine
isMACDSwitchLong  = crossover(MACDdelta, 0)
isMACDSwitchShort = crossunder(MACDdelta, 0)
isMACDCross       = crossover(MACDdelta, 0) or crossunder(MACDdelta, 0)

buySignal =  (histLine-histLine[1]) + (avgRSI - avgRSI[1])

// === ACTION ===
isPosLong    = strategy.position_size > 0
isPosShort   = strategy.position_size < 0
isNoMarginPos= strategy.position_size == 0
entryLong  = (isNoMarginPos or isPosShort) and ( buySignal >  triggerPosLvl )
entryShort = (isNoMarginPos or isPosLong ) and ( buySignal < -triggerPosLvl ) 

if inTimeframe()
    strategy.entry("Long" , strategy.long,  comment="Entry Long",  when=entryLong )
    strategy.entry("Short", strategy.short, comment="Entry Short", when=entryShort)
    strategy.entry("Long" , strategy.long,  comment="Switch Long", when=entryLong)
    strategy.entry("Short", strategy.short, comment="Switch Short",when=entryShort)
    strategy.exit("Stop (long SL/TP)",  loss=stopLossValue, profit=takeProfitValue, when=entryLong )  
    strategy.exit("Stop (short SL/TP)", loss=stopLossValue, profit=takeProfitValue, when=entryShort)  
    strategy.close("Long" , when=entryShort)
    strategy.close("Short", when=entryLong)    

// === DRAW ===
posColor = isNoMarginPos ?  color.black : isPosLong ? color.green : color.red
plot(100, color=posColor,style=plot.style_area, transp=90, histbase=0)
        
plot(buySignal+overBought, color=color.green)
plot(50+macdLine/4, color=color.yellow)
plot(50+signalLine/4, color=color.orange)
histColor = histLine[1]-histLine > 0 ? color.red : color.green
plot(overSold+histLine/2, color=histColor, style=plot.style_histogram, histbase=overSold, transp=50, linewidth=2)

rsicolor = avgRSI>overBought ? color.red : avgRSI<overSold ? color.green : color.blue
plot(avgRSI,color=rsicolor, linewidth=2)
//plot(vrsi,color=color.purple, linewidth=2)
hline(overBought, color=color.red)
hline(overSold, color=color.green)
hline(50, color=color.gray)


もっと