Files
QuanTAlib/lib/momentum/rsi/rsi.pine
T
Miha Kralj 24e86d762a Add documentation links for various volatility indicators and channels
- Updated BBWN, BBWP, CCV, CV, CVI, EWMA, GKV, HLV, HV, Jvolty, JVOLTYN, MASSI, NATR, RSV, RV, RVI, TR, UI, VOV, VR, YZV indicators with documentation links.
- Added documentation links for Aberration, Acceleration Bands, Andrews' Pitchfork, Adaptive Price Zone, ATR Bands, Bollinger Bands, Center of Gravity, Donchian Channels, Decay Min-Max Channel, Detrended Synthetic Price, EACP, EBSW, HOMOD, Jurik Volatility Bands, Keltner Channel, MA Envelope, Min-Max Channel, Price Channel, Regression Channels, Standard Deviation Channel, Stoller Average Range Channel, Super Trend Bands, Ultimate Bands, Ultimate Channel, VWAP Bands, and VWAP with Standard Deviation Bands.
2026-02-18 11:55:48 -08:00

37 lines
1.2 KiB
Plaintext

// The MIT License (MIT)
// © mihakralj
//@version=6
indicator("Relative Strength Index (RSI)", "RSI", overlay=false)
//@function Calculates Relative Strength Index using Wilder's smoothing
//@param src Source series to calculate RSI for
//@param len Lookback period for RSI calculation
//@returns RSI value measuring momentum and overbought/oversold conditions
rsi(series float src, simple int len) =>
if len <= 0
runtime.error("Length must be greater than 0")
float u = math.max(src - src[1], 0)
float d = math.max(src[1] - src, 0)
float alpha = 1/len, float smoothUp = 0.0, float smoothDown = 0.0
if bar_index < len
smoothUp := u
smoothDown := d
else
smoothUp := nz(smoothUp[1]) * (1 - alpha) + u * alpha
smoothDown := nz(smoothDown[1]) * (1 - alpha) + d * alpha
float rs = smoothDown == 0 ? 0 : smoothUp/smoothDown
float rsi = smoothDown == 0 ? 100 : 100 - (100 / (1 + rs))
rsi
// ---------- Main loop ----------
// Inputs
i_length = input.int(14, "Length", minval=1)
i_source = input.source(close, "Source")
// Calculation
rsi_value = rsi(i_source, i_length)
// Plot
plot(rsi_value, "RSI", color=color.yellow, linewidth=2)