I've been trying to modify a built-in code from Tradingview about RSI Divergences.
I would like it to plot only the divergences that starts outside of the Over Sold Level and Over Bought Level.
If the divergences either finish in or out the Over Sold Level or Over Bought Level doesn't matter.
Here is the code, if anyone could take a look and help me out, it would be much appreciated!
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © siringaacuta
//@version=5
indicator(title="RSI Divergence Indicator", format=format.price, timeframe="", timeframe_gaps=true)
lbR = input(title="Pivot Lookback Right", defval=20, tooltip = "For bearish Div")
lbL = input(title="Pivot Lookback Left", defval=20, tooltip = "For bullish Div")
rsi = ta.rsi(close, 14)
obLevel = 70
osLevel = 30
hline(70, title="Overbought", color=#787B86, linestyle=hline.style_dotted, linewidth = 2)
hline(30, title="Oversold", color=#787B86, linestyle=hline.style_dotted, linewidth = 2)
plot(rsi, title="RSI", linewidth=2, color=#2962FF)
plFound = na(ta.pivotlow(rsi, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(rsi, lbL, lbR)) ? false : true
// Regular Bullish
// Osc: Higher Low
rsiHL = rsi[lbR] > ta.valuewhen(plFound, rsi[lbR], 1)
// Price: Lower Low
priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)
bullCond = priceLL and rsiHL and plFound
plot(plFound ? rsi[lbR] : na,offset=-lbR,title="Regular Bullish",linewidth=2,color=(bullCond ? color.green : na))
plotshape(bullCond ? rsi[lbR] : na,offset=-lbR,title="Regular Bullish Label",text=" Bull ",style=shape.labelup,location=location.absolute,color=color.green,textcolor=color.white)
// Regular Bearish
// Osc: Lower High
rsiLH = rsi[lbR] < ta.valuewhen(phFound, rsi[lbR], 1)
// Price: Higher High
priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)
bearCond = priceHH and rsiLH and phFound
plot(phFound ? rsi[lbR] : na,offset=-lbR,title="Regular Bearish",linewidth=2,color=(bearCond ? color.red : na))
plotshape(bearCond ? rsi[lbR] : na,offset=-lbR,title="Regular Bearish Label",text=" Bear ",style=shape.labeldown,location=location.absolute,color=color.red,textcolor=color.white)
I tried to modify the code but the only result I managed to get is to plot only the divergences that starts and ends outside the Over Sold Level and Over Bought Level and that filters out too much for me.