[_ParkF]RSI Divergence_overlay
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ParkF

//@version=5
indicator("[_ParkF]RSI_Div(Overlay)", overlay=true)

// rsi divergence
// input
group1 = 'RSI DIV SETTING'
len = input(14, 'RSI Period', group=group1)
lb = input(15, 'Pivot Leftbars', group=group1)
rb = input(2, 'Pivot Rightbars', group=group1)
bear_src = input(close, 'Bearish Divs_Pivot Source', group=group1)
bull_src = input(close, 'Bullish Divs_Pivot Source', group=group1)
lvl = input.int(5, 'Lookback Level_Divs', options=[1, 2, 3, 4, 5], group=group1)
divsw = input(true, 'Divergence On / Off Switch', group=group1)

// get indicator
rsi = ta.rsi(close, len)

// pivot
bar = bar_index[rb]
ph = ta.pivothigh(bear_src, lb, rb)
pl = ta.pivotlow(bull_src, lb, rb)

// div function
hi0 = ta.valuewhen(ph, bear_src[rb], 0), hi1 = ta.valuewhen(ph, bear_src[rb], 1)
hi2 = ta.valuewhen(ph, bear_src[rb], 2), hi3 = ta.valuewhen(ph, bear_src[rb], 3)
hi4 = ta.valuewhen(ph, bear_src[rb], 4), hi5 = ta.valuewhen(ph, bear_src[rb], 5)

lo0 = ta.valuewhen(pl, bull_src[rb], 0), lo1 = ta.valuewhen(pl, bull_src[rb], 1)
lo2 = ta.valuewhen(pl, bull_src[rb], 2), lo3 = ta.valuewhen(pl, bull_src[rb], 3)
lo4 = ta.valuewhen(pl, bull_src[rb], 4), lo5 = ta.valuewhen(pl, bull_src[rb], 5)

dh0 = ta.valuewhen(ph, rsi[rb], 0), dh1 = ta.valuewhen(ph, rsi[rb], 1)
dh2 = ta.valuewhen(ph, rsi[rb], 2), dh3 = ta.valuewhen(ph, rsi[rb], 3)
dh4 = ta.valuewhen(ph, rsi[rb], 4), dh5 = ta.valuewhen(ph, rsi[rb], 5)

dl0 = ta.valuewhen(pl, rsi[rb], 0), dl1 = ta.valuewhen(pl, rsi[rb], 1)
dl2 = ta.valuewhen(pl, rsi[rb], 2), dl3 = ta.valuewhen(pl, rsi[rb], 3)
dl4 = ta.valuewhen(pl, rsi[rb], 4), dl5 = ta.valuewhen(pl, rsi[rb], 5)

bear_div1 = hi0 > hi1
     and dh1 > dh0
bear_div2 = hi0 > hi1 and hi0 > hi2
     and dh2 > dh0 and dh2 > dh1 and lvl >= 2
bear_div3 = hi0 > hi1 and hi0 > hi2 and hi0 > hi3
     and dh3>dh0 and dh3>dh1 and dh3>dh2 and lvl >= 3
bear_div4 = hi0 > hi1 and hi0 > hi2 and hi0 > hi3 and hi0 > hi4
     and dh4 > dh0 and dh4 > dh1 and dh4 > dh2 and dh4 > dh3 and lvl >= 4
bear_div5 = hi0>hi1 and hi0>hi2 and hi0>hi3 and hi0 > hi4 and hi0 > hi5
     and dh5 > dh0 and dh5 > dh1 and dh5 > dh2 and dh5 > dh3 and dh5 > dh4 and lvl >= 5
     
bull_div1 = lo0 < lo1
     and dl1 < dl0
bull_div2 = lo0<lo1 and lo0<lo2
     and dl2 < dl0 and dl2 < dl1 and lvl >= 2
bull_div3 = lo0 < lo1 and lo0 < lo2 and lo0 < lo3
     and dl3 < dl0 and dl3 < dl1 and dl3 < dl2 and lvl >= 3
bull_div4 = lo0 < lo1 and lo0 < lo2 and lo0 < lo3 and lo0 < lo4
     and dl4 < dl0 and dl4 < dl1 and dl4 < dl2 and dl4 < dl3 and lvl >= 4
bull_div5 = lo0 < lo1 and lo0 < lo2 and lo0 < lo3 and lo0 < lo4 and lo0 < lo5
      and dl5 < dl0 and dl5 < dl1 and dl5 < dl2 and dl5 < dl3 and dl5 < dl4 and lvl >= 5

_bear1 = bear_div1 and not bear_div1[1], _bear2 = bear_div2 and not bear_div2[1]
_bear3 = bear_div3 and not bear_div3[1], _bear4 = bear_div4 and not bear_div4[1]
_bear5 = bear_div5 and not bear_div5[1]

_bull1 = bull_div1 and not bull_div1[1], _bull2 = bull_div2 and not bull_div2[1]
_bull3 = bull_div3 and not bull_div3[1], _bull4 = bull_div4 and not bull_div4[1]
_bull5 = bull_div5 and not bull_div5[1]

// plotshape
plotshape(title='bear_div1', series=_bear1 and divsw, style=shape.triangledown, color=#ff0000, location=location.abovebar, size=size.tiny, offset=-2)
plotshape(title='bear_div2', series=_bear2 and divsw, style=shape.triangledown, color=#ff0000, location=location.abovebar, size=size.tiny, offset=-2)
plotshape(title='bear_div3', series=_bear3 and divsw, style=shape.triangledown, color=#ff0000, location=location.abovebar, size=size.tiny, offset=-2)
plotshape(title='bear_div4', series=_bear4 and divsw, style=shape.triangledown, color=#ff0000, location=location.abovebar, size=size.tiny, offset=-2)
plotshape(title='bear_div5', series=_bear5 and divsw, style=shape.triangledown, color=#ff0000, location=location.abovebar, size=size.tiny, offset=-2)
plotshape(title='bull_div1', series=_bull1 and divsw, style=shape.triangleup, color=#00ff0a, location=location.belowbar, size=size.tiny, offset=-2)
plotshape(title='bull_div2', series=_bull2 and divsw, style=shape.triangleup, color=#00ff0a, location=location.belowbar, size=size.tiny, offset=-2)
plotshape(title='bull_div3', series=_bull3 and divsw, style=shape.triangleup, color=#00ff0a, location=location.belowbar, size=size.tiny, offset=-2)
plotshape(title='bull_div4', series=_bull4 and divsw, style=shape.triangleup, color=#00ff0a, location=location.belowbar, size=size.tiny, offset=-2)
plotshape(title='bull_div5', series=_bull5 and divsw, style=shape.triangleup, color=#00ff0a, location=location.belowbar, size=size.tiny, offset=-2)