XAUUSD Scalping EA for MT5: How to Build a Gold Scalping Bot
Gold (XAUUSD) is one of the most popular markets for scalping bots on MetaTrader 5. It moves a lot in short periods, trades almost around the clock, and most brokers offer it with tight spreads during the London and New York sessions. It is also one of the easiest markets to lose money on with a careless EA. This guide explains how a gold scalping EA works, the rules it needs, a simple MQL5 structure, and what to check before running one live.
What makes gold different
- Price and digits. XAUUSD is quoted around thousands of dollars with 2 or 3 decimals depending on the broker. A "pip" means different things at different brokers, so the EA should work in price or points, never fixed pip values.
- Contract size. At many brokers 1 lot is 100 ounces, so a 1 dollar move is 100 dollars per lot. Some brokers use other sizes. Always read
SYMBOL_TRADE_CONTRACT_SIZEinstead of assuming. - Spread changes. Spreads widen around the daily rollover, at session opens and during news. A scalper that ignores spread will enter at the worst moments.
- News spikes. US data such as NFP, CPI and FOMC decisions can move gold several dollars in seconds, with slippage on stops.
- Symbol names. The same market can be called XAUUSD, XAUUSDm, GOLD or XAUUSD.r depending on the broker.
Rules a gold scalping EA needs
- A clear entry signal. For example a fast EMA crossing a slow EMA on M1 or M5, in the direction of a higher timeframe trend.
- Maximum spread filter. Skip the trade if the current spread is above a limit you set.
- Session filter. Trade only the hours you choose, often London and New York, and stay out around rollover.
- News filter. Pause before and after high-impact events.
- Risk-based lot size. Size each trade from the stop distance and a fixed percentage of the balance.
- Hard limits. Maximum trades per day, maximum daily loss, and one position at a time unless the strategy says otherwise.
A simple MQL5 structure
This is a skeleton, not a finished strategy. It shows the checks every gold scalper should run before sending an order:
#include <Trade/Trade.mqh>
CTrade trade;
input double RiskPercent = 0.5;
input int MaxSpreadPts = 300;
input int StopPts = 1500;
input int TakePts = 2000;
input int StartHour = 8, EndHour = 20;
input long Magic = 2611;
int fastH, slowH;
int OnInit() {
fastH = iMA(_Symbol, PERIOD_M5, 9, 0, MODE_EMA, PRICE_CLOSE);
slowH = iMA(_Symbol, PERIOD_M5, 21, 0, MODE_EMA, PRICE_CLOSE);
trade.SetExpertMagicNumber(Magic);
return (fastH == INVALID_HANDLE || slowH == INVALID_HANDLE) ? INIT_FAILED : INIT_SUCCEEDED;
}
double LotsForRisk(int stopPts) {
double tickVal = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);
double riskMoney = AccountInfoDouble(ACCOUNT_BALANCE) * RiskPercent / 100.0;
double lossPerLot = stopPts * _Point / tickSize * tickVal;
double step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double lots = MathFloor(riskMoney / lossPerLot / step) * step;
return MathMax(SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN),
MathMin(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX)));
}
void OnTick() {
static datetime lastBar = 0;
datetime bar = iTime(_Symbol, PERIOD_M5, 0);
if (bar == lastBar) return; // act once per new bar
lastBar = bar;
MqlDateTime t; TimeCurrent(t);
if (t.hour < StartHour || t.hour >= EndHour) return;
if (SymbolInfoInteger(_Symbol, SYMBOL_SPREAD) > MaxSpreadPts) return;
if (PositionSelect(_Symbol)) return; // one position at a time
double f[], s[];
if (CopyBuffer(fastH, 0, 1, 2, f) < 2 || CopyBuffer(slowH, 0, 1, 2, s) < 2) return;
ArraySetAsSeries(f, true); ArraySetAsSeries(s, true);
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double lots = LotsForRisk(StopPts);
if (f[1] <= s[1] && f[0] > s[0])
trade.Buy(lots, _Symbol, ask, ask - StopPts * _Point, ask + TakePts * _Point);
else if (f[1] >= s[1] && f[0] < s[0])
trade.Sell(lots, _Symbol, bid, bid + StopPts * _Point, bid - TakePts * _Point);
}
Stops and spread are in points, so check the values against your broker's digits for XAUUSD. A real EA also adds a daily loss limit, a news filter, checks the result of every order, and respects the broker's minimum stop distance.
Backtesting a gold scalper honestly
- Use Every tick based on real ticks in the MT5 Strategy Tester. Scalping results on generated ticks are often far too good.
- Test with your broker's real spread and commission, not a fixed low spread.
- Test several years, including high-volatility periods, not just the last few months.
- Forward test on a demo account for a few weeks. Live slippage on gold is the most common reason a good backtest fails.
A word on free gold EAs and GitHub code
There are many free XAUUSD scalping bots on GitHub and forums. They can be useful for learning, but check them carefully before letting them near real money. Watch for martingale or grid logic that adds to losing trades, no stop loss, results shown only on generated ticks, and fixed pip values that break on a different broker. If an EA claims very high returns with almost no drawdown, the backtest is almost always wrong.
Frequently asked questions
Which timeframe is best for scalping gold?
Most gold scalpers trade M1 or M5 entries with a filter from M15 or H1. The right choice depends on the strategy and your broker's costs.
Can I use a gold scalping EA on a prop firm account?
Yes, if it follows the firm's rules. Many prop firms ban certain styles such as tick scalping or news trading, and all of them enforce daily loss and drawdown limits, so the EA must stop before a rule is broken.
Will the same EA work with every broker?
Only if it reads symbol settings such as digits, contract size and stop level from the broker instead of hard-coding them, and handles symbol name suffixes.
Is gold scalping profitable?
It can be for some strategies, but costs are high relative to small targets. No EA can guarantee profit. Test it properly and start small.
Want a gold EA built properly?
We build custom MT5 Expert Advisors for XAUUSD with spread, session and news filters, risk-based lot sizing and prop firm limits, with full .mq5 source code. See MT5 Expert Advisor development. Related: MT5 EA for prop firms and build your first MQL5 EA.