Summary: Gold Scalper Safety EA is an MQL4 expert advisor for XAUUSD that combines RSI divergence detection, volatility filtering, and controlled single-position scalping. Suitable for M15 timeframe.




Gold Scalper Safety EA is engineered for traders seeking stable, low-risk scalping on XAUUSD gold. Unlike high-frequency scalpers that generate excessive commission costs, this EA focuses on high-quality reversal signals during optimal volatility windows. The strategy detects RSI oversold/overbought conditions with divergence confirmation, applies ATR volatility filters to avoid low-volatility chop zones, and uses a single position controller to prevent overtrading. Each trade includes fixed stop loss and take profit based on ATR multiples, with daily loss protection and Friday close mechanisms.

Recommended Timeframe: M15
Trading Logic:
1. RSI Reversal Detection: Wait for RSI(14) to enter oversold (<30) or overbought (>70), then return above/below threshold.
2. Divergence Confirmation: Price makes lower low but RSI makes higher low (bullish) or vice versa (bearish).
3. Volatility Filter: ATR(14) must be above minimum threshold (not too choppy) but below max (not too wild).
4. Time Filter: Trade only during active sessions (avoid Asian consolidation).
5. Risk Management: Fixed stop loss (250 points), take profit (400 points), maximum 1 trade at a time.

```mql4
//+------------------------------------------------------------------+
//| GoldScalperSafetyEA.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 RSIPeriod = 14; // RSI period for reversal detection
input int RSIOverbought = 70; // RSI overbought level (sell zone)
input int RSIOversold = 30; // RSI oversold level (buy zone)
input int ATRPeriod = 14; // ATR period for volatility filter
input double ATREnabledMin = 150; // Minimum ATR points (avoid chop)
input double ATREnabledMax = 500; // Maximum ATR points (avoid wild spikes)
input int StopLossPoints = 250; // Fixed stop loss in points
input int TakeProfitPoints = 400; // Fixed take profit in points
input int StartHour = 8; // Trading start hour (GMT)
input int EndHour = 20; // Trading end hour (GMT)
input int MagicNumber = 202422; // 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;

//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
fridayCloseExecuted = false;
return(INIT_SUCCEEDED);
}

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

//+------------------------------------------------------------------+
//| Check if within trading hours |
//+------------------------------------------------------------------+
bool IsTradingHour()
{
datetime currentTime = TimeCurrent();
int hourGMT = TimeHour(currentTime);
return (hourGMT >= StartHour && hourGMT < EndHour);
}

//+------------------------------------------------------------------+
//| Check for bullish divergence (price lower low, RSI higher low) |
//+------------------------------------------------------------------+
bool IsBullishDivergence()
{
double priceLow1 = iLow(Symbol(), PERIOD_M15, iLowest(Symbol(), PERIOD_M15, MODE_LOW, 5, 2));
double priceLow2 = iLow(Symbol(), PERIOD_M15, iLowest(Symbol(), PERIOD_M15, MODE_LOW, 5, 8));
double rsi1 = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, iLowest(Symbol(), PERIOD_M15, MODE_LOW, 5, 2));
double rsi2 = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, iLowest(Symbol(), PERIOD_M15, MODE_LOW, 5, 8));

if(priceLow2 < priceLow1 && rsi2 > rsi1 && rsi2 < RSIOversold + 10)
return true;
return false;
}

//+------------------------------------------------------------------+
//| Check for bearish divergence (price higher high, RSI lower high) |
//+------------------------------------------------------------------+
bool IsBearishDivergence()
{
double priceHigh1 = iHigh(Symbol(), PERIOD_M15, iHighest(Symbol(), PERIOD_M15, MODE_HIGH, 5, 2));
double priceHigh2 = iHigh(Symbol(), PERIOD_M15, iHighest(Symbol(), PERIOD_M15, MODE_HIGH, 5, 8));
double rsi1 = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, iHighest(Symbol(), PERIOD_M15, MODE_HIGH, 5, 2));
double rsi2 = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, iHighest(Symbol(), PERIOD_M15, MODE_HIGH, 5, 8));

if(priceHigh2 > priceHigh1 && rsi2 < rsi1 && rsi2 > RSIOverbought - 10)
return true;
return false;
}

//+------------------------------------------------------------------+
//| Check if RSI exit condition met |
//+------------------------------------------------------------------+
bool RSIExtremeExit(int direction)
{
double rsi = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, 1);

if(direction == 1) // long exit when RSI > 50
return (rsi > 50);
else if(direction == -1) // short exit when RSI < 50
return (rsi < 50);
return false;
}

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

// Trading hour filter
if(!IsTradingHour())
{
Comment("Outside trading hours");
return;
}

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

// Check existing position
int posCount = CountPositions();
if(posCount > 0)
{
// Exit management: close when RSI returns to neutral
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
int direction = (OrderType() == OP_BUY) ? 1 : -1;
if(RSIExtremeExit(direction))
{
if(OrderType() == OP_BUY)
OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
Print("Position closed by RSI neutral signal");
}
}
}
}
return;
}

// Get ATR for volatility filter
double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
if(atr < ATREnabledMin * Point || atr > ATREnabledMax * Point)
{
Comment("Volatility outside acceptable range. ATR: ", atr/Point);
return;
}

// Get RSI values
double rsi = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, 1);
double rsi_prev = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, 2);

int cmd = -1;
double sl = 0, tp = 0;
double ask = Ask;
double bid = Bid;

// Buy signal: RSI exits oversold OR bullish divergence
bool buySignal = (rsi_prev <= RSIOversold && rsi > RSIOversold) || IsBullishDivergence();
// Sell signal: RSI exits overbought OR bearish divergence
bool sellSignal = (rsi_prev >= RSIOverbought && rsi < RSIOverbought) || IsBearishDivergence();

if(buySignal)
{
cmd = OP_BUY;
sl = bid - StopLossPoints * Point;
tp = bid + TakeProfitPoints * Point;
}
else if(sellSignal)
{
cmd = OP_SELL;
sl = ask + StopLossPoints * Point;
tp = ask - TakeProfitPoints * Point;
}

if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?ask:bid), 3, sl, tp, "Gold Scalper Safety", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("OrderSend failed: ", GetLastError());
else
Print("Scalp order 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, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
```
Reference: Original MQL4 code inspired by gold scalping principles from recent community EAs .
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.