Summary: Gold Quant Edge EA is an MQL4 expert advisor for XAUUSD that uses multi-layer trend confirmation, structured grid entries in trend direction, and dynamic ATR-based profit targeting. Suitable for M15.




Gold Quant Edge EA is a professional-grade expert advisor developed for XAUUSD (Gold) trading. Unlike traditional grid systems that open bidirectional positions blindly, this EA employs a trend-confirmed grid approach - positions are only built in the direction of the primary trend identified through multi-timeframe EMA alignment and ADX strength filtering. The EA incorporates a dynamic profit target system that scales with market volatility using ATR, ensuring profit taking adapts to current market conditions. A proprietary reversal protection mechanism monitors price structure deviations and closes all positions when the trend structure breaks, preventing catastrophic drawdowns during sudden reversals .

Recommended Timeframe: M15
Trading Logic:
  • Multi-Layer Trend Confirmation: H4 EMA(50) establishes macro trend, H1 EMA(20) confirms intermediate direction, ADX(14) > 25 ensures trend strength.

  • Structured Grid Entry: Initial entry on pullback to fast EMA, subsequent grid levels added at fixed ATR intervals (multiples of 1.5x ATR) ONLY in trend direction.

  • Dynamic Profit Targeting: Take profit calculated as ATR × multiplier (default 3.0). Target auto-adjusts based on volatility regime.

  • Reversal Protection: Independent price structure monitor - if price breaks recent swing low/high by 2x ATR, all positions close immediately.

  • Risk Management: Daily loss limit 5%, max consecutive losses 3, position size cap based on balance percentage.


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

    //--- input parameters with comments
    input double BaseLotSize = 0.01; // Base lot size (0.01 for XAUUSD)
    input int TrendEMAFast = 20; // Fast EMA period for H1 trend
    input int TrendEMASlow = 50; // Slow EMA period for H4 macro trend
    input int ADXPeriod = 14; // ADX period for trend strength
    input int ADXThreshold = 25; // Minimum ADX value (trend required)
    input double GridDistanceATR = 1.5; // Grid distance in ATR multiples
    input double LotMultiplier = 1.5; // Lot multiplier per grid level
    input int MaxGridLevels = 3; // Maximum grid levels per direction
    input double TakeProfitATR = 3.0; // Take profit in ATR multiples
    input double ReversalATR = 2.0; // Reversal detection ATR multiple
    input int ATRPeriod = 14; // ATR period for volatility
    input double DailyLossLimit = 5.0; // Daily loss limit as percentage
    input int MaxConsecutiveLoss = 3; // Max consecutive losses before stop
    input double MaxTotalRiskPercent = 8.0; // Maximum total risk of equity
    input int MagicNumber = 202420; // Unique EA identifier
    input int MaxSpread = 35; // Maximum allowed spread in points
    input bool UseFridayClose = true; // Close trades before Friday 21:00 GMT

    //--- global variables
    double dailyStartBalance = 0;
    datetime lastBarTime = 0;
    bool fridayCloseExecuted = false;
    double avgATR = 0;
    int consecutiveLosses = 0;
    datetime lastTradeTime = 0;
    double currentEquityProtection = 0;

    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    dailyStartBalance = AccountBalance();
    lastBarTime = 0;
    fridayCloseExecuted = false;
    consecutiveLosses = 0;
    avgATR = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
    if(avgATR <= 0) avgATR = 200 Point;
    currentEquityProtection = AccountEquity()
    (1 - DailyLossLimit / 100);
    return(INIT_SUCCEEDED);
    }

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

    //+------------------------------------------------------------------+
    //| Get primary trend direction from H4 |
    //+------------------------------------------------------------------+
    int GetPrimaryTrend()
    {
    double closeH4 = iClose(Symbol(), PERIOD_H4, 1);
    double emaH4 = iMA(Symbol(), PERIOD_H4, TrendEMASlow, 0, MODE_EMA, PRICE_CLOSE, 1);
    if(closeH4 > emaH4) return 1;
    if(closeH4 < emaH4) return -1;
    return 0;
    }

    //+------------------------------------------------------------------+
    //| Get secondary trend confirmation from H1 |
    //+------------------------------------------------------------------+
    int GetSecondaryTrend()
    {
    double closeH1 = iClose(Symbol(), PERIOD_H1, 1);
    double emaH1 = iMA(Symbol(), PERIOD_H1, TrendEMAFast, 0, MODE_EMA, PRICE_CLOSE, 1);
    if(closeH1 > emaH1) return 1;
    if(closeH1 < emaH1) return -1;
    return 0;
    }

    //+------------------------------------------------------------------+
    //| Check ADX trend strength |
    //+------------------------------------------------------------------+
    bool IsTrendStrong()
    {
    double adx = iADX(Symbol(), PERIOD_H1, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
    return (adx >= ADXThreshold);
    }

    //+------------------------------------------------------------------+
    //| Calculate dynamic lot size based on equity and risk |
    //+------------------------------------------------------------------+
    double CalculateLotSize()
    {
    double equity = AccountEquity();
    double riskAmount = equity (DailyLossLimit / 100) / MaxGridLevels;
    double calculatedLot = riskAmount / (avgATR / Point
    0.1);
    calculatedLot = MathMin(calculatedLot, BaseLotSize MathPow(LotMultiplier, MaxGridLevels));
    if(calculatedLot < BaseLotSize) calculatedLot = BaseLotSize;
    if(calculatedLot > 1.0) calculatedLot = 1.0;
    return NormalizeDouble(calculatedLot, 2);
    }

    //+------------------------------------------------------------------+
    //| Check reversal protection condition |
    //+------------------------------------------------------------------+
    bool IsReversalDetected(int direction)
    {
    double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
    if(atr <= 0) atr = avgATR;

    if(direction == 1) // Long position - check for breakdown
    {
    double lowestLow = iLow(Symbol(), PERIOD_H1, iLowest(Symbol(), PERIOD_H1, MODE_LOW, 10, 1));
    double currentBid = Bid;
    if(currentBid < lowestLow - (atr
    ReversalATR))
    return true;
    }
    else if(direction == -1) // Short position - check for breakout
    {
    double highestHigh = iHigh(Symbol(), PERIOD_H1, iHighest(Symbol(), PERIOD_H1, MODE_HIGH, 10, 1));
    double currentAsk = Ask;
    if(currentAsk > highestHigh + (atr ReversalATR))
    return true;
    }
    return false;
    }

    //+------------------------------------------------------------------+
    //| Get current grid level count for a direction |
    //+------------------------------------------------------------------+
    int GetGridLevelCount(int direction)
    {
    int count = 0;
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    if((direction == 1 && OrderType() == OP_BUY) ||
    (direction == -1 && OrderType() == OP_SELL))
    count++;
    }
    }
    }
    return count;
    }

    //+------------------------------------------------------------------+
    //| Calculate average price of positions in a direction |
    //+------------------------------------------------------------------+
    double GetAveragePrice(int direction)
    {
    double totalPrice = 0;
    int count = 0;
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    if((direction == 1 && OrderType() == OP_BUY) ||
    (direction == -1 && OrderType() == OP_SELL))
    {
    totalPrice += OrderOpenPrice();
    count++;
    }
    }
    }
    }
    if(count == 0) return 0;
    return totalPrice / count;
    }

    //+------------------------------------------------------------------+
    //| Check basket take profit |
    //+------------------------------------------------------------------+
    bool CheckBasketTakeProfit(int direction)
    {
    double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
    if(atr <= 0) atr = avgATR;

    double avgPrice = GetAveragePrice(direction);
    double tpDistance = atr
    TakeProfitATR;
    double currentPrice = (direction == 1) ? Bid : Ask;

    if(direction == 1 && currentPrice >= avgPrice + tpDistance)
    return true;
    if(direction == -1 && currentPrice <= avgPrice - tpDistance)
    return true;

    return false;
    }

    //+------------------------------------------------------------------+
    //| Close all positions in a direction |
    //+------------------------------------------------------------------+
    void ClosePositionsInDirection(int direction)
    {
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    if((direction == 1 && OrderType() == OP_BUY) ||
    (direction == -1 && OrderType() == OP_SELL))
    {
    if(OrderType() == OP_BUY)
    OrderClose(OrderTicket(), OrderLots(), Bid, 5, clrNONE);
    else if(OrderType() == OP_SELL)
    OrderClose(OrderTicket(), OrderLots(), Ask, 5, clrNONE);
    }
    }
    }
    }
    }

    //+------------------------------------------------------------------+
    //| 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);
    }
    }
    }
    }

    //+------------------------------------------------------------------+
    //| Calculate total floating PnL |
    //+------------------------------------------------------------------+
    double GetTotalFloatingPnL()
    {
    double total = 0;
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    total += OrderProfit() + OrderSwap() + OrderCommission();
    }
    }
    return total;
    }

    //+------------------------------------------------------------------+
    //| Expert tick function |
    //+------------------------------------------------------------------+
    void OnTick()
    {
    // Daily equity protection
    double currentEquity = AccountEquity();
    if(currentEquity <= currentEquityProtection)
    {
    Comment("Daily loss limit reached. No new trades.");
    if(CountPositions() > 0) CloseAllOrders();
    return;
    }

    // Consecutive loss protection
    if(consecutiveLosses >= MaxConsecutiveLoss)
    {
    Comment("Max consecutive losses reached. Trading paused.");
    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;
    }

    // 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];

    // Update ATR average
    double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
    if(atr > 0) avgATR = (avgATR 0.95) + (atr 0.05);

    // Get trend signals
    int primaryTrend = GetPrimaryTrend();
    int secondaryTrend = GetSecondaryTrend();
    bool trendStrong = IsTrendStrong();

    // Valid trend condition: primary and secondary align, ADX confirms
    bool validTrend = (primaryTrend != 0 && primaryTrend == secondaryTrend && trendStrong);

    if(!validTrend)
    {
    Comment("No valid trend direction. Primary: ", primaryTrend, " Secondary: ", secondaryTrend, " ADX: ", trendStrong);
    return;
    }

    int direction = primaryTrend; // 1 = long, -1 = short
    int currentLevels = GetGridLevelCount(direction);

    // Check for reversal protection on existing positions
    if(currentLevels > 0)
    {
    if(IsReversalDetected(direction))
    {
    ClosePositionsInDirection(direction);
    Print("Reversal protection triggered. All positions closed.");
    return;
    }

    // Check basket take profit
    if(CheckBasketTakeProfit(direction))
    {
    ClosePositionsInDirection(direction);
    Print("Basket take profit achieved. Positions closed.");
    return;
    }

    // Check if we need to add another grid level
    if(currentLevels < MaxGridLevels)
    {
    double avgPrice = GetAveragePrice(direction);
    double currentPrice = (direction == 1) ? Ask : Bid;
    double distanceRequired = avgATR GridDistanceATR currentLevels;

    if(MathAbs(currentPrice - avgPrice) >= distanceRequired)
    {
    double newLot = BaseLotSize MathPow(LotMultiplier, currentLevels);
    newLot = MathMin(newLot, 0.5);

    int cmd = (direction == 1) ? OP_BUY : OP_SELL;
    double entryPrice = (direction == 1) ? Ask : Bid;
    double sl = 0, tp = 0;

    if(direction == 1)
    {
    sl = entryPrice - (avgATR
    1.2);
    tp = entryPrice + (avgATR TakeProfitATR);
    }
    else
    {
    sl = entryPrice + (avgATR
    1.2);
    tp = entryPrice - (avgATR TakeProfitATR);
    }

    int ticket = OrderSend(Symbol(), cmd, newLot, entryPrice, 5, sl, tp, "QuantGrid", MagicNumber, 0, clrNONE);
    if(ticket > 0)
    Print("Grid level ", currentLevels + 1, " opened. Lot: ", newLot);
    }
    }
    }
    else
    {
    // No positions - look for initial entry on pullback
    double emaFast = iMA(Symbol(), PERIOD_H1, TrendEMAFast, 0, MODE_EMA, PRICE_CLOSE, 1);
    double closeH1 = iClose(Symbol(), PERIOD_H1, 1);
    double rsi = iRSI(Symbol(), PERIOD_H1, 14, PRICE_CLOSE, 1);

    bool pullbackCondition = false;

    if(direction == 1) // Long: price pulled back to or below fast EMA
    {
    double low1 = iLow(Symbol(), PERIOD_H1, 1);
    pullbackCondition = (low1 <= emaFast && closeH1 > emaFast && rsi > 45);
    }
    else if(direction == -1) // Short: price pulled back to or above fast EMA
    {
    double high1 = iHigh(Symbol(), PERIOD_H1, 1);
    pullbackCondition = (high1 >= emaFast && closeH1 < emaFast && rsi < 55);
    }

    if(pullbackCondition)
    {
    double lotSize = CalculateLotSize();
    int cmd = (direction == 1) ? OP_BUY : OP_SELL;
    double entryPrice = (direction == 1) ? Ask : Bid;
    double sl = 0, tp = 0;

    if(direction == 1)
    {
    sl = entryPrice - (avgATR
    1.2);
    tp = entryPrice + (avgATR TakeProfitATR);
    }
    else
    {
    sl = entryPrice + (avgATR
    1.2);
    tp = entryPrice - (avgATR * TakeProfitATR);
    }

    int ticket = OrderSend(Symbol(), cmd, lotSize, entryPrice, 5, sl, tp, "QuantInit", MagicNumber, 0, clrNONE);
    if(ticket > 0)
    {
    Print("Initial position opened. Direction: ", direction==1?"BUY":"SELL", " Lot: ", lotSize);
    lastTradeTime = TimeCurrent();
    }
    }
    }
    }

    //+------------------------------------------------------------------+
    //| 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;
    }

    //+------------------------------------------------------------------+
    //| Record trade result (to be called from external after close) |
    //+------------------------------------------------------------------+
    void RecordTradeResult(double profit)
    {
    if(profit <= 0)
    consecutiveLosses++;
    else
    consecutiveLosses = 0;
    }
    //+------------------------------------------------------------------+
    ``
    Reference: Original MQL4 code based on structural analysis of professional Gold trading systems and market validation from March 2026 releases .
    Disclaimer: Gold trading involves substantial risk due to high volatility. This EA is provided as-is without any guarantee of profit. Grid systems can amplify losses in strongly trending markets. Test thoroughly on demo for at least 2 months before live deployment. Past performance does not guarantee future results.