MACD Divergence Indicator: Pivots With A Minimum Gap

Written by Dominic Walsh · Published · Last updated

The macd divergence indicator adds one rule most divergence tools leave out. Two pivots have to sit at least `MinPivotGap` bars apart before the code will pair them. Neighbouring pivots on a jagged stretch describe the same wobble, and pairing them produces noise.

This build ships as a compiled .ex4 for MT4 and .ex5 for MT5; the screenshots below come from EURUSD H1.

Platforms MT4 (.ex4) + MT5 (.ex5)
Chart window Main price chart
Plots 2: BUY (Bullish Divergence), SELL (Bearish Divergence)
Alerts popup
Inputs 8

What the MACD Divergence Indicator draws on the chart

BUY (Bullish Divergence) places a lime arrow under a lower price low with a higher reading.

SELL (Bearish Divergence) places a red arrow above the mirror case.

Both sit twelve points from the bar.

The MACD line stays internal, so the price chart carries only the marks.

  • BUY (Bullish Divergence): arrow, lime, width 2, arrow code 233
  • SELL (Bearish Divergence): arrow, red, width 2, arrow code 234
MACD divergence indicator on EURUSD H1 with gap-filtered pivot pairings
MACD divergence indicator on EURUSD H1 with gap-filtered pivot pairings

How the MACD Divergence Indicator calculates its values

Confirming pivots on both sides

`PivotLeft` `3` and `PivotRight` `3` set the bars a pivot must beat on either side. Keeping them as separate inputs lets you demand more history without adding confirmation delay, or the other way round.

Pairing with a minimum gap

The search runs back up to `LookbackBars` `50` bars for an earlier pivot of the same kind. `MinPivotGap` `5` then rejects anything closer than five bars. That rule is what removes the same-wobble pairings.

Comparing the readings

iMACD supplies the main line at both pivot bars, at `FastEMA` `12`, `SlowEMA` `26` and `SignalSMA` `9`. Bullish needs the newer low below the older one while the reading sits higher.

How to read the signals

A mark means price made a new extreme and the reading did not follow.

The minimum gap keeps marks from clustering on a jagged stretch.

Fifty bars is the furthest back the pairing reaches.

Divergence describes disagreement, which is not the same as a turn.

Alert channels in this build: popup, one message per closed bar.

`EnableAlerts` ships on and raises a terminal popup when a fresh divergence confirms. This build carries no push, email or sound channel. The guard permits one message per closed bar.

Every MACD Divergence Indicator input

Core settings

Setting What it does Default
FastEMA MACD Fast EMA. Fast average inside the calculation, default `12`. 12
SlowEMA MACD Slow EMA. Slow average inside the calculation, default `26`. 26
SignalSMA MACD Signal SMA. Signal smoothing, default `9`. 9
LookbackBars Divergence scan window. Furthest the pairing reaches back, default `50` bars. 50
PivotLeft Bars left for pivot. Bars a pivot must beat on the older side, default `3`. 3
PivotRight Bars right for pivot. Bars it must beat on the newer side, default `3`. 3
MinPivotGap Min bars between two pivots. Smallest bar gap between paired pivots, default `5`. 5

Alert settings

Setting What it does Default
EnableAlerts Alert on new divergence. Popup on a fresh divergence, default `true`. on
MACD divergence indicator inputs: LookbackBars, PivotLeft, PivotRight, MinPivotGap
MACD divergence indicator inputs: LookbackBars, PivotLeft, PivotRight, MinPivotGap

Settings that fit your chart

Divergence wants a market that swings rather than drifts. EURUSD H1 is the reference chart, where `LookbackBars` at `50` covers two days. On M5 the pivot test fires often and the minimum gap earns its place. On H4 the pairings are rare and each spans a real swing.

When the MACD Divergence Indicator fails

A three-bar pivot test is loose, so plenty of small turns qualify.

The reading can disagree with price through a whole trend without it ending.

Only one earlier pivot gets compared, so everything between the two is ignored.

Copy the compiled MACD Divergence Indicator file into your MQL4/Indicators folder, and the MT5 build into MQL5/Indicators, then restart the terminal and drag it onto a chart. The step-by-step guide with screenshots is at how to install MT4 and MT5 indicators.

Download the MACD Divergence Indicator for MT4 and MT5

The zip holds the compiled MACD Divergence Indicator build for MT4 and MT5, plus a short README. Compiled files only, no source. Enter your email below and the download link arrives in your inbox.

Download this indicator free

Enter your email and the file is yours. You also get the full MT4 and MT5 library.

  • 1,380+ indicators
  • MT4 and MT5 files
  • No spam, unsubscribe any time

FAQ

What does MinPivotGap at 5 prevent?

Pairing two pivots that sit almost on top of each other. On a jagged stretch several pivots form within a few bars, and comparing them describes one wobble rather than two swings. Five bars is the smallest separation accepted.

Why keep PivotLeft and PivotRight separate?

Because they do different jobs. `PivotLeft` `3` sets how much history a pivot must beat, which controls significance. `PivotRight` `3` is confirmation delay, and every bar there is a bar of lateness in the mark.

How far back does LookbackBars pair?

Fifty bars, which on EURUSD H1 is about two days. The search stops there because two swings further apart than that rarely have much to say about each other. Widen it on higher timeframes.

Does the BUY (Bullish Divergence) mark need both pivots confirmed?

Yes. Both the fresh pivot and the older one must have passed the same left and right tests. That is why the mark appears `PivotRight` bars after the low it describes rather than on it.

Are results guaranteed?

No. Trading involves risk. Results are not guaranteed; past performance is not indicative of future results. Use the MACD Divergence Indicator as one input. Always backtest before trading live.

Category: this is part of our Oscillators collection. Browse more Oscillators for MetaTrader, or explore every MT4 indicator and MT5 indicator on the site.

External references

Dominic Walsh - Forex trader and MT4/MT5 developer

About the author

Written by Dominic Walsh, a Forex trader and MT4/MT5 indicator, Expert Advisor and script developer. Every tool on forexmt4systems.com is tested on live charts before release and ships with ready-to-use compiled MT4 (.ex4) and MT5 (.ex5) files. Learn more about the trader and developer behind this site.

How we build, test and correct every tool: Editorial & Testing Policy. Trading carries risk; see the disclaimer.

Leave a Comment