◆ New — Strategy Validator, Code Fixer & Prop Compliance Checker are live. Try one free →
Home / Indicators / Moving Averages (SMA & EMA)
Trend indicator

Moving Averages (SMA & EMA)

A smoothed line of average price that helps traders see the underlying trend through noisy candles.

Illustrative diagram — not live market data.

What it is

A moving average (MA) takes the average price over a chosen number of bars and plots it as a single line that updates each bar. The Simple Moving Average (SMA) weights every bar in the window equally. The Exponential Moving Average (EMA) gives more weight to recent bars, so it tracks current price more closely. Both are lagging, trend-following tools used to smooth price and make direction easier to see. They are educational aids for reading past price behaviour, not predictions of where price will go, and no indicator can remove the risk of loss in trading.

How it works

Pick a length (number of bars), for example 20. For an SMA, add up the closing prices of the last 20 bars and divide by 20; as each new bar forms, the oldest price drops out and the newest comes in, so the line slides forward. An EMA also averages recent prices, but instead of treating all bars equally it applies a smoothing factor derived from the length (commonly 2 / (length + 1)) that fades older bars exponentially, so the most recent prices count more. The result: a shorter length hugs price and reacts fast but wiggles more; a longer length is smoother and slower. An EMA of the same length reacts faster than the SMA because of its recency weighting. Because every value depends on past bars, an MA always lags actual price.

How traders read it

Common settings

Lengths are chosen to match your timeframe and intent. Common references include 9/10/12 and 20 for short-term reads, 50 for an intermediate view, and 100/200 for longer-term context. The 50 and 200 are widely watched, and the 12/26 EMA pair underlies the MACD. Source is usually the close. There is no universally correct setting — shorter means faster and noisier, longer means smoother and slower.

Strengths

Pitfalls to watch

Pine v6 example

//@version=6
indicator("Moving Averages (SMA & EMA)", overlay = true)

length = input.int(20, "Length", minval = 1)
src    = input.source(close, "Source")

smaLine = ta.sma(src, length)
emaLine = ta.ema(src, length)

plot(smaLine, "SMA", color = color.blue)
plot(emaLine, "EMA", color = color.orange)

Pro tip: Match the MA to your timeframe and decide up front whether you want responsiveness (shorter length or EMA) or stability (longer length or SMA) — then judge it on the whole price context, not the line alone. MAs tend to add the most clarity in trending conditions and the least in sideways ranges, where their signals tend to whipsaw. This is educational context only, not advice to trade.

Built an indicator from this? Run it through the Validator to catch look-ahead bias and repainting, or convert a strategy to Pine Script.

Educational only — not financial advice, not a recommendation to trade. No indicator is predictive; trading involves substantial risk of loss.

Catch the bug that compiles.Run auditGet Pro