MyAlgoMate
// INDICATOR FORMULA

MACD formula: how to calculate MACD (with Excel)

How the Moving Average Convergence Divergence is worked out — the MACD line, the signal line and the histogram, one column at a time. With a worked example, the Excel formulas, a free Excel sheet and a box to calculate MACD from your own prices.

Last updated:

Download the free MACD Excel sheet (.xlsx)

Needs: closing prices · Standard settings: Fast EMA 12 · Slow EMA 26 · Signal 9

The MACD formula

EMA (n):  first value = simple average of the first n closes
          next value  = (Close − previous EMA) × 2 ÷ (n + 1) + previous EMA

MACD line   = EMA (12) − EMA (26)
Signal line = EMA (9) of the MACD line
Histogram   = MACD line − Signal line

2 ÷ (n + 1) is the EMA's weight: 2 ÷ 13 for the 12 EMA, 2 ÷ 27 for the 26 EMA and 2 ÷ 10 for the signal line. The EMA formula page explains it in more detail.

How to calculate MACD step by step

  1. Work out the 12-period EMA of the close. Its first value, on Day 12, is the simple average of the first 12 closes: 1,016.45. From Day 13 it follows the EMA formula: (1,083.55 − 1,016.45) × 2 ÷ 13 + 1,016.45 = 1,026.77.
  2. Work out the 26-period EMA the same way. Its first value is the average of the first 26 closes, on Day 26: 1,001.11. After that it uses 2 ÷ 27 as the weight.
  3. Subtract to get the MACD line. Day 26: 963.40 − 1,001.11 = −37.70.
  4. Work out the signal line, a 9-period EMA of the MACD line. It needs 9 MACD values (Day 26 to Day 34), so it starts on Day 34 with their simple average: −45.33. From Day 35: (−48.31 − −45.33) × 2 ÷ 10 + −45.33 = −45.93.
  5. Subtract again for the histogram. Day 35: −48.31 − −45.93 = −2.38.
  6. Repeat for every candle. On Day 60 the example's MACD line is 17.88, the signal line 11.23 and the histogram 6.64.

Worked example — and calculate MACD 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 MACD 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

MACD line: 17.8758Signal (9): 11.2317Histogram: 6.6442
MACD calculated from the example prices
DayCloseEMA 12EMA 26MACD lineSignal (9)Histogram
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.801,016.4500
Day 131,083.551,026.7731
Day 141,067.951,033.1080
Day 151,063.501,037.7837
Day 161,030.201,036.6170
Day 171,012.201,032.8605
Day 18981.601,024.9743
Day 19970.551,016.6013
Day 20960.901,008.0319
Day 21978.301,003.4577
Day 22958.85996.5950
Day 23946.75988.9265
Day 24937.95981.0840
Day 25927.85972.8942
Day 26911.20963.40271,001.1058−37.7030
Day 27905.15954.4408993.9979−39.5571
Day 28885.95943.9037985.9944−42.0906
Day 29874.70933.2570977.7504−44.4933
Day 30855.80921.3405968.7170−47.3764
Day 31862.15912.2343960.8231−48.5888
Day 32866.45905.1906953.8325−48.6420
Day 33845.10895.9459945.7783−49.8324
Day 34851.55889.1157938.7984−49.6827−45.3296−4.3531
Day 35860.20884.6672932.9763−48.3091−45.9255−2.3836
Day 36854.80880.0722927.1855−47.1133−46.1631−0.9502
Day 37877.20879.6303923.4828−43.8525−45.70101.8484
Day 38888.45880.9872920.8878−39.9006−44.54094.6403
Day 39905.40884.7430919.7406−34.9975−42.63227.6347
Day 40896.80886.5979918.0413−31.4433−40.39448.9511
Day 41889.60887.0598915.9345−28.8747−38.09059.2158
Day 42895.30888.3275914.4060−26.0785−35.68819.6096
Day 43919.00893.0464914.7463−21.7000−32.890511.1905
Day 44918.35896.9392915.0133−18.0740−29.927211.8532
Day 45906.25898.3717914.3641−15.9925−27.140211.1478
Day 46932.10903.5606915.6779−12.1173−24.135612.0184
Day 47944.35909.8359917.8018−7.9658−20.901712.9358
Day 48948.70915.8150920.0905−4.2755−17.576413.3009
Day 49966.35923.5896923.51710.0725−14.046714.1191
Day 50955.60928.5143925.89372.6206−10.713213.3338
Day 51969.10934.7583929.09415.6641−7.437713.1019
Day 52984.35942.3878933.18719.2006−4.110113.3107
Day 53980.90948.3127936.721411.5913−0.969812.5611
Day 54962.50950.4954938.631011.86441.597010.2674
Day 55960.20951.9884940.228711.75973.62968.1301
Day 56967.35954.3517942.237712.11415.32656.7876
Day 57971.00956.9130944.368212.54486.77015.7747
Day 58981.75960.7341947.137213.59688.13555.4614
Day 59994.85965.9827950.671515.31129.57065.7406
Day 601,012.35973.1161955.240317.875811.23176.6442
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. MACD 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 MACD 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 12

    C13 =AVERAGE(B2:B13) — this cell only (the starting value).

    C14 =(B14-C13)*2/13+C13 — then copy it down to the last row.

  4. D

    EMA 26

    D27 =AVERAGE(B2:B27) — this cell only (the starting value).

    D28 =(B28-D27)*2/27+D27 — then copy it down to the last row.

  5. E

    MACD line

    E27 =C27-D27 — then copy it down to the last row.

  6. F

    Signal (9)

    F35 =AVERAGE(E27:E35) — this cell only (the starting value).

    F36 =(E36-F35)*2/10+F35 — then copy it down to the last row.

  7. G

    Histogram

    G35 =E35-F35 — then copy it down to the last row.

MACD needs 26 closes before its first value and 34 before the first signal-line value, so the top rows of those columns stay empty.

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 MACD Excel sheet (.xlsx)

What is the MACD indicator?

MACD stands for Moving Average Convergence Divergence. It is a line drawn from past prices. It compares two moving averages of the closing price. Gerald Appel created it in the late 1970s (Wikipedia: MACD). It has three parts: the MACD line, the signal line and the histogram. It is made from past prices only. It does not forecast anything.

What the three parts show

The three parts of MACD
PartHow it is madeWhat it is
MACD line12 EMA − 26 EMAAbove zero when the 12 EMA is above the 26 EMA, below zero when it is under it
Signal line9 EMA of the MACD lineA smoothed copy of the MACD line, so it moves more slowly
HistogramMACD line − signal lineThe gap between the two lines, drawn as bars. It is not trading volume

The places where the lines cross each other or cross zero are simply where one number becomes larger than another. MACD is worked out from past closes only; it is a description of them, not a forecast, and how you use it is your own decision.

How the MACD lines are read

This is a list of the common names, not advice.

  • Signal-line crossover. The MACD line moves from one side of the signal line to the other. The histogram is zero at that point.
  • Zero-line crossover. The MACD line moves from one side of zero to the other. It means the 12 EMA and the 26 EMA have swapped places.
  • Divergence.Price and MACD move in different directions, for example price makes a new high while MACD does not. Some guides also name a "hidden" kind, where the pattern is the other way round.

These readings can be wrong. The lines can cross many times and then nothing follows. You decide what to do with them. MyAlgoMate gives you technology, not trading calls.

Where MACD has limits

  • It follows price. It is built from past closes, so it can only describe what has already happened.
  • Sideways prices. When price moves in a narrow range, the two lines can cross each other often.
  • Other settings. 12, 26 and 9 are the usual numbers. Some guides mention others, such as 5, 35 and 5. Different settings give different numbers.
  • It is in price units. See the scale test below.

Scale test: the same prices, ten times bigger

MACD is a difference of two averages of price, so it is in points, not percent. To see this, we ran the same calculation on the example closes and then on the same closes multiplied by 10. Every MACD value comes out 10 times bigger. The table is worked out by the same code as the calculator above.

MACD line on the example closes and on the same closes times 10
DayCloseMACD lineMACD line (closes × 10)Ratio
Day 26911.20-37.7030-377.030210.00 ×
Day 35860.20-48.3091-483.091410.00 ×
Day 601012.3517.8758178.758310.00 ×

This is why a high-priced stock shows bigger MACD numbers than a low-priced one. The percent version is called PPO.

Why your MACD may not match your chart

  • The starting point. Use plenty of history — 100 or more candles before the date you check — so the EMAs settle.
  • How the EMA starts. This page (and most charts) starts each EMA with a simple average; some tools start from the first close instead.
  • Signal line type. Some charts let the signal line be a simple average instead of an EMA.
  • Different prices. Spot vs futures, or a different timeframe.
// FAQ

MACD calculation — questions, answered.

MACD line = 12-period EMA of the close − 26-period EMA of the close. The signal line is a 9-period EMA of the MACD line, and the histogram is the MACD line minus the signal line. Each EMA starts with a simple average of its first values and then follows EMA = (value − previous EMA) × 2 ÷ (n + 1) + previous EMA.

Closes in column B from row 2. 12 EMA: in C13 =AVERAGE(B2:B13), from C14 =(B14-C13)*2/13+C13. 26 EMA: in D27 =AVERAGE(B2:B27), from D28 =(B28-D27)*2/27+D27. MACD line in E27: =C27-D27. Signal: in F35 =AVERAGE(E27:E35), from F36 =(E36-F35)*2/10+F35. Histogram in G35: =E35-F35. Copy each formula down. The free sheet on this page has it all filled in.

Histogram = MACD line − signal line, candle by candle. It is above zero when the MACD line is above the signal line and below zero when it is under it; its height is the gap between the two lines.

12 and 26 are the lengths of the fast and slow exponential moving averages of the close, and 9 is the length of the EMA used for the signal line. They are the settings Gerald Appel used in the 1970s and the default on most charts. Changing them changes every number on this page — the calculator lets you try other settings.

Mostly because of where the EMAs start. An EMA keeps a little of every earlier price, so MACD from 60 candles differs slightly from MACD from 1,000 candles — the gap fades with more history. Some charts also start an EMA from the first close instead of a simple average, or let you switch the signal line to a simple average.

Moving Average Convergence Divergence. The name describes the two moving averages it compares: when they move closer the MACD line shrinks towards zero, and when they move apart it grows.

No. The histogram is only the MACD line minus the signal line. It has nothing to do with the number of shares or contracts traded.

Yes. The calculator on this page has boxes for the fast, slow and signal lengths. Some charting guides mention other sets, such as 5, 35 and 5. Every number changes when you change the settings, so compare like with like.

In points — the same units as the price — because it is the difference between two moving averages of price. That is why MACD values of a high-priced stock look much larger than those of a low-priced one. The percentage version is called PPO: (12 EMA − 26 EMA) ÷ 26 EMA × 100.

More indicator formulas, with Excel sheets

Turn your own rules into automated orders.

Already use MACD 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