Posts

Showing posts with the label Trend Following

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

Why Do Traders Use EMA? Exponential Moving Average Explained with Pine Script v6

Image
When price moves sharply, a Simple Moving Average (SMA) reacts slowly because it weights every bar in its window equally. The Exponential Moving Average (EMA) solves this by applying a geometrically decaying multiplier, giving the most recent bars a heavier influence and allowing the line to track price changes more responsively. This article dissects the EMA formula mathematically, compares it to SMA behavior, and implements both from scratch in Pine Script v6. 1. The Mathematics Behind EMA The EMA is defined by a recursive formula. Given a smoothing factor k and a source series P : $$k = \frac{2}{n + 1}$$ $$\text{EMA}_t = P_t \cdot k + \text{EMA}_{t-1} \cdot (1 - k)$$ Where n is the period length. The multiplier k determines how aggressively the EMA responds to new data. A smaller n produces a larger k , meaning the EMA reacts faster. A larger n produces a smaller k , smoothing out noise more aggressively. Weight Distribution...