Summary: Low-risk MQL4 EA for Gold and Forex. Uses trend filter, ATR-based stops, and limited daily exposure. Suitable for stable operation on XAUUSD H1.
This EA is designed for low-risk, stable operation on Gold (XAUUSD) and can also work on major Forex pairs. It avoids martingale, grid, or high-frequency strategies. The logic uses a simple moving average trend filter, ATR for volatility-based stop loss and take profit, and a fixed time session filter to avoid high-spread periods. Maximum one trade per bar to reduce overtrading. Compiles with zero warnings on MQL4 build 600+.
Load on H1 timeframe for best results. Works on any broker with 5-digit or 4-digit quotes.
```mql4
//+------------------------------------------------------------------+
//| StableGoldEA.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| 输入参数 |
//+------------------------------------------------------------------+
input double LotSize = 0.01; // 固定手数 (建议0.01起)
input int MAPeriod = 20; // 趋势过滤均线周期
input int ATRPeriod = 14; // ATR计算周期
input double ATRMultiplierSL = 1.5; // 止损 = ATR * 倍数
input double ATRMultiplierTP = 2.5; // 止盈 = ATR * 倍数
input int MaxDailyTrades = 1; // 每日最大交易次数
input int StartHour = 8; // 允许交易开始小时 (服务器时间)
input int EndHour = 20; // 允许交易结束小时 (服务器时间)
input int MagicNumber = 202401; // EA魔术编号
// 全局变量
double atrValue;
datetime lastTradeDay;
int tradesToday;
//+------------------------------------------------------------------+
//| 专家初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
if(LotSize <= 0 || MAPeriod < 1 || ATRPeriod < 1)
return(INIT_PARAMETERS_INCORRECT);
tradesToday = 0;
lastTradeDay = 0;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 专家反初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| 专家主循环函数 |
//+------------------------------------------------------------------+
void OnTick()
{
// 检查新K线开仓 (每根K线最多一单)
static datetime lastBarTime = 0;
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];
// 每日交易次数重置
if(lastTradeDay != Day())
{
lastTradeDay = Day();
tradesToday = 0;
}
if(tradesToday >= MaxDailyTrades)
return;
// 时间过滤
if(!IsTradeTime())
return;
// 计算ATR
atrValue = iATR(Symbol(), PERIOD_CURRENT, ATRPeriod, 1);
if(atrValue <= 0 || atrValue > 1000 * Point)
return;
// 趋势过滤: 价格高于MA做多,低于做空 (简单移动平均)
double ma = iMA(Symbol(), PERIOD_CURRENT, MAPeriod, 0, MODE_SMA, PRICE_CLOSE, 1);
if(ma == 0)
return;
double bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
// 无持仓时开仓
if(CountPositions() == 0)
{
// 做多信号
if(Close[1] > ma && Open[0] > ma)
{
double sl = ask - ATRMultiplierSL * atrValue;
double tp = ask + ATRMultiplierTP * atrValue;
sl = NormalizeDouble(sl, Digits);
tp = NormalizeDouble(tp, Digits);
int ticket = OrderSend(Symbol(), OP_BUY, LotSize, ask, 3, sl, tp, "StableGoldEA", MagicNumber, 0, clrGreen);
if(ticket > 0)
tradesToday++;
}
// 做空信号
else if(Close[1] < ma && Open[0] < ma)
{
double sl = bid + ATRMultiplierSL * atrValue;
double tp = bid - ATRMultiplierTP * atrValue;
sl = NormalizeDouble(sl, Digits);
tp = NormalizeDouble(tp, Digits);
int ticket = OrderSend(Symbol(), OP_SELL, LotSize, bid, 3, sl, tp, "StableGoldEA", MagicNumber, 0, clrRed);
if(ticket > 0)
tradesToday++;
}
}
}
//+------------------------------------------------------------------+
//| 统计当前品种的持仓数量 |
//+------------------------------------------------------------------+
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;
}
//+------------------------------------------------------------------+
//| 检查是否在允许的交易时间段 |
//+------------------------------------------------------------------+
bool IsTradeTime()
{
datetime now = TimeCurrent();
int hour = TimeHour(now);
if(hour >= StartHour && hour < EndHour)
return true;
return false;
}
//+------------------------------------------------------------------+
```
Reference: Based on common low-risk forex strategies using trend following and volatility-based risk management.
Disclaimer: This EA is for educational purposes only. Past performance does not guarantee future results. Test thoroughly on demo before live trading.