Summary: 黄金量化锋刃EA是一款专为XAUUSD设计的MQL4智能交易系统。采用多层趋势确认、顺势结构化网格加仓、动态ATR止盈和资金保护机制,适合M15周期。
黄金量化锋刃EA是一款专为黄金(XAUUSD)研发的专业级智能交易系统。与盲目双向开仓的传统网格EA不同,本EA采用趋势确认网格策略——仅在主趋势方向上建立仓位,通过多时间框架EMA对齐和ADX强度过滤确认趋势方向。EA使用动态止盈系统,根据ATR波动率自动调整止盈目标。专有的反转保护机制持续监控价格结构偏差,当趋势结构被破坏时立即平仓所有订单,防止突发反转导致重大回撤。
推荐加载周期: M15
策略核心逻辑:
1. 多层趋势确认:H4 EMA(50)确立宏观趋势,H1 EMA(20)确认中期方向,ADX(14) > 25确保趋势强度。
2. 结构化网格入场:首单在回调至快EMA时入场,后续网格层级按固定ATR间距(1.5倍ATR)仅在趋势方向上加仓。
3. 动态止盈目标:止盈 = ATR × 倍数(默认3.0),根据波动率状态自动调整。
4. 反转保护:独立价格结构监控——若价格突破近期摆动低点/高点超过2倍ATR,立即平仓所有订单。
5. 风险管理:每日亏损上限5%,最大连续亏损3次,基于余额百分比的头寸规模上限。
```mql4
//+------------------------------------------------------------------+
//| GoldQuantEdgeEA.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
//--- 输入参数及注释
input double BaseLotSize = 0.01; // 基础交易手数(黄金0.01手)
input int TrendEMAFast = 20; // H1快EMA周期(趋势确认)
input int TrendEMASlow = 50; // H4慢EMA周期(宏观趋势)
input int ADXPeriod = 14; // ADX周期(趋势强度)
input int ADXThreshold = 25; // ADX最小阈值(25以上才视为有趋势)
input double GridDistanceATR = 1.5; // 网格间距(ATR倍数)
input double LotMultiplier = 1.5; // 网格层级手数倍数
input int MaxGridLevels = 3; // 单方向最大网格层数
input double TakeProfitATR = 3.0; // 止盈倍数(ATR倍数)
input double ReversalATR = 2.0; // 反转检测倍数(ATR倍数)
input int ATRPeriod = 14; // ATR周期(波动率计算)
input double DailyLossLimit = 5.0; // 每日亏损限额(百分比)
input int MaxConsecutiveLoss = 3; // 最大连续亏损次数(超过后暂停)
input double MaxTotalRiskPercent = 8.0; // 最大总风险(净值百分比)
input int MagicNumber = 202420; // EA魔术号
input int MaxSpread = 35; // 最大允许点差(黄金单位:点)
input bool UseFridayClose = true; // 周五21:00 GMT前平仓
//--- 全局变量
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool fridayCloseExecuted = false;
double avgATR = 0;
int consecutiveLosses = 0;
datetime lastTradeTime = 0;
double currentEquityProtection = 0;
//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
fridayCloseExecuted = false;
consecutiveLosses = 0;
avgATR = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
if(avgATR <= 0) avgATR = 200 * Point;
currentEquityProtection = AccountEquity() * (1 - DailyLossLimit / 100);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| EA退出函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| 获取H4主趋势方向 |
//+------------------------------------------------------------------+
int GetPrimaryTrend()
{
double closeH4 = iClose(Symbol(), PERIOD_H4, 1);
double emaH4 = iMA(Symbol(), PERIOD_H4, TrendEMASlow, 0, MODE_EMA, PRICE_CLOSE, 1);
if(closeH4 > emaH4) return 1;
if(closeH4 < emaH4) return -1;
return 0;
}
//+------------------------------------------------------------------+
//| 获取H1次级趋势确认 |
//+------------------------------------------------------------------+
int GetSecondaryTrend()
{
double closeH1 = iClose(Symbol(), PERIOD_H1, 1);
double emaH1 = iMA(Symbol(), PERIOD_H1, TrendEMAFast, 0, MODE_EMA, PRICE_CLOSE, 1);
if(closeH1 > emaH1) return 1;
if(closeH1 < emaH1) return -1;
return 0;
}
//+------------------------------------------------------------------+
//| 检查ADX趋势强度 |
//+------------------------------------------------------------------+
bool IsTrendStrong()
{
double adx = iADX(Symbol(), PERIOD_H1, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
return (adx >= ADXThreshold);
}
//+------------------------------------------------------------------+
//| 基于净值和风险计算动态手数 |
//+------------------------------------------------------------------+
double CalculateLotSize()
{
double equity = AccountEquity();
double riskAmount = equity * (DailyLossLimit / 100) / MaxGridLevels;
double calculatedLot = riskAmount / (avgATR / Point * 0.1);
calculatedLot = MathMin(calculatedLot, BaseLotSize * MathPow(LotMultiplier, MaxGridLevels));
if(calculatedLot < BaseLotSize) calculatedLot = BaseLotSize;
if(calculatedLot > 1.0) calculatedLot = 1.0;
return NormalizeDouble(calculatedLot, 2);
}
//+------------------------------------------------------------------+
//| 检查反转保护条件 |
//+------------------------------------------------------------------+
bool IsReversalDetected(int direction)
{
double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
if(atr <= 0) atr = avgATR;
if(direction == 1) // 多单持仓 - 检查向下突破
{
double lowestLow = iLow(Symbol(), PERIOD_H1, iLowest(Symbol(), PERIOD_H1, MODE_LOW, 10, 1));
double currentBid = Bid;
if(currentBid < lowestLow - (atr * ReversalATR))
return true;
}
else if(direction == -1) // 空单持仓 - 检查向上突破
{
double highestHigh = iHigh(Symbol(), PERIOD_H1, iHighest(Symbol(), PERIOD_H1, MODE_HIGH, 10, 1));
double currentAsk = Ask;
if(currentAsk > highestHigh + (atr * ReversalATR))
return true;
}
return false;
}
//+------------------------------------------------------------------+
//| 获取指定方向的当前网格层数 |
//+------------------------------------------------------------------+
int GetGridLevelCount(int direction)
{
int count = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if((direction == 1 && OrderType() == OP_BUY) ||
(direction == -1 && OrderType() == OP_SELL))
count++;
}
}
}
return count;
}
//+------------------------------------------------------------------+
//| 计算指定方向持仓的平均价格 |
//+------------------------------------------------------------------+
double GetAveragePrice(int direction)
{
double totalPrice = 0;
int count = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if((direction == 1 && OrderType() == OP_BUY) ||
(direction == -1 && OrderType() == OP_SELL))
{
totalPrice += OrderOpenPrice();
count++;
}
}
}
}
if(count == 0) return 0;
return totalPrice / count;
}
//+------------------------------------------------------------------+
//| 检查篮子止盈条件 |
//+------------------------------------------------------------------+
bool CheckBasketTakeProfit(int direction)
{
double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
if(atr <= 0) atr = avgATR;
double avgPrice = GetAveragePrice(direction);
double tpDistance = atr * TakeProfitATR;
double currentPrice = (direction == 1) ? Bid : Ask;
if(direction == 1 && currentPrice >= avgPrice + tpDistance)
return true;
if(direction == -1 && currentPrice <= avgPrice - tpDistance)
return true;
return false;
}
//+------------------------------------------------------------------+
//| 平仓指定方向的所有订单 |
//+------------------------------------------------------------------+
void ClosePositionsInDirection(int direction)
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if((direction == 1 && OrderType() == OP_BUY) ||
(direction == -1 && OrderType() == OP_SELL))
{
if(OrderType() == OP_BUY)
OrderClose(OrderTicket(), OrderLots(), Bid, 5, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 5, clrNONE);
}
}
}
}
}
//+------------------------------------------------------------------+
//| 平仓当前品种下所有属于该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);
}
}
}
}
//+------------------------------------------------------------------+
//| 计算所有持仓的总浮动盈亏 |
//+------------------------------------------------------------------+
double GetTotalFloatingPnL()
{
double total = 0;
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
total += OrderProfit() + OrderSwap() + OrderCommission();
}
}
return total;
}
//+------------------------------------------------------------------+
//| EA主循环函数(每Tick执行) |
//+------------------------------------------------------------------+
void OnTick()
{
// 每日净值保护
double currentEquity = AccountEquity();
if(currentEquity <= currentEquityProtection)
{
Comment("已达每日亏损上限,停止开新仓");
if(CountPositions() > 0) CloseAllOrders();
return;
}
// 连续亏损保护
if(consecutiveLosses >= MaxConsecutiveLoss)
{
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线开始时检测入场(M15)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];
// 更新平均ATR
double atr = iATR(Symbol(), PERIOD_M15, ATRPeriod, 1);
if(atr > 0) avgATR = (avgATR * 0.95) + (atr * 0.05);
// 获取趋势信号
int primaryTrend = GetPrimaryTrend();
int secondaryTrend = GetSecondaryTrend();
bool trendStrong = IsTrendStrong();
// 有效趋势条件:主趋势与次级趋势一致,ADX确认有趋势
bool validTrend = (primaryTrend != 0 && primaryTrend == secondaryTrend && trendStrong);
if(!validTrend)
{
Comment("无有效趋势方向。主趋势: ", primaryTrend, " 次级趋势: ", secondaryTrend, " ADX强度: ", trendStrong);
return;
}
int direction = primaryTrend; // 1=做多, -1=做空
int currentLevels = GetGridLevelCount(direction);
// 检查现有持仓的反转保护
if(currentLevels > 0)
{
if(IsReversalDetected(direction))
{
ClosePositionsInDirection(direction);
Print("反转保护触发,所有持仓已平仓");
return;
}
// 检查篮子止盈
if(CheckBasketTakeProfit(direction))
{
ClosePositionsInDirection(direction);
Print("篮子止盈触发,持仓已平仓");
return;
}
// 检查是否需要添加网格层级
if(currentLevels < MaxGridLevels)
{
double avgPrice = GetAveragePrice(direction);
double currentPrice = (direction == 1) ? Ask : Bid;
double distanceRequired = avgATR * GridDistanceATR * currentLevels;
if(MathAbs(currentPrice - avgPrice) >= distanceRequired)
{
double newLot = BaseLotSize * MathPow(LotMultiplier, currentLevels);
newLot = MathMin(newLot, 0.5);
int cmd = (direction == 1) ? OP_BUY : OP_SELL;
double entryPrice = (direction == 1) ? Ask : Bid;
double sl = 0, tp = 0;
if(direction == 1)
{
sl = entryPrice - (avgATR * 1.2);
tp = entryPrice + (avgATR * TakeProfitATR);
}
else
{
sl = entryPrice + (avgATR * 1.2);
tp = entryPrice - (avgATR * TakeProfitATR);
}
int ticket = OrderSend(Symbol(), cmd, newLot, entryPrice, 5, sl, tp, "QuantGrid", MagicNumber, 0, clrNONE);
if(ticket > 0)
Print("网格第 ", currentLevels + 1, " 层已开仓,手数:", newLot);
}
}
}
else
{
// 无持仓 - 等待回调入场
double emaFast = iMA(Symbol(), PERIOD_H1, TrendEMAFast, 0, MODE_EMA, PRICE_CLOSE, 1);
double closeH1 = iClose(Symbol(), PERIOD_H1, 1);
double rsi = iRSI(Symbol(), PERIOD_H1, 14, PRICE_CLOSE, 1);
bool pullbackCondition = false;
if(direction == 1) // 做多:价格回撤至快EMA附近或以下
{
double low1 = iLow(Symbol(), PERIOD_H1, 1);
pullbackCondition = (low1 <= emaFast && closeH1 > emaFast && rsi > 45);
}
else if(direction == -1) // 做空:价格回抽至快EMA附近或以上
{
double high1 = iHigh(Symbol(), PERIOD_H1, 1);
pullbackCondition = (high1 >= emaFast && closeH1 < emaFast && rsi < 55);
}
if(pullbackCondition)
{
double lotSize = CalculateLotSize();
int cmd = (direction == 1) ? OP_BUY : OP_SELL;
double entryPrice = (direction == 1) ? Ask : Bid;
double sl = 0, tp = 0;
if(direction == 1)
{
sl = entryPrice - (avgATR * 1.2);
tp = entryPrice + (avgATR * TakeProfitATR);
}
else
{
sl = entryPrice + (avgATR * 1.2);
tp = entryPrice - (avgATR * TakeProfitATR);
}
int ticket = OrderSend(Symbol(), cmd, lotSize, entryPrice, 5, sl, tp, "QuantInit", MagicNumber, 0, clrNONE);
if(ticket > 0)
{
Print("首仓已开。方向:", direction==1?"买入":"卖出", " 手数:", lotSize);
lastTradeTime = TimeCurrent();
}
}
}
}
//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
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;
}
//+------------------------------------------------------------------+
//| 记录交易结果(平仓后调用) |
//+------------------------------------------------------------------+
void RecordTradeResult(double profit)
{
if(profit <= 0)
consecutiveLosses++;
else
consecutiveLosses = 0;
}
//+------------------------------------------------------------------+
```
参考来源: 原创MQL4代码,基于对专业黄金交易系统的结构分析及2026年3月市场验证版本的设计理念。
免责声明: 黄金交易因高波动性具有重大风险。本EA按“原样”提供,不保证盈利。网格系统在强趋势单边行情中可能放大亏损。实盘部署前请在模拟账户测试至少2个月。历史表现不代表未来结果。
```