- 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
| Input | Default | What it changes |
|---|---|---|
| MACD fast EMA | 12 | Fast EMA of the MACD line. |
| MACD slow EMA | 26 | Slow EMA of the MACD line. |
| MACD signal period | 9 | Smoothing of the signal line. |
| Trend filter EMA period | 200 | Crosses are taken only on the matching side of this EMA. |
| Require zero-line side | false | If true, buys need the cross below zero and sells above zero. |
| Stop-loss distance (x ATR) | 1.5 | Stop distance as a multiple of ATR(14). |
| Take-profit (R multiple) | 2.0 | Target as a multiple of the stop distance. |
| Exit on opposite cross | true | Closes the trade on the opposite signal-line cross. |
| Risk per trade (% of balance) | 1.0 | Share of balance risked between entry and stop. |
| Fixed lots | 0 | Fixed volume instead of risk %; 0 = risk-based. |
| Maximum spread (points) | 20 | Skips signals when the spread is wider; 0 = off. |
| Trading start / end hour | 7 / 20 | Server-time window for new trades; equal values = 24 hours. |
| Move stop to break-even | true | Stop to entry + 20 points once profit reaches 1R. |
| Use trailing stop | false | Trails the stop 2 x ATR behind price. |
| Magic number | 2611003 | Identifies 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.

