我正在使用以下代码绘制 RSI 上的背离。基本上,这是一个标准的 RSI 代码,根据我的要求进行了细微的调整。
当我将“Pivot Lookback Right & Left”设置为“5”时,5 天后会绘制 RSI 背离。当我将周期减少到较低时,分歧就会扭曲。
我的目标是在背离形成后立即识别并摆脱 5 天的等待期。
感谢您在实现相同目标方面的投入。
//@version=4
study(title="My RSI(14)", format=format.price, resolution="")
len = input(title="RSI Period", minval=1, defval=14, group="RSI", inline="RSI")
src = input(title="RSI Source", defval=close, group="RSI", inline="RSI")
plotBull = input(title="Bullish Div", defval=false, group="Plot Divergence", inline="Plot-Div")
plotHiddenBull = input(title="Positive Reversal", defval=true, group="Plot Divergence", inline="Plot-Div")
plotBear = input(title="Bearish Div", defval=false, group="Plot Divergence", inline="Plot-Div")
plotHiddenBear = input(title="Negative Reversal", defval=true, group="Plot Divergence", inline="Plot-Div")
**lbR = input(title="Pivot Lookback Right", defval=5, group="RSI-Div", inline="RSI-Div")
lbL = input(title="Pivot Lookback Left", defval=5, group="RSI-Div", inline="RSI-Div")**
rangeUpper = input(title="Max of Lookback Range", defval=60, group="RSI-Div", inline="RSI-Div")
rangeLower = input(title="Min of Lookback Range", defval=5, group="RSI-Div", inline="RSI-Div")
osc = rsi(src, len)
// draw horizontal lines
topLevel = hline(100, title="Upper Bound", linestyle=hline.style_dotted)
obLevel = hline(80, title="Overbought", linestyle=hline.style_dashed)
neutralLevel60 = hline(60, title="Level-60", linestyle=hline.style_dashed)
neutralLevel40 = hline(40, title="Level-40", linestyle=hline.style_dashed)
osLevel = hline(20, title="Oversold", linestyle=hline.style_dashed)
bottomLevel = hline(0, title="Lower Bound", linestyle=hline.style_dotted)
// fill zones
fill(obLevel, neutralLevel60, title="Background", color=color.green, transp=90)
fill(neutralLevel60, neutralLevel40, title="Background", color=color.orange, transp=90)
fill(neutralLevel40, osLevel, title="Background", color=color.red, transp=90)
// Plot RSI with overbought/oversold zones and fill it with color
plot(osc, title="RSI(14)", linewidth=1, color=#8D1699)
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)
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=" PR ",
style=shape.labelup,
location=location.absolute,
color=bullColor,
textcolor=textColor,
transp=0
)
//------------------------------------------------------------------------------
// 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=" NR ",
style=shape.labeldown,
location=location.absolute,
color=bearColor,
textcolor=textColor,
transp=0
)