Skip to content

Free MT5 Expert Advisor

MACD Trend EA

MT5 Expert Advisor that trades MACD signal-line crossovers in the direction of the 200 EMA, with an ATR stop-loss and R target.

Momentumv1.0MetaTrader 5 (MQL5) Compiles with 0 errors Non-repainting signals Stop-loss on every trade
Strategy
Buys when the MACD line crosses above its signal line while price is above the 200 EMA, and sells the opposite. The stop is 1.5 x ATR and the target is 2R.
Risk model
1% of balance per trade (configurable), stop-loss on every order, one position at a time

What it does

The MACD Trend EA uses the MACD signal-line crossover as a momentum trigger and the 200 EMA as a trend filter. The idea is to join an existing trend when momentum swings back in its favour, rather than trading every MACD cross.

Trades are opened with an ATR-based stop-loss, an optional reward-to-risk target and position sizing based on a fixed percentage of your balance.

How it works

On every new bar the EA reads the last two closed bars of the standard MACD (12, 26, 9) and a 200-period EMA.

  • Bullish cross: the MACD line was at or below the signal line two bars ago and is above it on the last closed bar.
  • Trend filter: a bullish cross is traded only when the last close is above the 200 EMA; a bearish cross only when it is below.
  • Optional zero-line rule: with it enabled, a buy also requires the MACD line to be below zero at the cross (a pullback inside an uptrend) and a sell requires it above zero.
  • Stop-loss: 1.5 x ATR(14) from the entry price. Take-profit: 2 x the stop distance.
  • Exit: by default an open position is closed on the opposite signal-line cross.
  • Skipped signals (filter, spread, hours, open position, stop too close) are printed to the Experts log.

How to use it

Use it on liquid instruments that trend, such as EURUSD, USDJPY or gold on H1 and H4. The zero-line option makes the EA more selective: it then only buys momentum turning up from below zero while the long-term trend is up, which many traders treat as a pullback entry.

The MACD lesson explains how the two lines are built if you want to tune the periods.

Settings

InputDefaultWhat it changes
MACD fast EMA12Fast EMA of the MACD line.
MACD slow EMA26Slow EMA of the MACD line.
MACD signal period9Smoothing of the signal line.
Trend filter EMA period200Crosses are taken only on the matching side of this EMA.
Require zero-line sidefalseIf true, buys need the cross below zero and sells above zero.
Stop-loss distance (x ATR)1.5Stop distance as a multiple of ATR(14).
Take-profit (R multiple)2.0Target as a multiple of the stop distance.
Exit on opposite crosstrueCloses the trade on the opposite signal-line cross.
Risk per trade (% of balance)1.0Share of balance risked between entry and stop.
Fixed lots0Fixed volume instead of risk %; 0 = risk-based.
Maximum spread (points)20Skips signals when the spread is wider; 0 = off.
Trading start / end hour7 / 20Server-time window for new trades; equal values = 24 hours.
Move stop to break-eventrueStop to entry + 20 points once profit reaches 1R.
Use trailing stopfalseTrails the stop 2 x ATR behind price.
Magic number2611003Identifies this EA's trades.

Limitations

  • MACD is built from moving averages, so signals lag price, especially on fast reversals.
  • Signal-line crosses are frequent; in flat markets near the 200 EMA many will fail.
  • Closing on every opposite cross can cut winners short before the 2R target is reached; test both settings.
  • A gap or news spike can trigger a cross with an unusually wide ATR stop.

Before you run it live

Test on a demo account and in the MetaTrader 5 Strategy Tester over several years first. No backtest results are published for this EA, and any test describes the past only.

  • Match the spread limit and trading hours to your broker's spreads, stop level and session times, and include commission and swap in tests.
  • Start with 1% risk or less and check the lot sizes in the Experts log; the Position Size Calculator is a useful cross-check.
  • Use a different magic number on each chart, and keep the terminal (or a VPS) running so stops are managed.

Check how often the exit cross closes trades before the target in the Strategy Tester and decide whether that suits you.

Key inputs

Key inputs of MACD Trend EA
InputDefaultDescription
MACD fast EMA12Fast EMA of the MACD line.
MACD slow EMA26Slow EMA of the MACD line.
MACD signal period9Smoothing of the signal line.
Trend filter EMA period200Crosses are taken only on the matching side of this EMA.
Require zero-line sidefalseIf true, buys need the cross below zero and sells above zero.
Stop-loss distance (x ATR)1.5Stop distance as a multiple of ATR(14).
Take-profit (R multiple)2.0Target as a multiple of the stop distance.

Educational tools, not advice. Automated trading can lose money quickly. Indicators and Expert Advisors describe price; they do not predict it, and past behaviour does not guarantee future results. Test everything on a demo account first and never risk money you cannot afford to lose. See our disclaimer.

Keep exploring

Related mt5 expert advisors