MyAlgoMate
// INDICATOR FORMULA

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:

Download the free EMA Excel sheet (.xlsx)

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

  1. Choose the period and work out k. For the 20 EMA, k = 2 ÷ 21 = 0.0952.
  2. Start with a simple average. The first EMA, on Day 20, is the average of the first 20 closes: 1,018.39.
  3. 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.
  4. 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.

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

EMA (20): 959.6428
EMA calculated from the example prices
DayCloseEMA (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.3925
Day 21978.301,014.5742
Day 22958.851,009.2671
Day 23946.751,003.3131
Day 24937.95997.0880
Day 25927.85990.4939
Day 26911.20982.9421
Day 27905.15975.5334
Day 28885.95967.0016
Day 29874.70958.2110
Day 30855.80948.4576
Day 31862.15940.2378
Day 32866.45933.2104
Day 33845.10924.8189
Day 34851.55917.8409
Day 35860.20912.3513
Day 36854.80906.8702
Day 37877.20904.0445
Day 38888.45902.5593
Day 39905.40902.8298
Day 40896.80902.2556
Day 41889.60901.0503
Day 42895.30900.5026
Day 43919.00902.2643
Day 44918.35903.7963
Day 45906.25904.0300
Day 46932.10906.7033
Day 47944.35910.2887
Day 48948.70913.9469
Day 49966.35918.9377
Day 50955.60922.4293
Day 51969.10926.8742
Day 52984.35932.3480
Day 53980.90936.9720
Day 54962.50939.4033
Day 55960.20941.3839
Day 56967.35943.8569
Day 57971.00946.4419
Day 58981.75949.8046
Day 59994.85954.0946
Day 601,012.35959.6428
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. 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:

  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

    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:

20-period EMA and SMA on the example prices
DayCloseEMA (20)SMA (20)
Day 20960.901,018.391,018.39
Day 25927.85990.491,005.67
Day 30855.80948.46972.55
Day 40896.80902.26894.53
Day 50955.60922.43899.18
Day 601,012.35959.64953.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.

EMA period, multiplier, weight on the latest n candles, and candles until the starting value counts for less than 1%
Period (n)Multiplier kWeight on latest n candlesCandles until start value < 1%
5 EMA0.333386.8%12
9 EMA0.200086.6%21
12 EMA0.153886.5%28
20 EMA0.095286.5%47
26 EMA0.074186.5%60
50 EMA0.039286.5%116
100 EMA0.019886.5%231
200 EMA0.010086.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"] = ema

The 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.

// FAQ

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.

Share this page