Divergence
Divergence
// get indicators
rsiin = 14
stochin = 14
stochsmain = 3
mfiin = 14
cciin = 10
momein = 10
macdin = 12
macdsigin = 26
macddelin = 9
cmfin = 21
cmfsmain = 21
rsi = ta.rsi(close1, rsiin) // RSI
[macd, signal, deltamacd] = ta.macd(close1, macdin, macdsigin, macddelin) // MACD
moment = ta.mom(close1, momein) // Momentum
cci = ta.cci(close1, cciin) // CCI
Obv = obv1 // OBV
stk = ta.sma(ta.stoch(close1, high1, low1, stochin), stochsmain) // Stoch
vmacfastin = 26
vmacslowin = 55
maFast = ta.vwma(close1, vmacfastin) // volume weighted macd
maSlow = ta.vwma(close1, vmacslowin)
vwmacd = maFast - maSlow
Cmfm = (close1 - low1 - (high1 - close1)) / (high1 - low1) // Chaikin money flow
Cmfv = Cmfm * volume1
cmf = ta.sma(Cmfv, cmfin) / ta.sma(volume1, cmfsmain)
Mfi = ta.mfi(close1, mfiin) // Moneyt Flow Index
if arrived
divlen := len
break
divlen
// function to check negative regular or positive hidden divergence
// cond == 1 => negative_regular, cond == 2=> positive_hidden
negative_regular_negative_hidden_divergence(src, cond) =>
divlen = 0
prsc = source == 'Close' ? close1 : high1
// if indicators higher than last value and close price is higher than las
close
if dontconfirm or src < src[1] or close1 < close1[1]
startpoint = dontconfirm ? 0 : 1 // don't check last candle
// we search last 15 PPs
for x = 0 to maxpp - 1 by 1
len = bar_index - array.get(ph_positions, x) + prd
// if we reach non valued array element or arrived 101. or previous
bars then we don't search more
if array.get(ph_positions, x) == 0 or len > maxbars
break
if len > 5 and (cond == 1 and src[startpoint] < src[len] and
prsc[startpoint] > nz(array.get(ph_vals, x)) or cond == 2 and src[startpoint] >
src[len] and prsc[startpoint] < nz(array.get(ph_vals, x)))
slope1 = (src[startpoint] - src[len]) / (len - startpoint)
virtual_line1 = src[startpoint] - slope1
slope2 = (close1[startpoint] - nz(close1[len])) / (len -
startpoint)
virtual_line2 = close1[startpoint] - slope2
arrived = true
for y = 1 + startpoint to len - 1 by 1
if src[y] > virtual_line1 or nz(close1[y]) > virtual_line2
arrived := false
break
virtual_line1 -= slope1
virtual_line2 -= slope2
virtual_line2
if arrived
divlen := len
break
divlen
// check minimum number of divergence, if less than showlimit then delete all
divergence
total_div = 0
for x = 0 to array.size(all_divergences) - 1 by 1
total_div += math.round(math.sign(array.get(all_divergences, x)))
total_div
for x = 0 to 10 by 1
div_type = -1
for y = 0 to 3 by 1
if array.get(all_divergences, x * 4 + y) > 0 // any divergence?
div_type := y
if y % 2 == 1
dnumdiv_top += 1
top_label_col := array.get(div_colors, y)
top_label_col
if y % 2 == 0
dnumdiv_bottom += 1
bottom_label_col := array.get(div_colors, y)
bottom_label_col
if not array.includes(distances, array.get(all_divergences, x * 4 + y))
// line not exist ?
array.push(distances, array.get(all_divergences, x * 4 + y))
// draw labels
if showindis != 'Don\'t Show' or shownum
if shownum and dnumdiv_top > 0
divergence_text_top += str.tostring(dnumdiv_top)
divergence_text_top
if shownum and dnumdiv_bottom > 0
divergence_text_bottom += str.tostring(dnumdiv_bottom)
divergence_text_bottom
if divergence_text_top != ''
if divergence_text_bottom != ''