⬡ INDICATOR · WITH CODE · 11 MIN READ

Moving averages: SMA, EMA and WMA — which to use.

The most fundamental indicator of all — the basis of almost every strategy. Let's understand the three variations, when to use each, the popular averages (9, 21, 50, 200) and ready-to-use code.

By the RoboTraderIA Team· updated May 2026· beginner level

If you were to learn a single indicator, it would be the moving average. It smooths out price noise to reveal the trend, acts as dynamic support/resistance, and is the building block of other indicators (the MACD is moving averages; Bollinger Bands revolve around a moving average). But there's more than one type, and choosing the wrong one costs you lagging or false signals. Let's clear it up.

01What a moving average is

A moving average calculates the average price over a number of periods and "walks" along with time — at each new candle, it recalculates. The result is a smooth line that filters out small swings and shows the underlying direction. The longer the period, the smoother (and slower) the line.

02SMA, EMA and WMA: the real difference

All three calculate an average, but they distribute the weight differently:

TypeHow it weights pricesCharacteristicBest for
SMAEqual weight to allSmoother, slowerSeeing the underlying trend, filtering noise
EMAMore weight to recentReacts fasterFast signals, day trading
WMALinearly decreasing weightIntermediate reactionA middle ground between SMA and EMA

The SMA (Simple Moving Average) treats the price from 20 candles ago with the same weight as the current candle. The EMA (Exponential) gives much more importance to recent prices, so it "hugs" price more and reacts faster to changes. The WMA (Weighted) sits in the middle, with linearly decreasing weights.

price EMA (fast) SMA (slow) price turn
After a turn in price, the EMA (green) follows faster; the SMA (gold) takes longer to turn.

Fundamental trade-off: fast reaction (EMA) = catches trends early, but gives more false signals. Smoothing (SMA) = fewer false signals, but enters late. There's no "best" — there's the right one for your goal. Day trading leans EMA; an underlying-trend view, SMA.

03The popular averages (and why they matter)

Some periods are so widely used that they become "self-fulfilling" — so many people watch them that price reacts to them:

  • 9 and 21: short term, popular in day trading. The 9×21 crossover is a classic.
  • 20: the basis of Bollinger Bands and a short-to-medium reference.
  • 50: the medium-term trend. Heavily watched.
  • 200: the queen. The long-term trend. Price above the 200 = structural uptrend; below = downtrend. Used as a master filter.

"Golden cross" and "death cross": when the 50 average crosses above the 200, it's called a golden cross (a long-term bullish signal); below, a death cross (bearish). They're so closely watched that they make the news — and move the market for that very reason.

04The practical uses

  • Trend direction: price above the average = up; below = down. Simple and effective.
  • Dynamic support/resistance: in a trend, price often pulls back to the average and respects it — a re-entry zone.
  • Crossovers: a fast average crossing a slow one generates signals (the basis of the moving-average crossover strategy).
  • Filter: only trading in the direction of the 200 average eliminates many bad trades against the larger trend.

05Coding the averages

Pine Script (TradingView)
//@version=5
indicator("Moving Averages", overlay=true)

p = input.int(21, "Period")

sma = ta.sma(close, p)   // simple
ema = ta.ema(close, p)   // exponential
wma = ta.wma(close, p)   // weighted

plot(sma, "SMA", color=color.orange)
plot(ema, "EMA", color=color.lime)
plot(wma, "WMA", color=color.blue)

// trend filter with the 200 average
ma200 = ta.sma(close, 200)
uptrend = close > ma200
plot(ma200, "MA200", color=uptrend ? color.green : color.red, linewidth=2)
Python (with pandas)
import pandas as pd

def averages(prices, period=21):
    sma = prices.rolling(period).mean()
    ema = prices.ewm(span=period, adjust=False).mean()
    # WMA: linear weights 1..n
    weights = pd.Series(range(1, period+1))
    wma = prices.rolling(period).apply(
        lambda x: (x * weights).sum() / weights.sum(), raw=True)
    return sma, ema, wma

sma, ema, wma = averages(df["close"])

# trend filter: only go long above the MA200
df["ma200"] = df["close"].rolling(200).mean()
can_buy = df["close"].iloc[-1] > df["ma200"].iloc[-1]

Ready to build the strategy?

The moving-average crossover is the #1 strategy in our collection — with complete code.

See strategies →

06Common mistakes

The 3 classic mistakes: (1) using too short an average and getting chopped up by noise; (2) trading crossovers in a ranging market, where they cross constantly and produce "whipsaw"; (3) forgetting that a moving average is a lagging indicator — it confirms the trend, it doesn't predict it. A moving average is never a standalone trigger; it's context and a filter.

07Frequently asked questions

What's the difference between SMA and EMA?

The SMA weights every price in the period equally; the EMA gives more weight to recent ones, reacting faster. EMA is preferred for fast signals and day trading; SMA for smoothing and seeing the underlying trend.

Which moving average should you use?

EMA for fast reaction, SMA for smoothing. Periods 9 and 21 for the short term; 50 and 200 for the long trend. There's no universal "best" — it depends on the goal and the asset, validated by backtest.

What is the 200 moving average?

The 200-period MA is the long-term trend reference. Price above = structural uptrend; below = downtrend. Widely used as a master trend filter in strategies.

What is a golden cross?

When the 50 average crosses above the 200 — a long-term bullish signal. The opposite (crossing below) is the death cross. They're so closely watched that they make the news and influence the market.

Does a moving average predict price?

No. It's a lagging indicator — calculated from past prices. It confirms and smooths the trend, acts as dynamic support/resistance and a filter, but doesn't predict. Anyone expecting a forecast will be disappointed.