Quant from Scratch by QuantWright

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 notes

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

EMA–
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 MACD line compares two averages, not two days. It can stay above zero through a down day.
MACD = fast − slow, so above zero means the fast EMA is above the slow one.
True in this picture, but the MACD line doesn't tell you that: both averages look back weeks, not a year.

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.

Fast / slow / signal
The histogram crossed zero–

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

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.

Rule
Buys and sells that year–
Days in the market–

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.

Your guess: it ends
Before costs?
After costs?

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.

Ahead of holding?
Average lead?

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?

That is the settings alone: 3 × 3 × 3 = 27. Each rule reads them in its own way.
27 settings × 3 rules = 81 counts the zero-line cross 27 times. It ignores the signal length, so it has only 3 × 3 = 9 different sets of trades.
Signal-line cross: 3 × 3 × 3 = 27. Histogram turn: 27. Zero-line cross: 3 × 3 = 9, because it ignores the signal length. 27 + 27 + 9 = 63 versions.

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.

Costs
Beat holding?
The best?
The customary version?

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

Lead, 2015–2021–
Lead, 2022–2024?
Its place of 63 in 2022–2024?

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.

Stayed best?

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 preciseBuys 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.
Theory

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

EMAt = EMAt−1 + α × (Ct − EMAt−1),   α = 2 ÷ (n + 1)
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
Worked example: your six closes

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.

Holds when
  • 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).
Breaks when

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?

α = 2 ÷ 20 = 0.1, so 40.0 + 0.1 × (43.0 − 40.0) = 40.3.
α = 2 ÷ (19 + 1) = 0.1, so 40.0 + 0.1 × (43.0 − 40.0) = 40.3: a tenth of the way from 40 to 43.
Theory

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

MACDt = EMAfast(C)t − EMAslow(C)t
signalt = EMAsig(MACD)t
histogramt = MACDt − signalt
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
Worked example: the last day of your simulated 2017, at 12/26/9

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.

Holds when
  • all three lengths are stated: "MACD" alone doesn't say which;
  • each EMA has run long enough to forget its first value.
Breaks when

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?

Signal: 0.40 + 0.2 × (0.90 − 0.40) = 0.50. Histogram: 0.90 − 0.50 = 0.40.
First today's signal: α = 2 ÷ 10 = 0.2, so 0.40 + 0.2 × (0.90 − 0.40) = 0.50. Then the histogram: 0.90 − 0.50 = 0.40.

What this means for your test

Lesson E1's checks, on the MACD.

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

Practice 1 of 2

A friend's spreadsheet

The situationA friend codes a 4-day EMA. Yesterday's EMA was 30 and today's close is 35. Their sheet says today's EMA is 31.25.

What should it be?

Hint: the share is 2 ÷ (n + 1). Move that share of the way from 30 to 35.

α = 2 ÷ 5 = 0.4, so 30 + 0.4 × (35 − 30) = 32.
α = 2 ÷ (4 + 1) = 0.4, so 30 + 0.4 × (35 − 30) = 30 + 2 = 32.
Their 31.25 used a share of 1 ÷ 4. That is the smoothing lesson E3's RSI uses, not the EMA the MACD is built from.
Practice 2 of 2

"Beat the market by 6 points a year"

The situationAn invented sales page says: "Our MACD setting beat holding by 6 points a year from 2015 to 2021, after costs." The small print: it was the best of 63 versions, on one market.

Which question tells you the most?

This lesson's one market showed the same: a best of 63 leading by 6.0 points a year. On later years it came 48th of 63, and across 1,000 simulated markets the best of 63 beat holding on later years, after costs, in 207.
That share comes from the years the version was picked on, with the same luck. Ask for later years and other markets.
You'd need them to rebuild the version, but they don't show whether its lead was luck.
✓

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

Lesson notes

The MACD is three lines built from two exponential moving averages of the price. "Trade the MACD" is not one rule: which line a rule reads and which three lengths it uses made 63 versions in this lesson. The lesson's simulated markets were built so that past prices say nothing about which way the next move goes, so its counts show what these rules and costs produce when there is nothing to find. After costs, on 1,000 simulated markets, the customary version (the signal-line cross with fast, slow and signal lengths of 12, 26 and 9 days) trailed just holding in 865, and the best of the 63 on one stretch of years stayed best on the next in 46, against about 16 for a version drawn at random. The counts cannot show whether the MACD works in any real market; that needs its own test.

The exponential moving average

A close is a day's last price. An exponential moving average (EMA) of the closes moves, each day, 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); its first value, in this lesson, is the simple average of the first n closes. Six made-up closes, 20, 21, 22, 25, 27 and 21, with n = 3: the share is 2 ÷ 4 = ½ and the first value (20 + 21 + 22) ÷ 3 = 21. Then 21 + ½ × (25 − 21) = 23, 23 + ½ × (27 − 23) = 25, and 25 + ½ × (21 − 25) = 23.

The 3-day simple average, lesson E2's kind, ends at (25 + 27 + 21) ÷ 3 = 24.33. The newest close counts for half of this EMA and a third of the simple average, so the EMA fell further. No close ever drops out of an EMA: each counts for less every day.

The three lines

Take a fast EMA of the closes (12 days by custom) and a slow one (26 days). The MACD line is the fast EMA minus the slow EMA, so it is above zero exactly when the fast EMA is above the slow one. The signal line is an EMA of the MACD line (9 days by custom). The histogram is the MACD line minus the signal line. The settings are written 12/26/9; they are a custom, not the result of a calculation.

A simulated market is prices made up by a computer. The lesson's simulated markets rise on average; beyond that, past prices say nothing about which way the next move goes. In the simulated year 2017 of the one market the lesson shows, the histogram crossed zero 30, 20 and 10 times with the settings 8/20/6, 12/26/9 and 16/32/12; a rule that trades on those crossings trades that often.

Three rules from one indicator

Each rule is either in the market or out of it, in cash. It decides at a day's close and trades at the next day's first price, because the close isn't known until trading ends. The signal-line cross is in the market while the MACD line is above the signal line. The zero-line cross is in the market while the MACD line is above zero; it is lesson E2's cross of two averages, here two EMAs, and never reads the signal line. The histogram turn is in after a day the histogram rose (its bar is higher than the day before's, above zero or not) and out after a day it fell. In that same simulated year, at 12/26/9, the three rules made 20, 6 and 67 buys and sells.

The customary version against just holding

Just holding buys on the first day and never sells. Results are in % a year, the steady yearly growth giving the same total, and a result's lead is its % a year minus holding's, in points (a point is 1% a year). Every buy and every sell costs 0.1% of the price; holding pays that once, which is lesson E1's rule. On the lesson's simulated market, from late 2015 to the end of 2021, holding made 6.3% a year. The customary version made 5.7% before costs; its buys and sells, about 20 a year, cost 2.1 of that and left 3.6%: a lead of 3.6 − 6.3 = −2.7 points a year.

One market is one draw, so the lesson ran the same version on 1,000 more simulated markets built the same way. After costs it beat holding in 135 and trailed it in 865, so the one market was typical; the average lead was −6.4 points a year. Those markets rise on average, and the version was in them on only about half the days (50.5% on average) and paid about 2 points a year in costs. The counts show what luck alone produces there; they are not a result about any real market.

63 versions

A version is the same idea with different settings, giving its own set of trades. With fast 8, 12 or 16 days, slow 20, 26 or 32 and signal 6, 9 or 12 there are 3 × 3 × 3 = 27 settings. The signal-line cross and the histogram turn use all three lengths: 27 versions each. The zero-line cross ignores the signal length: 3 × 3 = 9. So there are 27 + 27 + 9 = 63 versions, not 81.

The years 2015 to 2021 are the choosing years: a version is chosen on them. On the lesson's market, 39 of the 63 beat holding there before costs and 26 after. The best, a histogram turn at 16/20/12, led by 12.7 points a year before costs and 6.0 after. Costs took 6.7. The customary version came 41st; a version's place counts from the largest lead after costs.

Across the 1,000 simulated markets the choosing years looked different. The best version after costs (the largest lead, even one below zero) was a histogram turn in only 63 markets, a zero-line cross in 538 and a signal-line cross in 399. In 637 markets no version beat holding after costs on those years; on average 5.6 of the 63 did. The lesson's market was an unusual draw: only 83 of the 1,000 had 26 or more versions ahead after costs. On the choosing years the customary version's place averaged 23.7 of 63, where a version drawn at random averages 32.

Does the best stay best?

Before any version was run, 2022 to 2024 were set aside: years held out, to be used once. On the lesson's market the best version of the choosing years made 3.2% a year there after costs against holding's 10.0%: a lead of −6.8 points a year, 48th of the 63. Across the 1,000 simulated markets, after costs, the pick (the best of 63 on the choosing years) stayed best on the held-out years in 46. A blind draw, one version taken at random, would be the best in 1 market of 63: 1,000 ÷ 63, about 16. After costs the pick beat holding on the held-out years in 207 markets; the customary version, with no picking, in 208.

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 (9.5 buys and sells a year, against 21.8 and 72.1 for the other two) and was in the market most (58% of days, against about half). In these rising markets both would lift a rule's place in any stretch of years, and neither is a forecast of prices. That is what the averages suggest, not a controlled test. 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), so choosing settings added nothing to the place.

Formula: the exponential moving average

An exponential moving average is a running average: each day it moves a share α of the way from yesterday's value 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".

EMAt = EMAt−1 + α × (Ct − EMAt−1),   α = 2 ÷ (n + 1)
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

The worked example is the six closes in "The exponential moving average" above.

Holds whenBreaks when

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

Formula: 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).

MACDt = EMAfast(C)t − EMAslow(C)t
signalt = EMAsig(MACD)t
histogramt = MACDt − signalt
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
Worked example: the last day of the lesson's simulated 2017, at 12/26/9

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 (from the rounded values; unrounded it is 0.35).

Holds whenBreaks when

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

One more worked example

Made-up round numbers

Closes 10, 12, 14, 18 and 12, n = 3: α = 2 ÷ 4 = 0.5. First value: (10 + 12 + 14) ÷ 3 = 12. Then 12 + 0.5 × (18 − 12) = 15, and 15 + 0.5 × (12 − 15) = 13.5.

Now the three lines, with a fast EMA of 13.5, a slow EMA of 12.9, yesterday's signal line at 0.2 and a signal length of 4. MACD = 13.5 − 12.9 = 0.6. The signal's α is 2 ÷ 5 = 0.4: 0.2 + 0.4 × (0.6 − 0.2) = 0.36. Histogram = 0.6 − 0.36 = 0.24.

Common mistakes

Check yourself

  1. A 9-day EMA was 80 yesterday, and today's close is 85. What is today's EMA?
    Answer

    α = 2 ÷ (9 + 1) = 0.2, so 80 + 0.2 × (85 − 80) = 81.

  2. The fast EMA is 52.4 and the slow EMA 51.9. Yesterday's signal line was 0.30 and the signal length is 9. What are today's MACD line, signal line and histogram?
    Answer

    MACD = 52.4 − 51.9 = 0.5. Signal: α = 0.2, so 0.30 + 0.2 × (0.5 − 0.30) = 0.34. Histogram = 0.5 − 0.34 = 0.16.

  3. A test uses fast 5 or 12 days, slow 26 or 35, signal 5, 9 or 12, and trades the signal-line cross or the zero-line cross. How many versions?
    Answer

    Signal-line cross: 2 × 2 × 3 = 12. Zero-line cross: 2 × 2 = 4, because it ignores the signal length. 12 + 4 = 16.

  4. In the lesson's simulation, after costs, the best of 63 versions stayed best on held-out years in 46 of 1,000 simulated markets, against about 16 for a blind draw. What does that suggest, and what does it not show?
    Answer

    After costs the pick stayed best about three times as often as a blind draw's 16. The averages suggest why: in 538 of the 1,000 markets the pick was a zero-line cross, the rule that trades least; that is not a controlled test. It does not show that picking found anything in the prices: the pick's average held-out place equalled the average for all versions of its rule, and after costs it beat holding in 207 markets against 208 for the customary version with no picking. And it is a simulation; a real market needs its own test.

Try it yourself

The rules, written before the code. Daily closes. EMA of length n: α = 2 ÷ (n + 1); the first value, on the n-th close, is the simple average of the first n; after that EMA = old EMA + α × (close − old EMA). MACD line = fast EMA − slow EMA. Signal line = an EMA of the MACD line, started the same way. Histogram = MACD − signal. Signal-line cross: in the market when MACD is above signal. Zero-line cross: in when MACD is above 0. Histogram turn: in when the histogram is above yesterday's, out when below, unchanged when equal. In the market or in cash, never short. Decide at each close, fill at the next open. 0.1% of the price per buy and per sell, or no costs. Compared with buying at the first test close and holding, which pays 0.1% once.

Run the script. Every number in this lesson comes from one Python script (below). It needs simmarket.py, the course's simulated market, in the same folder. Add a fourth rule or other lengths and watch the version count and the counts across markets move.

By hand. In a spreadsheet with closes in column B from row 2: the 12-day EMA starts in C13 with =AVERAGE(B2:B13), then C14 is =C13+2/13*(B14-C13), copied down. The 26-day EMA starts in D27 with =AVERAGE(B2:B27), then D28 is =D27+2/27*(B28-D27). The MACD line in E27 is =C27-D27. The signal line starts in F35 with =AVERAGE(E27:E35), then F36 is =F35+2/10*(E36-F35). The histogram in G35 is =E35-F35. Copy each down.

Written by QuantWright from the published definition of the indicator: the formulas only. No code from any indicator script or charting platform.

The code behind every number (Python, SimMarket seed 20261109)
Loading…

Going further: three rules, one family

The three rules ask the same kind of question at three speeds. The zero-line cross asks whether the fast EMA is above the slow one. The signal-line cross asks whether the MACD line is above its own average, which it is after the line has been rising. The histogram turn asks whether the histogram rose today. Each step reacts sooner and trades more: across the 1,000 simulated markets the three rules averaged 9.5, 21.8 and 72.1 buys and sells a year, so at 0.1% each their bills were about 1, 2 and 7 points a year.

In these markets, where past prices say nothing about which way the next move goes, the bill and the days in the market do most of the ranking. Among the 63 versions, the average place on the choosing years was 18.0, 24.5 and 44.2 for the zero-line cross, the signal-line cross and the histogram turn; on the held-out years, 21.7, 26.9 and 40.5. The order is the same in both stretches, and it is the order of the bills. The 63 versions are also close relatives, not 63 separate tries: versions of one rule with neighbouring lengths trade on many of the same days (lesson E1's second theory card).

For the preciseThe lesson's market: SimMarket Market 1, seed 20261109, effect=False (no planted pattern), daily bars, the same data and periods as lessons E2 and E3. The 1,000 markets: the same generator with seeds 2026120401 to 2026121400, effect off. First decision at the close of 9 Oct 2015 (the 200th day); by then the first value of the slowest EMA weighs less than 0.003% of it. Choosing years: that close to 31 Dec 2021 (6.23 calendar years); held-out: to 31 Dec 2024 (3.00 years); each version carries its position across. % a year = exp(sum of daily log returns)1 ÷ years − 1. Fill at the next open: the night before the open earns the old position, the day after it the new one. Costs: a factor (1 − 0.001) per buy or sell; holding pays it once, at its buy. The lesson's market: holding 6.32% a year before its cost and 6.30% after (choosing), 10.00% (held-out). Customary version: 5.70% a year before costs, 3.60% after (125 buys and sells, 20.07 a year; 1.0570 × 0.99920.07 = 1.0360), lead −0.62 then −2.70, in the market 46.6% of days, 41st of 63; held-out 4.37% a year, lead −5.63, 28th. Best after costs: histogram turn 16/20/12, +12.74 before costs, +6.02 after, 58.28 buys and sells a year (19.06% a year before costs, and 1.1906 × 0.99958.28 = 1.1232, so 12.32% after: 6.74 points of costs, of which holding's own cost offsets 0.02), in the market 50.6% of days; held-out 3.23% a year, lead −6.77, 48th. Before costs 39 of 63 beat holding (signal-line cross 12, zero-line cross 0, histogram turn 27); after costs 26 (5, 0, 21). On the held-out years 3 of the 63 beat holding, all zero-line crosses. The 1,000 markets, customary version: beat holding in 241 before costs and 135 after (choosing years), 208 after costs (held-out); average lead −4.34 before costs, −6.44 after, −6.99 held-out; 20.6 buys and sells a year and 50.5% of days in the market on average; average place 23.7, median 21; best of 63 in 7 markets, top ten in 144. Versions beating holding on the choosing years: 14.3 of 63 on average before costs, 5.6 after; at least one after costs in 363 markets. A histogram-turn version beat holding after costs in 55 markets. The pick: beat holding in 363 markets on the choosing years and 207 on the held-out years; average lead −1.33 then −6.19; stayed best in 46 (a blind draw: about 16), top ten in 312; average held-out place 25.2, median 24; best of its own rule on the held-out years in 101 markets, against 76.9 for a blind draw within the rule. Of all 63,000 held-out results, 15.6% beat holding. The worked example's histogram is 0.353 before rounding; 0.36 comes from the two-decimal values shown.

Honest notes

Where this comes from