Indicator · Pine Script v6 · Own pane

RSI Divergence [Ahmed Edition]

by Ahmed-GoCode· code credited to Ahmed - Quant-Edge Indicators

The script calculates the Relative Strength Index (RSI) alongside customizable overbought and oversold thresholds.

View on GitHub

GitHub stars53on the repository
Pine versionv6current
Lines of code111
Settings9inputs
LicenceMPL-2.0
Last update2 months ago17 Aug 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

111 lines of Pine Script v6 from Quant-Edge-Indicators/RSI & Momentum/RSI Divergences.pinescript, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

RSI Divergences.pinescript
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Ahmed - Quant-Edge Indicators
//@version=6
indicator("RSI Divergence [Ahmed Edition]", shorttitle="RSI Div A", overlay=false, max_labels_count=500)

// ================== INPUTS ==================
len     = input.int(14, minval=1, title="RSI Length")
ob      = input.int(70, title="Overbought", minval=0, maxval=100)
os      = input.int(30, title="Oversold", minval=0, maxval=100)
omid    = input.int(50, title="Midline Value", minval=0, maxval=100)
hideMid = input.bool(false, title="Hide Midline")

lookback = input.int(90, title="Lookback Period (bars)", minval=1)
showPiv  = input.bool(true, title="Show Pivots?")
shortLbl = input.bool(false, title="Short Labels?")
hideLbls = input.bool(false, title="Hide Labels & Use Background")

// ================== RSI ==================
rsi = ta.rsi(close, len)

// Levels
hOB   = hline(ob, "Overbought", color=color.red)
hOS   = hline(os, "Oversold", color=color.green)
hMid  = hline(omid, "Midline", color=color.new(color.gray, hideMid ? 100 : 70))

plot(rsi, color=(rsi > ob or rsi < os) ? color.red : color.black)
fill(hMid, hOB, color=color.new(color.lime, 92))
fill(hOS, hMid, color=color.new(color.red, 92))

// ================== PIVOTS ==================
highestBar = ta.highestbars(rsi, lookback)
lowestBar  = ta.lowestbars(rsi, lookback)

var float maxClose = na
var float maxRSI   = na
var float minClose = na
var float minRSI   = na

// Update max/min
if highestBar == 0
    maxClose := close
    maxRSI   := rsi
else
    maxClose := nz(maxClose[1], close)
    maxRSI   := nz(maxRSI[1], rsi)

if lowestBar == 0
    minClose := close
    minRSI   := rsi
else
    minClose := nz(minClose[1], close)
    minRSI   := nz(minRSI[1], rsi)

// Pivot conditions
pivHigh = (maxRSI == maxRSI[2] and maxRSI[2] != maxRSI[3])
pivLow  = (minRSI == minRSI[2] and minRSI[2] != minRSI[3])

// ================== DIVERGENCES ==================
bearDiv = close[1] > close[2] and rsi[1] < maxRSI and rsi <= rsi[1]
bullDiv = close[1] < close[2] and rsi[1] > minRSI and rsi >= rsi[1]

// ================== ALERTS ==================
alertcondition(bearDiv, title="Bearish Divergence", message="Bear Div")
alertcondition(bullDiv, title="Bullish Divergence", message="Bull Div")
alertcondition(pivHigh, title="Pivot High", message="Pivot High")
alertcondition(pivLow, title="Pivot Low", message="Pivot Low")

// Extra Alerts (Enter/Exit)
bearExit  = ta.crossunder(rsi, os)   // RSI drops below oversold
bullExit  = ta.crossover(rsi, ob)    // RSI breaks above overbought
bearEnter = ta.crossunder(rsi, ob)   // RSI falls below OB
bullEnter = ta.crossover(rsi, os)    // RSI rises above OS

alertcondition(bearExit,  title="Bear Exit",  message="Bear Exit")
alertcondition(bullExit,  title="Bull Exit",  message="Bull Exit")
alertcondition(bearEnter, title="Bear Enter", message="Bear Enter")
alertcondition(bullEnter, title="Bull Enter", message="Bull Enter")

// ================== LABELS ==================
if bearDiv and not hideLbls
    label.new(bar_index - 1, rsi[1] + 1,
        shortLbl ? "▼" : "BEAR",
        color=color.red, textcolor=color.white,
        style=label.style_label_down, yloc=yloc.price, size=size.small)

if bullDiv and not hideLbls
    label.new(bar_index - 1, rsi[1] - 1,
        shortLbl ? "▲" : "BULL",
        color=color.green, textcolor=color.white,
        style=label.style_label_up, yloc=yloc.price, size=size.small)

if showPiv and pivHigh and not hideLbls
    label.new(bar_index - 2, maxRSI + 1,
        shortLbl ? "P" : "PIVOT",
        color=color.blue, textcolor=color.white,
        style=label.style_label_down, yloc=yloc.price, size=size.tiny)

if showPiv and pivLow and not hideLbls
    label.new(bar_index - 2, minRSI - 1,
        shortLbl ? "P" : "PIVOT",
        color=color.blue, textcolor=color.white,
        style=label.style_label_up, yloc=yloc.price, size=size.tiny)

// ================== BACKGROUND COLORING ==================
bgcolor(
     hideLbls ?
     bearDiv ? color.new(color.red, 85) :
     bullDiv ? color.new(color.green, 85) :
     (showPiv and (pivHigh or pivLow) ? color.new(color.blue, 85) : na)
     : na, transp=90
)

Overview

RSI Divergence [Ahmed Edition] tracks recent high and low values over a user-defined lookback period to detect price and momentum divergence. Bullish and bearish divergences are highlighted directly on the oscillator pane with custom labels. Users can adjust the RSI length, lookback period, level thresholds, and label display styles. Built-in alert conditions trigger on divergence signals, pivot detection, and key level crossovers.

Details

Published by

Ahmed-GoCode on GitHub

Code credit

Ahmed - Quant-Edge Indicators, as written in the script's header

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum, Divergence

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

fair-value-gapfibonaccimarket-structurepine-scriptrsi-strategysmart-money-conceptstechnical-analysistradingviewalgorithmic-tradingcrypto-trading

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
RSI Lengthint14len
Overboughtint70ob
Oversoldint30os
Midline Valueint50omid
Hide MidlineboolfalsehideMid
Lookback Period (bars)int90lookback
Show Pivots?booltrueshowPiv
Short Labels?boolfalseshortLbl
Hide Labels & Use BackgroundboolfalsehideLbls
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
330 stars

This indicator calculates the MACD oscillator from a configurable source with fast and slow lengths, then plots the histogram, MACD line, and signal line in a separate pane. It detects regular bullish divergences (price lower lows with oscillator higher lows),

Indicator v5 Oscillators & momentumDivergence
Updated 2 months ago · None stated GitHub repository of MACD + Divergences [Alorse]
218 stars

This indicator calculates the Relative Strength Index (RSI) across four user-selected timeframes and plots the results in a separate pane. It identifies regular bullish and bearish divergences between price pivot points and the primary timeframe RSI value. Add

Indicator v6 Oscillators & momentumDivergence
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe RSI with Divergence

Bubbles Indicator

by capissimo
114 stars

The Bubbles Indicator measures volatility squeezes and expansion bursts using Bollinger Bands combined with a sinusoid oscillator and RSI divergence. It processes price data through scaled Bollinger Bands to highlight volatility changes while confirming trend

Indicator v4 Volatility & bandsOscillators & momentum Repaints
Updated 1 year ago · None stated GitHub repository of Bubbles Indicator

RSI [SystemAlpha]

by iamc1oud
56 stars

This indicator plots a standard Relative Strength Index (RSI) in a separate pane alongside customizable overbought and oversold threshold levels with shaded fills. It identifies pivot highs and lows on the oscillator to detect regular and hidden bullish and be

Indicator v4 Oscillators & momentumDivergence Repaints
Updated 2 months ago · None stated GitHub repository of RSI [SystemAlpha]
55 stars

This indicator calculates a 14-period RSI from close and plots it in a separate pane with dotted overbought and dashed oversold lines at 70 and 30. It marks bullish divergence when a five-bar pivot low in price is lower than the price twenty bars earlier while

Indicator v6 Oscillators & momentumDivergence
Updated 6 months ago · Apache-2.0 GitHub repository of RSI Divergence Detector

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers