Summary: 黄金顶点EA是一款专为XAUUSD设计的MQL4智能交易系统。结合ADX趋势强度检测、RSI动量过滤和结构化逆势网格层,配合ATR动态止损,适合H1周期稳定运行。




黄金顶点EA专为黄金(XAUUSD)设计,基于对2026年上半年专业商业EA的分析。与纯马丁格尔或简单趋势跟踪系统不同,本EA采用混合方法:首先使用ADX(趋势强度)和RSI(动量)识别趋势方向,然后入场主趋势交易。一个受控的、有限层级的逆势网格机制仅在特定回撤条件满足时激活,但严格限制层数以防止失控亏损。所有仓位都受到基于ATR的动态止损和整体止盈系统保护。包含每日净值保护、点差控制和周五平仓机制。

推荐加载周期: H1
策略核心逻辑:
1. 主趋势过滤:ADX(14) > 25确认趋势市场。EMA(50)判断方向。
2. 动量确认:做多要求RSI(14) > 55,做空要求 < 45。
3. 主趋势入场:价格回撤至EMA(50)并出现确认性K线形态。
4. 受控网格层(逆势):最多额外2个仓位,放置于平均入场价1.5倍和2.0倍ATR距离处。固定手数(无递进)。仅当主仓位达到-1.2倍ATR亏损后激活。
5. 风险管理:基于ATR的止损(1.8倍ATR),所有持仓整体止盈目标2.5倍ATR,盈利达到1.0倍ATR后启动追踪止损。

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

//--- 输入参数及注释
input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
input int TrendEMAPeriod = 50; // EMA周期(趋势方向)
input int ADXPeriod = 14; // ADX周期(趋势强度)
input int ADXThreshold = 25; // 最小ADX阈值(趋势市场要求)
input int RSIPeriod = 14; // RSI周期(动量过滤)
input int RSIUpperThreshold = 55; // RSI做多最小阈值
input int RSILowerThreshold = 45; // RSI做空最大阈值
input int ATRPeriod = 14; // ATR周期(波动率)
input double ATRStopMultiplier = 1.8; // 止损倍数(ATR倍数)
input double ATRTakeMultiplier = 2.5; // 止盈倍数(ATR倍数)
input double GridTriggerATR = 1.2; // 网格激活阈值(亏损达到x倍ATR时触发)
input double GridDistanceATR = 1.5; // 网格距离(ATR倍数)
input int MaxGridLevels = 2; // 最大逆势网格层数
input double TrailingStartATR = 1.0; // 追踪止损启动(盈利达到x倍ATR)
input double TrailingStepATR = 0.5; // 追踪步长(ATR倍数)
input int MagicNumber = 202423; // EA魔术号
input int MaxSpread = 35; // 最大允许点差(黄金单位:点)
input double DailyLossLimit = 5.0; // 每日亏损限额(账户余额百分比)
input bool UseFridayClose = true; // 周五21:00 GMT前平仓

//--- 全局变量
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool fridayCloseExecuted = false;
double avgATR = 0;
double avgEntryPrice = 0;
int primaryDirection = 0;
datetime gridActivationTime = 0;

//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
fridayCloseExecuted = false;
avgATR = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(avgATR <= 0) avgATR = 250 * Point;
return(INIT_SUCCEEDED);
}

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

//+------------------------------------------------------------------+
//| 使用ADX检查市场是否为趋势市 |
//+------------------------------------------------------------------+
bool IsTrending()
{
double adx = iADX(Symbol(), PERIOD_H1, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
return (adx >= ADXThreshold);
}

//+------------------------------------------------------------------+
//| 使用EMA获取趋势方向 |
//+------------------------------------------------------------------+
int GetTrendDirection()
{
double close1 = iClose(Symbol(), PERIOD_H1, 1);
double ema = iMA(Symbol(), PERIOD_H1, TrendEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
if(close1 > ema) return 1;
if(close1 < ema) return -1;
return 0;
}

//+------------------------------------------------------------------+
//| 计算所有开仓的平均入场价 |
//+------------------------------------------------------------------+
double CalculateAvgEntryPrice()
{
double totalPrice = 0;
int totalOrders = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
totalPrice += OrderOpenPrice();
totalOrders++;
}
}
}
if(totalOrders == 0) return 0;
return totalPrice / totalOrders;
}

//+------------------------------------------------------------------+
//| 计算当前浮动亏损(以ATR为单位) |
//+------------------------------------------------------------------+
double CalculateDrawdownATR(double atr)
{
if(atr <= 0) return 0;
double avgPrice = CalculateAvgEntryPrice();
if(avgPrice == 0) return 0;

double currentPrice = (primaryDirection == 1) ? Bid : Ask;
double drawdown = 0;

if(primaryDirection == 1 && currentPrice < avgPrice)
drawdown = (avgPrice - currentPrice) / atr;
else if(primaryDirection == -1 && currentPrice > avgPrice)
drawdown = (currentPrice - avgPrice) / atr;

return drawdown;
}

//+------------------------------------------------------------------+
//| 检查主趋势入场条件(趋势回撤) |
//+------------------------------------------------------------------+
bool CheckPrimaryEntry(int direction, double &entryPrice, double &sl, double &tp, double atr)
{
if(direction == 0) return false;

double ema = iMA(Symbol(), PERIOD_H1, TrendEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H1, 1);
double low1 = iLow(Symbol(), PERIOD_H1, 1);
double high1 = iHigh(Symbol(), PERIOD_H1, 1);
double open1 = iOpen(Symbol(), PERIOD_H1, 1);
double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);

if(direction == 1) // 做多
{
bool pulledToEMA = (low1 <= ema && close1 > ema);
bool bullishCandle = (close1 > open1);
bool rsiOk = (rsi >= RSIUpperThreshold);

if(pulledToEMA && bullishCandle && rsiOk)
{
entryPrice = Ask;
sl = entryPrice - (atr * ATRStopMultiplier);
tp = entryPrice + (atr * ATRTakeMultiplier);
return true;
}
}
else if(direction == -1) // 做空
{
bool pulledToEMA = (high1 >= ema && close1 < ema);
bool bearishCandle = (close1 < open1);
bool rsiOk = (rsi <= RSILowerThreshold);

if(pulledToEMA && bearishCandle && rsiOk)
{
entryPrice = Bid;
sl = entryPrice + (atr * ATRStopMultiplier);
tp = entryPrice - (atr * ATRTakeMultiplier);
return true;
}
}
return false;
}

//+------------------------------------------------------------------+
//| 检查网格入场条件(逆势回血) |
//+------------------------------------------------------------------+
bool CheckGridEntry(int direction, double &entryPrice, double &sl, double &tp, double atr)
{
if(direction == 0) return false;
if(CalculateAvgEntryPrice() == 0) return false;

double avgPrice = CalculateAvgEntryPrice();
double currentPrice = (direction == 1) ? Ask : Bid;
double priceDiff = MathAbs(currentPrice - avgPrice);
double requiredDiff = atr * GridDistanceATR;

// 仅当回撤超过阈值且未达最大层数时添加网格仓位
double drawdownATR = CalculateDrawdownATR(atr);
if(drawdownATR >= GridTriggerATR)
{
if(direction == 1 && currentPrice < avgPrice - requiredDiff)
{
entryPrice = Ask;
sl = entryPrice - (atr * ATRStopMultiplier);
tp = entryPrice + (atr * ATRTakeMultiplier);
return true;
}
else if(direction == -1 && currentPrice > avgPrice + requiredDiff)
{
entryPrice = Bid;
sl = entryPrice + (atr * ATRStopMultiplier);
tp = entryPrice - (atr * ATRTakeMultiplier);
return true;
}
}
return false;
}

//+------------------------------------------------------------------+
//| 计算整体持仓浮动盈亏 |
//+------------------------------------------------------------------+
double CalculateBasketProfit()
{
double totalProfit = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
totalProfit += OrderProfit() + OrderSwap() + OrderCommission();
}
}
}
return totalProfit;
}

//+------------------------------------------------------------------+
//| 检查整体止盈 - 浮动盈亏为正时平仓 |
//+------------------------------------------------------------------+
bool CheckBasketTakeProfit(double atr)
{
double basketProfit = CalculateBasketProfit();
double avgPrice = CalculateAvgEntryPrice();
if(avgPrice == 0) return false;

double currentPrice = (primaryDirection == 1) ? Bid : Ask;
double move = MathAbs(currentPrice - avgPrice);
double targetMove = atr * ATRTakeMultiplier;

if(basketProfit > 0 && move >= targetMove * 0.6) // 提前止盈
{
CloseAllOrders();
Print("整体止盈触发,盈利:", basketProfit);
return true;
}
else if(basketProfit > 5.0) // 最小盈利阈值
{
CloseAllOrders();
Print("整体止盈触发(正盈利),盈利:", basketProfit);
return true;
}
return false;
}

//+------------------------------------------------------------------+
//| 管理所有持仓的追踪止损 |
//+------------------------------------------------------------------+
void ManageTrailingStop(double atr)
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
double activate = atr * TrailingStartATR;
double step = atr * TrailingStepATR;
double newSL = 0;

if(OrderType() == OP_BUY)
{
double profit = Bid - OrderOpenPrice();
if(profit >= activate)
{
newSL = Bid - step;
if(newSL > OrderStopLoss())
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
else if(OrderType() == OP_SELL)
{
double profit = OrderOpenPrice() - Ask;
if(profit >= activate)
{
newSL = Ask + step;
if(newSL < OrderStopLoss() || OrderStopLoss() == 0)
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
}
}
}
}

//+------------------------------------------------------------------+
//| 更新追踪变量(平均ATR和主方向) |
//+------------------------------------------------------------------+
void UpdateTracking(double atr)
{
if(atr > 0) avgATR = (avgATR * 0.95) + (atr * 0.05);

int posCount = CountPositions();
if(posCount > 0 && primaryDirection == 0)
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
primaryDirection = (OrderType() == OP_BUY) ? 1 : -1;
break;
}
}
}
}
else if(posCount == 0)
{
primaryDirection = 0;
}
}

//+------------------------------------------------------------------+
//| 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(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
{
Comment("当前点差过大:", MarketInfo(Symbol(), MODE_SPREAD));
return;
}

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

// 获取ATR
double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(atr <= 0) atr = avgATR;

// 更新追踪变量
UpdateTracking(atr);

// 管理现有持仓
int posCount = CountPositions();
if(posCount > 0)
{
// 检查整体止盈
if(CheckBasketTakeProfit(atr))
return;

// 管理追踪止损
ManageTrailingStop(atr);

// 检查网格入场(追加仓位)
if(posCount < MaxGridLevels + 1 && primaryDirection != 0)
{
double entryPrice = 0, sl = 0, tp = 0;
if(CheckGridEntry(primaryDirection, entryPrice, sl, tp, atr))
{
int cmd = (primaryDirection == 1) ? OP_BUY : OP_SELL;
int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Apex Grid", MagicNumber, 0, clrNONE);
if(ticket > 0)
Print("网格层已添加,方向:", cmd==OP_BUY?"买入":"卖出");
}
}
return;
}

// --- 无持仓 - 检查主趋势入场 ---

// 检查趋势市场
if(!IsTrending())
{
Comment("ADX低于阈值,无明确趋势");
return;
}

// 获取趋势方向
int trendDir = GetTrendDirection();
if(trendDir == 0)
{
Comment("无明确趋势方向");
return;
}

// 波动率保护
if(atr > avgATR * 2.0 && avgATR > 0)
{
Comment("当前波动率过高,ATR: ", atr);
return;
}

// 检查主趋势入场
double entryPrice = 0, sl = 0, tp = 0;
if(CheckPrimaryEntry(trendDir, entryPrice, sl, tp, atr))
{
int cmd = (trendDir == 1) ? OP_BUY : OP_SELL;
int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Apex Primary", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("开仓失败,错误码:", GetLastError());
else
{
Print("主趋势开仓成功,方向:", cmd==OP_BUY?"买入":"卖出");
primaryDirection = trendDir;
}
}
}

//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
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);
}
}
}
}
//+------------------------------------------------------------------+
```
参考来源: 原创MQL4代码,策略架构参考了2026年上半年发布的多款商业黄金EA的逻辑结构与设计理念。
免责声明: 黄金交易因高波动性具有重大风险。本EA按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
```