Summary: Gold Horizon EA is an MQL4 expert advisor for XAUUSD that uses multi-timeframe EMA trend confirmation, ATR-based dynamic stops, and daily loss limits. Suitable for H1 timeframe stable operation.




Gold Horizon EA is designed specifically for gold (XAUUSD) to achieve stable, low-risk operation by focusing on trend alignment and volatility awareness. Unlike aggressive grid or martingale systems, this EA prioritizes signal quality over trade frequency. It uses multi-timeframe trend confirmation (H1 and H4 EMAs) to ensure alignment with the broader market direction. An ATR-based volatility filter prevents entries during excessive market noise, while a dynamic ATR stop loss adapts to current gold volatility. The EA includes daily equity protection, spread control, and Friday close mechanisms to safeguard against weekend gaps.

Recommended Timeframe: H1
Trading Logic:
  • Multi-Timeframe Trend: H4 EMA(50) determines the primary trend direction. H1 must trade in the same direction.

  • Momentum Filter: RSI(14) must confirm momentum (>50 for long, <50 for short).

  • Entry Trigger: Price retraces to H1 EMA(20) and closes beyond it in the trend direction.

  • Volatility Guard: Current ATR(14) must not exceed 1.8x the 20-period average ATR.

  • Risk Management: Dynamic stop loss at 1.5x ATR, take profit at 3x ATR. Trailing stop activates after 1x ATR profit.


  • ``mql4
    //+------------------------------------------------------------------+
    //| GoldHorizonEA.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 XAUUSD)
    input int H1TrendMAPeriod = 50; // H1 EMA period for trend filter
    input int H4TrendMAPeriod = 50; // H4 EMA period for primary trend
    input int EntryMAPeriod = 20; // EMA period for entry pullback
    input int RSIPeriod = 14; // RSI period for momentum filter
    input int ATRPeriod = 14; // ATR period for volatility management
    input double ATRMaxMultiplier = 1.8; // Max ATR multiplier (volatility guard)
    input double ATRStopMultiplier = 1.5; // Stop loss as multiple of ATR
    input double ATRTakeMultiplier = 3.0; // Take profit as multiple of ATR
    input double TrailingStartATR = 1.0; // Trailing activates at profit (x ATR)
    input double TrailingStepATR = 0.5; // Trailing step (x ATR)
    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
    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;

    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    dailyStartBalance = AccountBalance();
    lastBarTime = 0;
    fridayCloseExecuted = false;
    avgATR = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
    if(avgATR <= 0) avgATR = 250 Point;
    return(INIT_SUCCEEDED);
    }

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

    //+------------------------------------------------------------------+
    //| Get higher timeframe trend direction (H4) |
    //+------------------------------------------------------------------+
    int GetHTFTrend()
    {
    double closeH4 = iClose(Symbol(), PERIOD_H4, 1);
    double emaH4 = iMA(Symbol(), PERIOD_H4, H4TrendMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    if(closeH4 > emaH4) return 1; // Bullish
    if(closeH4 < emaH4) return -1; // Bearish
    return 0;
    }

    //+------------------------------------------------------------------+
    //| Check pullback entry condition |
    //+------------------------------------------------------------------+
    bool CheckPullbackEntry(int direction, double &entryPrice, double &sl, double &tp, double atr)
    {
    double emaFast = iMA(Symbol(), PERIOD_H1, EntryMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double open1 = iOpen(Symbol(), PERIOD_H1, 1);

    if(direction == 1) // Long: price pulled back to EMA20, then closed above
    {
    double low1 = iLow(Symbol(), PERIOD_H1, 1);
    bool pulledToEMA = (low1 <= emaFast && close1 > emaFast);
    bool bullishClose = (close1 > open1);

    if(pulledToEMA && bullishClose)
    {
    entryPrice = Ask;
    sl = entryPrice - (atr
    ATRStopMultiplier);
    tp = entryPrice + (atr ATRTakeMultiplier);
    return true;
    }
    }
    else if(direction == -1) // Short: price pulled back to EMA20, then closed below
    {
    double high1 = iHigh(Symbol(), PERIOD_H1, 1);
    bool pulledToEMA = (high1 >= emaFast && close1 < emaFast);
    bool bearishClose = (close1 < open1);

    if(pulledToEMA && bearishClose)
    {
    entryPrice = Bid;
    sl = entryPrice + (atr
    ATRStopMultiplier);
    tp = entryPrice - (atr ATRTakeMultiplier);
    return true;
    }
    }
    return false;
    }

    //+------------------------------------------------------------------+
    //| Manage trailing stop for open position |
    //+------------------------------------------------------------------+
    void ManageTrailingStop(double atr)
    {
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    double activate = atr
    TrailingStartATR;
    double step = atr TrailingStepATR;
    double newSL = 0;

    if(OrderType() == OP_BUY)
    {
    double profit = Bid - OrderOpenPrice();
    if(profit >= activate)
    {
    newSL = Bid - step;
    if(newSL > OrderStopLoss())
    OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
    }
    }
    else if(OrderType() == OP_SELL)
    {
    double profit = OrderOpenPrice() - Ask;
    if(profit >= activate)
    {
    newSL = Ask + step;
    if(newSL < OrderStopLoss() || OrderStopLoss() == 0)
    OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
    }
    }
    break;
    }
    }
    }
    }

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

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

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

    // Manage existing position
    int posCount = CountPositions();
    if(posCount > 0)
    {
    double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
    if(atr > 0) ManageTrailingStop(atr);
    return;
    }

    // Get indicators
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double emaH1 = iMA(Symbol(), PERIOD_H1, H1TrendMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);
    double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);

    // Update average ATR for volatility filter
    if(atr > 0) avgATR = (avgATR 0.95) + (atr 0.05);

    // Volatility guard
    if(atr > avgATR * ATRMaxMultiplier && avgATR > 0)
    {
    Comment("Volatility too high. ATR: ", atr);
    return;
    }

    // Get H4 trend direction
    int h4Trend = GetHTFTrend();
    if(h4Trend == 0)
    {
    Comment("No clear H4 trend direction");
    return;
    }

    // H1 trend alignment check
    bool h1Bullish = (close1 > emaH1);
    bool h1Bearish = (close1 < emaH1);

    int allowedDirection = 0;
    if(h4Trend == 1 && h1Bullish) allowedDirection = 1;
    else if(h4Trend == -1 && h1Bearish) allowedDirection = -1;

    if(allowedDirection == 0)
    {
    Comment("H1 and H4 trends not aligned");
    return;
    }

    // RSI momentum filter
    if(allowedDirection == 1 && rsi < 50)
    {
    Comment("RSI below 50, no bullish momentum");
    return;
    }
    if(allowedDirection == -1 && rsi > 50)
    {
    Comment("RSI above 50, no bearish momentum");
    return;
    }

    // Check pullback entry
    double entryPrice = 0, sl = 0, tp = 0;
    if(CheckPullbackEntry(allowedDirection, entryPrice, sl, tp, atr))
    {
    int cmd = (allowedDirection == 1) ? OP_BUY : OP_SELL;
    int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Gold Horizon", MagicNumber, 0, clrNONE);
    if(ticket < 0)
    Print("OrderSend failed: ", GetLastError());
    else
    Print("Order opened successfully. Direction: ", cmd==OP_BUY?"BUY":"SELL");
    }
    }

    //+------------------------------------------------------------------+
    //| 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: Original MQL4 code inspired by structural analysis from Gold Horizon EA concept.
    Disclaimer: Gold trading involves significant risk due to high volatility. This EA is provided as-is without any guarantee of profit. Test thoroughly on a demo account before live deployment. Past performance does not guarantee future results.