Indicator · Pine Script v6 · Own pane

RSI Divergence

by rubbermetal· code credited to claygrace74

RSI Divergence subtracts a slow RSI from a fast RSI and plots the difference as a four-color histogram in a separate pane.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code88
Settings9inputs
LicenceMPL-2.0
Last update4 months ago10 Jun 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

88 lines of Pine Script v6 from RSI Divergence.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

RSI Divergence.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © claygrace74

//@version=6
indicator(title='RSI Divergence', shorttitle='RSI Div')

// ==================== RSI Settings ====================
var rsi_group = "RSI Settings"
rsi_fast_src = input.source(close, "Fast RSI Source", group=rsi_group)
rsi_fast_len = input.int(5, "Fast RSI Length", minval=1, group=rsi_group)
rsi_slow_src = input.source(close, "Slow RSI Source", group=rsi_group)
rsi_slow_len = input.int(14, "Slow RSI Length", minval=1, group=rsi_group)

// ==================== Display Settings ====================
var disp_group = "Display Settings"
i_showFast     = input.bool(false, "Show Fast RSI Line", group=disp_group)
i_showSlow     = input.bool(false, "Show Slow RSI Line", group=disp_group)
i_showSignals  = input.bool(true, "Show Divergence Shift Signals", group=disp_group)
bullDivClr     = input.color(color.rgb(0, 238, 71), "Bullish Divergence Color", group=disp_group)
bearDivClr     = input.color(color.rgb(255, 8, 0), "Bearish Divergence Color", group=disp_group)

// ==================================================================================
// RSI Calculations (manual for fast/slow independence)
// ==================================================================================
rsi_up_fast   = ta.rma(math.max(ta.change(rsi_fast_src), 0), rsi_fast_len)
rsi_down_fast = ta.rma(-math.min(ta.change(rsi_fast_src), 0), rsi_fast_len)
rsi_fast      = rsi_down_fast == 0 ? 100 : rsi_up_fast == 0 ? 0 : 100 - 100 / (1 + rsi_up_fast / rsi_down_fast)

rsi_up_slow   = ta.rma(math.max(ta.change(rsi_slow_src), 0), rsi_slow_len)
rsi_down_slow = ta.rma(-math.min(ta.change(rsi_slow_src), 0), rsi_slow_len)
rsi_slow      = rsi_down_slow == 0 ? 100 : rsi_up_slow == 0 ? 0 : 100 - 100 / (1 + rsi_up_slow / rsi_down_slow)

// Divergence: fast RSI minus slow RSI
divergence = rsi_fast - rsi_slow
isBullish  = divergence > 0
isBearish  = divergence < 0

// Shift detection — first bar where divergence changes sign
bullShift = ta.crossover(divergence, 0)
bearShift = ta.crossunder(divergence, 0)

// Momentum — is divergence getting stronger or weaker
divAccel     = ta.change(divergence)
growingBull  = isBullish and divAccel > 0
shrinkBull   = isBullish and divAccel <= 0
growingBear  = isBearish and divAccel < 0
shrinkBear   = isBearish and divAccel >= 0

// 4-color histogram
histColor = growingBull ? bullDivClr : shrinkBull ? color.new(bullDivClr, 50) : growingBear ? bearDivClr : color.new(bearDivClr, 50)

// ==================================================================================
// Grid Layout
// ==================================================================================
h70 = hline(70, "OB", color=color.new(color.red, 60), linestyle=hline.style_dashed)
h30 = hline(30, "OS", color=color.new(color.green, 60), linestyle=hline.style_dashed)
h50 = hline(50, "Mid", color=color.new(color.gray, 60), linestyle=hline.style_dotted)
hZero = hline(0, "Zero", color=color.new(color.gray, 50), linestyle=hline.style_solid)

// ==================================================================================
// Plots
// ==================================================================================

// Divergence histogram — the core visual
plot(divergence, title='Divergence', linewidth=2, style=plot.style_columns, color=histColor, histbase=0)

// Zero line emphasis
plot(0, title="Zero Ref", color=color.new(color.white, 70), linewidth=1)

// Optional fast/slow RSI lines
plot(i_showFast ? rsi_fast : na, title='Fast RSI', linewidth=1, color=color.rgb(255, 152, 36))
plot(i_showSlow ? rsi_slow : na, title='Slow RSI', linewidth=1, color=color.rgb(44, 29, 255))

// Divergence shift signals
plotshape(i_showSignals and bullShift, style=shape.triangleup,   location=location.bottom, color=bullDivClr, text="Bull", textcolor=bullDivClr, size=size.tiny, title="Bullish Shift")
plotshape(i_showSignals and bearShift, style=shape.triangledown, location=location.top,    color=bearDivClr, text="Bear", textcolor=bearDivClr, size=size.tiny, title="Bearish Shift")

// Background tint when divergence is strong
bgcolor(math.abs(divergence) > 10 ? (isBullish ? color.new(bullDivClr, 92) : color.new(bearDivClr, 92)) : na, title="Strong Divergence BG")

// ==================================================================================
// Alert Conditions
// ==================================================================================
alertcondition(bullShift,                    "Bullish Divergence Shift",  "Fast RSI crossed above slow RSI — bullish momentum shift")
alertcondition(bearShift,                    "Bearish Divergence Shift",  "Fast RSI crossed below slow RSI — bearish momentum shift")
alertcondition(growingBull,                  "Bullish Divergence Growing","Bullish divergence is accelerating")
alertcondition(growingBear,                  "Bearish Divergence Growing","Bearish divergence is accelerating")
alertcondition(math.abs(divergence) > 15,    "Extreme Divergence",       "RSI divergence exceeded 15 — strong momentum imbalance")

Overview

Color shows whether the gap is positive or negative and whether it is growing or shrinking. Markers appear on the bar where the gap crosses zero, and the fast and slow RSI lines can be shown. This is a fast-versus-slow RSI comparison, not a price divergence detector. Lengths and sources are adjustable.

Details

Published by

rubbermetal on GitHub

Code credit

claygrace74, 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.

Settings

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

SettingTypeDefaultVariable
Fast RSI Sourcesourceclosersi_fast_src
Fast RSI Lengthint5rsi_fast_len
Slow RSI Sourcesourceclosersi_slow_src
Slow RSI Lengthint14rsi_slow_len
Show Fast RSI Lineboolfalsei_showFast
Show Slow RSI Lineboolfalsei_showSlow
Show Divergence Shift Signalsbooltruei_showSignals
Bullish Divergence Colorcolorcolor.rgb(0, 238, 71)bullDivClr
Bearish Divergence Colorcolorcolor.rgb(255, 8, 0)bearDivClr
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