Summary: Gold Macro Align EA is an MQL4 expert advisor for XAUUSD that aligns trends across H1/H4 timeframes, uses ATR-based stop loss and trailing, and includes equity protection. Suitable for H1 timeframe stable operation.




Gold Macro Align EA is engineered specifically for XAUUSD to achieve stable, long-term performance by focusing on macro trend alignment. Unlike aggressive systems, this EA prioritizes high-quality setups by requiring trend confirmation across multiple timeframes (H1 and H4 EMAs). It incorporates ATR-based volatility filtering to avoid trading during excessive market noise, a dynamic ATR stop loss that adapts to current gold volatility, and an adaptive trailing stop to lock in profits as trends develop. The EA includes daily equity protection, spread control, and Friday close mechanisms to safeguard against weekend gaps.

Recommended Timeframe: H1
Trading Logic:
1. Macro Trend Alignment: H4 EMA(200) defines the primary trend direction; H1 EMA(50) must align with the same direction.
2. Momentum Confirmation: RSI(14) must confirm momentum (>50 for long, <50 for short).
3. Entry Trigger: Price closes beyond the recent 20-period high/low after trend alignment.
4. Volatility Guard: Current ATR(14) must not exceed 1.8x the 20-period average ATR.
5. Risk Management: Dynamic stop loss at 1.5x ATR, take profit at 3x ATR. Adaptive trailing stop activates after 1.5x ATR profit.

```mql4
//+------------------------------------------------------------------+
//| GoldMacroAlignEA.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 H4TrendPeriod = 200; // H4 EMA period for macro trend
input int H1TrendPeriod = 50; // H1 EMA period for trend alignment
input int BreakoutPeriod = 20; // Period for breakout high/low detection
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.5; // Trailing activates at profit (x ATR)
input double TrailingStepATR = 0.75; // Trailing step (x ATR)
input int MagicNumber = 202421; // 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 macro trend direction from H4 EMA(200) |
//+------------------------------------------------------------------+
int GetMacroTrend()
{
double closeH4 = iClose(Symbol(), PERIOD_H4, 1);
double emaH4 = iMA(Symbol(), PERIOD_H4, H4TrendPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
if(closeH4 > emaH4) return 1; // Bullish
if(closeH4 < emaH4) return -1; // Bearish
return 0;
}

//+------------------------------------------------------------------+
//| Check if H1 trend aligns with macro trend |
//+------------------------------------------------------------------+
bool IsTrendAligned(int macroTrend)
{
if(macroTrend == 0) return false;
double closeH1 = iClose(Symbol(), PERIOD_H1, 1);
double emaH1 = iMA(Symbol(), PERIOD_H1, H1TrendPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);

if(macroTrend == 1) return (closeH1 > emaH1);
else return (closeH1 < emaH1);
}

//+------------------------------------------------------------------+
//| Check breakout from recent range |
//+------------------------------------------------------------------+
bool CheckBreakoutEntry(int direction, double &entryPrice, double &sl, double &tp, double atr)
{
if(direction == 0) return false;

double highBreakout = iHigh(Symbol(), PERIOD_H1, iHighest(Symbol(), PERIOD_H1, MODE_HIGH, BreakoutPeriod, 2));
double lowBreakout = iLow(Symbol(), PERIOD_H1, iLowest(Symbol(), PERIOD_H1, MODE_LOW, BreakoutPeriod, 2));
double close1 = iClose(Symbol(), PERIOD_H1, 1);
double open1 = iOpen(Symbol(), PERIOD_H1, 1);

// Bullish breakout: close above range high with bullish candle
if(direction == 1 && close1 > highBreakout && close1 > open1)
{
entryPrice = Ask;
sl = entryPrice - (atr * ATRStopMultiplier);
tp = entryPrice + (atr * ATRTakeMultiplier);
return true;
}
// Bearish breakout: close below range low with bearish candle
else if(direction == -1 && close1 < lowBreakout && close1 < open1)
{
entryPrice = Bid;
sl = entryPrice + (atr * ATRStopMultiplier);
tp = entryPrice - (atr * ATRTakeMultiplier);
return true;
}
return false;
}

//+------------------------------------------------------------------+
//| Manage adaptive trailing stop |
//+------------------------------------------------------------------+
void ManageAdaptiveTrailing(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) ManageAdaptiveTrailing(atr);
return;
}

// Get indicators
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 macro trend direction
int macroTrend = GetMacroTrend();
if(macroTrend == 0)
{
Comment("No clear macro trend direction");
return;
}

// Check H1 trend alignment
if(!IsTrendAligned(macroTrend))
{
Comment("H1 trend not aligned with macro trend");
return;
}

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

// Check breakout entry
double entryPrice = 0, sl = 0, tp = 0;
if(CheckBreakoutEntry(macroTrend, entryPrice, sl, tp, atr))
{
int cmd = (macroTrend == 1) ? OP_BUY : OP_SELL;
int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Gold Macro Align", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("OrderSend failed: ", GetLastError());
else
Print("Breakout entry opened. 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 2025 macro trend alignment principles for XAUUSD.
Disclaimer: Gold trading involves significant risk due to high volatility and leverage. This EA is provided as-is without any guarantee of profit. Test thoroughly on a demo account for at least 3 months before live deployment. Past performance does not guarantee future results.