DivergenceMACD
DivergenceMACD in ProBuilder detects bullish and bearish divergences between price and the MACD, returning +1, -1, or 0. Syntax, parameters, and examples.
Syntax
DivergenceMACD[FastPeriod, SlowPeriod, SignalPeriod, Bars](price)Parameters
| Name | Type | Default | Description |
|---|---|---|---|
FastPeriod | integer | 12 | Period of the fast EMA in the underlying MACD calculation. |
SlowPeriod | integer | 26 | Period of the slow EMA in the underlying MACD calculation. |
SignalPeriod | integer | 9 | Period of the signal line EMA applied to the MACD line. |
Bars | integer | 20 | Number of recent bars examined for a divergence pattern. |
price | price source | close | Series used for both the MACD calculation and the price extremes. |
Formula
The detector compares the direction of price extremes with the direction of MACD extremes over the last Bars bars:
Bullish divergence: price makes lower lows while the MACD makes higher lows -> +1
Bearish divergence: price makes higher highs while the MACD makes lower highs -> -1
No divergence detected -> 0The underlying MACD is the standard construction: fast EMA minus slow EMA, with a signal line EMA over the result.
How it works
The MACD trails price momentum, so when price pushes to a new extreme that the MACD refuses to confirm, the move is running on less momentum than before. DivergenceMACD automates that comparison. On each bar it computes the MACD from the given periods, locates the relevant price and indicator swings inside the detection window, and reports the outcome as a discrete signal: +1, -1, or 0.
Because the output is a stepped signal series rather than a continuous line, it is usually rendered as a histogram or consumed directly in strategy and screener conditions.
For manual verification on a chart, note how the related MACD instructions map to the indicator's components: MACD[FastPeriod, SlowPeriod, SignalPeriod](price) returns the histogram (line minus signal), MACDLine returns the MACD line itself, and MACDsignal returns the signal line. Comparing detector output against a chart panel only makes sense when the panel uses the same three periods and the same price source.
Examples
Example 1, Divergence signal histogram (Indicator)
// Standard 12/26/9 MACD settings, 20-bar detection window on the close
i = DivergenceMACD[12,26,9,20](close)
return i style(histogram)Plots the raw detector output as a histogram: +1 bars mark bullish divergences, -1 bars mark bearish divergences, and zero means no divergence inside the window.
Example 2, Bearish divergence scan (ProScreener)
// List instruments printing a bearish MACD divergence on the current bar
d = DivergenceMACD[12, 26, 9, 20](close)
hist = MACD[12, 26, 9](close)
SCREENER[d = -1](hist AS "MACD hist")Returns instruments where price made higher highs against a weakening MACD, with the current MACD histogram value shown for context.
Example 3, Divergence entry confirmed by the histogram (ProOrder)
// Buy a bullish divergence once the MACD histogram turns positive
signal = DivergenceMACD[12, 26, 9, 20](close)
hist = MACD[12, 26, 9](close)
IF NOT LongOnMarket AND signal = 1 AND hist CROSSES OVER 0 THEN
BUY 1 CONTRACT AT MARKET
SET STOP %LOSS 2
ENDIF
IF LongOnMarket AND signal = -1 THEN
SELL AT MARKET
ENDIFThe divergence flags the setup and the MACD histogram crossing above zero, meaning the MACD line moved back above its signal line, confirms the turn before entering.
Interpretation
- +1, bullish divergence. Price set a lower low while the MACD set a higher low. Downward momentum is fading; sometimes precedes an upward reversal.
- -1, bearish divergence. Price set a higher high while the MACD set a lower high. Upward momentum is fading.
- 0, no signal. No qualifying divergence inside the detection window.
Divergence marks weakening momentum, not a confirmed reversal. Strong trends can print repeated divergences while continuing, so most workflows pair the signal with a confirmation trigger or a higher-timeframe trend filter.
Common errors and gotchas
- Forgetting the price argument. Unlike
DivergenceCCI, this instruction takes a price source in parentheses after the brackets:DivergenceMACD[12,26,9,20](close). Omitting it raises a syntax error. - Comparing against the wrong MACD output.
MACD[12,26,9](close)returns the histogram, not the MACD line. When checking detector signals against a chart, useMACDLinefor the line andMACDsignalfor the signal line, and keep all three periods identical to the detector's. - Parameter order. The order is fast, slow, signal, bars. Swapping
SlowPeriodandBars, for exampleDivergenceMACD[12,20,9,26](close), compiles and silently detects against a different indicator. - Acting on every divergence in a trend. Bearish divergences in a persistent uptrend fail repeatedly. Filter by trend direction or require confirmation, as in Example 3.
Related instructions
DivergenceCCI, the same detector built on the CCI.DivergenceRSI, the same detector built on RSI.MACD, MACD histogram, line minus signal.MACDLine, the MACD line, fast EMA minus slow EMA.MACDsignal, the signal line, an EMA of the MACD line.CCI, deviation-based oscillator also used for divergence work.RSI, bounded momentum oscillator with comparable divergence logic.ExponentialAverage, the smoothing method underlying all MACD components.
