MT5 XAUUSD EAs: Sizing, Regime Gates and Setfile Risk
A practical framework for gold EAs: ATR-normalized position sizing in MQL5, volatility regime gates, group-stop exit geometry, and the backtest protocol that stops a tuned setfile from ending your account.
Most XAUUSD expert advisors do not blow up because the entry logic is wrong. They blow up because the sizing and the regime gate are wrong, and because the .set file that goes live was tuned on the wrong data. Gold punishes both mistakes faster than any FX pair.
Here is the framework we use when reviewing a gold EA — the same one you can apply to any MT5 system in ten minutes.
1. Fixed lots on XAUUSD is a bug, not a setting
Gold's volatility is not stationary. A daily ATR(14) of $84 today versus a $35 range in a quiet regime means the same 0.10 lot position carries 2.5x the dollar risk depending on the month. That is how a system with a solid backtest produces a live equity curve nobody recognises.
Volatility-normalize the position instead. The whole risk model collapses into one function:
int hAtrFast = INVALID_HANDLE;
double AtrVal(int handle)
{
double buf[];
if(handle == INVALID_HANDLE) return 0.0;
if(CopyBuffer(handle, 0, 0, 1, buf) < 1) return 0.0;
return buf[0];
}
double CalcLots(double riskPercent, double stopDistance)
{
double balance = AccountInfoDouble(ACCOUNT_BALANCE);
double riskMoney = balance * riskPercent / 100.0;
double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
if(tickSize <= 0.0 || tickValue <= 0.0) return 0.0;
double lossPerLot = (stopDistance / tickSize) * tickValue;
if(lossPerLot <= 0.0) return 0.0;
double lots = MathFloor((riskMoney / lossPerLot) / step) * step;
// 0.01 minimum lot on a small account means the real risk exceeds
// the requested risk. Warn or skip the trade - never silently oversize.
if(lots < minLot)
{
PrintFormat("Risk %.2f%% with a %.1f stop needs %.4f lots; broker min is %.2f. Trade skipped.",
riskPercent, stopDistance, riskMoney / lossPerLot, minLot);
return 0.0;
}
return lots;
}Two details people get wrong here: SYMBOL_TRADE_TICK_VALUE must be read on your broker's gold symbol (suffixes matter — XAUUSDm, GOLD, XAUUSD.pro all differ), and that lots < minLot branch is not defensive padding. On a $5,000 account running a 1.5-ATR stop near $125 on gold, a 0.5% risk budget demands 0.002 lots. The smallest trade your broker will accept is five times your intended risk. Skipping the trade is the correct behaviour.
2. Gate entries on regime, not on time
Gold trends violently and ranges painfully. An EA with no volatility regime filter fires during both. A gate that only allows entries when volatility is expanding and direction is established is worth more than any indicator tweak:
- Fast/slow ATR ratio ≥ 1.2 — e.g. ATR(14) / ATR(100) on the working timeframe. Lowering this to trade quiet markets is the single most common way a gold EA degrades; sensitivity testing on the M30 breakout model we follow shows the ratio should be raised, never lowered.
- Trend separation filter — require a minimum distance between EMA(50) and EMA(200) in ATR units, not in points. "EMA50 above EMA200" is true in a dead range too and tells you nothing.
- Breakout margin — require the entry level to be exceeded by a fraction of ATR (≈0.6 ATR on a 48-bar channel is a reasonable starting point). This filters out the marginal pokes that fill you into a false break.
3. Exit geometry: the group stop is the strategy
A concrete, publicly documented XAUUSD M30 momentum-breakout design (Aureus Gold, whose spec and setfiles are published on the MQL5 blogs) is instructive because the exits — not the entry — carry the edge:
- No fixed take profit. A trailing exit activates at 3.0 ATR of profit and trails by 0.35 ATR, letting winners run through gold's expansion phases.
- Hard group stop at 1.5 ATR caps the total risk of the entire position group, not each order.
- Recovery adds capped at 3 equal-size orders — no lot doubling. Adverse-move averaging is allowed only inside a bounded, equally-sized basket where the group stop always defines total risk.
That last point is the line between "recovery logic" and a martingale that ends accounts. If your recovery step multiplies volume, you are not trading a strategy, you are holding a leveraged bet that the pullback ends.
4. The setfile is your risk dial, not a performance dial
Same entry signals, three published risk tiers on 22.5 years of XAUUSD M30 real-tick data (2004–2026, $10,000 start, MT5 strategy tester):
- Defensive (risk ×0.5): +370%, profit factor 1.32, max equity drawdown 17.5%
- Standard (3% per group stop): +1,766%, PF 1.41, max equity DD 27.6%
- Aggressive (risk ×2): +5,744%, PF 1.34, max equity DD 40.1% — and a relative equity drawdown around 89%
Read that table properly. Profit factor barely moves across the three tiers (1.32 → 1.41 → 1.34). The multiplier is what changed, and beyond roughly 2x risk the relative drawdown exceeds 90%. That is why the model's own documentation refuses to publish a tier above Aggressive. Setfiles multiply exposure; they do not create edge.
5. Backtest protocol that survives contact with gold
- 100% real ticks, never "1 minute OHLC". Gold's intrabar behaviour on news is exactly where OHLC modelling lies to you.
- Your broker's data and your broker's costs. Spreads, swaps, and symbol specs differ materially. A backtest on someone else's feed is a brochure, not a validation.
- Model spread conservatively — e.g. 0.30 when your live average is 0.18. Costs are the parameter most likely to turn a winner into a loser.
- Split in-sample and out-of-sample, then never touch the OOS data again. Exploration on one window, validation on another, single pass on validation.
- Prefer plateaus to peaks. A parameter set that only works at one exact value is an artifact of the optimizer, not a property of gold. Move each input ±20% and check the equity curve degrades gracefully.
- MFE/MAE analysis on every trade. If your take profit sits inside the noise band and your stop sits outside the typical adverse excursion, your exits are curve-fit.
- Walk-forward. Re-optimize on a rolling window and trade the following out-of-sample period. Optimizer-only results are the least trustworthy number in retail algo trading.
6. Portfolio reality checks people skip
- Small accounts distort risk. Below roughly $25,000, the 0.01 minimum lot on higher timeframes is larger than the requested risk, so returns and drawdowns both overshoot the configured settings. In one published gold portfolio, default settings on $5,000 produced +190% with a 22.4% drawdown versus +80% with 17.1% on $10,000 — the extra return was nothing but accidental leverage.
- Do not port gold presets to other symbols. The same multi-engine gold portfolio lost money on EURUSD, GBPUSD, USDJPY and BTCUSD in testing. Gold's volatility structure and session behaviour are the edge; cross-applying the preset destroys it.
- Funded account rules outrank performance. If your firm caps daily loss at 5% and max drawdown at 10%, configure 3–4% and 6–8% so a bad day does not end the account. Set the limit inside the rule, not on it.
Pre-flight checklist before any .set goes live
- Real-tick backtest on your broker's data, spreads modelled conservatively.
- Out-of-sample period untouched during parameter selection.
- One position / one magic number, max spread filter, and a news-time guard active.
- Verified
CalcLots()behaviour at your account size — confirm no trade silently oversizes. - Group stop and daily loss limit tested against a funded-account rule set.
- Two weeks on demo with the exact setfile, then live with half the intended risk for two more weeks.
Sources
- MQL5 Blogs — Aureus Gold (XAUUSD M30): set files per risk tier, 22.5-year real-tick results
- MQL5 Blogs — Aurum Gold Portfolio user guide, settings and set files
- MetaTrader 5 — Strategy Optimization documentation
- GitHub — xauusd-trend-follow: MQL5 trend-following EA, ATR risk management, FTMO-style circuit breakers
Related reading: the EA board · the position-size calculator · the graded track record.
Pipocket EA analysis is educational. Third-party performance figures are backtests published by their authors, not live results, and past performance does not indicate future results. Always validate on your own broker's data before risking capital.