//@version=5
indicator('Divergence', overlay=true, max_bars_back=4000)
lb = input.int(5, title='Left Bars', minval=1)
rb = input.int(5, title='Right Bars', minval=1)
showhidden = input(false, title='Show Hidden Divergences')
chcut = input(true, title='Check Cut-Through in indicators !')
shownum = input(true, title='Show Divergence Number')
showindis = input(false, title='Show Indicator Names')
showpivot = input(false, title='Show Pivot Points')
chwidth = input(true, title='Change Width by Number of Divergence')
showlimit = input.int(1, title='Minimum Number of Divergence', minval=1, maxval=11)
calcmacd = input(true, title='MACD')
calcmacda = input(true, title='MACD Histogram')
calcrsi = input(true, title='RSI')
calcstoc = input(true, title='Stochastic')
calccci = input(true, title='CCI')
calcmom = input(true, title='Momentum')
calcobv = input(true, title='OBV')
calcdi = input(true, title='Diosc')
calcvwmacd = input(true, title='VWmacd')
calccmf = input(true, title='Chaikin Money Flow')
calcmfi = input(true, title='Money Flow Index')
rsi = ta.rsi(close, 14)
[macd, signal, deltamacd] = ta.macd(close, 12, 26, 9)
moment = ta.mom(close, 10)
cci = ta.cci(close, 10)
Obv = ta.obv
stk = ta.sma(ta.stoch(close, high, low, 14), 3)
DI = ta.change(high) - -ta.change(low)
trur = ta.rma(ta.tr, 14)
diosc = fixnan(100 * ta.rma(DI, 14) / trur)
maFast = ta.vwma(close, 12)
maSlow = ta.vwma(close, 26)
vwmacd = maFast - maSlow
Cmfm = (close - low - (high - close)) / (high - low)
Cmfv = Cmfm * volume
cmf = ta.sma(Cmfv, 21) / ta.sma(volume, 21)
Mfi = ta.mfi(close, 14)
float top = na
float bot = na
top := ta.pivothigh(lb, rb)
bot := ta.pivotlow(lb, rb)
plotshape(top and showpivot, text='[PH]', style=shape.labeldown,
color=color.new(color.white, 0), textcolor=color.new(color.black, 0),
location=location.abovebar, offset=-rb)
plotshape(bot and showpivot, text='[PL]', style=shape.labeldown,
color=color.new(color.white, 0), textcolor=color.new(color.black, 0),
location=location.belowbar, offset=-rb)
topc = 0
botc = 0
topc := top ? lb : nz(topc[1]) + 1
botc := bot ? lb : nz(botc[1]) + 1
newtop = ta.pivothigh(lb, 0)
emptyh = true
if not na(newtop) and (newtop > high[topc] and not showhidden or newtop <
high[topc] and showhidden)
diff = (newtop - high[topc]) / topc
hline = newtop - diff
for x = 1 to topc - 1 by 1
if close[x] > hline
emptyh := false
break
hline -= diff
hline
else
emptyh := false
emptyh
nocut1(indi, len) =>
_ret = true
diff = (indi - nz(indi[len])) / len
ln = indi - diff
for x = 1 to len - 1 by 1
if nz(indi[x]) > ln
_ret := false
break
ln -= diff
ln
_ret
rsiok = nocut1(rsi, topc)
macdok = nocut1(macd, topc)
deltamacdok = nocut1(deltamacd, topc)
momentok = nocut1(moment, topc)
cciok = nocut1(cci, topc)
obvok = nocut1(ta.obv, topc)
stkok = nocut1(stk, topc)
dioscok = nocut1(diosc, topc)
vwmacdok = nocut1(vwmacd, topc)
cmfok = nocut1(cmf, topc)
mfiok = nocut1(Mfi, topc)
negdivergence = 0
negdivtxt = ''
if emptyh and not na(newtop) and not showhidden
if calcrsi and rsi[topc] > rsi and (not chcut or rsiok)
negdivergence += 1
negdivtxt := 'RSI\n'
negdivtxt
if calcmacd and macd[topc] > macd and (not chcut or macdok)
negdivergence += 1
negdivtxt += 'MACD\n'
negdivtxt
if calcmacda and deltamacd[topc] > deltamacd and (not chcut or deltamacdok)
negdivergence += 1
negdivtxt += 'MACD Hist\n'
negdivtxt
if calcmom and moment[topc] > moment and (not chcut or momentok)
negdivergence += 1
negdivtxt += 'Momentum\n'
negdivtxt
if calccci and cci[topc] > cci and (not chcut or cciok)
negdivergence += 1
negdivtxt += 'CCI\n'
negdivtxt
if calcobv and Obv[topc] > Obv and (not chcut or obvok)
negdivergence += 1
negdivtxt += 'OBV\n'
negdivtxt
if calcstoc and stk[topc] > stk and (not chcut or stkok)
negdivergence += 1
negdivtxt += 'Stoch\n'
negdivtxt
if calcdi and diosc[topc] > diosc and (not chcut or dioscok)
negdivergence += 1
negdivtxt += 'Diosc\n'
negdivtxt
if calcvwmacd and vwmacd[topc] > vwmacd and (not chcut or vwmacdok)
negdivergence += 1
negdivtxt += 'VWMacd\n'
negdivtxt
if calccmf and cmf[topc] > cmf and (not chcut or cmfok)
negdivergence += 1
negdivtxt += 'CMF\n'
negdivtxt
if calcmfi and Mfi[topc] > Mfi and (not chcut or mfiok)
negdivergence += 1
negdivtxt += 'MFI\n'
negdivtxt
hnegdivergence = 0
hnegdivtxt = ''
if emptyh and not na(newtop) and showhidden
if calcrsi and rsi[topc] < rsi and (not chcut or rsiok)
hnegdivergence += 1
hnegdivtxt := 'RSI\n'
hnegdivtxt
if calcmacd and macd[topc] < macd and (not chcut or macdok)
hnegdivergence += 1
hnegdivtxt += 'MACD\n'
hnegdivtxt
if calcmacda and deltamacd[topc] < deltamacd and (not chcut or deltamacdok)
hnegdivergence += 1
hnegdivtxt += 'MACD Hist\n'
hnegdivtxt
if calcmom and moment[topc] < moment and (not chcut or momentok)
hnegdivergence += 1
hnegdivtxt += 'Momentum\n'
hnegdivtxt
if calccci and cci[topc] < cci and (not chcut or cciok)
hnegdivergence += 1
hnegdivtxt += 'CCI\n'
hnegdivtxt
if calcobv and Obv[topc] < Obv and (not chcut or obvok)
hnegdivergence += 1
hnegdivtxt += 'OBV\n'
hnegdivtxt
if calcstoc and stk[topc] < stk and (not chcut or stkok)
hnegdivergence += 1
hnegdivtxt += 'Stoch\n'
hnegdivtxt
if calcdi and diosc[topc] < diosc and (not chcut or dioscok)
hnegdivergence += 1
hnegdivtxt += 'Diosc\n'
hnegdivtxt
if calcvwmacd and vwmacd[topc] < vwmacd and (not chcut or vwmacdok)
hnegdivergence += 1
hnegdivtxt += 'VWMacd\n'
hnegdivtxt
if calccmf and cmf[topc] < cmf and (not chcut or cmfok)
hnegdivergence += 1
hnegdivtxt += 'CMF\n'
hnegdivtxt
if calcmfi and Mfi[topc] < Mfi and (not chcut or mfiok)
hnegdivergence += 1
hnegdivtxt += 'MFI\n'
hnegdivtxt
newareah = false
newareah := top ? false : nz(newareah[1], false)
if negdivergence >= showlimit or hnegdivergence >= showlimit
var line divlh = na
var label labh = na
if newareah
line.delete(divlh)
label.delete(labh)
newwd = not showhidden ? not chwidth ? 2 : negdivergence <= 2 ? 2 :
negdivergence <= 5 ? 3 : negdivergence <= 8 ? 4 : 5 : not chwidth ? 2 :
hnegdivergence <= 2 ? 2 : hnegdivergence <= 5 ? 3 : hnegdivergence <= 8 ? 4 : 5
divlh := line.new(bar_index - topc, high[topc], bar_index, high,
color=color.red, width=newwd)
if shownum or showindis
txt = showindis ? showhidden ? hnegdivtxt : negdivtxt : ''
txt += (shownum ? showhidden ? str.tostring(hnegdivergence) :
str.tostring(negdivergence) : '')
labh := label.new(bar_index, na, text=txt, color=color.red,
textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)
labh
newareah := true
newareah
newbot = ta.pivotlow(lb, 0)
emptyl = true
if not na(newbot) and (newbot < low[botc] and not showhidden or newbot > low[botc]
and showhidden)
diff = (newbot - low[botc]) / botc
lline = newbot - diff
for x = 1 to botc - 1 by 1
if close[x] < lline
emptyl := false
break
lline -= diff
lline
else
emptyl := false
emptyl
nocut2(indi, len) =>
_ret = true
diff = (indi - nz(indi[len])) / len
ln = indi - diff
for x = 1 to len - 1 by 1
if nz(indi[x]) < ln
_ret := false
break
ln -= diff
ln
_ret
rsiok := nocut2(rsi, botc)
macdok := nocut2(macd, botc)
deltamacdok := nocut2(deltamacd, botc)
momentok := nocut2(moment, botc)
cciok := nocut2(cci, botc)
obvok := nocut2(ta.obv, botc)
stkok := nocut2(stk, botc)
dioscok := nocut2(diosc, botc)
vwmacdok := nocut2(vwmacd, botc)
cmfok := nocut2(cmf, botc)
mfiok := nocut2(Mfi, botc)
posdivergence = 0
posdivtxt = ''
if emptyl and not na(newbot) and not showhidden
if calcrsi and rsi[botc] < rsi and (not chcut or rsiok)
posdivergence += 1
posdivtxt := 'RSI\n'
posdivtxt
if calcmacd and macd[botc] < macd and (not chcut or macdok)
posdivergence += 1
posdivtxt += 'MACD\n'
posdivtxt
if calcmacda and deltamacd[botc] < deltamacd and (not chcut or deltamacdok)
posdivergence += 1
posdivtxt += 'MACD Hist\n'
posdivtxt
if calcmom and moment[botc] < moment and (not chcut or momentok)
posdivergence += 1
posdivtxt += 'Momentum\n'
posdivtxt
if calccci and cci[botc] < cci and (not chcut or cciok)
posdivergence += 1
posdivtxt += 'CCI\n'
posdivtxt
if calcobv and Obv[botc] < Obv and (not chcut or obvok)
posdivergence += 1
posdivtxt += 'OBV\n'
posdivtxt
if calcstoc and stk[botc] < stk and (not chcut or stkok)
posdivergence += 1
posdivtxt += 'Stoch\n'
posdivtxt
if calcdi and diosc[botc] < diosc and (not chcut or dioscok)
posdivergence += 1
posdivtxt += 'Diosc\n'
posdivtxt
if calcvwmacd and vwmacd[botc] < vwmacd and (not chcut or vwmacdok)
posdivergence += 1
posdivtxt += 'VWMacd\n'
posdivtxt
if calccmf and cmf[botc] < cmf and (not chcut or cmfok)
posdivergence += 1
posdivtxt += 'CMF\n'
posdivtxt
if calcmfi and Mfi[botc] < Mfi and (not chcut or mfiok)
posdivergence += 1
posdivtxt += 'MFI\n'
posdivtxt
hposdivergence = 0
hposdivtxt = ''
if emptyl and not na(newbot) and showhidden
if calcrsi and rsi[botc] > rsi and (not chcut or rsiok)
hposdivergence += 1
hposdivtxt := 'RSI\n'
hposdivtxt
if calcmacd and macd[botc] > macd and (not chcut or macdok)
hposdivergence += 1
hposdivtxt += 'MACD\n'
hposdivtxt
if calcmacda and deltamacd[botc] > deltamacd and (not chcut or deltamacdok)
hposdivergence += 1
hposdivtxt += 'MACD Hist\n'
hposdivtxt
if calcmom and moment[botc] > moment and (not chcut or momentok)
hposdivergence += 1
hposdivtxt += 'Momentum\n'
hposdivtxt
if calccci and cci[botc] > cci and (not chcut or cciok)
hposdivergence += 1
hposdivtxt += 'CCI\n'
hposdivtxt
if calcobv and Obv[botc] > Obv and (not chcut or obvok)
hposdivergence += 1
hposdivtxt += 'OBV\n'
hposdivtxt
if calcstoc and stk[botc] > stk and (not chcut or stkok)
hposdivergence += 1
hposdivtxt += 'Stoch\n'
hposdivtxt
if calcdi and diosc[botc] > diosc and (not chcut or dioscok)
hposdivergence += 1
hposdivtxt += 'Diosc\n'
hposdivtxt
if calcvwmacd and vwmacd[botc] > vwmacd and (not chcut or vwmacdok)
hposdivergence += 1
hposdivtxt += 'VWMacd\n'
hposdivtxt
if calccmf and cmf[botc] > cmf and (not chcut or cmfok)
hposdivergence += 1
hposdivtxt := hnegdivtxt + 'CMF\n'
hposdivtxt
if calcmfi and Mfi[botc] > Mfi and (not chcut or mfiok)
hposdivergence += 1
hposdivtxt += 'MFI\n'
hposdivtxt
newareal = false
newareal := bot ? false : nz(newareal[1], false)
if posdivergence >= showlimit or hposdivergence >= showlimit
var line divl = na
var label lab = na
if newareal
line.delete(divl)
label.delete(lab)
newwd = not showhidden ? not chwidth ? 2 : posdivergence <= 2 ? 2 :
posdivergence <= 5 ? 3 : posdivergence <= 8 ? 4 : 5 : not chwidth ? 2 :
hposdivergence <= 2 ? 2 : hposdivergence <= 5 ? 3 : hposdivergence <= 8 ? 4 : 5
divl := line.new(bar_index - botc, low[botc], bar_index, low, color=color.lime,
width=newwd)
if shownum or showindis
txt = showindis ? showhidden ? hposdivtxt : posdivtxt : ''
txt += (shownum ? showhidden ? str.tostring(hposdivergence) :
str.tostring(posdivergence) : '')
lab := label.new(bar_index, na, text=txt, color=color.lime,
textcolor=color.black, style=label.style_label_up, yloc=yloc.belowbar)
lab
newareal := true
newareal
alertcondition(posdivergence >= showlimit and not showhidden, title='Positive
Divergence', message='Positive Divergence')
alertcondition(negdivergence >= showlimit and not showhidden, title='Negative
Divergence', message='Negative Divergence')
alertcondition(hposdivergence >= showlimit and showhidden, title='Positive Hidden
Divergence', message='Positive Hidden Divergence')
alertcondition(hnegdivergence >= showlimit and showhidden, title='Negative Hidden
Divergence', message='Negative Hidden Divergence')