//@version=3 //https://www.tradingview.com/u/tradingwithnoobreinier/ study("Cluster Algorithm II (Clone) v2", shorttitle="Cluster Algorithm II (Clone) v2") //inputs length = input(title="Length", type=integer, defval=14) overboughtlevel = input(title="Overbought Level", type=integer, defval=70) oversoldlevel = input(title="Oversold Level", type=integer, defval=30) src = input(title="Source", type=source, defval=hlc3) //divergence inputs lbR = input(title="Divergence Pivot Lookback Right", defval=5) lbL = input(title="Divergence Pivot Lookback Left", defval=5) rangeUpper = input(title="Divergence Max of Lookback Range", defval=60) rangeLower = input(title="Divergence Min of Lookback Range", defval=5) plotRegular = input(title="Plot Regular Divergence (Reversal) Signals", defval=false) plotHidden = input(title="Plot Hidden Divergence (Continuation) Signals", defval=false) //divergence colors bearColor = color(red, 0) bullColor = color(green, 0) hiddenBullColor = color(green, 0) hiddenBearColor = color(red, 0) textColor = color(white,0) noneColor = color(white, 100) //calculations f8 = 100 * src f10 = nz(f8[1]) v8 = f8 - f10 f18 = 3 / (length + 2) f20 = 1 - f18 f28 = 0.0 f28 := f20 * nz(f28[1]) + f18 * v8 f30 = 0.0 f30 := f18 * f28 + f20 * nz(f30[1]) vC = f28 * 1.5 - f30 * 0.5 f38 = 0.0 f38 := f20 * nz(f38[1]) + f18 * vC f40 = 0.0 f40 := f18 * f38 + f20 * nz(f40[1]) v10 = f38 * 1.5 - f40 * 0.5 f48 = 0.0 f48 := f20 * nz(f48[1]) + f18 * v10 f50 = 0.0 f50 := f18 * f48 + f20 * nz(f50[1]) v14 = f48 * 1.5 - f50 * 0.5 f58 = 0.0 f58 := f20 * nz(f58[1]) + f18 * abs(v8) f60 = 0.0 f60 := f18 * f58 + f20 * nz(f60[1]) v18 = f58 * 1.5 - f60 * 0.5 f68 = 0.0 f68 := f20 * nz(f68[1]) + f18 * v18 f70 = 0.0 f70 := f18 * f68 + f20 * nz(f70[1]) v1C = f68 * 1.5 - f70 * 0.5 f78 = 0.0 f78 := f20 * nz(f78[1]) + f18 * v1C f80 = 0.0 f80 := f18 * f78 + f20 * nz(f80[1]) v20 = f78 * 1.5 - f80 * 0.5 f88_ = 0.0 f90_ = 0.0 f88 = 0.0 f90_ := nz(f90_[1]) == 0 ? 1 : nz(f88[1]) <= nz(f90_[1]) ? nz(f88[1]) + 1 : nz(f90_[1]) + 1 f88 := nz(f90_[1]) == 0 and (length - 1 >= 5) ? length - 1 : 5 f0 = f88 >= f90_ and f8 != f10 ? 1 : 0 f90 = f88 == f90_ and f0 == 0 ? 0 : f90_ v4_ = f88 < f90 and v20 > 0 ? (v14 / v20 + 1) * 50 : 50 rsx = v4_ > 100 ? 100 : v4_ < 0 ? 0 : v4_ current = rsx[0] previous = rsx[1] //plot the rsx line plot(rsx, title="RSX Line", linewidth=2, color=current > previous ? #0ebb23 : #ff0000, transp=10) transparent = color(white, 100) //plot the lines maximumlevelline = hline(100, title="Maximum Level", color=transparent) minimumlevelline = hline(0, title="Minimum Level", color=transparent) overboughtlevelline = hline(overboughtlevel, title="Overbought Level", linestyle=dashed) oversoldlevelline = hline(oversoldlevel, title="Oversold Level", linestyle=dashed) //ovebought oversold area color conditions overboughtfillcolor = rsx > overboughtlevel ? red : transparent oversoldfillcolor = rsx < oversoldlevel ? green : transparent //fill the ovebought oversold areas fill(maximumlevelline, overboughtlevelline, color=overboughtfillcolor, transp=60) fill(minimumlevelline, oversoldlevelline, color=oversoldfillcolor, transp=60) plFound = na(pivotlow(current, lbL, lbR)) ? false : true phFound = na(pivothigh(current, lbL, lbR)) ? false : true _inRange(cond) => bars = barssince(cond == true) rangeLower <= bars and bars <= rangeUpper //------------------------------------------------------------------------------ // Regular Bullish Divergence // Osc: Higher Low oscHL = current[lbR] > valuewhen(plFound, current[lbR], 1) and _inRange(plFound[1]) // Price: Lower Low priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1) bullCond = plotRegular and priceLL and oscHL and plFound plot( plFound ? current[lbR] : na, offset=-lbR, title="Regular Bullish Divergence", linewidth=2, color=(bullCond ? bullColor : noneColor), transp=0 ) plotshape( bullCond ? current[lbR] : na, offset=-lbR, title="Regular Bullish Label", text=" Bull ", style=shape.labelup, location=location.absolute, color=bullColor, textcolor=textColor, transp=0 ) //------------------------------------------------------------------------------ // Regular Bearish // Osc: Lower High oscLH = current[lbR] < valuewhen(phFound, current[lbR], 1) and _inRange(phFound[1]) // Price: Higher High priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1) bearCond = plotRegular and priceHH and oscLH and phFound plot( phFound ? current[lbR] : na, offset=-lbR, title="Regular Bearish Divergence", linewidth=2, color=(bearCond ? bearColor : noneColor), transp=0 ) plotshape( bearCond ? current[lbR] : na, offset=-lbR, title="Regular Bearish Label", text=" Bear ", style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=textColor, transp=0 ) //------------------------------------------------------------------------------ // Hidden Bullish // Osc: Lower Low oscLL = current[lbR] < valuewhen(plFound, current[lbR], 1) and _inRange(plFound[1]) // Price: Higher Low priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1) hiddenBullCond = plotHidden and priceHL and oscLL and plFound plot( plFound ? current[lbR] : na, offset=-lbR, title="Hidden Bullish", linewidth=2, color=(hiddenBullCond ? hiddenBullColor : noneColor), transp=0 ) plotshape( hiddenBullCond ? current[lbR] : na, offset=-lbR, title="Hidden Bullish Label", text=" H Bull ", style=shape.labelup, location=location.absolute, color=bullColor, textcolor=textColor, transp=0 ) //------------------------------------------------------------------------------ // Hidden Bearish // Osc: Higher High oscHH = current[lbR] > valuewhen(phFound, current[lbR], 1) and _inRange(phFound[1]) // Price: Lower High priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1) hiddenBearCond = plotHidden and priceLH and oscHH and phFound plot( phFound ? current[lbR] : na, offset=-lbR, title="Hidden Bearish", linewidth=2, color=(hiddenBearCond ? hiddenBearColor : noneColor), transp=0 ) plotshape( hiddenBearCond ? current[lbR] : na, offset=-lbR, title="Hidden Bearish Label", text=" H Bear ", style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=textColor, transp=0 )