IndicatorsIntermediate·5 min read·

Weighted Moving Average (WMA) Explained

The Weighted Moving Average (WMA) is a moving average that gives the most recent candle the largest weight and the oldest candle the smallest, in a straight line. It reacts faster than a Simple Moving Average without the infinite memory of an Exponential Moving Average. This guide covers the exact formula, a calculation you can check by hand, and where the WMA helps or hurts.

What is a Weighted Moving Average?

A WMA of period n looks at the last n closing prices and multiplies each one by a weight. The newest close gets weight n, the one before it n − 1, and so on down to the oldest close, which gets weight 1. The weighted values are summed and divided by the sum of the weights.

Because the weights fall off linearly, a new price has a large effect on the WMA immediately, and a price that is about to leave the window already contributes very little. That is why the WMA turns earlier than an SMA of the same length.

The formula

With P1 the newest close and Pn the oldest close in the window: WMA = (n·P1 + (n−1)·P2 + … + 2·P(n−1) + 1·Pn) / (n(n+1)/2).

The denominator n(n+1)/2 is just the sum 1 + 2 + … + n. For a 5-period WMA it is 15, for 10 periods it is 55, and for 21 periods it is 231. Dividing by it keeps the result on the same price scale as the input.

// closes: oldest first, newest last
function wma(closes, period) {
  const denom = (period * (period + 1)) / 2;
  const out = [];
  for (let i = period - 1; i < closes.length; i++) {
    let sum = 0;
    for (let j = 0; j < period; j++) {
      sum += closes[i - j] * (period - j); // newest gets weight = period
    }
    out.push(sum / denom);
  }
  return out;
}

A worked example

Take five closes, oldest to newest: 10, 11, 12, 13, 14. For a 5-period WMA the weights are 1, 2, 3, 4, 5 in that order.

Weighted sum = 10×1 + 11×2 + 12×3 + 13×4 + 14×5 = 10 + 22 + 36 + 52 + 70 = 190. Divide by 15 and the WMA is 12.67.

The 5-period SMA of the same closes is 60 / 5 = 12.00. In a steady uptrend the WMA sits closer to the latest price because the latest prices count more. An EMA(5) seeded from the first close lands at about 12.40 here, between the two, although the EMA value depends on how it was seeded.

WMA vs SMA vs EMA

All three smooth price, but they distribute weight differently, and that decides how fast they react and how much noise they let through.

  • SMA: every price in the window has equal weight. Smoothest and slowest; it also jumps when an old, extreme price drops out of the window.
  • WMA: weights fall linearly to zero at the edge of the window. Faster than an SMA of the same period, and old prices fade out gradually instead of dropping off a cliff.
  • EMA: weights decay geometrically and never quite reach zero, so every past price still has a tiny influence. Responsive like a WMA, but its early values depend on the seed.
  • For the same period, lag is roughly SMA > EMA ≈ WMA. The differences matter most around turning points, not in quiet trends.

Common periods and uses

There is nothing magic about any period, but a few show up often because traders share conventions.

  • 9 or 10: short-term trend and momentum on intraday charts.
  • 21: a common swing-trading baseline on H1 to D1 charts.
  • 50 and 100: medium-term trend filters.
  • Crossovers of a fast and slow WMA (for example 10 and 21) as a trend-change trigger.
  • As a building block: the Hull Moving Average is built from WMAs to reduce lag further.

Common mistakes

Faster is not automatically better. The responsiveness that gets you in earlier also produces more whipsaws in ranging markets, so a WMA crossover system will usually trade more often and pay more in spreads than an SMA version.

  • Comparing a WMA and an SMA with the same period and assuming they mean the same thing. A 21 WMA reacts noticeably faster than a 21 SMA.
  • Using it on an incomplete candle. The last value changes until the candle closes; decide whether your rules use closed candles only.
  • Optimising the period on one market and timeframe, then assuming it transfers to another.
  • Treating price crossing the WMA as a signal on its own, without a trend or volatility filter.

Getting WMA values from RealMarketAPI

You do not have to calculate the WMA yourself. RealMarketAPI computes it from closing prices on the server. Pass your API key, a symbol, a timeframe and a period between 2 and 200. The indicator endpoints are available on the PRO plan and above.

The response is a list of the form { data: [ ... ], totalCount }, where each item carries a candle openTime and the WMA value. Check the documentation for the exact field names and the timeframes your plan includes.

GET https://api.realmarketapi.com/api/v1/indicator/wma?apiKey=YOUR_KEY&symbolCode=XAUUSD&timeFrame=H1&period=21

Frequently asked questions

Why is the denominator n(n+1)/2?

It is the sum of the weights 1 + 2 + … + n. Dividing the weighted sum by it turns the result back into a price, so the WMA can be plotted on the same chart as the candles.

Is the WMA better than the EMA?

Neither is better in general. They react at similar speeds; the WMA has a fixed window and forgets old data completely, while the EMA keeps a small memory of all past prices. Pick one, test it on your market, and stick with it.

Which price does the WMA use?

Usually the close of each candle, which is what RealMarketAPI uses. Some charting tools let you switch to typical price or the high/low midpoint, which gives slightly different values.

What period should I use?

Start with a commonly used value such as 21 for swing trading or 9 to 10 for short-term work, then validate it on historical data for the exact symbol and timeframe you trade.

TA
Trading Academy Editors
Independent education team. Reviewed by practising traders and engineers.

Want to build on real data?

RealMarketAPI gives you REST and WebSocket access to forex, gold and crypto prices, candles and indicators. Start with the docs or grab a free API key.

Related articles