Indicator · Pine Script v6 · Own pane

RSI Divergence

by paulieb89

This indicator plots RSI in a separate pane and marks regular divergences between confirmed price pivots and RSI pivots.

View on GitHub

GitHub stars9on the repository
Pine versionv6current
Lines of code119
Settings9inputs
LicenceMIT
Last update2 months ago27 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

119 lines of Pine Script v6 from plugin/skills/pine/assets/rsi-divergence.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

rsi-divergence.pine
//@version=6
// RSI Divergence Indicator — quality-anchor example for the `pine` skill.
// Detects regular bullish and bearish divergences between price and RSI by
// comparing successive pivot lows / highs within a configurable lookback range.
indicator(
     title="RSI Divergence (v6 reference)",
     shorttitle="RSI Div",
     overlay=false,
     format=format.price,
     precision=2,
     max_labels_count=500,
     max_lines_count=500)

// ─── Inputs ──────────────────────────────────────────────────────────────────
rsi_length    = input.int(14,  title="RSI length",          minval=2,  maxval=200)
rsi_src       = input.source(close, title="RSI source")
pivot_left    = input.int(5,   title="Pivot lookback left",  minval=1,  maxval=50)
pivot_right   = input.int(5,   title="Pivot lookback right", minval=1,  maxval=50)
range_min     = input.int(5,   title="Min bars between pivots", minval=1,  maxval=100)
range_max     = input.int(60,  title="Max bars between pivots", minval=5,  maxval=500)
rsi_overbought = input.int(70, title="RSI overbought",       minval=50, maxval=100)
rsi_oversold   = input.int(30, title="RSI oversold",         minval=0,  maxval=50)
show_labels   = input.bool(true, title="Show divergence labels")

// ─── Core calculation ────────────────────────────────────────────────────────
rsi = ta.rsi(rsi_src, rsi_length)

// Confirmed pivot levels for both RSI and price.
// A confirmed pivot exists `pivot_right` bars back when it is found.
rsi_pl   = ta.pivotlow(rsi,   pivot_left, pivot_right)
rsi_ph   = ta.pivothigh(rsi,  pivot_left, pivot_right)
price_pl = ta.pivotlow(low,   pivot_left, pivot_right)
price_ph = ta.pivothigh(high, pivot_left, pivot_right)

// Track the most recent confirmed pivots and their bar indices.
var float last_rsi_pl_val      = na
var int   last_rsi_pl_bar      = na
var float last_rsi_ph_val      = na
var int   last_rsi_ph_bar      = na
var float last_price_pl_val    = na
var float last_price_ph_val    = na

bull_div = false
bear_div = false

// ─── Bullish divergence: price lower-low, RSI higher-low ─────────────────────
if not na(rsi_pl) and not na(price_pl)
    bars_since_prev = na(last_rsi_pl_bar) ? na : (bar_index - pivot_right) - last_rsi_pl_bar
    in_range = not na(bars_since_prev) and bars_since_prev >= range_min and bars_since_prev <= range_max
    if in_range
        price_lower_low = price_pl < last_price_pl_val
        rsi_higher_low  = rsi_pl   > last_rsi_pl_val
        oversold_zone   = last_rsi_pl_val < rsi_oversold or rsi_pl < rsi_oversold
        bull_div := price_lower_low and rsi_higher_low and oversold_zone
    // Update trackers regardless of divergence outcome.
    last_rsi_pl_val   := rsi_pl
    last_rsi_pl_bar   := bar_index - pivot_right
    last_price_pl_val := price_pl

// ─── Bearish divergence: price higher-high, RSI lower-high ───────────────────
if not na(rsi_ph) and not na(price_ph)
    bars_since_prev = na(last_rsi_ph_bar) ? na : (bar_index - pivot_right) - last_rsi_ph_bar
    in_range = not na(bars_since_prev) and bars_since_prev >= range_min and bars_since_prev <= range_max
    if in_range
        price_higher_high = price_ph > last_price_ph_val
        rsi_lower_high    = rsi_ph   < last_rsi_ph_val
        overbought_zone   = last_rsi_ph_val > rsi_overbought or rsi_ph > rsi_overbought
        bear_div := price_higher_high and rsi_lower_high and overbought_zone
    last_rsi_ph_val   := rsi_ph
    last_rsi_ph_bar   := bar_index - pivot_right
    last_price_ph_val := price_ph

// ─── Plots ───────────────────────────────────────────────────────────────────
plot(rsi, title="RSI", color=color.new(color.purple, 0), linewidth=2)
hline(rsi_overbought, title="Overbought", color=color.new(color.red,   60), linestyle=hline.style_dashed)
hline(50,             title="Midline",    color=color.new(color.gray,  70), linestyle=hline.style_dotted)
hline(rsi_oversold,   title="Oversold",   color=color.new(color.green, 60), linestyle=hline.style_dashed)

// Divergence markers on the RSI pane, offset back to the pivot bar.
plotshape(bull_div, title="Bullish divergence",
     style=shape.triangleup,
     location=location.bottom,
     color=color.new(color.green, 0),
     size=size.small,
     offset=-pivot_right)
plotshape(bear_div, title="Bearish divergence",
     style=shape.triangledown,
     location=location.top,
     color=color.new(color.red, 0),
     size=size.small,
     offset=-pivot_right)

if show_labels and bull_div
    label.new(
         x=bar_index - pivot_right,
         y=rsi_pl,
         text="Bull",
         style=label.style_label_up,
         color=color.new(color.green, 20),
         textcolor=color.white,
         size=size.small)

if show_labels and bear_div
    label.new(
         x=bar_index - pivot_right,
         y=rsi_ph,
         text="Bear",
         style=label.style_label_down,
         color=color.new(color.red, 20),
         textcolor=color.white,
         size=size.small)

// ─── Alerts ──────────────────────────────────────────────────────────────────
alertcondition(bull_div and barstate.isconfirmed,
     title="RSI Bullish Divergence",
     message="Bullish RSI divergence confirmed on {{ticker}} ({{interval}})")
alertcondition(bear_div and barstate.isconfirmed,
     title="RSI Bearish Divergence",
     message="Bearish RSI divergence confirmed on {{ticker}} ({{interval}})")

Overview

A bullish signal requires a lower price low, a higher RSI low, and an RSI reading in the oversold zone, within a set bar gap. Bearish detection compares a higher price high with a lower RSI high. RSI length and source, left and right pivot lookback, the bar range, overbought and oversold levels, and optional labels control the display. Pivots confirm only after the right lookback, so signals are delayed. It does not trade.

Details

Published by

paulieb89 on GitHub

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

ai-skillsclaudemcpmcp-serverpinescripttradingtradingview

Settings

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

SettingTypeDefaultVariable
RSI lengthint14rsi_length
RSI sourcesourceclosersi_src
Pivot lookback leftint5pivot_left
Pivot lookback rightint5pivot_right
Min bars between pivotsint5range_min
Max bars between pivotsint60range_max
RSI overboughtint70rsi_overbought
RSI oversoldint30rsi_oversold
Show divergence labelsbooltrueshow_labels
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