Posts

Showing posts with the label ATR

SuperTrend Indicator in Pine Script v6: Canonical Formula, Flip Logic, and Non-Repainting Signals

Image
Every trend-following trader eventually faces the same frustration: a moving average that lags too far behind price, generating entries long after the move has started and exits well into the reversal. The SuperTrend indicator was designed to solve exactly this problem — it combines Average True Range (ATR) volatility with a dynamic band-switching mechanism to produce a single, clean line that sits below price in an uptrend and above price in a downtrend. When price closes beyond the opposite band, the line flips, generating a confirmed signal on the closed bar. This post builds a precise, well-defined SuperTrend variant in Pine Script v6 , maps every formula term to its code variable, explains the exact flip conditions, and clarifies non-repainting behavior. What Is SuperTrend? The Mathematical Foundation SuperTrend is built on two components: a midpoint price and an ATR-scaled offset . The midpoint is typically the average of the hig...

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