//---- RSI divergence //indicator(title="Relative Strength Index", format=format.price, precision=2, timeframe="", timeframe_gaps=true) ma(source, length, type) => switch type "EMA" => ta.ema(source, length) "SMMA (RMA)" => ta.rma(source, length) "WMA" => ta.wma(source, length) "VWMA" => ta.vwma(source, length) //rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings") //rsiSourceInput = input.source(close, "Source", group="RSI Settings") //maTypeInput = input.string("EMA", title="MA Type", options=["EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings") //maLengthInput = input.int(14, title="MA Length", group="MA Settings") rsiLengthInput=14 rsiSourceInput = close maLengthInput = 14 maTypeInput ="EMA" showDivergence =true up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput) down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput) rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down)) rsiMA = ma(rsi, maLengthInput, maTypeInput) // Divergence lookbackRight = 5 lookbackLeft = 5 rangeUpper = 60 rangeLower = 5 bearColor = color.red bullColor = color.green textColor = color.white noneColor = color.new(color.white, 100) plFound = na(ta.pivotlow(rsi, lookbackLeft, lookbackRight)) ? false : true phFound = na(ta.pivothigh(rsi, lookbackLeft, lookbackRight)) ? false : true _inRange(cond) => bars = ta.barssince(cond == true) rangeLower <= bars and bars <= rangeUpper //------------------------------------------------------------------------------ // Regular Bullish // rsi: Higher Low rsiHL = rsi[lookbackRight] > ta.valuewhen(plFound, rsi[lookbackRight], 1) and _inRange(plFound[1]) // Price: Lower Low priceLL = low[lookbackRight] < ta.valuewhen(plFound, low[lookbackRight], 1) bullCondAlert = priceLL and rsiHL and plFound bullCond = showDivergence and bullCondAlert plotshape( bullCond ? rsi[lookbackRight] : na, offset=-lookbackRight, title="Regular Bullish Label", text="D", style=shape.labelup, location=location.bottom, color=bullColor, textcolor=textColor ) //------------------------------------------------------------------------------ // Regular Bearish // rsi: Lower High rsiLH = rsi[lookbackRight] < ta.valuewhen(phFound, rsi[lookbackRight], 1) and _inRange(phFound[1]) // Price: Higher High priceHH = high[lookbackRight] > ta.valuewhen(phFound, high[lookbackRight], 1) bearCondAlert = priceHH and rsiLH and phFound bearCond = showDivergence and bearCondAlert plotshape( bearCond ? rsi[lookbackRight] : na, offset=-lookbackRight, title="Regular Bearish Label", text=" D ", style=shape.labeldown, location=location.top, color=bearColor, textcolor=textColor )