Testing Popular Ideas · Lesson 4
MACD
Three lines from two averages, three rules to read them, and the 63 versions hiding inside.
Know the basics? Read the lesson notesAn average that leans toward today
Lesson E2 used the simple moving average: the plain average of the last few closes (a close is a day's last price). The MACD is built from another kind, the exponential moving average, or EMA: each day it moves a fixed share of the way from its old value to the new close.
For a length of n days the share is 2 ÷ (n + 1). With n = 3 it is 2 ÷ 4 = ½: each day the EMA moves halfway to the new close. Its first value here is the simple average of the first 3 closes, (20 + 21 + 22) ÷ 3 = 21. Press Next day.
Six closes and their 3-day EMA (made up)
Dots: the closes. Orange: the EMA. Dotted: the 3-day simple average.
Day 4: 21 + ½ × (25 − 21) = 23. Day 5: 23 + ½ × (27 − 23) = 25. Day 6: 25 + ½ × (21 − 25) = 23. The simple average ends at 24.33: the newest close counts for half of this EMA but a third of the simple average, so the EMA fell further.
Fast minus slow: the MACD line
Now take two EMAs of the same closes: a 12-day one, called fast because it follows the newest closes more closely, and a 26-day one, called slow. The MACD line is the fast EMA minus the slow EMA. The name is short for moving average convergence divergence. The indicator is Gerald Appel's (his book is in the sources below the lesson).
Below is one year of a simulated market: prices made up by a computer, not a real market. It rises on average; beyond that, past prices say nothing about which way the next move goes (we built it so).
Closes, two EMAs and the MACD line, 2017 (simulated)
Top: the close (grey), the fast EMA (orange) and the slow EMA (dotted). Bottom: the MACD line.
On the last day of this year the MACD line is above zero. What does that tell you?
The signal line and the histogram
The signal line is a 9-day EMA of the MACD line itself, so it trails the MACD line the way an average trails a price. The histogram is the MACD line minus the signal line, drawn as bars: above zero when the MACD line is above its signal line.
12, 26 and 9 are the customary settings, written 12/26/9. They are a custom, not the result of a calculation. Try a shorter set and a longer one.
One year of the MACD, 2017 (simulated)
Top: the MACD line (dark) and its signal line (orange). Bottom: the histogram; orange bars are above zero, grey bars below.
In this year the histogram crossed zero 30 times with 8/20/6, 20 times with 12/26/9 and 10 times with 16/32/12. A rule that trades on those crossings trades that often, so the settings have to be fixed before a rule exists.
Three rules from one indicator
"Trade the MACD" is not a rule yet. Here are three common readings, written exactly. Each is either in the market (owning it) or out of it (holding cash).
- Signal-line cross: in the market while the MACD line is above the signal line, out otherwise.
- Zero-line cross: in the market while the MACD line is above zero, out otherwise. This is lesson E2's cross of two averages, here two EMAs; it never looks at the signal line.
- Histogram turn: in the market after a day the histogram rose (its bar is higher than the day before's, above zero or not), out after a day it fell.
Each rule decides at a day's close and trades at the next day's first price, because the close isn't known until trading ends (lesson E2). Show each on the same year, at 12/26/9.
Three rules on one year, 2017, at 12/26/9 (simulated)
Top: the close; shaded: days the chosen rule is in the market. Bottom: what that rule reads.
Same indicator, same settings, same year: the signal-line cross made 20 buys and sells, the zero-line cross 6 and the histogram turn 67. One name, three different sets of trades.
The customary settings against just holding
Start with the signal-line cross at the customary settings, 12/26/9, on six years of the simulated market (late 2015 to the end of 2021). Compare it with just holding: buying on the first day and never selling. Results are in % a year: the steady yearly growth that gives the same total.
Every buy and every sell costs 0.1% of the price, for the broker's fee and a slightly worse price than the chart shows (lesson A16, Costs). Holding pays that once, which is lesson E1's rule.
Holding made 6.3% a year. After costs, where did the signal-line cross end?
The signal-line cross at 12/26/9 and holding, 2015–2021 (simulated)
Accounts starting at 100. Orange: the signal-line cross after costs. Grey: before costs. Dotted: holding.
Before costs it made 5.7% a year. Its buys and sells, about 20 a year, cost 2.1 of that and left 3.6% a year: behind holding's 6.3%.
One market is one draw
Was that market typical? We built 1,000 more simulated markets the same way, each with its own random moves, and ran the same signal-line cross on every one.
A result's lead is its % a year minus holding's, in points (a point is 1% a year). On the market above the lead was 3.6 − 6.3 = −2.7 points a year.
The lead over holding on 1,000 simulated markets, after costs
Each bar: how many markets had a lead of that size. Orange: ahead of holding.
After costs it beat holding in 135 of the 1,000 markets and trailed it in 865, so the market above was typical. The average lead was −6.4 points a year.
Why: these markets rise on average and it was in them on only about half the days (50.5% on average), and it paid about 2 points a year in costs.
These markets were built so that past prices say nothing about which way the next move goes, so the counts show what these rules and costs produce when there is nothing to find. They cannot show whether the MACD works in any real market.
How many versions?
The tempting next move is to try other settings and rules. Take three lengths for each setting (fast 8, 12 or 16 days; slow 20, 26 or 32; signal 6, 9 or 12) and the three rules. Each different set of trades is a version: the same idea with different settings (lesson E1, How we test an idea). The one just tested is the customary version.
One catch: the zero-line cross never reads the signal line. How many versions is that?
All 63, on one market and on 1,000
Here are all 63 versions on the same market and the same years, 2015 to 2021. Call those the choosing years: a version will be chosen on them. Each bar is one version's lead over holding. Show them, then turn the costs on.
63 versions: each one's lead over holding, 2015–2021 (simulated)
One bar per version, grouped by rule. Above the line: beat holding. Black bar: the customary version. With costs on, a dark tick shows each version's before-costs lead.
Before costs 39 of the 63 beat holding; after costs, 26. On this market the histogram turn looks like a find: its best version, at 16/20/12, led holding by 12.7 points a year before costs and by 6.0 after: costs took 6.7. The customary version came 41st of the 63: its place, counting from the largest lead after costs.
For the precise
The best version makes about 58 buys and sells a year. Its costs come to 6.7 points, more than 58 × 0.1% = 5.8, because each cost is a share of the whole account, and before costs this account grew 19% a year: 1.1906 × 0.99958.28 = 1.1232, so 19.06% a year becomes 12.32%.A find, or the luck of the best of many (lesson B7, The best of many)? Run the same 63 versions on the 1,000 markets.
Across the 1,000 markets the best version after costs was a histogram turn in only 63 of them (a zero-line cross in 538, a signal-line cross in 399). "Best" means the largest lead, even one below zero: in 637 markets no version beat holding after costs, and on average 5.6 of the 63 did.
So the market above was an unusual draw: only 83 of the 1,000 had 26 or more versions ahead after costs. The customary version's place averaged 23.7 of 63; a version drawn at random averages 32.
Does the best stay best?
The last of lesson E1's checks. Before any version was run, 2022 to 2024 were set aside: years held out to be used once (lesson B10, The held-out test). Take the best version on the choosing years, the histogram turn at 16/20/12, and run it once on the held-out years, costs included.
The best of 63 on three held-out years (simulated)
Accounts starting at 100 at the end of 2021. Orange: the best version of 2015–2021, after costs. Dotted: holding.
On the held-out years, after costs, it made 3.2% a year against holding's 10.0%: a lead of −6.8 points a year, 48th of the 63. Again, one market.
So on each of the 1,000 markets we took the best of the 63 on the choosing years, the pick, and ran it on the held-out years. If picking meant nothing at all, the pick would be the best there as often as a blind draw, one version taken at random: in 1 market of 63, which is 1,000 ÷ 63, about 16 markets. Guess how often it stayed best, then run them.
The pick's held-out place on 1,000 simulated markets
One bar per place, 1st on the left: how many markets' picks landed there. Dashed: a blind draw.
The pick stayed best in 46 of the 1,000 markets: more than a blind draw's 16. After costs it beat holding on the held-out years in 207 markets; the customary version, with no picking at all, in 208.
Why 46 and not 16? In 538 markets the pick was a zero-line cross. Only 9 of the 63 versions are zero-line crosses, so a blind draw picks one about 9 times in 63: 143 markets in 1,000. That rule on average traded least and was in the market most. In these rising markets both would lift a rule's place in any stretch of years; neither is a forecast of prices. That is what the averages suggest, not a controlled test. Choosing settings added nothing: after costs the pick's held-out place averaged 25.2 of 63, the same as all versions of its rule (a blind draw averages 32).
For the precise
Buys and sells a year, averaged over each rule's versions and the 1,000 markets: zero-line cross 9.5, signal-line cross 21.8, histogram turn 72.1. Days in the market: 58%, against about half for the other two.The exponential moving average
An exponential moving average (EMA) is a running average: each day it moves a share α of the way from yesterday's EMA to today's close. For a length of n days, α = 2 ÷ (n + 1), the smoothing factor. No close ever drops out: each counts for (1 − α) times what it counted the day before, hence "exponential".
- Ct
- the close on day t
- EMAt−1
- yesterday's EMA; in this course the first one, on day n, is the simple average of the first n closes
- α
- today's close's share of the new EMA (the Greek letter alpha)
- n
- the length, in days
n = 3, so α = 2 ÷ 4 = 0.5. First value: (20 + 21 + 22) ÷ 3 = 21. Then 21 + 0.5 × (25 − 21) = 23, then 23 + 0.5 × (27 − 23) = 25, then 25 + 0.5 × (21 − 25) = 23.
- the closes are in order, one per day, none missing or wrong;
- the EMA has run a few times n days, so its first value no longer matters (every test here started after 200 days).
Two tools start the EMA differently (from the simple average of the first n closes, or from the first close alone): for the first weeks their values differ, and so can the trades. A wrong price also stays in an EMA for many days (lesson B4, Data quality).
Try it on new numbers. A 19-day EMA was 40.0 yesterday, and today's close is 43.0. What is today's EMA?
The three lines of the MACD
The MACD (moving average convergence divergence) is three lines built from the close with three lengths: fast, slow and signal. With the customary lengths the MACD line needs 26 closes before it exists, and the signal line 34 (26 + 9 − 1).
- EMAfast(C), EMAslow(C)
- EMAs of the close with the fast and the slow length (12 and 26 days by custom)
- EMAsig(MACD)
- an EMA of the MACD line, with the signal length (9 days by custom); in this course it starts from the simple average of its first 9 values
Fast EMA 137.65, slow EMA 136.47: MACD = 137.65 − 136.47 = 1.18. The signal line was 0.73 the day before, and its α is 2 ÷ (9 + 1) = 0.2: 0.73 + 0.2 × (1.18 − 0.73) = 0.73 + 0.09 = 0.82. Histogram = 1.18 − 0.82 = 0.36.
- all three lengths are stated: "MACD" alone doesn't say which;
- each EMA has run long enough to forget its first value.
Values are compared across prices of different sizes: the MACD is in price units, so 1.18 on a price near 140 is under 1% of it, and on a price near 14 over 8%.
Try it. Yesterday's signal line was 0.40. Today the MACD line is 0.90, and the signal length is 9. What is today's histogram?
What this means for your test
Lesson E1's checks, on the MACD.
- Write the exact rule before you run it.
"MACD" names an indicator, not a rule. Say which line the rule reads, the three lengths, the price each trade gets and the costs.
- Subtract the costs, then compare with just holding.
In this simulation the customary version beat holding after costs in 135 of 1,000 markets.
- Count the versions, then hold out years.
63 here. After costs, the best of 63 stayed best on held-out years in 46 of 1,000 simulated markets; a blind draw would in about 16.
A friend's spreadsheet
What should it be?
Hint: the share is 2 ÷ (n + 1). Move that share of the way from 30 to 35.
"Beat the market by 6 points a year"
Which question tells you the most?
Lesson done
The MACD is three lines: a fast EMA minus a slow EMA, an EMA of that difference, and the gap between the two. Which line a rule reads and which three lengths it uses made 63 versions here. These markets were built so that past prices say nothing about which way the next move goes, so the counts show what these rules and costs produce when there is nothing to find: after costs the customary version trailed just holding in 865 of 1,000 markets, and the best of 63 stayed best on held-out years in 46 (a blind draw: about 16). They cannot show whether the MACD works in any real market.
Key ideas
- EMA
- a running average that moves a share α = 2 ÷ (n + 1) of the way to each new close; in this course it starts from the simple average of the first n closes
- MACD line
- the fast EMA minus the slow EMA of the close (12 and 26 days by custom)
- Signal line
- an EMA of the MACD line (9 days by custom)
- Histogram
- the MACD line minus the signal line
- Version
- the same idea with different settings and its own set of trades; every one tried is counted
Formulas
EMAt = EMAt−1 + α × (Ct − EMAt−1), α = 2 ÷ (n + 1)
MACD = EMAfast(C) − EMAslow(C) · signal = EMAsig(MACD) · histogram = MACD − signal