# code rsi tradingview divergence

By [Limpaca](https://paragraph.com/@royes) · 2023-05-09

---

[https://www.tradingview.com/script/Z1q3OQRE-RSI-Divergence-longdh/](https://www.tradingview.com/script/Z1q3OQRE-RSI-Divergence-longdh/)

// @version=5

indicator(title='RSI Divergence',shorttitle='RSI Div', overlay=false, timeframe='')

// {FUNCTION ma(source, length, type) => switch type "SMA" => ta.sma(source, length) "Bollinger Bands" => ta.sma(source, length) "EMA" => ta.ema(source, length) "SMMA (RMA)" => ta.rma(source, length) "WMA" => ta.wma(source, length) "VWMA" => ta.vwma(source, length) //}

//{USER MENU rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings") rsiSourceInput = input.source(close, "Source", group="RSI Settings") maTypeInput = input.string("SMA", title="MA Type", options=\["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"\], group="MA Settings") maLengthInput = input.int(14, title="MA Length", group="MA Settings") bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings") //}

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) // isBB = maTypeInput == "Bollinger Bands"

// {DIVERGENCE INPUT lbR = 5,lbL = 5, rangeUpper = 60, rangeLower = 5 plotBull = input(title='Plot Bullish', defval=true,group="Toggle Divergence Plotting"), plotBear = input(title='Plot Bearish', defval=true) plotHiddenBull = input(title='Plot Hidden Bullish', defval=true), plotHiddenBear = input(title='Plot Hidden Bearish', defval=true) // }

osc = rsi plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true,phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true

// {RSI TREND IDENTIFICATION \_inRange(cond) => bars = ta.barssince(cond == true) rangeLower <= bars and bars <= rangeUpper

oscHL = osc\[lbR\] > ta.valuewhen(plFound, osc\[lbR\], 1) and \_inRange(plFound\[1\]), oscLL = osc\[lbR\] < ta.valuewhen(plFound, osc\[lbR\], 1) and \_inRange(plFound\[1\]) oscHH = osc\[lbR\] > ta.valuewhen(phFound, osc\[lbR\], 1) and \_inRange(phFound\[1\]),oscLH = osc\[lbR\] < ta.valuewhen(phFound, osc\[lbR\], 1) and \_inRange(phFound\[1\])

// }

// {PRICE TREND IDENTIFICATION priceLL = low\[lbR\] < ta.valuewhen(plFound, low\[lbR\], 1), priceHL = low\[lbR\] > ta.valuewhen(plFound, low\[lbR\], 1) priceHH = high\[lbR\] > ta.valuewhen(phFound, high\[lbR\], 1),priceLH = high\[lbR\] < ta.valuewhen(phFound, high\[lbR\], 1) //}

//{DIVERGENCE IDENTIFICATION bullCond = plotBull and priceLL and oscHL and plFound, hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound bearCond = plotBear and priceHH and oscLH and phFound, hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound // }

// { COLOR ASSIGNMENT bearColor = color.red, bullColor = color.green, hiddenBullColor = color.new(color.aqua, 0), hiddenBearColor = color.new(color.orange, 0) textColor = color.white,noneColor = color.new(color.white, 100) //}

// { PLOTSplot(osc, title='RSI', linewidth=2, color=color.rgb(255, 189, 68)) plot(rsiMA, "RSI-based MA",linewidth=1, color=color.purple) rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86) hline(50, "RSI Middle Band", color=color.new(#787B86, 50)) rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86) // fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")

rsi80 = hline(80, "RSI80", color=#787B86) rsi75 = hline(75, "RSI75", color=#787B86) rsi65 = hline(65, "RSI65", color=#787B86) rsi60 = hline(60, "RSI60", color=#787B86) rsi40 = hline(40, "RSI40", color=#787B86) rsi35 = hline(35, "RSI35", color=#787B86) rsi25 = hline(25, "RSI30", color=#787B86) rsi20 = hline(20, "RSI20", color=#787B86) fill(rsi75, rsi80, color=color.rgb(255, 255, 255, 35), title="RSI 75-80") fill(rsi60, rsi65, color=color.rgb(255, 255, 255, 75), title="RSI 60-65") fill(rsi35, rsi40, color=color.rgb(255, 255, 255, 75), title="RSI 35-40") fill(rsi20, rsi25, color=color.rgb(255, 255, 255, 35), title="RSI 20-25") fill(rsi40, rsi60, color=color.rgb(150, 88, 179, 73), title="RSI 20-25")

// bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) \* bbMultInput : na, title = "Upper Bollinger Band", color=color.green) // bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) \* bbMultInput : na, title = "Lower Bollinger Band", color=color.green) // fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill") plot(plFound ? osc\[lbR\] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=bullCond ? bullColor : noneColor, transp=0) plot(plFound ? osc\[lbR\] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=hiddenBullCond ? hiddenBullColor : noneColor, transp=0) plot(phFound ? osc\[lbR\] : na, offset=-lbR, title='Regular Bearish', linewidth=2, color=bearCond ? bearColor : noneColor, transp=0) plot(phFound ? osc\[lbR\] : na, offset=-lbR, title='Hidden Bearish', linewidth=2, color=hiddenBearCond ? hiddenBearColor : noneColor, transp=0)

cross\_dnema = ta.crossunder(50, rsiMA) cross\_upema = ta.crossunder(rsiMA, 50) plotshape(cross\_upema, style=shape.circle, color=color.green, size=size.small, location=location.bottom) plotshape(cross\_dnema, style=shape.circle, color=color.red, size=size.small, location=location.top)

cross\_dn = ta.crossunder(rsi, rsiMA) and rsi>50 and rsi <70 and math.max(rsi\[2\],rsi\[3\],rsi\[4\],rsi\[5\],rsi\[6\])>70 cross\_up = ta.crossunder(rsiMA, rsi) and rsi<50 and rsi >30 and math.min(rsi\[2\],rsi\[3\],rsi\[4\],rsi\[5\],rsi\[6\])<30 plotshape(cross\_up, style=shape.cross, color=color.green, size=size.small, location=location.bottom) plotshape(cross\_dn, style=shape.cross, color=color.red, size=size.small, location=location.top)

---

*Originally published on [Limpaca](https://paragraph.com/@royes/code-rsi-tradingview-divergence)*
