Posts

Showing posts with the label RMA

How Does ATR Measure Volatility? A Pine Script v6 Deep Dive into Average True Range

Image
When building systematic trading tools, quantifying market volatility is a foundational requirement — and the Average True Range (ATR) is one of the most mathematically rigorous ways to do it. Developed by J. Welles Wilder Jr., ATR measures the average magnitude of price movement over a rolling window, making it indispensable for dynamic stop-loss placement, position sizing, and volatility-adaptive indicators. This article dissects the ATR formula, explains its Pine Script v6 implementation, and demonstrates how to use it for stop-loss engineering. 1. The Mathematics of True Range Before ATR can be computed, we must define the True Range (TR) for each bar. TR captures the full extent of price movement, including gaps between sessions. It is defined as the maximum of three values: $$TR = \max\left(H - L,\; |H - C_{prev}|,\; |L - C_{prev}|\right)$$ Where: $H$ = Current bar's High $L$ = Current bar's Low $C_{prev}$ = P...

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