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:
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 line2 ÷ (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
- 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.
- 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.
- Subtract to get the MACD line. Day 26: 963.40 − 1,001.11 = −37.70.
- 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.
- Subtract again for the histogram. Day 35: −48.31 − −45.93 = −2.38.
- 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.
Latest values
| Day | Close | EMA 12 | EMA 26 | MACD line | Signal (9) | Histogram |
|---|---|---|---|---|---|---|
| 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 | 1,016.4500 | ||||
| Day 13 | 1,083.55 | 1,026.7731 | ||||
| Day 14 | 1,067.95 | 1,033.1080 | ||||
| Day 15 | 1,063.50 | 1,037.7837 | ||||
| Day 16 | 1,030.20 | 1,036.6170 | ||||
| Day 17 | 1,012.20 | 1,032.8605 | ||||
| Day 18 | 981.60 | 1,024.9743 | ||||
| Day 19 | 970.55 | 1,016.6013 | ||||
| Day 20 | 960.90 | 1,008.0319 | ||||
| Day 21 | 978.30 | 1,003.4577 | ||||
| Day 22 | 958.85 | 996.5950 | ||||
| Day 23 | 946.75 | 988.9265 | ||||
| Day 24 | 937.95 | 981.0840 | ||||
| Day 25 | 927.85 | 972.8942 | ||||
| Day 26 | 911.20 | 963.4027 | 1,001.1058 | −37.7030 | ||
| Day 27 | 905.15 | 954.4408 | 993.9979 | −39.5571 | ||
| Day 28 | 885.95 | 943.9037 | 985.9944 | −42.0906 | ||
| Day 29 | 874.70 | 933.2570 | 977.7504 | −44.4933 | ||
| Day 30 | 855.80 | 921.3405 | 968.7170 | −47.3764 | ||
| Day 31 | 862.15 | 912.2343 | 960.8231 | −48.5888 | ||
| Day 32 | 866.45 | 905.1906 | 953.8325 | −48.6420 | ||
| Day 33 | 845.10 | 895.9459 | 945.7783 | −49.8324 | ||
| Day 34 | 851.55 | 889.1157 | 938.7984 | −49.6827 | −45.3296 | −4.3531 |
| Day 35 | 860.20 | 884.6672 | 932.9763 | −48.3091 | −45.9255 | −2.3836 |
| Day 36 | 854.80 | 880.0722 | 927.1855 | −47.1133 | −46.1631 | −0.9502 |
| Day 37 | 877.20 | 879.6303 | 923.4828 | −43.8525 | −45.7010 | 1.8484 |
| Day 38 | 888.45 | 880.9872 | 920.8878 | −39.9006 | −44.5409 | 4.6403 |
| Day 39 | 905.40 | 884.7430 | 919.7406 | −34.9975 | −42.6322 | 7.6347 |
| Day 40 | 896.80 | 886.5979 | 918.0413 | −31.4433 | −40.3944 | 8.9511 |
| Day 41 | 889.60 | 887.0598 | 915.9345 | −28.8747 | −38.0905 | 9.2158 |
| Day 42 | 895.30 | 888.3275 | 914.4060 | −26.0785 | −35.6881 | 9.6096 |
| Day 43 | 919.00 | 893.0464 | 914.7463 | −21.7000 | −32.8905 | 11.1905 |
| Day 44 | 918.35 | 896.9392 | 915.0133 | −18.0740 | −29.9272 | 11.8532 |
| Day 45 | 906.25 | 898.3717 | 914.3641 | −15.9925 | −27.1402 | 11.1478 |
| Day 46 | 932.10 | 903.5606 | 915.6779 | −12.1173 | −24.1356 | 12.0184 |
| Day 47 | 944.35 | 909.8359 | 917.8018 | −7.9658 | −20.9017 | 12.9358 |
| Day 48 | 948.70 | 915.8150 | 920.0905 | −4.2755 | −17.5764 | 13.3009 |
| Day 49 | 966.35 | 923.5896 | 923.5171 | 0.0725 | −14.0467 | 14.1191 |
| Day 50 | 955.60 | 928.5143 | 925.8937 | 2.6206 | −10.7132 | 13.3338 |
| Day 51 | 969.10 | 934.7583 | 929.0941 | 5.6641 | −7.4377 | 13.1019 |
| Day 52 | 984.35 | 942.3878 | 933.1871 | 9.2006 | −4.1101 | 13.3107 |
| Day 53 | 980.90 | 948.3127 | 936.7214 | 11.5913 | −0.9698 | 12.5611 |
| Day 54 | 962.50 | 950.4954 | 938.6310 | 11.8644 | 1.5970 | 10.2674 |
| Day 55 | 960.20 | 951.9884 | 940.2287 | 11.7597 | 3.6296 | 8.1301 |
| Day 56 | 967.35 | 954.3517 | 942.2377 | 12.1141 | 5.3265 | 6.7876 |
| Day 57 | 971.00 | 956.9130 | 944.3682 | 12.5448 | 6.7701 | 5.7747 |
| Day 58 | 981.75 | 960.7341 | 947.1372 | 13.5968 | 8.1355 | 5.4614 |
| Day 59 | 994.85 | 965.9827 | 950.6715 | 15.3112 | 9.5706 | 5.7406 |
| Day 60 | 1,012.35 | 973.1161 | 955.2403 | 17.8758 | 11.2317 | 6.6442 |
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:
- A
Day
A2 downwards: your dates, oldest at the top.
- B
Close
B2 downwards: the closing prices, oldest at the top.
- 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. - 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. - E
MACD line
E27
=C27-D27— then copy it down to the last row. - 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. - 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
| Part | How it is made | What it is |
|---|---|---|
| MACD line | 12 EMA − 26 EMA | Above zero when the 12 EMA is above the 26 EMA, below zero when it is under it |
| Signal line | 9 EMA of the MACD line | A smoothed copy of the MACD line, so it moves more slowly |
| Histogram | MACD line − signal line | The 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.
| Day | Close | MACD line | MACD line (closes × 10) | Ratio |
|---|---|---|---|---|
| Day 26 | 911.20 | -37.7030 | -377.0302 | 10.00 × |
| Day 35 | 860.20 | -48.3091 | -483.0914 | 10.00 × |
| Day 60 | 1012.35 | 17.8758 | 178.7583 | 10.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.
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.