Posts

Showing posts with the label Momentum Oscillator

How to Use the RSI Indicator in Pine Script v6: Overbought, Oversold, and Signal Logic

Image
The Relative Strength Index (RSI) is a momentum oscillator that measures the speed and magnitude of recent price changes on a normalized 0–100 scale. In this article, we examine the mathematical foundation of RSI, how Pine Script v6 computes it internally, and how to build a verified, production-ready RSI indicator with overbought/oversold signal logic from scratch. 1. What Is RSI? The Mathematical Definition RSI was introduced by J. Welles Wilder Jr. in 1978. It is defined as: $$RSI = 100 - \frac{100}{1 + RS}$$ where RS (Relative Strength) is the ratio of the average gain to the average loss over a lookback period $n$: $$RS = \frac{\text{Average Gain}_n}{\text{Average Loss}_n}$$ Wilder used a Wilder Smoothing (RMA) — also called Exponential Moving Average with $\alpha = \frac{1}{n}$ — to compute the rolling averages. The recurrence relation is: $$\text{AvgGain}_t = \frac{\text{Gain}_t + (n-1) \cdot \text{AvgGain}_{t-1}}{n}$$ The ...