Pine script RSI上的图散度

Pine script RSI上的图散度,pine-script,Pine Script,我使用下面的代码来绘制RSI上的分歧。基本上,这是一个标准的RSI代码,根据我的要求进行了一些小的调整 当我将“Pivot Lookback Right&Left”设置为“5”时,RSI偏离在5天后绘制。当我把周期减小到更低的时候,散度就会失真 我的目标是在分歧形成后尽快发现分歧,并摆脱5天的等待期 感谢您对实现同样目标的投入 //@version=4 study(title="My RSI(14)", format=format.price, resolution=&quo

我使用下面的代码来绘制RSI上的分歧。基本上,这是一个标准的RSI代码,根据我的要求进行了一些小的调整

当我将“Pivot Lookback Right&Left”设置为“5”时,RSI偏离在5天后绘制。当我把周期减小到更低的时候,散度就会失真

我的目标是在分歧形成后尽快发现分歧,并摆脱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
     )
/@version=4
研究(title=“我的RSI(14)”,format=format.price,resolution=”“)
len=输入(title=“RSI Period”,minval=1,deffal=14,group=“RSI”,inline=“RSI”)
src=input(title=“RSI Source”,deffal=close,group=“RSI”,inline=“RSI”)
plotBull=input(title=“看涨Div”,deffal=false,group=“绘图偏差”,inline=“绘图Div”)
plotHiddenBull=input(title=“正反转”,deffal=true,group=“绘图偏差”,inline=“绘图偏差”)
plotBear=input(title=“熊市Div”,deffal=false,group=“绘图分歧”,inline=“绘图分歧”)
plotHiddenBear=input(title=“负反转”,deffal=true,group=“绘图偏差”,inline=“绘图偏差”)
**lbR=输入(title=“Pivot Lookback Right”,deffal=5,group=“RSI Div”,inline=“RSI Div”)
lbL=输入(title=“Pivot Lookback Left”,deffal=5,group=“RSI Div”,inline=“RSI Div”)**
rangeUpper=输入(title=“回望范围的最大值”,deffal=60,group=“RSI Div”,inline=“RSI Div”)
rangeLower=输入(title=“回望范围的最小值”,deffal=5,group=“RSI Div”,inline=“RSI Div”)
osc=rsi(src,len)
//画水平线
topLevel=hline(100,title=“上限”,linestyle=hline.style\u虚线)
obLevel=hline(80,title=“超买”,linestyle=hline.style\u虚线)
neutralLevel60=hline(60,title=“Level-60”,linestyle=hline.style\u虚线)
neutralLevel40=hline(40,title=“Level-40”,linestyle=hline.style\u虚线)
osLevel=hline(20,title=“超卖”,linestyle=hline.style\u虚线)
bottomLevel=hline(0,title=“下限”,linestyle=hline.style\u虚线)
//填充区
填充(obLevel,neutralLevel60,title=“Background”,color=color.green,transp=90)
填充(neutralLevel60,neutralLevel40,title=“Background”,color=color.orange,transp=90)
填充(中性级别40,osLevel,title=“Background”,color=color.red,transp=90)
//用超买/超卖区域绘制RSI,并用颜色填充
绘图(osc,title=“RSI(14)”,线宽=1,颜色=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)
PLFOND=na(数据透视下限(osc、lbL、lbR))?假:真
phFound=na(数据透视高(osc、lbL、lbR))?假:真
_范围(秒)=>
条数=条数(cond==true)
rangeLower valuewhen(plFound,低[lbR],1)
hiddenBullCond=绘图HIDDENBULL和PRICHL以及oscLL和plFound
密谋(
PLF?osc[lbR]:不适用,
偏移量=-lbR,
title=“隐藏看涨”,
线宽=2,
颜色=(hiddenBullCond?hiddenBullColor:noneColor),
transp=0
)
绘图形状(
hiddenBullCond?osc[lbR]:na,
偏移量=-lbR,
title=“隐藏的看涨标签”,
text=“PR”,
style=shape.labelup,
位置=位置。绝对,
颜色=牛头色,
textcolor=textcolor,
transp=0
)
//------------------------------------------------------------------------------
//定期看跌
//Osc:低-高
oscLH=osc[lbR]valuewhen(phFound,高[lbR],1)
bearCond=plotBear和priceHH以及oscLH和phFound
密谋(
PHF?osc[lbR]:不适用,
偏移量=-lbR,
title=“定期看跌”,
线宽=2,
颜色=(bearCond?bearColor:noneColor),
transp=0
)
绘图形状(
bearCond?osc[lbR]:不适用,
偏移量=-lbR,
title=“常规熊市标签”,
text=“熊”,
style=shape.labeldown,
位置=位置。绝对,
颜色=熊色,
textcolor=textcolor,
transp=0
)
//------------------------------------------------------------------------------
//隐藏的熊市
//Osc:更高-更高
oscHH=osc[lbR]>valuewhen(phFound,osc[lbR],1)和_inRange(phFound[1])
//价格:低-高
priceLH=高[lbR]