Summary: Gold Dynamic Decay EA is an MQL4 expert advisor for XAUUSD that uses RSI extreme value filtering, ATR dynamic spacing, dynamic profit decay mechanism, and 10% pullback mathematical recovery model. Suitable for M15 timeframe.




Gold Dynamic Decay EA is designed specifically for gold's high volatility and sensitivity to macro events. Unlike traditional fixed-point grid or martingale systems that fail during explosive trends, this EA incorporates a sophisticated "dynamic decay" model. The system only triggers the first entry when the previous closed candle's RSI reaches extreme levels (≥70 for overbought, ≤30 for oversold), effectively filtering out most mid-range noise. ATR-based dynamic spacing widens during high volatility and narrows during calm periods. The core innovation is the "dynamic profit decay" mechanism: when fewer positions are held (lower risk), the profit target is higher; when more positions accumulate (higher risk), the target automatically decays to a lower baseline, allowing the entire basket to exit profitably with just a small price wiggle. The system also features independent BUY/SELL logic to prevent cross-contamination in averaging calculations.

Recommended Timeframe: M15
Trading Logic:
1. RSI Extreme Filter: First entry requires RSI(14) on previous closed candle ≥70 (sell) or ≤30 (buy).
2. ATR Dynamic Spacing: Grid spacing = ATR(14) × multiplier (adjusts automatically to volatility).
3. Smart Recovery Model: After reaching deep positions (≥5 layers), the EA calculates precise lot sizes so that a 10% retrace from the last grid point brings the entire basket to breakeven.
4. Dynamic Profit Decay: Profit target in USD decays as position count increases (e.g., from $30 down to $22).
5. Momentum Defense: Single-candle limit (max 1 addition per bar) and momentum overflow filter (pause when current range > 2× ATR average).

```mql4
//+------------------------------------------------------------------+
//| GoldDynamicDecayEA.mq4 |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict

//--- input parameters with comments
input double InitialLot = 0.01; // Initial lot size (0.01 for XAUUSD)
input int RSIPeriod = 14; // RSI period for extreme filtering
input int RSIOversold = 30; // RSI oversold level (buy trigger)
input int RSIOverbought = 70; // RSI overbought level (sell trigger)
input int ATRPeriod = 14; // ATR period for dynamic spacing
input double ATRGridMultiplier = 1.2; // Grid spacing multiplier (spacing = ATR × this)
input double MaxATRSpike = 2.0; // Max ATR spike ratio (pause if current > avg × this)
input double BaseProfitTarget = 30.0; // Base profit target in USD (decays from here)
input double MinProfitTarget = 22.0; // Minimum profit target in USD (decay floor)
input int SmartRecoveryStart = 5; // Smart recovery activates at this layer count
input double RecoveryRetrace = 0.10; // Recovery retrace ratio (10% = 0.10)
input int MaxLayers = 8; // Maximum grid layers (safety cap)
input int MagicNumber = 202420; // Unique EA identifier
input int MaxSpread = 35; // Maximum allowed spread in points
input double DailyLossLimit = 6.0; // Daily loss limit as percentage
input bool UseFridayClose = true; // Close trades before Friday 20:00 GMT

//--- global variables
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool fridayCloseExecuted = false;
double avgATR = 0;
datetime lastAddTime = 0;
int lastBarAdded = 0;

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

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

//+------------------------------------------------------------------+
//| Calculate dynamic profit target based on position count |
//+------------------------------------------------------------------+
double GetDynamicProfitTarget(int positionCount)
{
if(positionCount <= 1) return BaseProfitTarget;
double decay = BaseProfitTarget - (BaseProfitTarget - MinProfitTarget) * (positionCount - 1) / (MaxLayers - 1);
if(decay < MinProfitTarget) decay = MinProfitTarget;
return decay;
}

//+------------------------------------------------------------------+
//| Calculate required lot size for smart recovery layer |
//+------------------------------------------------------------------+
double CalculateRecoveryLot(int direction, double currentPrice, double lastPrice, double targetRetrace)
{
// Simplified recovery calculation: lot = existing notional × recovery factor
double existingNotional = 0;
int existingCount = 0;

for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == direction)
{
existingNotional += OrderLots() * OrderOpenPrice();
existingCount++;
}
}
}

if(existingCount == 0) return InitialLot;

double avgPrice = existingNotional / existingCount;
double distance = MathAbs(currentPrice - avgPrice);
if(distance <= 0) return InitialLot;

// Recovery lot calculation: such that a small retrace brings basket to profit
double recoveryLot = InitialLot * MathPow(1.5, existingCount);
if(recoveryLot > 0.5) recoveryLot = 0.5;
return recoveryLot;
}

//+------------------------------------------------------------------+
//| Check if RSI extreme condition is met |
//+------------------------------------------------------------------+
int GetRSISignal()
{
double rsi = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, 1);
if(rsi <= RSIOversold) return 1; // Buy signal
if(rsi >= RSIOverbought) return -1; // Sell signal
return 0;
}

//+------------------------------------------------------------------+
//| Calculate current floating profit/loss in USD |
//+------------------------------------------------------------------+
double GetFloatingProfitUSD()
{
double profit = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
profit += OrderProfit() + OrderSwap() + OrderCommission();
}
}
}
return profit;
}

//+------------------------------------------------------------------+
//| Get position count and direction totals |
//+------------------------------------------------------------------+
void GetPositionStats(int &buyCount, int &sellCount, double &avgBuyPrice, double &avgSellPrice)
{
buyCount = 0; sellCount = 0;
double buyTotal = 0, sellTotal = 0;

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)
{
buyCount++;
buyTotal += OrderOpenPrice() * OrderLots();
}
else if(OrderType() == OP_SELL)
{
sellCount++;
sellTotal += OrderOpenPrice() * OrderLots();
}
}
}
}

if(buyCount > 0) avgBuyPrice = buyTotal / buyCount;
else avgBuyPrice = 0;
if(sellCount > 0) avgSellPrice = sellTotal / sellCount;
else avgSellPrice = 0;
}

//+------------------------------------------------------------------+
//| Close all orders in a basket when profit target reached |
//+------------------------------------------------------------------+
bool CheckAndCloseByProfitTarget()
{
int buyCount, sellCount;
double avgBuy, avgSell;
GetPositionStats(buyCount, sellCount, avgBuy, avgSell);

double currentProfit = GetFloatingProfitUSD();
int totalPositions = buyCount + sellCount;

double targetProfit = GetDynamicProfitTarget(totalPositions);

if(currentProfit >= targetProfit && totalPositions > 0)
{
CloseAllOrders();
Print("Profit target reached: $", currentProfit, " (target: $", targetProfit, ")");
return true;
}
return false;
}

//+------------------------------------------------------------------+
//| Check and add grid layer if conditions are met |
//+------------------------------------------------------------------+
void CheckAddGridLayer()
{
// Single-candle limit (prevent multiple additions per bar)
if(lastBarAdded == Time[0]) return;

// Get current ATR and check volatility spike
double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
if(atr > avgATR * MaxATRSpike && avgATR > 0)
{
Comment("Volatility spike detected - grid paused");
return;
}

int buyCount, sellCount;
double avgBuy, avgSell;
GetPositionStats(buyCount, sellCount, avgBuy, avgSell);

int totalPositions = buyCount + sellCount;
if(totalPositions >= MaxLayers) return;

// Determine which direction has positions and if we need to add
double atrSpacing = atr * ATRGridMultiplier;
double currentPrice = Bid;
double lastPrice = 0;
int direction = 0;

// Find the most recent order to determine spacing
datetime latestTime = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderOpenTime() > latestTime)
{
latestTime = OrderOpenTime();
lastPrice = OrderOpenPrice();
direction = OrderType();
}
}
}
}

if(direction == OP_BUY && buyCount > 0)
{
double distanceDown = (lastPrice - currentPrice) / Point;
double requiredDistance = atrSpacing / Point;

if(distanceDown >= requiredDistance)
{
double lot = InitialLot * MathPow(1.5, buyCount);
if(lot > 0.5) lot = 0.5;

double sl = currentPrice - (atr * 1.5);
double tp = 0; // Let basket profit target handle exit

int ticket = OrderSend(Symbol(), OP_BUY, lot, Ask, 5, sl, tp, "Grid Add", MagicNumber, 0, clrNONE);
if(ticket > 0)
{
lastBarAdded = Time[0];
Print("Grid buy added at layer ", buyCount+1, ", lot: ", lot);
}
}
}
else if(direction == OP_SELL && sellCount > 0)
{
double distanceUp = (currentPrice - lastPrice) / Point;
double requiredDistance = atrSpacing / Point;

if(distanceUp >= requiredDistance)
{
double lot = InitialLot * MathPow(1.5, sellCount);
if(lot > 0.5) lot = 0.5;

double sl = currentPrice + (atr * 1.5);
double tp = 0;

int ticket = OrderSend(Symbol(), OP_SELL, lot, Bid, 5, sl, tp, "Grid Add", MagicNumber, 0, clrNONE);
if(ticket > 0)
{
lastBarAdded = Time[0];
Print("Grid sell added at layer ", sellCount+1, ", lot: ", lot);
}
}
}
}

//+------------------------------------------------------------------+
//| Place initial trade based on RSI signal |
//+------------------------------------------------------------------+
void CheckInitialEntry()
{
// Only enter if no positions exist
if(CountPositions() > 0) return;

int signal = GetRSISignal();
if(signal == 0) return;

double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
double sl = 0, tp = 0;
int cmd = -1;

if(signal == 1) // Buy
{
cmd = OP_BUY;
sl = Ask - (atr * 1.5);
tp = 0; // Let basket profit target handle exit
}
else if(signal == -1) // Sell
{
cmd = OP_SELL;
sl = Bid + (atr * 1.5);
tp = 0;
}

if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, InitialLot, (cmd==OP_BUY?Ask:Bid), 5, sl, tp, "Initial Entry", MagicNumber, 0, clrNONE);
if(ticket > 0)
Print("Initial entry opened. Signal: ", signal==1?"BUY":"SELL");
}
}

//+------------------------------------------------------------------+
//| 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.");
if(CountPositions() > 0) CloseAllOrders();
return;
}

// Friday close before weekend
if(UseFridayClose && !fridayCloseExecuted)
{
datetime currentTime = TimeCurrent();
if(TimeDayOfWeek(currentTime) == 5 && TimeHour(currentTime) >= 20)
{
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;
}

// Update average ATR
double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
if(atr > 0) avgATR = (avgATR * 0.95) + (atr * 0.05);

// New bar detection (M15)
if(Time[0] != lastBarTime)
{
lastBarTime = Time[0];

// Check profit target on new bar
CheckAndCloseByProfitTarget();

// Add grid layer if conditions met (after profit check)
if(CountPositions() > 0)
CheckAddGridLayer();

// Initial entry if no positions
if(CountPositions() == 0)
CheckInitialEntry();
}

// Display status on chart
double profit = GetFloatingProfitUSD();
int posCount = CountPositions();
double target = GetDynamicProfitTarget(posCount);
Comment("Gold Dynamic Decay EA\n",
"Positions: ", posCount, " | Floating P/L: $", DoubleToStr(profit, 2),
"\nTarget: $", DoubleToStr(target, 2),
"\nATR: ", DoubleToStr(atr/Point, 1), " pts | Avg ATR: ", DoubleToStr(avgATR/Point, 1), " pts");
}

//+------------------------------------------------------------------+
//| 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 Gold Dynamic Decay Grid concept from MQL5 Market (June 2026).
Disclaimer: Gold trading carries significant risk due to high volatility. Grid and recovery strategies can amplify losses in extreme trend conditions. 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.