Exponential moving average (EMA): the formula and how to calculate it (with Excel)
An exponential moving average (EMA) is an average of past closing prices that gives the newest close the most weight. Here is how it is worked out — the multiplier, where it starts and how each new close updates it — with a worked example of the 20 EMA, EMA vs SMA, the Excel formula, Python, a free Excel sheet and a box to calculate the EMA of your own prices for any period.
Last updated:
Needs: closing prices · Standard settings: Period 20
The EMA formula
Multiplier k = 2 ÷ (n + 1) (20 EMA: k = 2 ÷ 21 = 0.0952)
First EMA = simple average of the first n closes
EMA today = (Close − EMA yesterday) × k + EMA yesterday
= Close × k + EMA yesterday × (1 − k)So each day the EMA moves a fraction k of the way from yesterday's EMA towards today's close. With k = 0.0952, the newest close counts for about 9.5% of the 20 EMA, the one before it for 9.5% of the rest, and so on.
How to calculate EMA step by step
- Choose the period and work out k. For the 20 EMA, k = 2 ÷ 21 = 0.0952.
- Start with a simple average. The first EMA, on Day 20, is the average of the first 20 closes: 1,018.39.
- Update it with the next close. Day 21 closed at 978.30: (978.30 − 1,018.39) × 0.0952 + 1,018.39 = 1,014.57.
- Keep going, one candle at a time. Day 22 (close 958.85): (958.85 − 1,014.57) × 0.0952 + 1,014.57 = 1,009.27. By Day 60 the example's 20 EMA is 959.64.
Worked example — and calculate EMA 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 EMA for your own stock, clear the box and paste your closing prices from Excel or your broker's download.
Latest values
| Day | Close | EMA (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 |
| Day 21 | 978.30 | 1,014.5742 |
| Day 22 | 958.85 | 1,009.2671 |
| Day 23 | 946.75 | 1,003.3131 |
| Day 24 | 937.95 | 997.0880 |
| Day 25 | 927.85 | 990.4939 |
| Day 26 | 911.20 | 982.9421 |
| Day 27 | 905.15 | 975.5334 |
| Day 28 | 885.95 | 967.0016 |
| Day 29 | 874.70 | 958.2110 |
| Day 30 | 855.80 | 948.4576 |
| Day 31 | 862.15 | 940.2378 |
| Day 32 | 866.45 | 933.2104 |
| Day 33 | 845.10 | 924.8189 |
| Day 34 | 851.55 | 917.8409 |
| Day 35 | 860.20 | 912.3513 |
| Day 36 | 854.80 | 906.8702 |
| Day 37 | 877.20 | 904.0445 |
| Day 38 | 888.45 | 902.5593 |
| Day 39 | 905.40 | 902.8298 |
| Day 40 | 896.80 | 902.2556 |
| Day 41 | 889.60 | 901.0503 |
| Day 42 | 895.30 | 900.5026 |
| Day 43 | 919.00 | 902.2643 |
| Day 44 | 918.35 | 903.7963 |
| Day 45 | 906.25 | 904.0300 |
| Day 46 | 932.10 | 906.7033 |
| Day 47 | 944.35 | 910.2887 |
| Day 48 | 948.70 | 913.9469 |
| Day 49 | 966.35 | 918.9377 |
| Day 50 | 955.60 | 922.4293 |
| Day 51 | 969.10 | 926.8742 |
| Day 52 | 984.35 | 932.3480 |
| Day 53 | 980.90 | 936.9720 |
| Day 54 | 962.50 | 939.4033 |
| Day 55 | 960.20 | 941.3839 |
| Day 56 | 967.35 | 943.8569 |
| Day 57 | 971.00 | 946.4419 |
| Day 58 | 981.75 | 949.8046 |
| Day 59 | 994.85 | 954.0946 |
| Day 60 | 1,012.35 | 959.6428 |
The example prices are made up for this page — they are not real market data. EMA 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 EMA 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
EMA (20)
C21
=AVERAGE(B2:B21)— this cell only (the starting value).C22
=(B22-C21)*2/21+C21— then copy it down to the last row.
Rows 2 to 20 have no EMA: the first value needs 20 closes.
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 EMA Excel sheet (.xlsx)EMA vs SMA: what is different
Both are averages of past closes. A simple moving average (SMA) gives each of the last n closes the same weight and drops the oldest one completely when a new candle arrives. An exponential moving average gives the newest close the most weight and every older close a little less — it never drops a price completely, the price just counts for less and less. That is why an EMA reacts sooner when prices change, and why it changes more smoothly when an old price leaves the window.
The same example prices, 20-period averages, on the same days:
| Day | Close | EMA (20) | SMA (20) |
|---|---|---|---|
| Day 20 | 960.90 | 1,018.39 | 1,018.39 |
| Day 25 | 927.85 | 990.49 | 1,005.67 |
| Day 30 | 855.80 | 948.46 | 972.55 |
| Day 40 | 896.80 | 902.26 | 894.53 |
| Day 50 | 955.60 | 922.43 | 899.18 |
| Day 60 | 1,012.35 | 959.64 | 953.00 |
On Day 20 they are equal, because the EMA starts from the simple average. After that the EMA follows the newest closes more closely. The SMA calculation is on the moving average formula page.
How much weight each candle gets
Written out, today's close gets the weight k, yesterday's close k × (1 − k), the close before that k × (1 − k)², and so on — each older close counts (1 − k) times as much as the next one. With k = 2 ÷ (n + 1), about 86.5% of the total weight sits on the latest n candles, whatever the period. The rest is spread thinly over everything older, including the starting value.
| Period (n) | Multiplier k | Weight on latest n candles | Candles until start value < 1% |
|---|---|---|---|
| 5 EMA | 0.3333 | 86.8% | 12 |
| 9 EMA | 0.2000 | 86.6% | 21 |
| 12 EMA | 0.1538 | 86.5% | 28 |
| 20 EMA | 0.0952 | 86.5% | 47 |
| 26 EMA | 0.0741 | 86.5% | 60 |
| 50 EMA | 0.0392 | 86.5% | 116 |
| 100 EMA | 0.0198 | 86.5% | 231 |
| 200 EMA | 0.0100 | 86.5% | 461 |
The last column is why the starting point matters: a 20 EMA needs about 47 candles after its first value before that first value counts for less than 1%, and a 200 EMA needs about 461. Compare EMAs only after that much history, or two tools that started on different days will show different numbers.
Why the EMA lags the price
Every moving average is made only of past prices, so it always trails the latest price — it describes where the price has been, not where it will go. One way to measure the delay is the average age of the prices inside it. For an SMA of n candles the average age is (n − 1) ÷ 2 candles, and for an EMA with k = 2 ÷ (n + 1) it works out to the same (n − 1) ÷ 2: 9.5 candles for the 20 EMA, 99.5 for the 200 EMA. The EMA still turns sooner because its weight is packed onto the newest candles instead of being spread evenly.
- Longer period, more lag. A 200 EMA moves far more slowly than a 9 EMA.
- Sideways prices. When the price moves back and forth, it keeps crossing its EMA in both directions.
- Settings change the value. The period, the price used (close, open, high or low) and the starting point all change the number — check them before comparing two charts.
What an EMA crossover is
A crossover is the candle where a shorter EMA moves from below a longer EMA to above it, or the other way round — for example the 9 EMA crossing the 20 EMA. Charts also mark the price itself crossing an EMA. Chart sites give some pairs names, such as a "golden cross" or "death cross" for the 50 and the 200. A crossover only describes how two averages of past prices compare; it is not a forecast or advice. The MACD turns the gap between the 12 and the 26 EMA into one line, so a crossover of those two is where the MACD line passes zero.
EMA in Python
pandas has the EMA built in. With adjust=False it uses the same one-line update as the formula above, but it starts from the first close instead of a simple average. The second part below starts the way this page and the Excel sheet do:
import pandas as pd
df = pd.read_csv("prices.csv") # a column named Close
n = 20
# pandas' own EMA (starts from the first close)
df["ema_pandas"] = df["Close"].ewm(span=n, adjust=False).mean()
# Same as this page and the Excel sheet: start from the average of the first n closes
k = 2 / (n + 1)
ema = [None] * len(df)
ema[n - 1] = df["Close"].iloc[:n].mean()
for i in range(n, len(df)):
ema[i] = (df["Close"].iloc[i] - ema[i - 1]) * k + ema[i - 1]
df["ema"] = emaThe two columns differ at first and come together as more candles are added — the table above shows how many.
Common EMA periods
The formula is the same for every period — only n and therefore k change. Charts often show the 9, 20, 50 and 200 EMA; the calculator above takes any period from 2 to 200. A longer EMA needs more history to settle: for a 200 EMA, start from several hundred candles.
The EMA is also a building block of other indicators: the MACD formula is the difference between two EMAs, and Wilder's smoothing in the RSI and ATR is an EMA with k = 1 ÷ n. For the simple and weighted versions, see the moving average formula.
Why your EMA may not match your chart
- How much history went in. The difference fades as you add candles before the date you check.
- How it starts. A simple-average start (this page and most charts) vs starting from the first close.
- Which price. The close is usual, but some charts can use another source (such as (high + low) ÷ 2), and spot and futures prices differ.
An EMA describes past prices. It is a calculation, not a forecast or advice.
EMA calculation — questions, answered.
EMA today = (Close − EMA yesterday) × k + EMA yesterday, where k = 2 ÷ (n + 1). The first EMA is the simple average of the first n closes. Written the other way round it is EMA = Close × k + EMA yesterday × (1 − k).
Closes in column B from row 2. For a 20-period EMA, type =AVERAGE(B2:B21) in C21 (the first EMA), then =(B22-C21)*2/21+C21 in C22 and copy it down. Excel has no built-in EMA function, so this one formula is all you need. The free sheet on this page has it filled in.
k = 2 ÷ (n + 1): 9 EMA → 2 ÷ 10 = 0.2; 20 EMA → 2 ÷ 21 ≈ 0.0952; 50 EMA → 2 ÷ 51 ≈ 0.0392; 200 EMA → 2 ÷ 201 ≈ 0.00995. A longer EMA gives each new close less weight, so it moves more slowly.
An SMA gives the last n closes equal weight and forgets everything older. An EMA gives the newest close the most weight, and the weights of older closes shrink step by step without ever reaching zero. So an EMA reacts sooner to a new price, and it never has a hard cut-off.
An EMA never fully forgets old prices, so it depends on where you start. Starting from 60 candles gives a slightly different value from starting from 1,000 candles; the gap shrinks as you add history. Some tools also start the EMA from the first close instead of a simple average. Spot vs futures prices and the timeframe matter too.
An exponential moving average (EMA) is an average of past closing prices in which the newest close counts the most and each older close counts a little less. It is recalculated on every new candle, so it follows the price more closely than a simple average of the same length.
Lagging. It is built only from prices that have already happened, so it always trails the latest price. A shorter EMA trails less than a longer one, and an EMA turns sooner than a simple moving average of the same length because it puts more weight on the newest candles.
With pandas: df['Close'].ewm(span=20, adjust=False).mean() uses k = 2 ÷ (20 + 1). It starts from the first close, not from a 20-close average, so its early values differ from this page and the Excel sheet. To match exactly, set the first EMA to the average of the first 20 closes and apply the formula from there (code on this page).
It uses a 'damping factor', which is 1 − k, but it is set up for forecasting and starts differently, so its numbers won't line up with a chart's EMA. The one-line formula above is simpler and matches how charts calculate it.
More indicator formulas, with Excel sheets
Turn your own rules into automated orders.
Already use EMA 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.