MACD
MACD in ProBuilder returns the MACD histogram, the gap between the MACD line and its signal line, with standard 12/26/9 settings. Syntax and examples.
Syntax
MACD[S, L, Si](price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
S | integer | 12 | Period of the short exponential moving average. |
L | integer | 26 | Period of the long exponential moving average. Must be larger than S. |
Si | integer | 9 | Period of the exponential moving average applied to the MACD to form the signal line. |
price | price source | close | The price series the calculation runs on. Commonly close, also accepts open, high, low, typicalprice, or a custom variable. |
Formula
MACD line = EMA[S](price) - EMA[L](price)
Signal = EMA[Si](MACD line)
MACD[S, L, Si](price) = MACD line - Signal // the histogramThe result is unbounded and expressed in the instrument's price units. When the MACD line sits above its signal line the value is positive; when it sits below, the value is negative.
How it works
On every bar, ProBuilder computes the MACD line, the short EMA of price minus the long EMA, then smooths that line with an Si-period EMA to form the signal line. The MACD instruction returns the difference between the two, the series charting packages draw as the histogram. A crossing through zero therefore marks a signal-line crossover, not an EMA crossover.
The two component series are available as separate instructions: MACDLine returns the raw line and MACDsignal returns the signal line. MACDLine - MACDsignal reproduces the value returned by MACD.
MACD was designed by Gerald Appel as a trend-following momentum indicator. It combines two questions in one series: which side of the trend the short average is on, and how quickly the gap between the averages is changing.
Examples
Example 1, Rising MACD while price pulls back (Indicator)
// MACD with the standard 12, 26, 9 settings on closing prices
i1 = MACD[12,26,9](close)
FOR i = 20 DOWNTO 0 DO
IF(close[i] < close[i+1] AND i1 > i1[1]) THEN
result = 100
ELSE
result = 0
ENDIF
NEXT
RETURN resultPreserved from the source page. The condition pairs a falling close with a rising MACD. Note that each pass of the loop overwrites result, so the returned value reflects only the final iteration (i = 0), the current bar.
Example 2, Signal-line crossover strategy (ProOrder)
// Go long when the histogram turns positive, exit when it turns negative
DEFPARAM CumulateOrders = false
m = MACD[12,26,9](close)
IF NOT LongOnMarket AND m CROSSES OVER 0 THEN
BUY 1 CONTRACT AT MARKET
ENDIF
IF LongOnMarket AND m CROSSES UNDER 0 THEN
SELL AT MARKET
ENDIFBecause MACD returns the histogram, a zero cross means the MACD line has crossed its signal line, the classic MACD entry trigger expressed in a single condition.
Example 3, Improving momentum below zero (ProScreener)
// Instruments where a negative histogram has risen two bars in a row
hist = MACD[12,26,9](close)
improving = hist < 0 AND hist > hist[1] AND hist[1] > hist[2]
SCREENER[improving](hist AS "MACD hist")Returns instruments still below the signal line but with a shrinking gap, a momentum-improvement scan that fires before the actual crossover.
Interpretation
| Signal | Reading |
|---|---|
| Histogram above zero | MACD line above its signal line, upward momentum. |
| Histogram below zero | MACD line below its signal line, downward momentum. |
| Histogram crosses above zero | Bullish signal-line crossover, a common trigger condition. |
| Histogram shrinking toward zero | Momentum fading ahead of a possible crossover. |
For zero-line crossovers of the MACD line itself, the EMA crossover reading, test MACDLine against zero instead.
Divergences. A bullish divergence occurs when price prints a lower low while the MACD forms a higher low. A bearish divergence occurs when price prints a higher high while the MACD forms a lower high. Both are read as early signs of a possible reversal, and the DivergenceMACD instruction detects them directly.
Common errors and gotchas
- Histogram, not line.
MACDreturns the histogram. Code ported from platforms whereMACD()returns the line will misfire. UseMACDLinefor the line andMACDsignalfor the signal. - All three periods are required.
MACD[12,26](close)raises a syntax error. The periods go in square brackets, the price source in parentheses:MACD[12,26,9](close). - Loop overwrite. In the source pattern from Example 1, the FOR loop assigns
resulton every iteration, so earlier bars are discarded. To count matches over 21 bars, accumulate into a counter instead of overwriting. - Not comparable across instruments. MACD is unbounded and scales with price. A reading of 2.5 on an index and 0.002 on a forex pair carry no relative meaning. Screeners ranking by raw MACD mix scales.
- Lag in slow markets. Two nested EMAs make MACD a lagging measure. Crossovers arrive well after the turn on higher settings, and shortening the periods trades lag for whipsaws.
Related instructions
MACDLine, the MACD line as a standalone series.MACDsignal, the signal line, an EMA of the MACD.DivergenceMACD, built-in divergence detection.ExponentialAverage, the EMA that MACD is built from.PriceOscillator, percentage-based variant of the same idea.Momentum, raw price change over N bars.TRIX, triple-smoothed EMA rate of change.Average, simple moving average for smoothing MACD output.
