Summary: Gold Momentum Guard EA is an MQL4 expert advisor for XAUUSD that uses momentum strength detection, RSI trend alignment, and ATR-based risk management. Designed for stable operation on M15.




Gold Momentum Guard EA is built specifically for gold (XAUUSD) with stability as the core priority. Based on research of institutional-grade gold EAs, the strategy focuses on high-probability entry filtering rather than aggressive grid or martingale systems . The EA combines momentum detection during high-liquidity sessions (London/New York overlap) with RSI trend alignment and ATR volatility confirmation .

Recommended Timeframe: M15
Trading Logic:
  • Session Filter: Only trades during London (08:00-16:00 GMT) and New York (13:00-21:00 GMT) sessions.

  • Momentum Detection: Compares current bar's momentum strength (body/wick ratio) against recent average.

  • Trend Alignment: RSI(14) above 50 for long entries, below 50 for short entries.

  • Volatility Guard: ATR(20) must be within normal range (0.8x to 1.5x of 50-period average ATR).

  • Risk Management: Fixed SL (250 points) and TP (450 points), max 1 trade at a time, daily loss limit 5%.


  • ``mql4
    //+------------------------------------------------------------------+
    //| GoldMomentumGuardEA.mq4 |
    //+------------------------------------------------------------------+
    #property copyright ""
    #property link ""
    #property version "1.00"
    #property strict

    //--- input parameters with comments
    input double LotSize = 0.01; // Fixed lot size (0.01 for gold)
    input int StopLossPoints = 250; // Stop loss in points (250 = 25$ for 0.01 lot)
    input int TakeProfitPoints = 450; // Take profit in points (risk-reward ~1:1.8)
    input int RSIPeriod = 14; // RSI period for trend alignment
    input int ATRPeriod = 20; // ATR period for volatility detection
    input double MinATRMultiplier = 0.8; // Minimum ATR multiplier (skip if too low volatility)
    input double MaxATRMultiplier = 1.5; // Maximum ATR multiplier (skip if too high volatility)
    input int MomentumLookback = 10; // Lookback bars for momentum baseline
    input double MomentumMinRatio = 1.5; // Minimum momentum ratio (body/wick) to trigger entry
    input int MagicNumber = 202419; // Unique EA identifier
    input int MaxSpread = 35; // Maximum allowed spread in points
    input double DailyLossLimit = 5.0; // Daily loss limit as percentage of balance
    input bool UseSessionFilter = true; // Filter non-London/NY trading hours
    input bool UseFridayClose = true; // Close trades before Friday 21:00 GMT

    //--- global variables
    double dailyStartBalance = 0;
    datetime lastBarTime = 0;
    bool fridayCloseExecuted = false;
    double avgATR50 = 0;
    double momentumBaseline = 0;

    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    dailyStartBalance = AccountBalance();
    lastBarTime = 0;
    fridayCloseExecuted = false;

    // Initialize ATR baseline
    avgATR50 = iATR(Symbol(), PERIOD_M15, 50, 1);
    if(avgATR50 <= 0) avgATR50 = 200 Point;

    // Initialize momentum baseline
    double totalMomentum = 0;
    for(int i = 1; i <= MomentumLookback; i++)
    totalMomentum += GetMomentumRatio(i);
    momentumBaseline = totalMomentum / MomentumLookback;
    if(momentumBaseline <= 0) momentumBaseline = 1.0;

    return(INIT_SUCCEEDED);
    }

    //+------------------------------------------------------------------+
    //| Expert deinitialization function |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
    {
    Comment("");
    }

    //+------------------------------------------------------------------+
    //| Calculate momentum ratio (body / max wick) for a candle |
    //+------------------------------------------------------------------+
    double GetMomentumRatio(int shift)
    {
    double open = iOpen(Symbol(), PERIOD_M15, shift);
    double close = iClose(Symbol(), PERIOD_M15, shift);
    double high = iHigh(Symbol(), PERIOD_M15, shift);
    double low = iLow(Symbol(), PERIOD_M15, shift);

    double body = MathAbs(close - open);
    double upperWick = high - MathMax(open, close);
    double lowerWick = MathMin(open, close) - low;
    double maxWick = MathMax(upperWick, lowerWick);

    if(maxWick <= 0) return 1.0;
    return body / maxWick;
    }

    //+------------------------------------------------------------------+
    //| Check if current time is within allowed trading sessions |
    //+------------------------------------------------------------------+
    bool IsValidTradingSession()
    {
    datetime currentTime = TimeCurrent();
    int hour = TimeHour(currentTime);
    int minute = TimeMinute(currentTime);
    int currentTimeMinutes = hour
    60 + minute;

    // London session: 08:00 - 16:00 GMT (480 - 960 minutes)
    // New York session: 13:00 - 21:00 GMT (780 - 1260 minutes)
    bool isLondon = (currentTimeMinutes >= 480 && currentTimeMinutes < 960);
    bool isNewYork = (currentTimeMinutes >= 780 && currentTimeMinutes < 1260);

    return (isLondon || isNewYork);
    }

    //+------------------------------------------------------------------+
    //| Check if current volatility is within acceptable range |
    //+------------------------------------------------------------------+
    bool IsValidVolatility()
    {
    double currentATR = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
    if(currentATR <= 0) return false;

    // Update rolling average
    avgATR50 = (avgATR50 0.98) + (currentATR 0.02);

    double minATR = avgATR50 MinATRMultiplier;
    double maxATR = avgATR50
    MaxATRMultiplier;

    return (currentATR >= minATR && currentATR <= maxATR);
    }

    //+------------------------------------------------------------------+
    //| Check momentum surge condition |
    //+------------------------------------------------------------------+
    bool IsMomentumSurge(int direction)
    {
    double currentMomentum = GetMomentumRatio(1);
    if(currentMomentum <= 0) return false;

    // Update baseline
    momentumBaseline = (momentumBaseline 0.9) + (currentMomentum 0.1);

    bool isStrongMomentum = (currentMomentum >= momentumBaseline MomentumMinRatio);
    if(!isStrongMomentum) return false;

    double close1 = iClose(Symbol(), PERIOD_M15, 1);
    double open1 = iOpen(Symbol(), PERIOD_M15, 1);
    double high1 = iHigh(Symbol(), PERIOD_M15, 1);
    double low1 = iLow(Symbol(), PERIOD_M15, 1);

    if(direction == 1) // Bullish momentum
    return (close1 > open1 && (high1 - close1) < (close1 - open1)
    0.5);
    else if(direction == -1) // Bearish momentum
    return (close1 < open1 && (low1 - close1) > (close1 - open1) 0.5);

    return false;
    }

    //+------------------------------------------------------------------+
    //| Expert tick function |
    //+------------------------------------------------------------------+
    void OnTick()
    {
    // Daily equity protection
    double currentEquity = AccountEquity();
    double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance
    100;
    if(lossPercent >= DailyLossLimit)
    {
    Comment("Daily loss limit reached. No new trades.");
    return;
    }

    // Friday close before weekend
    if(UseFridayClose && !fridayCloseExecuted)
    {
    datetime currentTime = TimeCurrent();
    if(TimeDayOfWeek(currentTime) == 5 && TimeHour(currentTime) >= 21)
    {
    CloseAllOrders();
    fridayCloseExecuted = true;
    return;
    }
    if(TimeDayOfWeek(currentTime) != 5)
    fridayCloseExecuted = false;
    }

    // Session filter
    if(UseSessionFilter && !IsValidTradingSession())
    {
    Comment("Outside trading session");
    return;
    }

    // Spread filter
    if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
    {
    Comment("Spread too high: ", MarketInfo(Symbol(), MODE_SPREAD));
    return;
    }

    // New bar logic (M15)
    if(Time[0] == lastBarTime)
    return;
    lastBarTime = Time[0];

    // Check existing position
    if(CountPositions() > 0)
    return;

    // Volatility filter
    if(!IsValidVolatility())
    {
    Comment("Volatility out of range");
    return;
    }

    // RSI for trend alignment
    double rsi = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, 1);
    double close1 = iClose(Symbol(), PERIOD_M15, 1);
    double ema50 = iMA(Symbol(), PERIOD_M15, 50, 0, MODE_EMA, PRICE_CLOSE, 1);

    int cmd = -1;
    double sl = 0, tp = 0;
    double ask = Ask;
    double bid = Bid;

    // Long condition: RSI > 50 (bullish trend) and bullish momentum surge
    if(rsi > 50 && close1 > ema50 && IsMomentumSurge(1))
    {
    cmd = OP_BUY;
    sl = bid - StopLossPoints Point;
    tp = bid + TakeProfitPoints
    Point;
    }
    // Short condition: RSI < 50 (bearish trend) and bearish momentum surge
    else if(rsi < 50 && close1 < ema50 && IsMomentumSurge(-1))
    {
    cmd = OP_SELL;
    sl = ask + StopLossPoints Point;
    tp = ask - TakeProfitPoints
    Point;
    }

    if(cmd != -1)
    {
    int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?ask:bid), 5, sl, tp, "Gold Momentum Guard", MagicNumber, 0, clrNONE);
    if(ticket < 0)
    Print("OrderSend failed: ", GetLastError());
    }
    }

    //+------------------------------------------------------------------+
    //| Count open positions with this MagicNumber |
    //+------------------------------------------------------------------+
    int CountPositions()
    {
    int count = 0;
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    count++;
    }
    }
    return count;
    }

    //+------------------------------------------------------------------+
    //| Close all orders for this symbol and magic |
    //+------------------------------------------------------------------+
    void CloseAllOrders()
    {
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    if(OrderType() == OP_BUY)
    OrderClose(OrderTicket(), OrderLots(), Bid, 5, clrNONE);
    else if(OrderType() == OP_SELL)
    OrderClose(OrderTicket(), OrderLots(), Ask, 5, clrNONE);
    }
    }
    }
    }
    //+------------------------------------------------------------------+
    ``
    Reference: Inspired by institutional-grade EA designs including momentum filtering, session-based trading, and volatility-aware risk management .
    Disclaimer: Gold trading involves significant risk. This EA is provided as-is for educational purposes. Always test on a demo account before live trading. Past performance does not guarantee future results.