Moving average crossover EA: we built 5 versions and backtested them on gold

A moving average crossover EA buys when a fast EMA crosses above a slow one and sells on the opposite cross. We built one in the EA Builder and backtested it on gold (XAUUSD H1) from January 2024 to November 2026. The plain version lost 16%. Two changes, a stop sized by ATR and an exit on the opposite cross, turned the same entry into a profit factor of 1.70. Below is every build, block by block, with the MetaTrader 5 results.

Open the EA BuilderFree to build. No card, no install.

MetaTrader 5 chart of XAUUSD H1 in March 2026 with EMA 20, 50 and 200 and the backtest trades of version E: a sell held from 5,132 to 4,570

The result first: five versions side by side

Every version uses the same entry: EMA 20 crossing EMA 50 on a closed H1 candle, risk 1% of the balance per trade, one position at a time. Each row changes one thing from the version it builds on.

VersionWhat changedTradesWin rateProfit factorNet resultMax drawdown
ACross only. Stop 1,500 points, target 3,00027831.7%0.92-16.2%23.5%
BA + trade only with the EMA 200 trend18332.2%0.93-9.5%19.2%
CB + ADX 14 above 2015934.0%1.02+0.2%14.6%
DB with stop 2 x ATR and target 4 x ATR16940.2%1.38+33.0%10.1%
ED, but no target: exit on the opposite cross20030.5%1.70+83.8%17.9%

Backtest on past data, not a forecast. Net result includes swap. Drawdown is measured on closed trades. Full conditions in the next section.

Balance curves of the five EMA crossover EA versions on XAUUSD H1 from January 2024 to November 2026; E ends near 19,000 dollars, D near 13,300, A and B below 10,000
Balance of each version from a $10,000 start, trade by trade (before swap). A and B drift down; D climbs steadily; E makes most of its gain in long trends from mid-2025.

How we tested

SettingValue
Symbol and timeframeXAUUSD (gold), H1
Period1 Jan 2024 to 27 Nov 2026, 17,214 H1 candles
Tester model1-minute OHLC, MetaTrader 5 Strategy Tester
Price dataIC Markets demo server
Deposit and leverage$10,000, 1:100
Position sizeRisk 1% of balance per trade (lot from the stop distance)
CostsSpread from the data and swap included; no commission
EABuilt in the EA Builder, compiled as a free .ex5 on 29 Nov 2026

The numbers 20, 50, 200, ADX 20 and ATR 2/4 were chosen before testing and never tuned. Tuning them on the same data would make every version look better than it is.

Build version A in the EA Builder, block by block

  1. Find the Moving Average block

    Open the library on the left and type "moving". Drag Moving Average onto the canvas twice.

    Block library of the EA Builder searched for moving, showing the Moving Average, Bollinger Bands, Envelopes and MACD blocks
  2. Set the two averages

    Name the first EMA Fast: period 20, method Exponential, price Close. The second EMA Slow: period 50. Shift (bar index) 1 means the value of the last closed candle, so a signal never appears and vanishes inside a candle.

    Moving Average block set to EMA Fast, period 20, exponential method, close price, shift 1
  3. Wire them into a Crossover block

    EMA Fast into Value A, EMA Slow into Value B, logic "Crosses Above" for the buy. Add a second Crossover with "Crosses Below" for the sell. A dotted blue wire carries a number.

    EMA Fast and EMA Slow blocks wired into the Value A and Value B inputs of a Crossover Logic block set to Crosses Above
  4. Open the trade

    Connect each crossover to an Open Position block: Buy Market for the up cross, Sell Market for the down cross. The comment is written on every order, useful when you read the trade history later.

    Open Position block set to Buy Market with the comment EMA cross buy
  5. Size the position

    Trade Management: lot type Risk % per Trade, risk 1, max open trades 1. With a 1,500-point stop on gold the EA opened 0.06 lot at the start: $10,000 x 1% = $100, and 1,500 points is $15 a unit, $1,500 per lot, so 100 / 1,500 = 0.066, rounded down to the broker's step.

    Trade Management block with lot type Risk percent per trade, risk 1, max open trades 1
  6. Set the stop and target

    Stop Loss and Take Profit: Points, 1,500 and 3,000. On gold with two decimals that is a $15 stop and a $30 target.

    Stop Loss and Take Profit block set to 1500 points stop and 3000 points target
  7. Check the whole diagram and build

    Global blocks (setup, sizing, stops) need no wires. The two signal chains run left to right. Name the EA and press Download EA: the .ex5 is compiled on the server and is free.

    Export dialog of the EA Builder with the EA name Gold EMA Cross, Compile .ex5 marked free and source code locked
Complete diagram of version A: strategy setup, trade management, stop loss, two moving averages, two crossovers, buy and sell blocks
Version A complete: nine blocks.

Version A: gold rose about 75%, and the EA still lost 16%

Gold went from about $2,060 to over $5,000 during the test. Version A traded both directions and ended at $8,379. Buys made +$345, sells lost -$1,695, swap cost another -$271.

The win rate explains it. With the target at twice the stop, a trade pays 2 units or loses 1. Break-even is where 2 x wins = losses, which is one win in three, 33.3%. Version A won 31.7% of 278 trades: just under the line, and costs pushed it further down.

MetaTrader 5 chart of XAUUSD H1, 1 to 21 April 2026, with EMA 20 and 50 and version A's trades: ten crosses stopped out and one target hit
Version A in early April 2026. Small triangles are entries, a cross marks a stop-out, a tick a target. The averages weave around each other and almost every cross is stopped within hours.

Why a fixed 1,500-point stop stopped working

A stop in points is a fixed dollar distance. As the price of gold rose, the same distance became a smaller and smaller share of the price, and of the normal hourly swing.

Entry price (first trade of the period)1,500-point stop as % of price
$2,076 (January 2024)0.72%
$3,316 (July 2025)0.45%
$5,039 (March 2026)0.30%

By 2026 the stop was 2.4 times tighter relative to price than in 2024, and normal noise hit it. Rule of thumb: if the symbol's price changed a lot over your test period, do not use a stop in points. Use ATR or a percent of price, which grow with the market.

Version B: only trade in the direction of the EMA 200

Add a Price Data block (Close, shift 1), a third Moving Average (EMA 200) and two Crossover blocks set to compare instead of cross: A > B for buys, A < B for sells. Then a Logic Gate set to AND joins each trend check with its crossover before the Open Position block.

Price Data and EMA 200 blocks wired into two comparison blocks, A greater than B and A less than B
The trend check: close above EMA 200 allows buys, below allows sells.
Crossover blocks and trend comparisons joined by AND logic gates that feed the buy and sell blocks
AND gates: the cross and the trend must both be true on the same candle.

Result: 183 trades instead of 278, drawdown down from 23.5% to 19.2%, but profit factor still 0.93. The filter removed losing and winning trades in almost equal measure, because the stop, not the entry, was the problem.

MetaTrader 5 chart of XAUUSD H1, 1 to 21 April 2026, with EMA 20, 50 and 200 and version B's trades
The same weeks with version B: fewer entries, but the ones taken are still stopped by the tight fixed stop.

Version C: skip quiet markets with ADX

On top of B, an ADX 14 block compared with a Static Value of 20 joins both AND gates: no new trade when ADX is 20 or lower, which usually means a market without direction. Profit factor 1.02, net +0.2%, drawdown 14.6%. Break-even, with fewer and calmer losses. The fixed stop still capped what the entries could earn.

Diagram of version C with the ADX block and level 20 added to both logic gates
Version C: the ADX check feeds both gates.

Version D: let volatility set the stop

Back to B (no ADX), one setting changed: on the Stop Loss and Take Profit block, type ATR, stop 2 and target 4. The stop is now twice the average range of the last 14 candles, wherever the price is. Because the lot is sized from the stop, a wider stop automatically means a smaller lot: risk stays 1%. Lots ranged from 0.01 to 0.16 over the test.

Profit factor 1.38, net +33.0%, drawdown 10.1%, win rate 40.2%. Same entries as B, very different result: the exit was the weak part all along.

MetaTrader 5 chart of XAUUSD H1, 1 to 21 April 2026, with version D's trades using an ATR-based stop
Version D in the same weeks: the wider stop survives the noise that stopped A and B.

Version E: no target, exit on the opposite cross

From D, set the take profit type to None and add two Close Position blocks: Close All Buys on the down cross and Close All Sells on the up cross. A trade now stays open until the trend turns.

Profit factor 1.70, net +83.8%. But look at how it got there: win rate 30.5%, average win $365 against an average loss of $94, a longest run of 13 losing trades, and the average trade held 68 hours, so swap cost $799. Most of the profit came from buys (+$8,509) during gold's rally; sells added +$675.

MetaTrader 5 chart of XAUUSD H1 from 18 August to 12 September 2025 with version E's long trade from 3,368 to 3,617
A version E buy held from 3,368 to 3,617 between 22 August and 11 September 2025: +$1,493 on one trade.

Version E is the right design if you can sit through ten or more losses in a row while waiting for a trend. If you cannot, D is the calmer choice: lower return, half the losing streak.

Does it hold up? Gold 2020-2023 and EURUSD

A result from one symbol and one period proves little. We ran the same files, unchanged, on gold from January 2020 to December 2023 and on EURUSD H1 over the same 2024-2026 period.

Bar chart of profit factor for versions A, B, D and E on gold 2024-2026, gold 2020-2023 and EURUSD 2024-2026
Profit factor per test. Above 1.0 made money.
VersionGold 2024-2026Gold 2020-2023EURUSD 2024-2026
A-16.2%, PF 0.92-17.7%, PF 0.88+8.6%, PF 1.85, only 28 trades
B-9.5%, PF 0.93+21.2%, PF 1.29-11.1%, PF 0.55, 33 trades
D+33.0%, PF 1.38+29.2%, PF 1.24-38.7%, PF 0.74
E+83.8%, PF 1.70+73.5%, PF 1.37-40.4%, PF 0.76

On gold, D and E made money in both periods. On EURUSD both lost about 40%. Gold had several long one-way moves in both periods; EURUSD spent most of 2024-2026 in ranges, and a crossover pays only in trends. Two more things the table shows:

  • Points do not travel between symbols. 1,500 points is $15 on gold but 150 pips on EURUSD. Versions A and B on EURUSD were in effect a different, very slow strategy with too few trades to judge.
  • Holding costs grow with holding time. Version E paid $2,282 of swap on gold in 2020-2023. On a symbol with a high swap, an exit-on-cross design can lose money even with good entries.

What these numbers do not tell you

  • One broker's data and the 1-minute OHLC model. Real tick data and another broker's spread will move the numbers.
  • No commission. On a raw-spread account, add the commission per lot to every trade.
  • Gold rose strongly in both test periods. A design that rides long trends will look its best in such a market.
  • Past results. A backtest is a record of how these rules behaved on past prices, not a forecast. Run any version on a demo account before real money.

Build version E yourself

BlockSettings
Strategy SetupMagic number: auto generate. Slippage 10
Trade ManagementRisk % per trade 1, max open trades 1
Stop Loss and Take ProfitStop: ATR 2 (period 14). Take profit: None
Moving Average x 3EMA 20, EMA 50, EMA 200; close price; shift 1
Price DataClose, shift 1
Crossover x 420/50 Crosses Above; 20/50 Crosses Below; Close vs EMA 200 A > B; Close vs EMA 200 A < B
Logic Gate x 2AND: up cross + A > B; down cross + A < B
Open Position x 2Buy Market from the first gate; Sell Market from the second
Close Position x 2Close All Buys from the down cross; Close All Sells from the up cross

Open the EA BuilderFree. Build it, then test it on your own symbol before trusting these numbers.

Questions people ask before they start

Which moving averages work best for a crossover EA?
We did not optimise the periods. 20/50 with a 200 filter is a common default. Changing the exit (version D and E) mattered far more in our test than any change to the entry.
Why did the plain crossover lose money in a strong uptrend?
Its fixed 1,500-point stop became too tight as gold's price rose, and it also traded every sell signal against the rally. Sells alone lost $1,695.
Should a crossover EA use a take profit?
On gold in our test, exiting on the opposite cross (version E) earned more than a fixed 4 x ATR target (version D), but with a lower win rate, a longer losing streak and higher swap. Choose by the drawdown you can accept.
Can I use these settings on EURUSD?
Not without testing. The same D and E files lost about 40% on EURUSD H1 in 2024-2026.

Updated:

Keep reading