Master momentum trading with this advanced guide to Exponential Moving Averages in TradingView's Pine Script
The Exponential Moving Average (EMA) is a powerful technical indicator that gives more weight to recent prices, making it more responsive to new information than the Simple Moving Average (SMA). This makes EMAs ideal for:
//@version=5
indicator("My EMA Indicator", overlay=true)
// User input for EMA length
length = input(20, title="EMA Length", minval=1)
// Calculate EMA
emaValue = ta.ema(close, length)
// Plot with customizable color
plot(emaValue, color=color.new(color.purple, 0), linewidth=2)
Unlike SMA which weights all prices equally, EMA applies exponentially decreasing weights to older prices, making it more responsive to recent price action.
The EMA calculation involves two steps:
//@version=5
strategy("EMA Crossover Strategy", overlay=true)
// Inputs
fastLength = input(9, "Fast EMA Length")
slowLength = input(21, "Slow EMA Length")
// Calculate EMAs
fastEMA = ta.ema(close, fastLength)
slowEMA = ta.ema(close, slowLength)
// Plot
plot(fastEMA, "Fast EMA", color.green)
plot(slowEMA, "Slow EMA", color.red)
// Strategy logic
longCondition = ta.crossover(fastEMA, slowEMA)
shortCondition = ta.crossunder(fastEMA, slowEMA)
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
//@version=5
indicator("EMA Ribbon", overlay=true)
// Multiple EMAs for trend visualization
ema5 = ta.ema(close, 5)
ema10 = ta.ema(close, 10)
ema20 = ta.ema(close, 20)
ema50 = ta.ema(close, 50)
// Color based on trend direction
ribbonColor = ema5 > ema10 and ema10 > ema20 and ema20 > ema50 ? color.green :
ema5 < ema10 and ema10 < ema20 and ema20 < ema50 ? color.red : color.gray
// Plot all EMAs with gradient colors
plot(ema5, "EMA 5", color.new(color.green, 30), linewidth=2)
plot(ema10, "EMA 10", color.new(color.lime, 30), linewidth=2)
plot(ema20, "EMA 20", color.new(color.orange, 30), linewidth=2)
plot(ema50, "EMA 50", color.new(color.red, 30), linewidth=2)
// Background for visual clarity
bgcolor(ribbonColor, 90)
To get the most from EMAs in Pine Script:
//@version=5
indicator("Dynamic EMA", overlay=true)
// Base length adjusted by volatility
atrLength = input(14, "ATR Length")
volatilityFactor = input(1.5, "Volatility Factor")
baseLength = input(20, "Base EMA Length")
// Calculate dynamic length
atrValue = ta.atr(atrLength)
// Use a relative change in ATR to adjust length.
float atrChangeRatio = nz(ta.change(atrValue) / (nz(ta.valuewhen(ta.lowest(atrValue, 50), atrValue, 0)) + 1e-10))
float scaledLengthRaw = baseLength * (1 + atrChangeRatio * volatilityFactor)
// Ensure length stays within bounds and is an integer for EMA function
dynamicLength = math.max(5, math.min(50, int(scaledLengthRaw)))
// Plot dynamic EMA
plot(ta.ema(close, dynamicLength), "Dynamic EMA", color.purple)
EMAs can produce false signals in ranging markets. Always combine with other indicators or filters.
The EMA is one of Pine Script's most versatile tools when used properly. By understanding its mathematical foundation and combining it with other technical elements, you can create powerful trading systems that respond quickly to market changes while filtering out noise.
Get a high-performance Pine Script analysis tool for actionable market insights, designed for traders on the move.
This strategy runs in live mode on TradingView, helping you identify potential opportunities.
Get Pine Script Strategy