Summary: Gold Structural Flow EA is an MQL4 expert advisor for XAUUSD that identifies structural breaks using swing high/low analysis, applies ADX trend strength filters and ATR volatility guards, with multi-timeframe confirmation for stable H4 operation.




Gold Structural Flow EA is designed specifically for gold (XAUUSD) using a structural analysis approach that mimics institutional order flow detection. Unlike grid or martingale systems that blindly average into losing positions, this EA identifies key structural breaks — when price clears established swing highs or lows on higher timeframes — and enters in the direction of the breakout. The strategy uses a triple-filter mechanism: structural confirmation (break of recent 20-bar high/low), trend strength validation (ADX > 25), and volatility adaptation (ATR compared to 50-period average). The EA limits exposure to a single direction at a time, uses fixed lot sizes without progression, and includes a daily equity drawdown limit and Friday close protection.

Recommended Timeframe: H4
Trading Logic:
1. Structural Break Detection: Identify the highest high and lowest low of the last 20 candles. A breakout occurs when price closes beyond these levels.
2. Trend Strength Filter: ADX(14) must exceed a configurable threshold (default 25) to ensure the market is trending.
3. Volatility Guard: Current ATR(14) should not exceed 2.0 times the 50-period average ATR.
4. Multi-Timeframe Confirmation (optional): Higher timeframe EMA (H4 period 200) provides additional directional bias.
5. Risk Management: Fixed ATR-based stop loss (1.5x ATR), take profit at 2.5x ATR, trailing stop after 1x ATR profit.

```mql4
//+------------------------------------------------------------------+
//| GoldStructuralFlowEA.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 StructureLookback = 20; // Bars for swing high/low detection
input int ADXPeriod = 14; // ADX period for trend strength
input int ADXThreshold = 25; // Minimum ADX value for trending market
input int ATRPeriod = 14; // ATR period for volatility
input double ATRMaxMultiplier = 2.0; // Max ATR vs average ATR (volatility guard)
input double ATRStopMultiplier = 1.5; // Stop loss as multiple of ATR
input double ATRTakeMultiplier = 2.5; // 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 bool UseHTFTrendFilter = true; // Enable H4 200EMA trend filter
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;
double structureHigh = 0;
double structureLow = 0;

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

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

//+------------------------------------------------------------------+
//| Detect structural break levels (swing high/low) |
//+------------------------------------------------------------------+
void DetectStructureLevels()
{
int highestIdx = iHighest(Symbol(), PERIOD_H4, MODE_HIGH, StructureLookback, 1);
int lowestIdx = iLowest(Symbol(), PERIOD_H4, MODE_LOW, StructureLookback, 1);

if(highestIdx > 0)
structureHigh = iHigh(Symbol(), PERIOD_H4, highestIdx);
if(lowestIdx > 0)
structureLow = iLow(Symbol(), PERIOD_H4, lowestIdx);
}

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

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

//+------------------------------------------------------------------+
//| Check structural breakout entry |
//+------------------------------------------------------------------+
bool CheckStructuralBreakout(int direction, double &entryPrice, double &sl, double &tp, double atr)
{
if(direction == 0) return false;

double close1 = iClose(Symbol(), PERIOD_H4, 1);

// Bullish breakout: close above structure high
if(direction == 1 && structureHigh > 0)
{
if(close1 > structureHigh)
{
entryPrice = Ask;
sl = entryPrice - (atr * ATRStopMultiplier);
tp = entryPrice + (atr * ATRTakeMultiplier);
return true;
}
}
// Bearish breakout: close below structure low
else if(direction == -1 && structureLow > 0)
{
if(close1 < structureLow)
{
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 (H4)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];

// Update structure levels on each new bar
DetectStructureLevels();

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

// Get ATR for volatility filtering
double atr = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
if(atr > 0) avgATR = (avgATR * 0.95) + (atr * 0.05);

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

// Check ADX trend strength
if(!IsTrendStrong())
{
Comment("ADX below threshold. No strong trend.");
return;
}

// Determine allowed trading direction
int allowedDirection = 0;
int htfTrend = GetHTFTrend();

// Without HTF filter, allow both directions if structure breaks
if(!UseHTFTrendFilter)
{
double close1 = iClose(Symbol(), PERIOD_H4, 1);
if(close1 > structureHigh) allowedDirection = 1;
else if(close1 < structureLow) allowedDirection = -1;
}
else
{
if(htfTrend == 1 && structureHigh > 0)
allowedDirection = 1;
else if(htfTrend == -1 && structureLow > 0)
allowedDirection = -1;
}

if(allowedDirection == 0)
{
Comment("No valid structural setup");
return;
}

// Check structural breakout entry
double entryPrice = 0, sl = 0, tp = 0;
if(CheckStructuralBreakout(allowedDirection, entryPrice, sl, tp, atr))
{
int cmd = (allowedDirection == 1) ? OP_BUY : OP_SELL;
int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Structural Flow", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("OrderSend failed: ", GetLastError());
else
Print("Structural 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 structural analysis principles observed in recent gold trading systems.
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.