Get started
QUANT-FX INFRASTRUCTURE LTD · Co. no. 17496299

Why Gold EA Backtests Lie, and How to Test XAUUSD

A gold EA backtest usually errs in one direction: it charges less than the live market does. XAUUSD spreads, fills and broker rules change most at the moments a typical gold EA wants to trade, and a test that holds them constant hides the costs that decide whether the strategy survives.

QuantFX9 min read

Fixed spread versus variable spread on XAUUSD

Most misleading gold tests start with the spread. The MT4 tester charges one fixed spread for the whole run, either the number you type in or the spread at the moment you press Start. MT5's generated-tick modes (Every tick, 1 minute OHLC and Open prices only) take the spread stored with each one-minute bar, which on many servers is the lowest spread seen in that minute. Only Every tick based on real ticks replays the bid and ask the broker recorded, so the spread moves the way it moved.

On a major FX pair the gap between a typical spread and a bad one is small next to the stop. On gold it is not. Gold spreads widen sharply in thin hours, at the daily rollover and around US data, and they stay wide for longer than on the majors. On a 100 oz contract quoted to two decimals, one point is 0.01 USD per ounce, so every point of spread costs 1 USD per lot. A variable spread backtest charges the spread the account would have paid at each entry; a fixed one charges an average that the EA rarely meets when it trades.

The error is not random. Gold strategies tend to trade breakouts and session moves, which happen when the spread is wide. A fixed spread removes the cost exactly where the trades cluster, so the test is wrong in the EA's favour.

Slippage at the London and New York opens

The London open, the US data releases at 08:30 New York time and the US cash open at 09:30 are when gold volume arrives, and when fills drift furthest from the price the EA saw. Most retail gold accounts use market execution, where the order fills at the next available price and the deviation set with CTrade::SetDeviationInPoints() is ignored; it only applies to instant and request execution.

The MT5 tester's default delay setting is Zero latency, ideal execution: a market order fills at the price of the tick it was sent on. That is a fill no live account gets at the open. The tester can add a fixed delay in milliseconds or a random delay, and on real ticks the fill then comes from the price recorded after the delay, which is the closest the tester gets to open-hour slippage.

Stops-level rejections in MT5

Every MT5 symbol carries two broker limits. SYMBOL_TRADE_STOPS_LEVEL is the minimum distance, in points, between the current price and a stop loss, a target or a pending order price. SYMBOL_TRADE_FREEZE_LEVEL is the distance inside which an order or position close to its trigger can no longer be modified. A request that breaks the stops level comes back with TRADE_RETCODE_INVALID_STOPS (10016).

The tester reads both values from the symbol's specification when the run starts and applies them unchanged across years of history. Live, the picture is less tidy. On many servers a stops level of zero does not mean no limit; it means the minimum floats with the spread, so a stop placed inside a widened spread is rejected live while the tester accepted it. Some brokers also raise gold's limits around news.

The consequence depends on how the EA sends its stops. If the stop is attached to the order, the whole order is rejected and the trade never happens. If the EA opens first and sets the stop afterwards, the position runs without a stop until a modification succeeds. A tight trailing stop is worse again: it can be refused on every tick at exactly the moment it matters. None of this appears in a test that never sees a rejection.

News spikes and the daily rollover

Around non-farm payrolls, CPI and FOMC decisions, gold can move many times its normal spread in a second, with the spread itself widening and quotes thinning out. Bars hide most of this: MT5 builds bars from the bid, so a short position's stop, which triggers on the ask, can be hit by a spread spike that no bar shows. Only real ticks carry the ask.

A news blackout is the usual answer, and it has its own testing trap: MQL5's economic calendar functions do not work in the strategy tester. An EA that reads the calendar live and assumes the same in testing trades straight through every release in its backtest. We export the event list to a file and give it to the tester with #property tester_file, so the blackout runs the same way in both.

The daily rollover is the other hazard. Many brokers pause gold for a short break around 17:00 New York time, and the first quotes after the break often carry the widest spreads of the day. Swaps are also booked at the rollover, so an EA holding positions overnight pays a cost that a short test may barely show.

A worked example: the same gold EA backtest, two cost models

Illustrative numbers, example EA

An example breakout EA on XAUUSD, designed to capture 40 points per trade before costs, on a 100 oz contract where one point is 1 USD per lot. Example commission: 7 USD per lot per round trip. The first run uses a fixed 20-point spread with zero latency; the second uses real ticks from the same broker with a random delay. All figures are points per trade at 1 lot.

Cost lineFixed spread, zero latencyReal ticks, random delay
Average spread paid2038
Slippage, entry and exit08
Commission77
Total cost2753
Left of the 40 designed+13-13

The average spread is higher in the second run because the EA's entries cluster at the London open and around US data, not because the broker changed.

Same code, same broker, same history: the sign of the last line flips. The demo forward test then added a third finding the tester could not: some of the trailing-stop modifications near the open came back with 10016. The fixed-spread, zero-latency run was not a bug. It was a test that asked the wrong question, and every parameter optimised on it was tuned to costs that do not exist, a form of the overfitting we describe in avoiding overfitting.

The test setup we use for a gold EA

Our gold tests are built to charge at least what the live account will pay:

  • Real ticks from the broker and account type the EA will trade. Gold contract sizes, digits, commission and limits differ by broker, and so do the ticks.
  • History checked before results. We read History Quality in the report and count ticks per day, and flag any range where the tester fell back to generated ticks.
  • Execution delay switched on. A fixed delay close to what a demo account measures, then a random-delay run.
  • Limits read on every request. The EA reads the stops and freeze levels with SymbolInfoInteger() before each order or modification, logs every rejection and is also run against a stricter minimum of its own.
  • A cost stress run. A custom symbol built from the same ticks with the ask shifted up, so we see how much extra spread the logic tolerates.
  • Results split by hour. The open hours and the news windows are reported separately, so a strategy that only works in quiet hours is visible.
  • A demo forward test at your broker before any real money, with the spread, delay and retcode of every order logged.

What to ask before you trust a gold backtest

Whether the test came from a developer, a vendor or your own terminal, ask:

  1. Which modelling mode was used, and what did the report show for History Quality?
  2. Whose ticks were they, and is that the broker and account type you will trade?
  3. Was an execution delay set, or was it zero latency?
  4. How does the EA handle a 10016 rejection, and was that path ever tested?
  5. If the EA has a news filter, how did it get event data inside the tester?
  6. What does the test look like with the open hours and news windows separated out?

In practice at QuantFX

Gold is the core market of our own strategies, and the spread, stops-level and news handling above is what we build into every custom gold (XAUUSD) EA: a live spread check before each order, broker limits read on every request, session windows and a news blackout that works in the tester as it does live. You receive the source code, the real-tick tester reports and the demo forward-test log.

If you already run a gold EA and want to know whether its backtest holds up, an independent EA review re-tests it with these costs and compares its live record trade by trade with the backtest, in a written report.

EA builds are priced by class, from £6,000 with a 30% deposit. The review is £5,000, with the report in 3 to 7 business days. See the prices and start.

Gold EA backtests: questions we are asked

Why does my gold EA do worse live than in its backtest?

Most often because the backtest charged a fixed or minimum spread and filled every order with no delay, while the live account pays the wide spreads and slippage of the open hours and news, when gold EAs tend to trade. Stops-level rejections that the tester never produced are the next most common cause.

Is the Every tick mode good enough for testing gold?

No. Every tick generates prices from one-minute bars and uses the bar's stored spread, so it cannot show spread spikes inside the minute or the ask moving on its own. Use Every tick based on real ticks, check History Quality, and switch on an execution delay.

What is the stops level in MT5 and why does it reject my orders?

It is the broker's minimum distance, in points, between the current price and a stop, a target or a pending order. A request inside that distance is rejected with retcode 10016. On many servers a value of zero means the minimum floats with the spread, so stops that were fine in testing can be refused live when gold's spread widens.

Whose tick data should a gold EA be tested on?

The broker and account type it will trade. Tick histories, contract sizes, commissions and limits differ between brokers, so a test on another source tells you about that source. Third-party ticks can be imported into a custom symbol for longer history, but the final checks belong on your broker's data and a demo account there.

Can you re-test a gold EA I already run?

Yes, through an independent EA review. We need the source code, the backtest report with the inputs used and read-only investor access to the live account. The review re-tests the EA with real ticks, variable spreads and delay, compares live and tested trades, and reports what it found and the limits of the review.

Does a real-tick backtest tell me how a gold EA will trade live?

No. A realistic backtest removes the errors that make a test look better than the market allows, but it replays the past. It cannot model a different market regime, a broker changing its conditions or liquidity that is not there. That is why every build ends with a demo forward test.

Trading leveraged products carries a high risk of loss. This article is general engineering information, not advice on whether or how to trade. All numbers in the examples are illustrative.