RSI 격차 전략

저자:차오장, 날짜: 2023-11-16 11:36:45
태그:

img

전반적인 설명

이 전략은 잠재적인 거래 신호를 찾기 위해 RSI EMA 라인 사이의 RSI 지표와 크로스오버를 계산하여 긴 및 짧은 포지션을 결정합니다. 트렌드 다음 전략에 속합니다.

전략 논리

  1. RSI를 계산합니다. RSI가 50을 넘으면 구매 신호이고 50을 넘으면 판매 신호입니다.

  2. RSI의 20주기 EMA와 14주기 EMA를 계산합니다. 빠른 EMA가 느린 EMA를 넘어서면 구매 신호이고 아래로 넘어가면 판매 신호입니다.

  3. RSI와 가격 사이의 차이를 감지합니다.

  • 규칙적인 상승성 오차: 가격은 새로운 낮지만 RSI는 그렇지 않습니다, 구매 신호

  • 숨겨진 상승성 오차: 가격은 새로운 최고치를 올리고 RSI는 그렇지 않습니다, 구매 신호

  • 규칙적인 하향 오차: 가격은 새로운 최고치를 올리고 있지만 RSI는 그렇지 않습니다. 판매 신호

  • 숨겨진 하향 분차: 가격은 새로운 낮지만 RSI는 판매 신호가 없습니다.

  1. % 기반 및 ATR 기반을 포함한 Stop Loss 전략이 활성화될 수 있습니다.

이점 분석

  1. RSI 지표는 과잉 구매 및 과잉 판매 상황을 감지하는 데 좋습니다. EMA는 데이터의 잡음을 부드럽게 할 수 있습니다.

  2. RSI와 가격의 차이는 초기 반전 신호를 제공할 수 있습니다.

  3. 두 개의 지표의 신호를 결합하면 서로를 검증하고 전략 안정성을 향상시킬 수 있습니다.

  4. 스톱 로스 메커니즘은 단일 거래 손실을 제어합니다.

위험 분석

  1. 동력 지표로서, RSI는 가격이 급격히 변동할 때 실적이 떨어질 수 있습니다.

  2. EMA는 시간이 지연되어 있으며 전환점을 정확하게 찾을 수 없습니다.

  3. 추세가 계속될 때 이각 신호는 잘못된 신호를 일으킬 수 있습니다.

  4. 잘못된 스톱 손실 설정으로 인해 불필요한 손실이 발생할 수 있습니다.

  5. 적립액은 많을 수 있고 충분한 자본이 필요합니다.

최적화 방향

  1. 최적의 조합을 찾기 위해 RSI와 EMA 계산을 위해 다른 매개 변수를 테스트합니다.

  2. 전체 최적화를 위해 MACD와 같은 다른 지표로 EMA를 대체하는 것을 고려하십시오.

  3. 거짓 분리를 피하기 위해 확인 메커니즘을 추가합니다. 예를 들어 연속 신호가 필요합니다.

  4. 이윤을 확보하기 위해 수익을 취하는 전략을 추가합니다.

  5. 이 전략에 대한 트렌드 판단과 결합하여 진입을 위해 촛불 패턴과 같은 단기 신호를 포함합니다.

요약

이 전략은 RSI의 과반 구매/ 과반 판매 탐지, EMA와 트렌드 추적, 그리고 체계적인 트렌드 추적 시스템으로 오차의 역전 예측을 통합한다. 매개 변수 조정 및 앙상블 최적화로 좋은 결과를 얻을 수 있다. 그러나 트렌드 쇼크와 잘못된 신호와 같은 위험은 경계해야 한다. 적절한 돈 관리로, 이 전략은 중장기적으로 안정적인 초과 수익을 창출할 수 있다.


/*backtest
start: 2023-11-08 00:00:00
end: 2023-11-15 00:00:00
period: 30m
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy(title="RSI Divergence Indicator", overlay=false,pyramiding=2, default_qty_value=2,   default_qty_type=strategy.fixed, initial_capital=10000, currency=currency.USD)

len = input(title="RSI Period", minval=1, defval=14)
src = input(title="RSI Source", defval=close)
lbR = input(title="Pivot Lookback Right", defval=3)
lbL = input(title="Pivot Lookback Left", defval=1)
takeProfitRSILevel = input(title="Take Profit at RSI Level", minval=70, defval=80)

rangeUpper = input(title="Max of Lookback Range", defval=60)
rangeLower = input(title="Min of Lookback Range", defval=5)
plotBull = input(title="Plot Bullish", defval=true)
plotHiddenBull = input(title="Plot Hidden Bullish", defval=true)
plotBear = input(title="Plot Bearish", defval=true)
plotHiddenBear = input(title="Plot Hidden Bearish", defval=false)

sl_type = input("NONE", title="Trailing StopLoss Type", options=['ATR','PERC', 'NONE'])

stopLoss = input(title="Stop Loss%", defval=5, minval=1)

atrLength=input(14, title="ATR Length (for Trailing stop loss)")
atrMultiplier=input(3.5, title="ATR Multiplier (for Trailing stop loss)")


bearColor = color.red
bullColor = color.green
hiddenBullColor = color.new(color.green, 80)
hiddenBearColor = color.new(color.red, 80)
textColor = color.white
noneColor = color.new(color.white, 100)

osc = rsi(src, len)

plot(osc, title="RSI", linewidth=2, color=color.white)
hline(50, title="Middle Line", linestyle=hline.style_dotted)
obLevel = hline(70, title="Overbought", linestyle=hline.style_dotted)
osLevel = hline(30, title="Oversold", linestyle=hline.style_dotted)
fill(obLevel, osLevel, title="Background", color=color.gray, transp=90)

plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(pivothigh(osc, lbL, lbR)) ? false : true

_inRange(cond) =>
    bars = barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------
// Regular Bullish

// Osc: Higher Low
oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low
priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound

plot(
	 plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish",
	 linewidth=2,
	 color=(bullCond ? bullColor : noneColor),
	 transp=0
	 )


plotshape(
	 bullCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bullish Label",
	 text=" Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

//------------------------------------------------------------------------------
// Hidden Bullish

// Osc: Lower Low
oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low
priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)

hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

plot(
	 plFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish",
	 linewidth=2,
	 color=(hiddenBullCond ? hiddenBullColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBullCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bullish Label",
	 text=" H Bull ",
	 style=shape.labelup,
	 location=location.absolute,
	 color=bullColor,
	 textcolor=textColor,
	 transp=0
	 )

longCondition=bullCond or hiddenBullCond
//? osc[lbR] : na  
//hiddenBullCond
strategy.entry(id="RSIDivLE", long=true,  when=longCondition)


//Trailing StopLoss
////// Calculate trailing SL
/////////////////////////////////////////////////////
sl_val = sl_type == "ATR"      ? stopLoss * atr(atrLength) : 
         sl_type == "PERC" ? close * stopLoss / 100 : 0.00

trailing_sl = 0.0
trailing_sl :=   strategy.position_size>=1 ?  max(low  - sl_val, nz(trailing_sl[1])) :  na

//draw initil stop loss
//plot(strategy.position_size>=1 ? trailing_sl : na, color = color.blue , style=plot.style_linebr,  linewidth = 2, title = "stop loss")
//plot(trailing_sl, title="ATR Trailing Stop Loss", style=plot.style_linebr, linewidth=1, color=color.purple, transp=30)
//Trailing StopLoss
////// Calculate trailing SL
/////////////////////////////////////////////////////


//------------------------------------------------------------------------------
// Regular Bearish

// Osc: Lower High
oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Higher High
priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(
	 phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish",
	 linewidth=2,
	 color=(bearCond ? bearColor : noneColor),
	 transp=0
	 )

plotshape(
	 bearCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Regular Bearish Label",
	 text=" Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )

//------------------------------------------------------------------------------
// Hidden Bearish

// Osc: Higher High
oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Lower High
priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)

hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound

plot(
	 phFound ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish",
	 linewidth=2,
	 color=(hiddenBearCond ? hiddenBearColor : noneColor),
	 transp=0
	 )

plotshape(
	 hiddenBearCond ? osc[lbR] : na,
	 offset=-lbR,
	 title="Hidden Bearish Label",
	 text=" H Bear ",
	 style=shape.labeldown,
	 location=location.absolute,
	 color=bearColor,
	 textcolor=textColor,
	 transp=0
	 )
longCloseCondition=crossover(osc,takeProfitRSILevel) or bearCond
strategy.close(id="RSIDivLE", comment="Close All="+tostring(close - strategy.position_avg_price, "####.##"), when= abs(strategy.position_size)>=1  and  sl_type == "NONE" and longCloseCondition)

//close all on stop loss
strategy.close(id="RSIDivLE", comment="TSL="+tostring(close - strategy.position_avg_price, "####.##"),  when=abs(strategy.position_size)>=1 and (sl_type == "PERC"   or sl_type == "ATR" ) and crossunder(close, trailing_sl)  )  //close<ema55 and rsi5Val<20 //ema34<ema55  //close<ema89

src1 = close, 
len6 = input(14, minval=1, title="RSI Length")
len7 = input(20, minval=1, title="EMA of RSI Length")
len8 = input(14,minval=1, title="Fast EMA")
up = rma(max(change(src1), 0), len6)
down = rma(-min(change(src1), 0), len6)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
emaRSI = ema(rsi,len7)
fastEmaRSI=ema(rsi,len8)

plot(emaRSI, title="EMA of RSI", linewidth=1, color=color.red)
plot(fastEmaRSI,title="Fast EMA of RSI", linewidth=1,color = color.lime)
band1 = hline(80, title="Upper Line", linewidth=1, color=color.red)
band0 = hline(20, title="Lower Line", linewidth=1, color=color.lime)
fill(band1, band0, color=color.purple)

더 많은