Indicator · Pine Script v6 · Own pane

Relative Strength Index

by SiegerTerpstra· code credited to sajjetti

This indicator plots the Relative Strength Index, a momentum oscillator that measures the speed and magnitude of price changes using Wilder's RMA of gains and losses.

View on GitHub

GitHub stars5on the repository
Pine versionv6current
Lines of code151
Settings7inputs
LicenceNone statedfile: MPL-2.0
Last update9 months ago14 Jan 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

151 lines of Pine Script v6 from 4 - RSI (v6).pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

4 - RSI (v6).pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sajjetti

//@version=6

//-------------------------------------

// Release:                     2020-08-05
// Update:                      2026-01-04
// Author:                      sajjetti
// Email:                       contact@sajjetti.ai
// Website:                     sajjetti.ai
// Profile:                     https://www.tradingview.com/u/stmd/
// Pineversion:                 6
// Filename:                    4 - RSI (v6).pine

//-------------------------------------
// PURPOSE
// Relative Strength Index (RSI) - a momentum oscillator measuring the speed
// and magnitude of price changes. Developed by J. Welles Wilder Jr. in 1978.

//-------------------------------------
// FEATURES
// - Multi-timeframe support via Indicator Timeframe input
// - Classic RSI calculation using RMA (Wilder's smoothing)
// - Optional smoothing via additional MA pass
// - Offset parameter for historical analysis
// - Dynamic color based on rising/falling values
// - Configurable Overbought/Oversold bands

//-------------------------------------
// NOTES
// - Values oscillate between 0 and 100
// - Above 70 = overbought (potential sell signal)
// - Below 30 = oversold (potential buy signal)
// - RSI = 100 - (100 / (1 + RS))
// - RS = Average Gain / Average Loss (using RMA)
// - Divergence between price and RSI signals reversals

//-------------------------------------
// INDICATOR PARAMETERS

vVersion                        = "V2.1"
vPineVersion                    = "pine6"
vTitle                          = "Relative Strength Index"
vShort                          = "RSI"
vOverlay                        = false
vPrecision                      = 2

indicator( title = vTitle+" ["+vVersion+"|"+vPineVersion+"]", shorttitle = vShort+" ["+vVersion+"|"+vPineVersion+"]", overlay = vOverlay, precision = vPrecision )

//-------------------------------------
// INPUTS

var groupRsi                    = "RSI (Standard: close, 15, 1)"

vTimeframe                      = input.timeframe( defval = "", title = "Indicator Timeframe" )
vSource                         = input.source( defval = close, title = "Source", group = groupRsi )
vLength                         = input.int( defval = 15, minval = 1, maxval = 1024, title = "Length", group = groupRsi )
vSmooth                         = input.int( defval = 1, minval = 1, maxval = 1024, title = "Smooth", group = groupRsi )
vOffSet                         = input.int( defval = 0, minval = 0, maxval = 1024, title = "Offset", group = groupRsi )
vUpperBandValue                 = input.int( defval = 70, minval = 1, maxval = 100, title = "Upper Band", group = groupRsi )
vLowerBandValue                 = input.int( defval = 30, minval = 1, maxval = 100, title = "Lower Band", group = groupRsi )

//-------------------------------------
// COLORS

cBullish                        = #26A69A
cBearish                        = #EF5350
cUpperBand                      = #C0C0C0
cLowerBand                      = #C0C0C0

//-------------------------------------
// FUNCTIONS

// @function    Calculate upward or downward price movement
// @param       n (int) Offset for lookback
// @param       s (series float) Source data
// @param       t (string) Type: 'up' for gains, 'down' for losses
// @returns     (float) Positive movement value or zero
fMaxUpDown( n, s, t ) =>
    r = ( t == 'up' ) ? math.max( s[ n ] - s[ n + 1 ], 0 ) : math.max( s[ n + 1 ] - s[ n ], 0 )
    r

// @function    Simple Moving Average
// @param       n (int) Offset for lookback starting point
// @param       s (series float) Source data
// @param       le (int) Length of the averaging period
// @returns     (float) Average value over the specified period
fMa( n, s, le ) =>
    r = float( 0 )
    for i = n + 0 to ( n + ( le - 1 ) )
        r := ( r + s[ i ] )
    a = r / le
    a

// @function    Rolling Moving Average (Wilder's Smoothing)
// @param       n (int) Offset for source indexing
// @param       s (series float) Source data
// @param       le (int) Length for RMA calculation
// @returns     (float) RMA value with persistent state
fRma( n, s, le ) =>
    ma = fMa( n, s, le )
    var float r = na
    r := na( r[ 1 ] ) ? ma : ( ( r[ 1 ] * ( le - 1 ) ) + s[ n ] ) / le
    r

// @function    Relative Strength Index calculation
// @param       n (int) Offset for source indexing
// @param       le (int) Length for RSI calculation
// @param       s (series float) Source data
// @returns     (float) RSI value between 0 and 100
fRsi( n, le, s ) =>
    up   = fMaxUpDown( n, s, 'up' )
    down = fMaxUpDown( n, s, 'down' )

    upRma   = fRma( 0, up, le )
    downRma = fRma( 0, down, le )

    rs  = downRma == 0 ? na : ( upRma / downRma )
    rsi = na( rs ) ? na : ( 100 - ( 100 / ( 1 + rs ) ) )
    rsi

// @function    Calculate RSI with smoothing for MTF support
// @returns     (float) Smoothed RSI value
fCalcRsi() =>
    rsiBase = fRsi( vOffSet, vLength, vSource )
    fMa( 0, rsiBase, vSmooth )

// @function    Determines plot color based on trend direction
// @param       n (int) Offset for comparison
// @param       s (series float) Source data to evaluate
// @returns     (color) Green if rising, red if falling
fColor( n, s ) =>
    r = ( s[ n + 1 ] < s[ n ] ) ? cBullish : cBearish
    r

//-------------------------------------
// CALCULATIONS

vRsi                            = vTimeframe == "" ? fCalcRsi() : request.security( syminfo.tickerid, vTimeframe, fCalcRsi() )

//-------------------------------------
// PLOT

vUpperBand                      = hline( vUpperBandValue, color = cUpperBand, linestyle = hline.style_dashed, linewidth = 1, title = "Upper Band", editable = true )
vLowerBand                      = hline( vLowerBandValue, color = cLowerBand, linestyle = hline.style_dashed, linewidth = 1, title = "Lower Band", editable = true )

vPlotRsi                        = plot( vRsi, style = plot.style_line, color = fColor( 0, vRsi ), title = "RSI - ST" )

//-------------------------------------

Overview

Relative Strength Index draws the oscillator and configurable upper and lower bands in a separate pane, with the line colored according to whether the value is rising or falling. The main settings change the source, lookback length, optional smoothing, plot offset, band levels, and an optional calculation timeframe. It does not generate trade entries, exits, or stop-loss rules.

Details

Published by

SiegerTerpstra on GitHub

Code credit

sajjetti, as written in the script's header

Type

Indicator, Pine Script v6

Draws

Separate pane below the chart

Category

Oscillators & momentum

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

tradingviewindicatorindicators

Settings

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

SettingTypeDefaultVariable
Indicator TimeframetimeframevTimeframe
SourcesourceclosevSource
Lengthint15vLength
Smoothint1vSmooth
Offsetint0vOffSet
Upper Bandint70vUpperBandValue
Lower Bandint30vLowerBandValue
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

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

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

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop
261 stars

This indicator demonstrates an experimental graph-style scatter visualization within a separate pane using Pine Script arrays. It tracks two features across recent bars: the single-bar price change and a scaled 14-period Relative Strength Index. Values are loa

Indicator v4 Oscillators & momentum
Updated 4 years ago · None stated GitHub repository of Example: Graph Style - 4th Dimension RSI

KD指標

by TaichiS
219 stars

This script calculates and plots a Stochastic Oscillator (KD indicator) in a separate chart pane. It determines the fast stochastic value over a 9-period lookback, then applies a 3-period Simple Moving Average (SMA) to create the %K line, and a second 3-period

Indicator v4 Oscillators & momentum
Updated 5 years ago · None stated GitHub repository of KD指標

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