Gold Stability EA is designed for trading gold (XAUUSD) with a focus on capital preservation. It combines a simple moving average crossover with an ATR-based volatility filter to avoid ranging markets. Trades are opened only when trend direction is clear and volatility is above a safe threshold. Each trade has a fixed stop loss and take profit based on current ATR. No martingale, no grid, no risky money management.
Load Period Recommendation: M15 (15-minute chart)
Trading Logic:
Disclaimer: Trading foreign exchange and gold involves significant risk. This EA is provided for educational purposes only. Past performance does not guarantee future results. Use at your own risk.
```mql4
//+------------------------------------------------------------------+
//| GoldStabilityEA.mq4 |
//| Copyright 2025, Gold EA Trader |
//| https://www.example.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, Gold EA Trader"
#property link "https://www.example.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Input parameters (tunable) |
//+------------------------------------------------------------------+
input double LotSize = 0.01; // Fixed lot size (low risk)
input int FastMAPeriod = 10; // Fast MA period for trend direction
input int SlowMAPeriod = 30; // Slow MA period for trend direction
input int ATRPeriod = 14; // ATR period for SL/TP and volatility filter
input double ATRMultiplierSL = 1.5; // Stop Loss = ATR * MultiplierSL
input double ATRMultiplierTP = 2.5; // Take Profit = ATR * MultiplierTP
input double ATRThreshold = 1.2; // Min ATR ratio (current ATR / avgATR50) to trade
input int MagicNumber = 202505; // Unique EA identifier
input int Slippage = 30; // Slippage in points
//+------------------------------------------------------------------+
//| Global variables |
//+------------------------------------------------------------------+
double atrValue, avgATR50;
int lastBarTime = 0;
bool hasPosition = false;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(FastMAPeriod >= SlowMAPeriod)
{
Print("Error: FastMAPeriod must be less than SlowMAPeriod");
return(INIT_PARAMETERS_INCORRECT);
}
if(LotSize <= 0 || LotSize > 1.0)
{
Print("Error: LotSize must be between 0.01 and 1.0");
return(INIT_PARAMETERS_INCORRECT);
}
lastBarTime = 0;
hasPosition = false;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check for new bar (M15)
if(Time[0] == lastBarTime) return;
lastBarTime = Time[0];
// Refresh rates
RefreshRates();
// Check if position already exists
hasPosition = (OrderSelect(0, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber);
if(hasPosition) return;
// Calculate indicators
double fastMA = iMA(Symbol(), 0, FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
double slowMA = iMA(Symbol(), 0, SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
atrValue = iATR(Symbol(), 0, ATRPeriod, 1);
// Calculate average ATR of last 50 bars (volatility baseline)
double sumATR = 0;
for(int i=1; i<=50; i++)
sumATR += iATR(Symbol(), 0, ATRPeriod, i);
avgATR50 = sumATR / 50;
// Volatility filter: avoid extremely low volatility chop
if(avgATR50 == 0 || atrValue < ATRThreshold * avgATR50) return;
// Trend filter and entry signals
if(fastMA > slowMA) // Bullish
{
double sl = Bid - atrValue * ATRMultiplierSL;
double tp = Bid + atrValue * ATRMultiplierTP;
if(sl > 0 && tp > Bid)
{
int ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, Slippage, sl, tp, "Gold EA", MagicNumber, 0, clrGreen);
if(ticket < 0) Print("Buy order failed: ", GetLastError());
}
}
else if(fastMA < slowMA) // Bearish
{
double sl = Ask + atrValue * ATRMultiplierSL;
double tp = Ask - atrValue * ATRMultiplierTP;
if(tp > 0 && sl > Ask)
{
int ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, Slippage, sl, tp, "Gold EA", MagicNumber, 0, clrRed);
if(ticket < 0) Print("Sell order failed: ", GetLastError());
}
}
}
//+------------------------------------------------------------------+
```
Reference: Based on common low-risk trend following principles for gold. No martingale, no grid.