TrendPine Script v6

Exponential Moving Average Pine Script — Complete TradingView Guide

The Exponential Moving Average (EMA) is a trend-following indicator built into Pine Script v6 via ta.ema(source, length). Unlike the Simple Moving Average, the EMA applies exponential weighting — recent prices contribute more to the calculation than older ones — making it faster to respond to price changes and widely preferred for trend detection, crossover signals, and dynamic support/resistance. The EMA default period of 20 bars is the most common starting point, with 9, 21, 50, and 200 also heavily used across different trading styles. This guide covers the complete Pine Script v6 implementation using ta.ema(), all configurable parameters with recommended ranges, three concrete EMA trading strategies with specific entry and exit conditions, and a five-step guide to generating EMA scripts instantly with Pineify.

What Is the Exponential Moving Average?

The Exponential Moving Average is a trend-following overlay indicator that calculates a weighted average of price, giving exponentially greater importance to recent bars, used to identify trend direction and generate crossover trading signals. Unlike the Simple Moving Average (SMA), which assigns equal weight to every bar in the lookback window, the EMA applies a multiplier that causes older data to decay exponentially — making the EMA line more responsive to new price movements while still smoothing out short-term noise.

The EMA was widely adopted in technical analysis during the 1970s and 1980s as computing power made iterative calculations practical. While no single inventor is credited with the EMA formula, Gerald Appel popularized exponential smoothing in 1979 when he introduced the MACD (Moving Average Convergence Divergence), which is built entirely from two EMAs. Since then, the EMA has become one of the most widely used indicators on TradingView, with millions of traders using EMA crossovers as primary entry signals.

Core formula: EMA = Price × k + EMA(previous) × (1 − k), where k = 2 ÷ (length + 1). For a 20-period EMA, k ≈ 0.0952, meaning the current bar contributes 9.52% and the previous EMA carries forward 90.48%. For a 9-period EMA, k = 0.2, giving 20% weight to the current bar. In Pine Script v6, this entire calculation is handled by ta.ema(close, 20).

The EMA applies to all liquid asset classes: stocks, crypto, forex, and futures. It is most effective in trending markets and least reliable in sideways, choppy price action. Best timeframes are 15-minute to Daily charts — shorter timeframes (1m, 5m) produce excessive noise, while longer timeframes (Weekly, Monthly) are better served by the SMA 200 as a macro trend filter.

Best Markets

Stocks · Crypto · Forex · Futures

Best Timeframes

15m, 1H, 4H, Daily

Overlay

Yes — plots directly on price chart

EMA Pine Script Code Example

The code below plots a 20-period EMA as a blue line overlaid on the price chart using Pine Script v6. To add it to TradingView, open the Pine Script editor with Alt+P, paste the code, and click Add to chart. Change the 20 in ta.ema(close, 20) to any period that suits your trading style.

Pine Script v6
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pineify

//@version=6
indicator(title="Exponential Moving Average", overlay=true, max_labels_count=500)

// EMA — applies more weight to recent prices than older bars
p_ind_1 = ta.ema(close, 20) // EMA with default period 20

// Plot the EMA line on the price chart
plot(p_ind_1, "EMA", color.rgb(41, 98, 255, 0), 1)

Chart Preview

Exponential Moving Average indicator Pine Script code example in TradingView — EMA 20 plotted as overlay

EMA Parameters

ParameterDefault ValueDescriptionRecommended Range
sourcecloseThe price series to apply the EMA calculation to. Most commonly close, but can be open, high, hl2, or any numeric series.Any numeric series
length20Number of bars used in the EMA calculation. Controls the smoothing multiplier k = 2 ÷ (length + 1). Larger values produce a smoother, slower-reacting line; smaller values react faster to recent price changes.9–200 depending on style

Tuning Guide by Trading Style

  • Scalping (1m–5m charts): EMA 5–10 — fast response to micro price movements, high signal frequency
  • Day trading (15m–1H charts): EMA 9 + EMA 21 — standard short-term crossover pair used by day traders
  • Swing trading (4H–Daily charts): EMA 20 + EMA 50 — balanced trend identification with manageable lag
  • Position trading (Daily–Weekly charts): EMA 50 + EMA 200 — the "Golden Cross" and "Death Cross" macro trend signals

EMA Trading Strategies

The EMA is most effective as a trend direction filter and crossover signal generator. Below are three concrete strategies with specific entry and exit conditions.

Strategy 1 — EMA 9/21 Crossover (Short-Term Momentum)

Market environment: trending markets · Best timeframe: 15m, 1H

The EMA 9/21 crossover is the most widely used short-term EMA strategy. When the fast EMA (9) crosses above the slow EMA (21), it signals bullish momentum. Use ta.crossover(ta.ema(close, 9), ta.ema(close, 21)) to detect the signal in Pine Script.

  1. Calculate fast EMA: ema9 = ta.ema(close, 9)
  2. Calculate slow EMA: ema21 = ta.ema(close, 21)
  3. Long entry: ta.crossover(ema9, ema21) — EMA 9 crosses above EMA 21
  4. Confirm price is above EMA 50 (trend filter — avoid counter-trend longs)
  5. Exit: ta.crossunder(ema9, ema21) — EMA 9 crosses below EMA 21

Strategy 2 — EMA 200 Bull/Bear Filter + EMA 20 Pullback Entry

Market environment: trending bull markets · Best timeframe: Daily

The EMA 200 on the daily chart is the most widely-followed trend dividing line in institutional trading. Price above EMA 200 defines a bull market environment; pullbacks to the EMA 20 offer low-risk long entries within the larger trend.

  1. Calculate trend filter: ema200 = ta.ema(close, 200)
  2. Calculate pullback level: ema20 = ta.ema(close, 20)
  3. Condition 1 (trend): close > ema200 — only take long setups in bull territory
  4. Condition 2 (entry): price touches or closes within 0.5% of ema20 and then closes back above it
  5. Stop loss: below the EMA 50; hold until close below EMA 20 for more than 2 consecutive bars

Strategy 3 — EMA + RSI Confirmation (Trend + Momentum)

Market environment: trending and recovering markets · Best timeframe: 4H, Daily

Pairing the EMA 20 with RSI(14) filters out false breakouts. The EMA identifies trend direction while the RSI confirms that momentum supports the move. Enter only when both conditions align.

  1. Calculate trend EMA: ema20 = ta.ema(close, 20)
  2. Calculate momentum filter: rsi14 = ta.rsi(close, 14)
  3. Long entry: close crosses above ema20 AND rsi14 > 50 (momentum is bullish)
  4. Avoid: signals where rsi14 > 70 (overbought — entry is too late, increased reversal risk)
  5. Exit: close crosses below ema20 OR rsi14 falls below 45

Disclaimer: The strategies above are for educational purposes only and do not constitute investment advice. Past performance does not guarantee future results. Always apply proper risk management and position sizing.

How to Generate the EMA Indicator in Pineify

  1. 1

    Open Pineify

    Go to pineify.app and sign in — a free account is sufficient to generate EMA indicators.

  2. 2

    Click "New Indicator"

    Select "Indicator" as the script type from the creation menu on the dashboard.

  3. 3

    Describe the EMA you want

    Type a prompt such as: "Plot a 20-period EMA as a blue line overlay on the price chart using ta.ema(close, 20)." Pineify's AI Coding Agent generates the complete Pine Script v6 code in seconds.

  4. 4

    Copy to TradingView

    Click "Copy to TradingView" to copy the generated code, then open the TradingView Pine Script editor (Alt+P), paste the code, and click "Add to chart".

  5. 5

    Adjust the length parameter

    In the TradingView indicator settings panel, change the EMA length (e.g., from 20 to 9 or 50) to match your trading style — no code editing required.

Frequently Asked Questions

Related Pine Script Indicators

Build Your EMA Indicator in Seconds

Skip the manual coding. Pineify's AI Coding Agent generates complete, ready-to-use Pine Script indicators — including EMA crossover strategies with RSI confirmation — instantly for free.

Try Pineify Free