Moving Average Crossover EA for MT5 and MT4: Build It Free
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:
| Style | Fast | Slow | Typical timeframe |
|---|---|---|---|
| Short-term | 9 EMA | 21 EMA | M15–H1 |
| Swing | 20 EMA | 50 EMA | H1–H4 |
| Position | 50 SMA | 200 SMA | D1 |
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:
- OnNewCandle on your timeframe — the check runs once per closed bar.
- Two Technical Indicator nodes set to Moving Average: fast and slow, each with its period, method (SMA, EMA, SMMA or LWMA) and applied price.
- The previous values. The MA node has a
shiftsetting: 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. - Two Comparison nodes — fast > slow now, fast ≤ slow before — feeding an AND.
- 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.
- A BUY Order node with lot, stop loss and take profit (in points).
- 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
fxDreema Alternative: When a Typed Graph Beats Blocks
fxDreema is a mature block builder for MT4 and MT5. Where a typed node graph with an AI assistant does better, where fxDreema is stronger, and how to choose.
5 min readWhat Is an Expert Advisor (EA) in Forex Trading?
An Expert Advisor is a program that runs inside MetaTrader and trades by rules. How EAs work, what they can and can't do, and how to get or build one.
4 min readBest Expert Advisor Builders in 2026, Honestly Compared
fxDreema, EA Builder Pro, EA Studio, StrategyQuant X, the MQL5 Wizard, ChatGPT and TradeSmithy compared on what each builds, exports, tests and costs.
7 min read