Gold Trend Following EA MT5: MQL5 XAUUSD Trend Strategy & Code
This page presents a complete MQL5 Expert Advisor for trading XAUUSD (Gold) using a dual moving average trend-following strategy on MetaTrader 5. It includes full source code, entry and exit logic explanations, backtested performance stats from 2020–2025, and answers to the most common questions traders have about automating gold trend strategies.
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
The EA enters a long position when the fast EMA (20-period) crosses above the slow EMA (50-period) on the H1 chart, confirming an uptrend in XAUUSD. A short position is opened when the fast EMA crosses below the slow EMA, signaling a downtrend. An ATR-based filter ensures trades are only taken when volatility exceeds a minimum threshold, avoiding choppy low-momentum market conditions.
Exit Conditions
Each trade uses a fixed ATR multiplier to set the initial stop-loss (1.5x ATR) and take-profit (2.5x ATR), giving the strategy a favorable risk-reward ratio. A trailing stop activated at 1x ATR profit locks in gains as the trend extends, allowing the EA to ride sustained gold moves without giving back excessive open profit. Positions are also closed if a counter-signal EMA crossover occurs before the take-profit is reached.
MQL5 Expert Advisor Code
//+------------------------------------------------------------------+
//| GoldTrendFollowingEA.mq5 |
//| XAUUSD Dual EMA Trend Following Expert Advisor |
//| For MetaTrader 5 — MQL5 5.x compatible |
//| DISCLAIMER: For educational purposes only. Past performance |
//| does not guarantee future results. Trade at your own risk. |
//+------------------------------------------------------------------+
#property copyright "Pineify Example"
#property version "1.00"
#property strict
//--- Input parameters
input int FastEMA_Period = 20; // Fast EMA period
input int SlowEMA_Period = 50; // Slow EMA period
input int ATR_Period = 14; // ATR period
input double ATR_SL_Mult = 1.5; // Stop-loss ATR multiplier
input double ATR_TP_Mult = 2.5; // Take-profit ATR multiplier
input double ATR_Trail_Mult = 1.0; // Trailing stop ATR multiplier
input double ATR_Min_Filter = 0.5; // Min ATR in USD to enter trade
input double LotSize = 0.10; // Fixed lot size
input ulong MagicNumber = 20240001; // EA magic number
//--- Indicator handles
int handleFastEMA;
int handleSlowEMA;
int handleATR;
//--- Buffers
double fastEMABuf[];
double slowEMABuf[];
double atrBuf[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create indicator handles
handleFastEMA = iMA(_Symbol, PERIOD_H1, FastEMA_Period, 0, MODE_EMA, PRICE_CLOSE);
handleSlowEMA = iMA(_Symbol, PERIOD_H1, SlowEMA_Period, 0, MODE_EMA, PRICE_CLOSE);
handleATR = iATR(_Symbol, PERIOD_H1, ATR_Period);
if(handleFastEMA == INVALID_HANDLE || handleSlowEMA == INVALID_HANDLE || handleATR == INVALID_HANDLE)
{
Print("ERROR: Failed to create indicator handles.");
return INIT_FAILED;
}
// Set buffer as series (newest index = 0)
ArraySetAsSeries(fastEMABuf, true);
ArraySetAsSeries(slowEMABuf, true);
ArraySetAsSeries(atrBuf, true);
Print("GoldTrendFollowingEA initialized on ", _Symbol);
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(handleFastEMA);
IndicatorRelease(handleSlowEMA);
IndicatorRelease(handleATR);
Print("GoldTrendFollowingEA deinitialized. Reason: ", reason);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Only act on a new H1 bar
static datetime lastBarTime = 0;
datetime currentBarTime = iTime(_Symbol, PERIOD_H1, 0);
if(currentBarTime == lastBarTime)
return;
lastBarTime = currentBarTime;
// Copy indicator data (3 values: index 0 = current, 1 = prev)
if(CopyBuffer(handleFastEMA, 0, 0, 3, fastEMABuf) < 3) return;
if(CopyBuffer(handleSlowEMA, 0, 0, 3, slowEMABuf) < 3) return;
if(CopyBuffer(handleATR, 0, 0, 2, atrBuf) < 2) return;
double fastNow = fastEMABuf[1]; // Previous closed bar
double fastPrev = fastEMABuf[2];
double slowNow = slowEMABuf[1];
double slowPrev = slowEMABuf[2];
double atr = atrBuf[1];
// ATR volatility filter
if(atr < ATR_Min_Filter)
return;
bool bullCross = (fastPrev <= slowPrev) && (fastNow > slowNow);
bool bearCross = (fastPrev >= slowPrev) && (fastNow < slowNow);
int openLongs = CountPositions(POSITION_TYPE_BUY);
int openShorts = CountPositions(POSITION_TYPE_SELL);
// Close counter-direction positions on crossover
if(bullCross && openShorts > 0) CloseAllPositions(POSITION_TYPE_SELL);
if(bearCross && openLongs > 0) CloseAllPositions(POSITION_TYPE_BUY);
// Open new positions on crossover
if(bullCross && openLongs == 0)
OpenTrade(ORDER_TYPE_BUY, atr);
if(bearCross && openShorts == 0)
OpenTrade(ORDER_TYPE_SELL, atr);
// Manage trailing stops for open positions
ManageTrailingStop(atr);
}
//+------------------------------------------------------------------+
//| Open a market order |
//+------------------------------------------------------------------+
void OpenTrade(ENUM_ORDER_TYPE orderType, double atr)
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
double price, sl, tp;
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
if(orderType == ORDER_TYPE_BUY)
{
price = ask;
sl = NormalizeDouble(price - atr * ATR_SL_Mult, _Digits);
tp = NormalizeDouble(price + atr * ATR_TP_Mult, _Digits);
}
else
{
price = bid;
sl = NormalizeDouble(price + atr * ATR_SL_Mult, _Digits);
tp = NormalizeDouble(price - atr * ATR_TP_Mult, _Digits);
}
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = LotSize;
request.type = orderType;
request.price = price;
request.sl = sl;
request.tp = tp;
request.deviation = 10;
request.magic = MagicNumber;
request.comment = "GoldTrendEA";
request.type_filling = ORDER_FILLING_IOC;
if(!OrderSend(request, result))
Print("OrderSend failed: ", result.retcode, " ", result.comment);
else
Print("Order opened: ", EnumToString(orderType), " price=", price, " sl=", sl, " tp=", tp);
}
//+------------------------------------------------------------------+
//| Count open positions by type |
//+------------------------------------------------------------------+
int CountPositions(ENUM_POSITION_TYPE posType)
{
int count = 0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0) continue;
if(PositionGetString(POSITION_SYMBOL) == _Symbol &&
PositionGetInteger(POSITION_MAGIC) == (long)MagicNumber &&
PositionGetInteger(POSITION_TYPE) == posType)
count++;
}
return count;
}
//+------------------------------------------------------------------+
//| Close all positions of a given type |
//+------------------------------------------------------------------+
void CloseAllPositions(ENUM_POSITION_TYPE posType)
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0) continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
if(PositionGetInteger(POSITION_MAGIC) != (long)MagicNumber) continue;
if(PositionGetInteger(POSITION_TYPE) != posType) continue;
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_DEAL;
req.symbol = _Symbol;
req.position = ticket;
req.volume = PositionGetDouble(POSITION_VOLUME);
req.type = (posType == POSITION_TYPE_BUY) ? ORDER_TYPE_SELL : ORDER_TYPE_BUY;
req.price = (posType == POSITION_TYPE_BUY)
? SymbolInfoDouble(_Symbol, SYMBOL_BID)
: SymbolInfoDouble(_Symbol, SYMBOL_ASK);
req.deviation = 10;
req.magic = MagicNumber;
req.type_filling = ORDER_FILLING_IOC;
if(!OrderSend(req, res))
Print("Close failed ticket=", ticket, " retcode=", res.retcode);
}
}
//+------------------------------------------------------------------+
//| Manage ATR-based trailing stop |
//+------------------------------------------------------------------+
void ManageTrailingStop(double atr)
{
double trailDist = atr * ATR_Trail_Mult;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
ulong ticket = PositionGetTicket(i);
if(ticket == 0) continue;
if(PositionGetString(POSITION_SYMBOL) != _Symbol) continue;
if(PositionGetInteger(POSITION_MAGIC) != (long)MagicNumber) continue;
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double currentSL = PositionGetDouble(POSITION_SL);
double currentTP = PositionGetDouble(POSITION_TP);
ENUM_POSITION_TYPE pType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
double newSL = currentSL;
if(pType == POSITION_TYPE_BUY)
{
double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double trail = NormalizeDouble(bid - trailDist, _Digits);
// Only trail if in profit by at least 1x ATR
if(bid >= openPrice + trailDist && trail > currentSL)
newSL = trail;
}
else if(pType == POSITION_TYPE_SELL)
{
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double trail = NormalizeDouble(ask + trailDist, _Digits);
if(ask <= openPrice - trailDist && (trail < currentSL || currentSL == 0))
newSL = trail;
}
if(newSL != currentSL)
{
MqlTradeRequest req = {};
MqlTradeResult res = {};
req.action = TRADE_ACTION_SLTP;
req.symbol = _Symbol;
req.position = ticket;
req.sl = newSL;
req.tp = currentTP;
OrderSend(req, res);
}
}
}
//+------------------------------------------------------------------+Copy this code into MetaEditor (F4 in MT5), save in the Experts folder, and compile with F7.
Generate a Custom XAUUSD Trend-following 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.