Posts

Showing posts with the label EMA

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

How Does the MACD Indicator Work? Pine Script v6 Deep Dive into Crossovers and Momentum Signals

Image
The Moving Average Convergence Divergence (MACD) indicator is one of the most widely studied momentum oscillators in technical analysis, built entirely on the mathematical relationship between two exponential moving averages and their difference. This article dissects the MACD formula from first principles, explains every component with verified Pine Script v6 code, and demonstrates how to implement a fully functional MACD system without relying on the black-box ta.macd() built-in — so you understand exactly what is happening at every bar. 1. The Mathematics Behind MACD MACD is defined by three computed series, each derived from price: $$\text{MACD Line} = \text{EMA}_{\text{fast}}(\text{close}) - \text{EMA}_{\text{slow}}(\text{close})$$ $$\text{Signal Line} = \text{EMA}_{\text{signal}}(\text{MACD Line})$$ $$\text{Histogram} = \text{MACD Line} - \text{Signal Line}$$ The standard parameterization uses a 12-period fast EMA , a 26-perio...

Handling Timeframes in Pine Script v6: timeframe.period and timeframe.multiplier Explained

Image
When building indicators or strategies that must behave differently depending on whether the user is viewing a 1-minute chart versus a daily chart, Pine Script v6 provides two essential built-in variables: timeframe.period and timeframe.multiplier . Understanding how these two variables work together allows you to write scripts that dynamically adapt their logic to any chart resolution without hardcoding timeframe assumptions. 1. What Are timeframe.period and timeframe.multiplier ? Pine Script v6 exposes several built-in timeframe.* variables that describe the chart's current resolution at runtime. The two most important for dynamic adaptation are: Variable Type Description Example (1H chart) timeframe.period simple string The full timeframe string of the current chart (e.g., "60", "D", "W") "60" ...