Skip to content

Free MT5 Expert Advisor

EMA Crossover Trend EA

MT5 Expert Advisor that trades fast/slow EMA crossovers only in the direction of the 200 EMA, with an ATR stop and R-multiple target.

Trendv1.0MetaTrader 5 (MQL5) Compiles with 0 errors Non-repainting signals Stop-loss on every trade
Strategy
Buys when the 20 EMA crosses above the 50 EMA on a closed bar while price closes above the 200 EMA, and sells the mirror image. 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 EMA Crossover Trend EA automates one of the oldest trend-following ideas: buy when a fast exponential moving average crosses above a slower one, sell when it crosses below. To avoid trading every wiggle against the bigger picture, it only accepts crossovers that agree with a long 200-period EMA.

Every trade is opened with a volatility-based stop-loss and an optional fixed reward-to-risk target, and the position size is worked out from the stop distance so each trade risks a set percentage of your balance.

How it works

All signals are evaluated once per bar, on the bar that has just closed, so nothing changes while a candle is still forming.

  • Bullish cross: the fast EMA was at or below the slow EMA two bars ago and is above it on the last closed bar.
  • Trend filter: a bullish cross is traded only if that bar closed above the 200 EMA (a bearish cross only if it closed below). Rejected crosses are written to the Experts log with the reason.
  • Stop-loss: entry price minus 1.5 x ATR(14) for buys, plus 1.5 x ATR for sells.
  • Take-profit: entry plus 2 x the stop distance (2R). Set it to 0 to rely on the exit cross instead.
  • Exit cross: with the default setting, an open buy is closed when the fast EMA crosses back below the slow EMA, whether or not the trend filter agrees.
  • Size: lots = (balance x risk %) ÷ (stop distance ÷ tick size x tick value), rounded down to the volume step.

How to use it

This EA suits liquid pairs that trend for days at a time, such as EURUSD or GBPUSD on H1 and H4. Read the 200 EMA as the "permission" layer and the crossover as the trigger.

If you are new to the building blocks, the lessons on moving averages and the risk-reward ratio explain why the stop and target are expressed in R.

Settings

InputDefaultWhat it changes
Fast EMA period20Faster moving average used for the crossover.
Slow EMA period50Slower moving average used for the crossover.
Trend filter EMA period200Crossovers are taken only on the matching side of this EMA.
Stop-loss distance (x ATR)1.5Stop distance as a multiple of the 14-period ATR.
Take-profit (R multiple)2.0Target as a multiple of the stop distance; 0 disables it.
Exit on opposite crosstrueCloses the position on the opposite EMA 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 number2611001Identifies this EA's trades.

Limitations

  • Crossovers lag: by the time two EMAs cross, a good part of the move may already be over.
  • In sideways markets the EMAs cross back and forth and the EA will take a series of small losses.
  • The 200 EMA filter reduces counter-trend trades but also skips the start of new trends.
  • ATR stops widen in volatile conditions, which shrinks the position size; very quiet markets can produce stops close to the broker's minimum distance.

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.

Pay particular attention to how many crossovers the Strategy Tester shows in ranging months; that is where this design is weakest.

Key inputs

Key inputs of EMA Crossover Trend EA
InputDefaultDescription
Fast EMA period20Faster moving average used for the crossover.
Slow EMA period50Slower moving average used for the crossover.
Trend filter EMA period200Crossovers are taken only on the matching side of this EMA.
Stop-loss distance (x ATR)1.5Stop distance as a multiple of the 14-period ATR.
Take-profit (R multiple)2.0Target as a multiple of the stop distance; 0 disables it.
Risk per trade (% of balance)1.0Position size is calculated so the stop risks this share of balance.

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