Summary: Gold Horizon EA is an MQL4 expert advisor for XAUUSD that combines dual EMA trend detection, ATR volatility filtering, and strict equity-based risk management. Suitable for H1 timeframe.
Gold Horizon EA is a robust Expert Advisor designed specifically for Gold (XAUUSD) trading. Unlike aggressive martingale or grid systems that risk unlimited drawdown, this EA employs a disciplined trend-following approach combined with volatility-adaptive risk controls . It incorporates the latest stability features including spread protection, cooldown periods after losses, and Friday close mechanisms to protect against weekend gaps .
Recommended Timeframe: H1
Trading Logic:
1. Trend Detection: Dual EMA crossover (fast EMA 12, slow EMA 26) identifies directional bias.
2. Volatility Filter: ATR(14) ensures entries only occur during normal volatility regimes; extreme volatility triggers a cooldown.
3. Momentum Confirmation: RSI(14) above 50 for longs, below 50 for shorts to filter low-probability setups.
4. Risk Management: Fixed SL (300 points), trailing stop activation at 1x ATR profit, daily loss limit 5%, max spread 35 points.
```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 lot for XAUUSD)
input int FastEMAPeriod = 12; // Fast EMA period for trend detection
input int SlowEMAPeriod = 26; // Slow EMA period for trend detection
input int RSIPeriod = 14; // RSI period for momentum filter
input int ATRPeriod = 14; // ATR period for volatility-based stop loss
input double ATRStopMultiplier = 1.5; // Stop loss as multiple of ATR
input double ATRTakeMultiplier = 3.0; // Take profit as multiple of ATR
input double TrailingStart = 1.0; // Trailing stop activates after profit (x ATR)
input double TrailingStep = 0.5; // Trailing stop step (x ATR)
input int MagicNumber = 202419; // Unique EA identifier
input int MaxSpread = 35; // Maximum allowed spread in points (Gold)
input double DailyLossLimit = 5.0; // Daily loss limit as percentage of balance
input int CooldownMinutes = 30; // Cooldown after losing trade (minutes)
input bool UseFridayClose = true; // Close trades before Friday 21:00 GMT
//--- global variables
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool fridayCloseExecuted = false;
datetime lastLossTime = 0;
double avgATR = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
fridayCloseExecuted = false;
lastLossTime = 0;
avgATR = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(avgATR <= 0) avgATR = 200 * Point;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Check if cooldown is active |
//+------------------------------------------------------------------+
bool IsCooldownActive()
{
if(lastLossTime == 0) return false;
if(TimeCurrent() - lastLossTime < CooldownMinutes * 60) return true;
lastLossTime = 0;
return false;
}
//+------------------------------------------------------------------+
//| Manage trailing stop |
//+------------------------------------------------------------------+
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 newSL = 0;
if(OrderType() == OP_BUY)
{
double profitPoints = (Bid - OrderOpenPrice()) / Point;
if(profitPoints >= (TrailingStart * atr / Point))
{
newSL = Bid - (TrailingStep * atr);
if(newSL > OrderStopLoss())
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
else if(OrderType() == OP_SELL)
{
double profitPoints = (OrderOpenPrice() - Ask) / Point;
if(profitPoints >= (TrailingStart * atr / Point))
{
newSL = Ask + (TrailingStep * atr);
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;
}
// Cooldown after losing trade
if(IsCooldownActive())
{
Comment("Cooldown active for ", CooldownMinutes, " minutes after loss.");
return;
}
// Friday close before weekend gap
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
int currentSpread = MarketInfo(Symbol(), MODE_SPREAD);
if(currentSpread > MaxSpread)
{
Comment("Spread too high: ", currentSpread);
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;
}
// Calculate indicators on closed bar
double fastEMA = iMA(Symbol(), PERIOD_H1, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double slowEMA = iMA(Symbol(), PERIOD_H1, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double fastEMAPrev = iMA(Symbol(), PERIOD_H1, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 2);
double slowEMAPrev = iMA(Symbol(), PERIOD_H1, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 2);
double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);
double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(atr <= 0) atr = avgATR;
// Volatility filter - skip if ATR is too high relative to average
if(atr > avgATR * 1.8)
{
Comment("Volatility too high, waiting for cooldown.");
return;
}
// Update rolling average ATR
avgATR = (avgATR * 0.95) + (atr * 0.05);
int cmd = -1;
double sl = 0, tp = 0;
double ask = Ask;
double bid = Bid;
// Long condition: bullish EMA crossover + RSI above 50
if(fastEMAPrev <= slowEMAPrev && fastEMA > slowEMA && rsi > 50)
{
cmd = OP_BUY;
sl = bid - (atr * ATRStopMultiplier);
tp = bid + (atr * ATRTakeMultiplier);
}
// Short condition: bearish EMA crossover + RSI below 50
else if(fastEMAPrev >= slowEMAPrev && fastEMA < slowEMA && rsi < 50)
{
cmd = OP_SELL;
sl = ask + (atr * ATRStopMultiplier);
tp = ask - (atr * ATRTakeMultiplier);
}
if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?ask:bid), 3, sl, tp, "Gold Horizon", 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, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
```
Reference: Original MQL4 code incorporating modern risk management practices including cooldown periods, spread protection, and ATR-based dynamic stops.
Disclaimer: Forex and Gold trading involves substantial risk. This EA is provided as-is without any guarantee of profit. Test thoroughly on a demo account before live trading. Past performance does not guarantee future results.