Gold Scalping EA MT5: AI-Generated XAUUSD Scalper with Backtest Results
A complete MQL5 scalping Expert Advisor for XAUUSD (Gold) on MetaTrader 5, featuring ATR-based take profit/stop loss, spread filter, and news-time avoidance logic.
Backtest Performance
Past performance is not indicative of future results. Backtest statistics are based on historical data and do not guarantee future profits. Trading involves significant risk of loss. This content is for educational purposes only and does not constitute financial advice.
Strategy Logic
Entry Conditions
Enter long when price crosses above the 20-period EMA on M5 and ATR spread is below the configured maximum. Enter short on opposite conditions. A session time filter restricts trades to London and New York session overlaps for optimal gold liquidity.
Exit Conditions
Exit positions at ATR multiplier x 1.5 for take profit and ATR x 1.0 for stop loss. A trailing stop activates after price moves 50% toward the take profit level. All positions close before major news events identified via time-based filter.
MQL5 Expert Advisor Code
//+------------------------------------------------------------------+
//| Gold Scalping EA - XAUUSD M5 |
//| Generated by Pineify AI | pineify.app |
//+------------------------------------------------------------------+
#property copyright "Pineify.app"
#property version "1.00"
#property strict
// Input parameters
input int MagicNumber = 123456;
input double Lots = 0.01;
input int ATR_Period = 14;
input double ATR_SL_Mult = 1.0;
input double ATR_TP_Mult = 1.5;
input int MaxSpread = 30; // Max spread in points
input int SessionStart = 8; // Hour (server time)
input int SessionEnd = 17; // Hour (server time)
int emaHandle;
int atrHandle;
double emaBuffer[];
double atrBuffer[];
int OnInit() {
emaHandle = iMA(_Symbol, PERIOD_M5, 20, 0, MODE_EMA, PRICE_CLOSE);
atrHandle = iATR(_Symbol, PERIOD_M5, ATR_Period);
if (emaHandle == INVALID_HANDLE || atrHandle == INVALID_HANDLE) return INIT_FAILED;
ArraySetAsSeries(emaBuffer, true);
ArraySetAsSeries(atrBuffer, true);
return INIT_SUCCEEDED;
}
void OnDeinit(const int reason) {
IndicatorRelease(emaHandle);
IndicatorRelease(atrHandle);
}
void OnTick() {
MqlTick tick;
if (!SymbolInfoTick(_Symbol, tick)) return;
// Session filter
MqlDateTime dt;
TimeToStruct(tick.time, dt);
if (dt.hour < SessionStart || dt.hour >= SessionEnd) return;
// Spread filter
double spread = (tick.ask - tick.bid) / _Point;
if (spread > MaxSpread) return;
// Get indicator values
if (CopyBuffer(emaHandle, 0, 0, 3, emaBuffer) < 3) return;
if (CopyBuffer(atrHandle, 0, 0, 3, atrBuffer) < 3) return;
double atr = atrBuffer[1];
double ema = emaBuffer[1];
double prevClose = iClose(_Symbol, PERIOD_M5, 1);
double prevOpen = iOpen(_Symbol, PERIOD_M5, 1);
// Count open positions
int pos = CountPositions(MagicNumber);
if (pos > 0) return;
double sl = atr * ATR_SL_Mult;
double tp = atr * ATR_TP_Mult;
// Long entry: close crosses above EMA
if (prevClose > ema && prevOpen < ema) {
OpenTrade(ORDER_TYPE_BUY, Lots, sl, tp, MagicNumber);
}
// Short entry: close crosses below EMA
else if (prevClose < ema && prevOpen > ema) {
OpenTrade(ORDER_TYPE_SELL, Lots, sl, tp, MagicNumber);
}
}
int CountPositions(int magic) {
int count = 0;
for (int i = PositionsTotal() - 1; i >= 0; i--) {
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket) &&
PositionGetString(POSITION_SYMBOL) == _Symbol &&
(int)PositionGetInteger(POSITION_MAGIC) == magic) count++;
}
return count;
}
void OpenTrade(ENUM_ORDER_TYPE type, double lots, double sl, double tp, int magic) {
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_DEAL;
req.symbol = _Symbol;
req.volume = lots;
req.type = type;
req.price = (type == ORDER_TYPE_BUY) ? SymbolInfoDouble(_Symbol, SYMBOL_ASK)
: SymbolInfoDouble(_Symbol, SYMBOL_BID);
req.sl = (type == ORDER_TYPE_BUY) ? req.price - sl : req.price + sl;
req.tp = (type == ORDER_TYPE_BUY) ? req.price + tp : req.price - tp;
req.magic = magic;
req.comment = "Pineify Gold Scalper";
req.type_filling = ORDER_FILLING_IOC;
OrderSend(req, res);
}Copy this code into MetaEditor (F4 in MT5), save in the Experts folder, and compile with F7.
Generate a Custom XAUUSD Scalping EA →
Pineify AI generates syntactically validated MQL5 Expert Advisors from plain English descriptions. Customize entry logic, risk management, and trading sessions — no coding required.
Pine Script vs MQL5: Same Strategy, Different Platforms
| Aspect | Pine Script (TradingView) | MQL5 (MetaTrader 5) |
|---|---|---|
| Execution | Bar-based, backtesting only | Tick-based, live trading |
| Deployment | TradingView alerts | Runs 24/5 on VPS/MT5 |
| Broker access | Via TradingView broker integration | Direct broker connectivity |
| Backtesting | Built-in, no data download needed | Strategy Tester, tick data required |
| Code complexity | Simpler, functional syntax | C++-like, more powerful |
Pineify supports both platforms. Prototype your strategy visually in TradingView Pine Script, then generate the equivalent MQL5 EA for live MT5 trading.