Skip to content
Level 1 · Chart ReaderLessonPart 06 · page 5 of 1124 min
24Minutes
4AFL functions
5Sources
StandardRequires
AFL functions taught here4

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.

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

The histogram turns positive on the same bar that MACD crosses its signal line. The zero-line cross of MACD itself happens later — it is the slower of the two events, because it requires the underlying averages themselves to cross.
Bar12345
MACD()-0.40-0.150.100.320.28
Signal()-0.22-0.20-0.120.020.14
Histogram-0.180.050.220.300.14
Cross( MACD(), Signal() )01000
Cross( MACD(), 0 )00100
The histogram turns positive on the same bar that MACD crosses its signal line. The zero-line cross of MACD itself happens later — it is the slower of the two events, because it requires the underlying averages themselves to cross.

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.

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.

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.

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

Question 1. You want a 20/40 MACD with a 5-period signal line. Which pair of calls is right?
Show the answer and why

Answer: MACD( 20, 40 ) and Signal( 20, 40, 5 )

Signal takes fast, slow and signal periods, in that order. Signal( 5 ) would build a 5/26 MACD and smooth it over 9 bars, so the two lines on your chart would come from different underlying indicators.

Question 2. Why does the MACD histogram cross zero on the same bar that MACD crosses its signal line?
Histogram = MACD() - Signal();
Show the answer and why

Answer: Because the histogram is defined as the difference between the two lines, so it is zero exactly when they are equal

The histogram carries no information the two lines do not already carry. Its value is the gap, so its sign changes precisely when the lines cross. It is a rendering choice, not an extra indicator.

Question 3. A market advances 10 per cent quickly, then a further 10 per cent slowly. Price makes a higher high; MACD makes a lower high. What has the indicator detected?
Show the answer and why

Answer: That the rate of advance decreased — which is what a difference of averages measures

MACD reflects the speed of the advance, not its extent. A slower advance produces a lower MACD peak by arithmetic. Whether that pattern is followed by a decline more often than other bars are is a separate, testable question — and it needs a peak definition, a horizon and a base rate.

Question 4. Which statements about AmiBroker’s MACD are supported by its documentation? Select all that apply.
Show the answer and why

Answer: MACD() with no arguments uses fast = 12 and slow = 26, The reference page describes macd() as the red line and signal() as the blue one

The defaults and the colour convention are both on the MACD page. There is no histogram function — you write MACD() - Signal() — and no MACDa variant, so MACD is computed from the built-in price data.

Sources for this lesson

5 verified · checked 2026-08-31

  1. 01AFL Function Reference — MACDamibroker.com/guide/afl/macd.html2026-08-31
  2. 02AFL Function Reference — Signalamibroker.com/guide/afl/signal.html2026-08-31
  3. 03AFL Function Reference — EMA§ Author comment on initialisationamibroker.com/guide/afl/ema.html2026-08-31
  4. 04AFL Function Reference — Crossamibroker.com/guide/afl/cross.html2026-08-31
  5. 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.