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:
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
- Take the last 20 closes. The first full set is Day 1 to Day 20, so both averages start on Day 20.
- 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.
- 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.
- 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.
Latest values
| Day | Close | SMA (20) | WMA (20) |
|---|---|---|---|
| Day 1 | 1,001.15 | ||
| Day 2 | 987.95 | ||
| Day 3 | 1,009.15 | ||
| Day 4 | 1,012.60 | ||
| Day 5 | 993.25 | ||
| Day 6 | 1,008.60 | ||
| Day 7 | 1,013.60 | ||
| Day 8 | 1,001.55 | ||
| Day 9 | 1,031.55 | ||
| Day 10 | 1,040.05 | ||
| Day 11 | 1,033.15 | ||
| Day 12 | 1,064.80 | ||
| Day 13 | 1,083.55 | ||
| Day 14 | 1,067.95 | ||
| Day 15 | 1,063.50 | ||
| Day 16 | 1,030.20 | ||
| Day 17 | 1,012.20 | ||
| Day 18 | 981.60 | ||
| Day 19 | 970.55 | ||
| Day 20 | 960.90 | 1,018.3925 | 1,019.1183 |
| Day 21 | 978.30 | 1,017.2500 | 1,015.3000 |
| Day 22 | 958.85 | 1,015.7950 | 1,009.7381 |
| Day 23 | 946.75 | 1,012.6750 | 1,003.1624 |
| Day 24 | 937.95 | 1,008.9425 | 996.0457 |
| Day 25 | 927.85 | 1,005.6725 | 988.3226 |
| Day 26 | 911.20 | 1,000.8025 | 979.3252 |
| Day 27 | 905.15 | 995.3800 | 970.2155 |
| Day 28 | 885.95 | 989.6000 | 959.7936 |
| Day 29 | 874.70 | 981.7575 | 948.8507 |
| Day 30 | 855.80 | 972.5450 | 936.8548 |
| Day 31 | 862.15 | 963.9950 | 926.3410 |
| Day 32 | 866.45 | 954.0775 | 917.0510 |
| Day 33 | 845.10 | 942.1550 | 906.6721 |
| Day 34 | 851.55 | 931.3350 | 898.0431 |
| Day 35 | 860.20 | 921.1700 | 891.2683 |
| Day 36 | 854.80 | 912.4000 | 884.9474 |
| Day 37 | 877.20 | 905.6500 | 881.5950 |
| Day 38 | 888.45 | 900.9925 | 879.9569 |
| Day 39 | 905.40 | 897.7350 | 880.3767 |
| Day 40 | 896.80 | 894.5300 | 880.2876 |
| Day 41 | 889.60 | 890.0950 | 879.8181 |
| Day 42 | 895.30 | 886.9175 | 880.3138 |
| Day 43 | 919.00 | 885.5300 | 883.3693 |
| Day 44 | 918.35 | 884.5500 | 886.4950 |
| Day 45 | 906.25 | 883.4700 | 888.5617 |
| Day 46 | 932.10 | 884.5150 | 893.1931 |
| Day 47 | 944.35 | 886.4750 | 898.8917 |
| Day 48 | 948.70 | 889.6125 | 904.8179 |
| Day 49 | 966.35 | 894.1950 | 912.1262 |
| Day 50 | 955.60 | 899.1850 | 917.9743 |
| Day 51 | 969.10 | 904.5325 | 924.6329 |
| Day 52 | 984.35 | 910.4275 | 932.2345 |
| Day 53 | 980.90 | 917.2175 | 938.9462 |
| Day 54 | 962.50 | 922.7650 | 943.2588 |
| Day 55 | 960.20 | 927.7650 | 946.8240 |
| Day 56 | 967.35 | 933.3925 | 950.5940 |
| Day 57 | 971.00 | 938.0825 | 954.1757 |
| Day 58 | 981.75 | 942.7475 | 958.3345 |
| Day 59 | 994.85 | 947.2200 | 963.2967 |
| Day 60 | 1,012.35 | 952.9975 | 969.4995 |
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:
- A
Day
A2 downwards: your dates, oldest at the top.
- B
Close
B2 downwards: the closing prices, oldest at the top.
- C
SMA (20)
C21
=AVERAGE(B2:B21)— then copy it down to the last row. - 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
| Type | How the prices are weighted | Calculation |
|---|---|---|
| SMA | All n prices count the same | Sum of the n closes ÷ n |
| WMA | Newest counts most; weights 1 to n in equal steps | Weighted sum ÷ n × (n + 1) ÷ 2 |
| EMA | Newest counts most; weights shrink by a fixed ratio | Multiplier 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.
| Period n | SMA lag (n − 1) ÷ 2 | WMA lag (n − 1) ÷ 3 |
|---|---|---|
| 5 | 2 | 1.33 |
| 20 | 9.5 | 6.33 |
| 50 | 24.5 | 16.33 |
| 200 | 99.5 | 66.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.
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.