Summary: 黄金动量卫士EA是一款专为XAUUSD设计的MQL4智能交易系统,使用动量强度检测、RSI趋势对齐和基于ATR的风险管理,适合M15周期稳定运行。




黄金动量卫士EA专为黄金(XAUUSD)设计,以稳定性为核心优先。基于对机构级黄金EA的研究,该策略专注于高概率入场过滤,而非激进的网格或马丁系统。EA结合高流动性时段(伦敦/纽约重叠)的动量检测、RSI趋势对齐和ATR波动率确认。

推荐加载周期: M15
策略核心逻辑:
1. 时段过滤:仅在伦敦时段(08:00-16:00 GMT)和纽约时段(13:00-21:00 GMT)交易。
2. 动量检测:比较当前K线的动量强度(实体/最大影线)与近期平均值。
3. 趋势对齐:做多要求RSI(14)>50,做空要求RSI(14)<50。
4. 波动率保护:ATR(20)必须在正常范围内(50周期平均ATR的0.8倍至1.5倍)。
5. 风险管理:固定止损250点,止盈450点,同时持仓最多1单,每日亏损上限5%。

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

//--- 输入参数及注释
input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
input int StopLossPoints = 250; // 止损点数(250点≈2.5美元/0.01手)
input int TakeProfitPoints = 450; // 止盈点数(盈亏比约1:1.8)
input int RSIPeriod = 14; // RSI周期(趋势对齐)
input int ATRPeriod = 20; // ATR周期(波动率检测)
input double MinATRMultiplier = 0.8; // 最小ATR倍数(低波动跳过)
input double MaxATRMultiplier = 1.5; // 最大ATR倍数(高波动跳过)
input int MomentumLookback = 10; // 动量基准回溯K线数
input double MomentumMinRatio = 1.5; // 最小动量比率(实体/影线触发阈值)
input int MagicNumber = 202419; // EA魔术号
input int MaxSpread = 35; // 最大允许点差(单位:点)
input double DailyLossLimit = 5.0; // 每日亏损限额(账户余额百分比)
input bool UseSessionFilter = true; // 过滤非伦敦/纽约交易时段
input bool UseFridayClose = true; // 周五21:00 GMT前平仓

//--- 全局变量
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool fridayCloseExecuted = false;
double avgATR50 = 0;
double momentumBaseline = 0;

//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
fridayCloseExecuted = false;

// 初始化ATR基准
avgATR50 = iATR(Symbol(), PERIOD_M15, 50, 1);
if(avgATR50 <= 0) avgATR50 = 200 * Point;

// 初始化动量基准
double totalMomentum = 0;
for(int i = 1; i <= MomentumLookback; i++)
totalMomentum += GetMomentumRatio(i);
momentumBaseline = totalMomentum / MomentumLookback;
if(momentumBaseline <= 0) momentumBaseline = 1.0;

return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| EA退出函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}

//+------------------------------------------------------------------+
//| 计算K线的动量比率(实体 / 最大影线) |
//+------------------------------------------------------------------+
double GetMomentumRatio(int shift)
{
double open = iOpen(Symbol(), PERIOD_M15, shift);
double close = iClose(Symbol(), PERIOD_M15, shift);
double high = iHigh(Symbol(), PERIOD_M15, shift);
double low = iLow(Symbol(), PERIOD_M15, shift);

double body = MathAbs(close - open);
double upperWick = high - MathMax(open, close);
double lowerWick = MathMin(open, close) - low;
double maxWick = MathMax(upperWick, lowerWick);

if(maxWick <= 0) return 1.0;
return body / maxWick;
}

//+------------------------------------------------------------------+
//| 检查当前时间是否在允许的交易时段内 |
//+------------------------------------------------------------------+
bool IsValidTradingSession()
{
datetime currentTime = TimeCurrent();
int hour = TimeHour(currentTime);
int minute = TimeMinute(currentTime);
int currentTimeMinutes = hour * 60 + minute;

// 伦敦时段:08:00 - 16:00 GMT(480 - 960分钟)
// 纽约时段:13:00 - 21:00 GMT(780 - 1260分钟)
bool isLondon = (currentTimeMinutes >= 480 && currentTimeMinutes < 960);
bool isNewYork = (currentTimeMinutes >= 780 && currentTimeMinutes < 1260);

return (isLondon || isNewYork);
}

//+------------------------------------------------------------------+
//| 检查当前波动率是否在可接受范围内 |
//+------------------------------------------------------------------+
bool IsValidVolatility()
{
double currentATR = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
if(currentATR <= 0) return false;

// 更新滚动平均值
avgATR50 = (avgATR50 * 0.98) + (currentATR * 0.02);

double minATR = avgATR50 * MinATRMultiplier;
double maxATR = avgATR50 * MaxATRMultiplier;

return (currentATR >= minATR && currentATR <= maxATR);
}

//+------------------------------------------------------------------+
//| 检查动量爆发条件 |
//+------------------------------------------------------------------+
bool IsMomentumSurge(int direction)
{
double currentMomentum = GetMomentumRatio(1);
if(currentMomentum <= 0) return false;

// 更新基准
momentumBaseline = (momentumBaseline * 0.9) + (currentMomentum * 0.1);

bool isStrongMomentum = (currentMomentum >= momentumBaseline * MomentumMinRatio);
if(!isStrongMomentum) return false;

double close1 = iClose(Symbol(), PERIOD_M15, 1);
double open1 = iOpen(Symbol(), PERIOD_M15, 1);
double high1 = iHigh(Symbol(), PERIOD_M15, 1);
double low1 = iLow(Symbol(), PERIOD_M15, 1);

if(direction == 1) // 看涨动量
return (close1 > open1 && (high1 - close1) < (close1 - open1) * 0.5);
else if(direction == -1) // 看跌动量
return (close1 < open1 && (low1 - close1) > (close1 - open1) * 0.5);

return false;
}

//+------------------------------------------------------------------+
//| EA主循环函数(每Tick执行) |
//+------------------------------------------------------------------+
void OnTick()
{
// 每日净值保护
double currentEquity = AccountEquity();
double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance * 100;
if(lossPercent >= DailyLossLimit)
{
Comment("已达每日亏损上限,停止开新仓");
return;
}

// 周五收盘前平仓(规避周末跳空)
if(UseFridayClose && !fridayCloseExecuted)
{
datetime currentTime = TimeCurrent();
if(TimeDayOfWeek(currentTime) == 5 && TimeHour(currentTime) >= 21)
{
CloseAllOrders();
fridayCloseExecuted = true;
return;
}
if(TimeDayOfWeek(currentTime) != 5)
fridayCloseExecuted = false;
}

// 时段过滤
if(UseSessionFilter && !IsValidTradingSession())
{
Comment("非交易时段");
return;
}

// 点差过滤
if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
{
Comment("当前点差过大:", MarketInfo(Symbol(), MODE_SPREAD));
return;
}

// 仅在新K线开始时检测入场(M15)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];

// 检查现有持仓
if(CountPositions() > 0)
return;

// 波动率过滤
if(!IsValidVolatility())
{
Comment("波动率超出范围");
return;
}

// RSI趋势对齐
double rsi = iRSI(Symbol(), PERIOD_M15, RSIPeriod, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_M15, 1);
double ema50 = iMA(Symbol(), PERIOD_M15, 50, 0, MODE_EMA, PRICE_CLOSE, 1);

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

// 做多条件:RSI > 50(上涨趋势)且看涨动量爆发
if(rsi > 50 && close1 > ema50 && IsMomentumSurge(1))
{
cmd = OP_BUY;
sl = bid - StopLossPoints * Point;
tp = bid + TakeProfitPoints * Point;
}
// 做空条件:RSI < 50(下跌趋势)且看跌动量爆发
else if(rsi < 50 && close1 < ema50 && IsMomentumSurge(-1))
{
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), 5, sl, tp, "Gold Momentum Guard", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("开仓失败,错误码:", GetLastError());
}
}

//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
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;
}

//+------------------------------------------------------------------+
//| 平仓当前品种下所有属于该EA的订单 |
//+------------------------------------------------------------------+
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);
}
}
}
}
//+------------------------------------------------------------------+
```
参考来源: 参考机构级EA设计理念,包括动量过滤、时段交易和波动率感知风险管理。
免责声明: 黄金交易具有重大风险。本EA按“原样”提供,仅供学习参考。实盘交易前请在模拟账户充分测试。历史表现不代表未来结果。
```