MACD
MACD is the crossover from the previous lesson, drawn as a line instead of as two lines. Once you see that, most of what is written about it becomes easier to evaluate — and one of AmiBroker’s least forgiving argument lists becomes easy to remember.
What MACD measures
Section titled “What MACD measures”The published construction is a difference: a fast exponential average of price minus a slow one. When the fast average is above the slow, the difference is positive; when it is below, negative; and the zero line of a MACD chart is precisely the bar on which the two averages cross.
Fragment — not a complete formula
// These two are the same event. MACD is the crossover, rendered as one line.CrossOfAverages = Cross( EMA( Close, 12 ), EMA( Close, 26 ) );CrossOfZeroLine = Cross( MACD( 12, 26 ), 0 );The signal line is a smoothing of the MACD line itself — an average of an average of a difference of averages. It moves later than MACD by construction, so MACD crossing above its signal line is an earlier event than MACD crossing zero.
The histogram is the gap between them, MACD() - Signal(), drawn as bars. It crosses zero
on exactly the bar where MACD crosses its signal line, so the histogram is not extra
information — it is the same event, rendered so that the eye can see it coming.
One condition, three renderings of it
| Bar | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
MACD() | -0.40 | -0.15 | 0.10 | 0.32 | 0.28 |
Signal() | -0.22 | -0.20 | -0.12 | 0.02 | 0.14 |
Histogram | -0.18 | 0.05 | 0.22 | 0.30 | 0.14 |
Cross( MACD(), Signal() ) | 0 | 1 | 0 | 0 | 0 |
Cross( MACD(), 0 ) | 0 | 0 | 1 | 0 | 0 |
What the defaults really are
Section titled “What the defaults really are”This is the part worth memorising, because getting it wrong produces a chart that looks plausible and is not what you asked for.
| Function | Documented syntax | What f() with no arguments gives you |
|---|---|---|
MACD |
macd( fast = 12, slow = 26 ) |
a 12/26 MACD line |
Signal |
Signal( fast = 12, slow = 26, signal = 9 ) |
the 9-period signal of a 12/26 MACD |
The consequence is that any change of period must be made in both calls. Write it once and pass it through:
Fragment — not a complete formula
FastPeriod = 12;SlowPeriod = 26;SignalPeriod = 9;
MacdLine = MACD( FastPeriod, SlowPeriod );SignalLine = Signal( FastPeriod, SlowPeriod, SignalPeriod );Histogram = MacdLine - SignalLine;There is also no built-in histogram function. MACD() - Signal() is something you write
yourself, which is why so much shared AFL contains a subtly different version of it.
How MACD is commonly read
Section titled “How MACD is commonly read”Four readings dominate, all stated here as convention rather than as findings:
- Zero-line crossings are read as the trend changing sign. AmiBroker’s own language manual
uses exactly this as an illustration:
Buy = Cross( MACD(), 0 ); Sell = Cross( 0, MACD() ); - Signal-line crossings are read as an earlier, more sensitive version of the same idea.
- Histogram turns — the histogram shrinking while still positive — are read as momentum fading before the crossing arrives.
- Divergence — price making a higher high while MACD makes a lower high — is read as the move losing conviction.
The first three are simply different points along the same chain of smoothing, ordered from latest to earliest. The fourth deserves its own section, because it is where most of the confident writing about MACD lives.
Divergence, and why the evidence for it is weak
Section titled “Divergence, and why the evidence for it is weak”Start with the arithmetic. MACD is a difference of averages, so its level reflects the rate at which price has been advancing, not how high price is. If a market climbs 10 per cent in twenty bars and then climbs another 10 per cent over the following sixty bars, the second advance reaches a higher price and a lower MACD peak. That is a textbook bullish-price, bearish-indicator divergence, and it was produced by nothing more than the advance becoming slower.
Advances slow down constantly, including in the middle of trends that run for another year. Divergence is therefore not a rare signal that the indicator detects; it is a frequent mechanical consequence of deceleration.
Now the methodology. To test a divergence claim at all you have to answer questions that are almost never answered when the claim is made:
- Which highs? A divergence needs two peaks in price and two in the indicator. What counts as a peak? How many bars either side? Change that definition and the set of divergences changes completely.
- Over what window? Peaks three bars apart and peaks eighty bars apart are both called divergence.
- How long does the claim last? A signal with no horizon cannot be wrong: if price falls eventually, the divergence “worked”.
- What is the base rate? If divergences occur on 15 per cent of all bars, and price falls over the next month on 40 per cent of all bars, then a divergence “followed by a decline” 40 per cent of the time is telling you nothing at all.
Limitations that come from the construction
Section titled “Limitations that come from the construction”It is unbounded and in price units. A MACD reading of 1.8 means something completely different on an instrument priced at 30 and one priced at 3,000, and it means something different on the same instrument twenty years apart. There is no natural “high” MACD value. If you need comparability, normalise — dividing by the close is the simplest choice.
It is slow, deliberately. Three layers of smoothing sit between price and the histogram. Every one of them buys steadiness with lateness.
It cannot distinguish a strong move from a fast one. MACD sees only the difference between two averages of the close; the bar’s range, its volume and the presence of gaps are all invisible to it.
Its early bars are contaminated. Exponential averages are recursive and, in AmiBroker,
seeded from a simple average, so the first stretch of any MACD series carries the seed. The
MACD page documents no warm-up count. Discard a generous number of early bars.
Displaying it in AmiBroker
Section titled “Displaying it in AmiBroker”MACD belongs in its own pane: its values are in price units and would be invisible against the
price scale. Insert it from Window -> Charts by double-clicking, or apply a formula. The
convention AmiBroker states on the MACD page is that macd() is the red line and signal()
the blue one, which is worth keeping so that other people can read your charts.
Fragment — not a complete formula
FastPeriod = Param( "Fast", 12, 2, 100, 1 );SlowPeriod = Param( "Slow", 26, 3, 200, 1 );SignalPeriod = Param( "Signal", 9, 2, 100, 1 );
MacdLine = MACD( FastPeriod, SlowPeriod );SignalLine = Signal( FastPeriod, SlowPeriod, SignalPeriod );
Plot( MacdLine - SignalLine, "Histogram", colorGrey40, styleHistogram | styleThick );Plot( MacdLine, "MACD", colorRed, styleLine | styleThick );Plot( SignalLine, "Signal", colorBlue, styleLine );PlotGrid( 0, colorBlack, 9, 1, False );Plot the histogram first and the lines afterwards. Within a single Z-order layer AmiBroker
draws plots in reverse call order, so the last Plot in the code is drawn furthest back —
which means the first one you write ends up on top. If your lines vanish behind the
histogram bars, that is the reason, and GraphZOrder = 1; reverses the behaviour.
Turning this into a testable question
Section titled “Turning this into a testable question”Skip divergence for now; it needs a peak definition you do not yet have. Test the simplest claim instead, the one AmiBroker’s own manual illustrates:
On my universe, over my chosen period, do 20-day forward returns measured on bars where MACD(12,26) crossed above zero differ from 20-day forward returns measured on all other bars?
That version names the universe, the period, the event, the measurement and the horizon. It uses the event, not the state, so you are measuring what followed the crossing rather than what happened during the whole trend. And it has a built-in warning you should notice now: a zero-line crossing is, by definition, a bar on which a fast average moved above a slow one, which is a description of a market that has already risen. Any result must be compared against what generally follows such bars, not against zero. The final lesson of this part builds exactly that comparison.
MACD is the difference between a fast and a slow exponential average, so its zero line is the
crossover of those averages. The signal line is a smoothing of MACD, and the histogram is the
gap between them, which you write yourself as MACD() - Signal(). AmiBroker’s documented
defaults are MACD( fast = 12, slow = 26 ) and Signal( fast = 12, slow = 26, signal = 9 ),
so Signal( 9 ) is a trap and Signal cannot be used as a variable name. MACD is unbounded
and in price units, so it is not comparable across symbols or across eras without
normalisation. Divergence is a frequent mechanical consequence of a decelerating advance, and
a divergence claim without a peak definition, a horizon and a base rate is not yet testable.
Check your understanding
Sources for this lesson
5 verified · checked 2026-08-31
- 01AFL Function Reference — MACDamibroker.com/guide/afl/macd.html2026-08-31
- 02AFL Function Reference — Signalamibroker.com/guide/afl/signal.html2026-08-31
- 03AFL Function Reference — EMA§ Author comment on initialisationamibroker.com/guide/afl/ema.html2026-08-31
- 04AFL Function Reference — Crossamibroker.com/guide/afl/cross.html2026-08-31
- 05AmiBroker User's Guide — AFL language reference§ Identifiers and example formulasamibroker.com/guide/a_language.html2026-08-31
Every technical claim on this page was checked against the official AmiBroker documentation on the date shown. Where the course disagrees with folklore, the source is how you can tell which one to trust.