Blog

Moving Average Crossover EA for MT5 and MT4: Build It Free

TradeSmithy Team4 min read

Short answer: a moving average crossover EA buys when a fast moving average crosses above a slow one and sells (or exits) when it crosses back below. The part most home-made versions get wrong is detecting the cross — "fast is above slow" is true for hundreds of bars in a row, so the EA must compare the current bar with the previous one and trade only on the bar where the relationship flips.

How does a moving average crossover strategy work?

Two moving averages of price with different lengths — say a 20-period and a 50-period. When the short one crosses above the long one, recent prices are rising faster than the longer trend: a bullish signal. The reverse cross is bearish. With a 50 and 200 on daily charts, the two are called the golden cross and the death cross.

It is a trend-following strategy. It does well in sustained trends and gives back money in sideways markets, where the averages cross back and forth. That is not a bug to be engineered away with clever periods; it is the nature of the strategy, and a filter is usually the better fix.

Which moving average settings should you use?

There is no universally best pair. Common starting points:

StyleFastSlowTypical timeframe
Short-term9 EMA21 EMAM15–H1
Swing20 EMA50 EMAH1–H4
Position50 SMA200 SMAD1

EMA reacts faster than SMA; faster means earlier entries and more false signals. Make both periods EA inputs so you can test alternatives instead of guessing.

Detecting the cross correctly

The rule for a bullish cross is two comparisons at once:

  • Now: fast MA > slow MA
  • One bar ago: fast MA ≤ slow MA

Check once per closed candle, not every tick, or a cross that appears and disappears inside the bar will trigger a trade.

How to build it in TradeSmithy

Start from the Moving Average Crossover template, or build it from a blank canvas:

  1. OnNewCandle on your timeframe — the check runs once per closed bar.
  2. Two Technical Indicator nodes set to Moving Average: fast and slow, each with its period, method (SMA, EMA, SMMA or LWMA) and applied price.
  3. The previous values. The MA node has a shift setting: a second copy of each average with shift 1 reads one bar further back. Alternatively, store each average in a Variable at the end of every candle and compare against it on the next.
  4. Two Comparison nodes — fast > slow now, fast ≤ slow before — feeding an AND.
  5. A Positions & Orders node counting this EA's open positions, and a comparison "count = 0" into the same AND, so it holds one trade at a time.
  6. A BUY Order node with lot, stop loss and take profit (in points).
  7. The mirror image with a SELL Order, and OnNewCandle End to close every branch.

For the exit, either use fixed stop and target, or close on the opposite cross with a Close Order node set to "Buy Positions".

Add a trend filter (the three-MA version)

The most useful improvement is a third, slower average — a 200-period MA — and only taking buys when price is above it and sells when below. In the graph, that is one more indicator, one comparison, and one more input on the AND node. It cuts trades in the sideways stretches where crossovers bleed.

Make every number an input

Wire each period to a Global Parameter node and it becomes an EA input you can change in the terminal and optimise in MetaTrader's Strategy Tester — no re-export needed. How parameter ports work. Size positions from balance rather than a fixed lot with this pattern.

Build your crossover EA now

Start free on TradeSmithy, open the Moving Average Crossover template, set your periods, and export it as MQL5 or MQL4 — the Free plan includes one export a month. Then test it properly: our MT5 backtesting guide covers the settings. On Pro you can ask the AI assistant to add the trend filter for you.

Frequently asked questions

What is the best moving average crossover for forex?

None is best everywhere. Test a few pairs of periods on your symbol and timeframe, on data you did not tune on.

Is a moving average crossover EA profitable?

It can be in trending markets and loses in ranges. Filters, sensible stops and realistic costs decide the result.

Why does my crossover EA open many trades in a row?

It is checking "fast above slow" instead of detecting the cross, or it has no limit on open positions.

Can I build a 3 moving average crossover EA without code?

Yes. Add a third MA as a trend filter and combine the conditions with an AND node.

Keep reading

← More from the TradeSmithy blog