Loading…

MACD Divergence EA

Regular divergence between price and an oscillator is one of the oldest ideas in technical analysis: when price extends to a new extreme but the oscillator does not confirm it, momentum is fading even though price is still moving, and that disagreement is treated as an early warning of a reversal. This EA implements the MACD version of that idea mechanically, with a specific, honest definition of "swing point" so the signal is reproducible rather than a judgement call. A swing low only counts once InpPivotBars bars have closed on both sides of it with none of them lower - the same requirement that makes the pivot real is what stops the EA from ever repainting one. The unavoidable consequence is that every signal is InpPivotBars bars late: divergence trading always pays for a confirmed pivot with a delayed entry, and no amount of clever coding removes that trade-off, it just makes the lag explicit instead of hidden. The EA searches back from the newest possible pivot position for the two most recent confirmed swings and compares both the price extreme and the MACD main line reading at each. It only opens a trade on the bar where a fresh pivot is first confirmed, so a divergence that stays true for several bars in a row cannot fire the same signal repeatedly. The stop is sized from ATR rather than a fixed pip count, because how far price can reasonably travel before a divergence trade is proven wrong scales with recent volatility, not with a constant. Position sizing can run as a fixed lot or, with InpUseRiskSizing on, as a percentage of equity risked to that ATR-derived stop distance, using MODE_TICKVALUE and MODE_TICKSIZE rather than a hardcoded contract size so it prices correctly on gold, indices and JPY crosses as well as plain FX pairs. Notes and limitations - Only regular divergence is detected (price extreme vs oscillator extreme disagreeing). Hidden divergence, used by some traders as a continuation signal, is a different pattern and is not covered here. - Every signal is InpPivotBars bars late by construction, because that delay is what makes the pivot confirmed rather than a guess. This is not a lookahead-free shortcut around that cost - there is no such shortcut. - One position at a time, tracked by InpMagicNumber. While a position is open the EA does not evaluate new divergence signals; it waits for the stop or target to close the trade first. - Divergence trading has a well-documented failure mode: momentum can diverge and price can simply keep extending anyway. The ATR stop limits the damage; it does not prevent the loss.

Expert advisor · MT4 · v1.0 · Free

About MACD Divergence EA

MACD Divergence EA is an expert advisor (trading robot) for MetaTrader 4 based on MACD and ATR. It increases the lot size after a losing trade (martingale) and can size trades from your balance and risk percent. It has 15 settings, such as Macd fast period, Macd slow period and Macd signal period. MQL source code included.

Files in the download: MacdDivergenceEa.ex4, MacdDivergenceEa.mq4, MacdDivergenceEa.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Macd fast period12MACD fast EMA period
Macd slow period26MACD slow EMA period
Macd signal period9MACD signal period
Pivot bars3Bars required on each side to confirm a swing point
Lookback bars60How far back to search for the second swing point
Atr period14ATR period used to size the stop
Atr stop multiplier2.0Stop distance, in ATR multiples
Take profit pips60Take profit distance, in pips (0 = none)
Use risk sizingfalseSize by percent risk instead of a fixed lot
Risk percent1.0Percent of equity risked per trade, when risk sizing is on
Fixed lots0.10Lot size when risk sizing is off, or as a fallback
Magic number20260605Magic number for orders opened by this EA
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per trade action before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: MACD · Divergence · Momentum · Oscillator · Martingale · Risk & Lot Size · ATR · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria