MyAlgoMate
// INDICATOR FORMULA

Moving average formula: SMA and WMA (with Excel)

How the simple moving average and the weighted moving average of a stock are worked out, with a worked example of the 20-period SMA and WMA side by side, the Excel formulas, a free Excel sheet and a box to calculate them from your own prices.

Last updated:

Download the free SMA & WMA Excel sheet (.xlsx)

Needs: closing prices · Standard settings: Period 20

The SMA & WMA formula

SMA (n) = (C1 + C2 + … + Cn) ÷ n

WMA (n) = (1 × C1 + 2 × C2 + … + n × Cn) ÷ (1 + 2 + … + n)

C1 = the oldest of the last n closes, Cn = the latest
1 + 2 + … + n = n × (n + 1) ÷ 2        (= 210 for n = 20)

Both averages use only the last n closes and forget everything older. The SMA weighs them all the same; the WMA gives the latest close a weight of n and the oldest a weight of 1. For the exponential version see the EMA formula.

How to calculate SMA & WMA step by step

  1. Take the last 20 closes. The first full set is Day 1 to Day 20, so both averages start on Day 20.
  2. SMA: add them up and divide by 20. Day 20: 1,018.39. On Day 21 the window moves on — Day 1 drops out, Day 21 (close 978.30) comes in — and the SMA becomes 1,017.25.
  3. WMA: multiply the 20 closes by 1, 2, … 20 (oldest to latest), add them up and divide by 210. Day 21: 1,015.30. It sits closer to the recent closes than the SMA does, because they carry more weight.
  4. Repeat for every candle. On Day 60 the example's 20 SMA is 953.00 and its 20 WMA is 969.50.

Worked example — and calculate SMA & WMA from your own prices

The table works through 60 daily candles of an imaginary stock, column by column — the same columns as the Excel sheet. To calculate SMA & WMA for your own stock, clear the box and paste your closing prices from Excel or your broker's download.

One row per candle, oldest first. Paste straight from Excel or your broker's download; a date or time column becomes the row label. Filled with our example prices.

Latest values

SMA (20): 952.9975WMA (20): 969.4995
SMA & WMA calculated from the example prices
DayCloseSMA (20)WMA (20)
Day 11,001.15
Day 2987.95
Day 31,009.15
Day 41,012.60
Day 5993.25
Day 61,008.60
Day 71,013.60
Day 81,001.55
Day 91,031.55
Day 101,040.05
Day 111,033.15
Day 121,064.80
Day 131,083.55
Day 141,067.95
Day 151,063.50
Day 161,030.20
Day 171,012.20
Day 18981.60
Day 19970.55
Day 20960.901,018.39251,019.1183
Day 21978.301,017.25001,015.3000
Day 22958.851,015.79501,009.7381
Day 23946.751,012.67501,003.1624
Day 24937.951,008.9425996.0457
Day 25927.851,005.6725988.3226
Day 26911.201,000.8025979.3252
Day 27905.15995.3800970.2155
Day 28885.95989.6000959.7936
Day 29874.70981.7575948.8507
Day 30855.80972.5450936.8548
Day 31862.15963.9950926.3410
Day 32866.45954.0775917.0510
Day 33845.10942.1550906.6721
Day 34851.55931.3350898.0431
Day 35860.20921.1700891.2683
Day 36854.80912.4000884.9474
Day 37877.20905.6500881.5950
Day 38888.45900.9925879.9569
Day 39905.40897.7350880.3767
Day 40896.80894.5300880.2876
Day 41889.60890.0950879.8181
Day 42895.30886.9175880.3138
Day 43919.00885.5300883.3693
Day 44918.35884.5500886.4950
Day 45906.25883.4700888.5617
Day 46932.10884.5150893.1931
Day 47944.35886.4750898.8917
Day 48948.70889.6125904.8179
Day 49966.35894.1950912.1262
Day 50955.60899.1850917.9743
Day 51969.10904.5325924.6329
Day 52984.35910.4275932.2345
Day 53980.90917.2175938.9462
Day 54962.50922.7650943.2588
Day 55960.20927.7650946.8240
Day 56967.35933.3925950.5940
Day 57971.00938.0825954.1757
Day 58981.75942.7475958.3345
Day 59994.85947.2200963.2967
Day 601,012.35952.9975969.4995
Everything is worked out in your browser. Nothing you paste is stored or sent.

The example prices are made up for this page — they are not real market data. SMA & WMA is a calculation from past prices, not a forecast, a signal or advice to buy or sell. MyAlgoMate provides trading technology, not investment advice.

How to calculate SMA & WMA in Excel

Put the headings in row 1 and the first candle in row 2, oldest at the top. Then type these formulas — each column uses the ones to its left:

  1. A

    Day

    A2 downwards: your dates, oldest at the top.

  2. B

    Close

    B2 downwards: the closing prices, oldest at the top.

  3. C

    SMA (20)

    C21 =AVERAGE(B2:B21) — then copy it down to the last row.

  4. D

    WMA (20)

    D21 =SUMPRODUCT(B2:B21,ROW($1:$20))/210 — then copy it down to the last row.

ROW($1:$20) simply produces the weights 1 to 20; the dollar signs keep them fixed when you copy the formula down. For a different period, change 20 and 210 (= n × (n + 1) ÷ 2).

Prefer not to type them? The free sheet has every formula filled in for the example prices — replace the prices with yours and extend the rows. It opens in Microsoft Excel (we checked every value against the table above), and you can import it into Google Sheets or LibreOffice.

Download the free SMA & WMA Excel sheet (.xlsx)

What is the moving average indicator?

The moving average indicator is a line drawn on a price chart. Each point on it is the average of the last nprices, usually closes. When a new candle arrives, the oldest price drops out and the newest is added, so the line “moves”. Averaging smooths small ups and downs. It uses past prices only, so it describes what has already happened.

SMA vs WMA vs EMA: the difference

Simple, weighted and exponential moving average compared
TypeHow the prices are weightedCalculation
SMAAll n prices count the sameSum of the n closes ÷ n
WMANewest counts most; weights 1 to n in equal stepsWeighted sum ÷ n × (n + 1) ÷ 2
EMANewest counts most; weights shrink by a fixed ratioMultiplier 2 ÷ (n + 1)

The weights change the number, not the idea. The full working for the third type is on the EMA formula page.

Short, medium and long moving averages

A smaller n follows the latest prices closely. A larger n is smoother but slower to follow them; that delay is called lag. As labels, TradingView calls periods under 20 short, 20 to 60 medium and over 60 long (TradingView). Fidelity describes a 50-bar SMA as an intermediate-term average and a 200-bar SMA as a common proxy for the long term (Fidelity).

The table shows the lag in candles for a steady straight-line rise in price (prices 1, 2, … n). It is a maths illustration, not real market data.

Lag in candles of an SMA and a WMA on a steady straight-line rise
Period nSMA lag (n − 1) ÷ 2WMA lag (n − 1) ÷ 3
521.33
209.56.33
5024.516.33
20099.566.33

Rising line, price cross and crossover: what the words mean

  • Rising or falling line: the direction the average itself is moving from one candle to the next.
  • Price cross: a close that moves from one side of the average line to the other.
  • Crossover: a faster average (smaller n) moves above or below a slower one (larger n). “Golden cross” and “death cross” are names for this with the 50 and 200 averages.

All of these describe past data; what to do with them is the user's own decision. Two limits to know: an average always lags the latest price, and when prices move sideways the lines cross each other often.

Excel's Moving Average tool

Excel's Data Analysis add-in has a Moving Average tool with an “Interval” box. It calculates the same simple moving average as =AVERAGE over the last n rows — the interval is the period. The formula route above is quicker to change and works the same in Google Sheets.

Where moving averages appear

Moving averages are inside many other indicators: the middle line of Bollinger Bands is a 20 SMA, the MACD is the gap between two EMAs, and the stochastic %D is a 3-period SMA. A moving average describes past prices — a calculation, not a forecast or advice.

// FAQ

SMA & WMA calculation — questions, answered.

It is a line on a price chart made of the average of the last n prices (usually closes), worked out again for every candle. It smooths small ups and downs and uses only past prices, so it describes what has already happened.

The SMA gives all n prices the same weight. The EMA gives the newest price more weight, using the multiplier 2 ÷ (n + 1). Same prices, different numbers.

It means a faster average (smaller n) has moved above or below a slower one (larger n). It only describes where two averages stand; it is not an instruction. Rules you set yourself can be automated in software.

A simple moving average (SMA) adds up the last n closing prices and divides by n. Each new candle drops the oldest close and adds the newest, so the average 'moves' along the chart. A 20-day SMA uses the last 20 daily closes.

With closes in column B from row 2, type =AVERAGE(B2:B21) in C21 for a 20-period SMA and copy it down — Excel shifts the range one row each time. For a 20-period WMA type =SUMPRODUCT(B2:B21,ROW($1:$20))/210 in D21 and copy it down.

WMA = (1 × oldest close + 2 × next + … + n × newest close) ÷ (1 + 2 + … + n). The weights rise by one step per candle, so the newest close counts n times as much as the oldest. 1 + 2 + … + n = n × (n + 1) ÷ 2, which is 210 for a 20-period WMA.

Exactly the same way with n = 50 or n = 200: =AVERAGE of the last 50 or 200 closes. You need at least that many closes for the first value. Change the period in the calculator above to see it for your own prices.

None is better in general — they are three ways of averaging the same prices. The SMA weighs every close equally, the WMA weighs recent closes more in straight steps, and the EMA weighs them more in shrinking percentages and never fully forgets older prices. They give different numbers from the same data, so compare like with like.

For an SMA, almost always because of the prices: spot vs futures, a different timeframe, or a chart that adjusts past prices for splits and bonuses. Also check that the chart's average uses the close and the same period, and that it is the same type (simple, weighted or exponential).

More indicator formulas, with Excel sheets

Turn your own rules into automated orders.

Already use SMA & WMA in your own rules? MyAlgoMate's algo trading software places the orders your rules call for at 150+ Indian brokers — and TradeBridge turns your own TradingView or Chartink alerts into live orders. You decide the rules; we provide the technology.

Share this page