Indicator · Pine Script v4 · Own pane

RSI [SystemAlpha]

by iamc1oud· code credited to systemalphatrader

This indicator plots a standard Relative Strength Index (RSI) in a separate pane alongside customizable overbought and oversold threshold levels with shaded fills.

View on GitHub

Repaints

GitHub stars56on the repository
Pine versionv4older, still runs
Lines of code250
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last update2 months ago20 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

250 lines of Pine Script v4 from rsi-indicator.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

rsi-indicator.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © systemalphatrader

//@version=4

study(title='RSI [SystemAlpha]', shorttitle='RSI [SA]', format=format.price)

len         = input(title="RSI Period", minval=1, defval=14)
src         = input(title="RSI Source", defval=close)
lbR         = input(title="Pivot Lookback Right", defval=5)
lbL         = input(title="Pivot Lookback Left", defval=5)
ob          = input(70, minval=0, maxval = 100, title="Overbought")
os          = input(30, minval=0, maxval = 100, title="Oversold")
showsignals = input(true, title="Show Buy/Sell Signals")

//Divergence Input
divtype         = input(title="Divergence Type", defval='Regular', options=['Regular', 'Hidden', 'Both', 'None'])
plotBull        = divtype == 'Regular' or divtype == 'Both'
plotHiddenBull  = divtype == 'Hidden' or divtype == 'Both'
plotBear        = divtype == 'Regular' or divtype == 'Both'
plotHiddenBear  = divtype == 'Hidden' or divtype == 'Both'
showlabel       = input(true, title="Show Divergence Label")
showpivot       = input(false, title="Show Pivot Points")
rangeUpper      = input(title="Max of Lookback Range", defval=60)
rangeLower      = input(title="Min of Lookback Range", defval=5)
chcut           = true //input(true, title = "Check Cut-Through!")

bearColor       = color.red
bullColor       = color.green
hiddenBullColor = color.new(color.green, 50)
hiddenBearColor = color.new(color.red, 50)
textColor       = color.white
noneColor       = color.new(color.white, 100)

osc = rsi(src, len)

plot(osc, title="RSI", linewidth=1, color=color.blue)
hline(50, title="Centerline", linestyle=hline.style_dashed,color=color.gray)

lineBuyBottom   = plot(os, color=color.gray, title="Oversold Level")
lineSellTop     = plot(ob, color=color.gray, title="Overbought Level")

lineBuy     = plot(series=(osc < os ? osc : os), transp=100, title="n/a", editable=false)
lineSell    = plot(series=(osc > ob ? osc : ob), transp=100, title="n/a", editable=false)

fill(lineSellTop, lineBuyBottom, title="Background", color=color.gray, transp=80)
fill(plot1=lineBuy, plot2=lineBuyBottom, color=color.red, transp=50, title="Oversold Area")
fill(plot1=lineSellTop, plot2=lineSell, color=color.green, transp=50, title="Overbought Area")

float top = na
float bot = na
top := pivothigh(osc, lbL, lbR)
bot := pivotlow(osc, lbL, lbR)

phFound = na(top) ? false : true
plFound = na(bot) ? false : true

_inRange(cond) =>
    bars = barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

alertcondition(osc[1] > ob and osc[2] < ob, title="RSI value crosses over overbought level", message="Check charts for a RSI cross over overbought level")
alertcondition(osc[1] < ob and osc[2] > ob, title="RSI value crosses under overbought level", message="Check charts for a RSI cross under overbought level")
alertcondition(osc[1] > os and osc[2] < os, title="RSI value crosses over oversold level", message="Check charts for a RSI cross over oversold level")
alertcondition(osc[1] < os and osc[2] > os, title="RSI value crosses under oversold level", message="Check charts for a RSI cross under oversold level")

//No Cut cut-through
topc = 0, botc = 0
topc := top ? lbL : nz(topc[1]) + 1
botc := bot ? lbL : nz(botc[1]) + 1

// check cut-through in indicators
nocut1(indi, len)=>
    _ret = true
    diff = (indi - nz(indi[len])) / len
    ln = indi - diff
    for x = 1 to len -1
        if nz(indi[x]) > ln
            _ret := false
            break
        ln := ln - diff
    _ret

topok = nocut1(osc, topc)

// check cut-through in indicators
nocut2(indi, len)=>
    _ret = true
    diff = (indi - nz(indi[len])) / len
    ln = indi - diff
    for x = 1 to len -1
        if nz(indi[x]) < ln
            _ret := false
            break
        ln := ln - diff
    _ret

botok = nocut2(osc, botc)

//Show Pivot

plotshape(showpivot and phFound and (not chcut or topok) ? osc[lbR] : na,title="Pivot High",text="[PH]",style=shape.labeldown,color=color.white,textcolor=color.black,location=location.absolute,transp=0,offset = -lbR)
plotshape(showpivot and plFound and (not chcut or botok) ? osc[lbR] : na,title="Pivot Low",text="[PL]",style=shape.labeldown,color=color.white,textcolor=color.black,location=location.absolute,transp=0,offset = -lbR)

//------------------------------------------------------------------------------
// Regular Bullish

// Osc: Higher Low
oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low
priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)

bullCond = plotBull and priceLL and oscHL and plFound

plot(
     plFound ? osc[lbR] : na,
     offset=-lbR,
     title="Regular Bullish",
     linewidth=1,
     color=(bullCond ? bullColor : noneColor),
     transp=0
     )

plotshape(
     bullCond and showlabel ? osc[lbR] : na,
     offset=-lbR,
     title="Regular Bullish Label",
     text="R",
     style=shape.labelup,
     location=location.absolute,
     color=bullColor,
     textcolor=textColor,
     transp=0
     )

alertcondition(bullCond, title="Regular bullish divergence in RSI found", message="Check charts for a regular bullish divergence found with RSI")

//------------------------------------------------------------------------------
// Hidden Bullish

// Osc: Lower Low
oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low
priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)

hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

plot(
     plFound ? osc[lbR] : na,
     offset=-lbR,
     title="Hidden Bullish",
     linewidth=1,
     color=(hiddenBullCond ? hiddenBullColor : noneColor),
     transp=50
     )
     
plotshape(
     hiddenBullCond and showlabel ? osc[lbR] : na,
     offset=-lbR,
     title="Hidden Bullish Label",
     text="H",
     style=shape.labelup,
     location=location.absolute,
     color=bullColor,
     textcolor=textColor,
     transp=50
     )

alertcondition(hiddenBullCond, title="Hidden bullish divergence in RSI found", message="Check charts for a hidden bullish divergence found with RSI")

//------------------------------------------------------------------------------
// Regular Bearish

// Osc: Lower High
oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Higher High
priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(
     phFound ? osc[lbR] : na,
     offset=-lbR,
     title="Regular Bearish",
     linewidth=1,
     color=(bearCond ? bearColor : noneColor),
     transp=0
     )

plotshape(
     bearCond and showlabel ? osc[lbR] : na,
     offset=-lbR,
     title="Regular Bearish Label",
     text="R",
     style=shape.labeldown,
     location=location.absolute,
     color=bearColor,
     textcolor=textColor,
     transp=0
     )

alertcondition(bearCond, title="Regular bearish divergence in RSI found", message="Check charts for a regular bearish divergence found with RSI")

//------------------------------------------------------------------------------
// Hidden Bearish

// Osc: Higher High
oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Lower High
priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)

hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound

plot(
     phFound ? osc[lbR] : na,
     offset=-lbR,
     title="Hidden Bearish",
     linewidth=1,
     color=(hiddenBearCond ? hiddenBearColor : noneColor),
     transp=50
     )

plotshape(
     hiddenBearCond and showlabel ? osc[lbR] : na,
     offset=-lbR,
     title="Hidden Bearish Label",
     text="H",
     style=shape.labeldown,
     location=location.absolute,
     color=bearColor,
     textcolor=textColor,
     transp=50
     )

alertcondition(hiddenBearCond, title="Hidden bearish divergence in RSI found", message="Check charts for a hidden bearish divergence found with RSI")

// Buy/Sell Signals

// use curvature information to filter out some false positives
sellsignals = showsignals and phFound and osc[lbR] and osc[lbR] > ob
buysignals  = showsignals and plFound and osc[lbR] and osc[lbR] < os

plot(buysignals ? osc[lbR]: na, color = color.white , style = plot.style_circles, linewidth = 3, title="Buy Signal Outline", offset=-lbR)
plot(buysignals ? osc[lbR]: na, style=plot.style_circles, color= color.green, linewidth=2, title="Buy Signal", transp=0, offset=-lbR)
plot(sellsignals ? osc[lbR]: na, color = color.white , style = plot.style_circles, linewidth = 3, title="Sell Signal Outline", offset=-lbR)
plot(sellsignals ? osc[lbR]: na, style=plot.style_circles, color= color.red, linewidth=2, title="Sell Signal", transp=0, offset=-lbR)

Overview

RSI [SystemAlpha] identifies pivot highs and lows on the oscillator to detect regular and hidden bullish and bearish divergences against price movements. Users can configure the RSI length, source, pivot lookback periods, threshold levels, and lookback range for divergence calculations. Because pivot detection relies on right lookback bars, divergence labels and signals appear with a slight delay.

Details

Published by

iamc1oud on GitHub

Code credit

systemalphatrader, as written in the script's header

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum, Divergence

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

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

SettingTypeDefaultVariable
RSI Periodint14len
RSI Sourcesourceclosesrc
Pivot Lookback Rightint5lbR
Pivot Lookback Leftint5lbL
Overboughtint70ob
Oversoldint30os
Show Buy/Sell Signalsbooltrueshowsignals
Divergence TypestringRegulardivtype
Show Divergence Labelbooltrueshowlabel
Show Pivot Pointsboolfalseshowpivot
Max of Lookback Rangeint60rangeUpper
Min of Lookback Rangeint5rangeLower
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
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
53 stars

The script calculates the Relative Strength Index (RSI) alongside customizable overbought and oversold thresholds. It tracks recent high and low values over a user-defined lookback period to detect price and momentum divergence. Bullish and bearish divergences

Indicator v6 Oscillators & momentumDivergence
Updated 2 months ago · MPL-2.0 GitHub repository of RSI Divergence [Ahmed Edition]

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