MACD와 RSI를 통합하는 단기 거래 전략

저자:차오장, 날짜: 2023-09-13 14:59:32
태그:

이 전략은 MACD와 RSI를 통합하는 단기 거래 전략 (Short-term Trading Strategy Integrating MACD and RSI) 이라고 불립니다. 이 전략은 MACD와 RSI 지표의 신호를 결합하여 수익을 얻기 위해 짧은 기간 동안 시장 변동을 포착합니다.

MACD는 이동 평균 컨버전스 디버전스를 의미합니다. 빠른 라인, 느린 라인 및 히스토그램으로 구성됩니다. 빠른 라인이 느린 라인 위에 넘어가면 단기 가격 동력을 강화하고 구매 신호를 생성합니다. 빠른 라인이 느린 라인 아래에 넘어가면 약화 동력을 신호하고 판매 신호를 생성합니다.

RSI는 상대적 강도 지수를 뜻한다. 그것은 가격의 과잉 구매 및 과잉 판매 조건을 반영한다. 20 이하의 RSI는 과잉 판매이며 80 이상은 과잉 구매이다. 과잉 구매 구역은 잠재적 인 가격 하락의 경고이며, 과잉 판매 구역은 잠재적 인 반등에 대해 경고한다.

이 전략의 무역 신호는 두 가지 측면에서 나타납니다.

첫째, MACD 라인 크로스오버와 히스토그램 변경. 히스토그램이 음에서 양으로 변경되면, 단기간에 가격의 추진력을 증가시키고, 구매 기회를 나타냅니다. 히스토그램이 양에서 음으로 변경되면, 마감하는 추진력을 표시하고 판매를 제안합니다.

두 번째, RSI 과잉 구매/ 과잉 판매 수준. RSI를 결합하면 MACD에서 일부 잘못된 신호를 필터링하는 데 도움이됩니다. RSI가 낮을 때만 구매하고 RSI가 높을 때만 판매하면 정확도가 향상됩니다.

이 전략의 장점은 더 정확한 거래 신호를 위해 두 지표의 강점을 결합하고 단기 변동을 민감하게 포착하는 것입니다. 그러나 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)


더 많은