Summary: 黄金宏观对齐EA是一款专为XAUUSD设计的MQL4智能交易系统。对齐H1/H4时间框架趋势,使用ATR动态止损和自适应追踪止损,适合H1周期稳定运行。




黄金宏观对齐EA专为XAUUSD设计,通过聚焦宏观趋势对齐实现稳定的长期运行。与激进的系统不同,本EA要求多个时间框架(H1和H4 EMA)共同确认趋势,从而优先选择高质量的交易机会。它包含基于ATR的波动率过滤以避免在市场噪音过大时交易,动态ATR止损适应黄金当前的波动特性,以及自适应追踪止损在趋势发展中锁定利润。EA包含每日净值保护、点差控制和周五平仓机制,规避周末缺口风险。

推荐加载周期: H1
策略核心逻辑:
1. 宏观趋势对齐:H4 EMA(200)定义主要趋势方向;H1 EMA(50)必须同向。
2. 动量确认:RSI(14)必须确认动量(做多>50,做空<50)。
3. 入场触发:趋势对齐后,价格收盘突破近期20周期高低点。
4. 波动率保护:当前ATR(14)不得超过20周期平均ATR的1.8倍。
5. 风险管理:动态止损为1.5倍ATR,止盈为3倍ATR。盈利达到1.5倍ATR后启动自适应追踪止损。

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

//--- 输入参数及注释
input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
input int H4TrendPeriod = 200; // H4 EMA周期(宏观趋势)
input int H1TrendPeriod = 50; // H1 EMA周期(趋势对齐)
input int BreakoutPeriod = 20; // 突破高低点回溯周期
input int RSIPeriod = 14; // RSI周期(动量过滤)
input int ATRPeriod = 14; // ATR周期(波动率管理)
input double ATRMaxMultiplier = 1.8; // 最大ATR倍数(波动率保护)
input double ATRStopMultiplier = 1.5; // 止损倍数(ATR倍数)
input double ATRTakeMultiplier = 3.0; // 止盈倍数(ATR倍数)
input double TrailingStartATR = 1.5; // 追踪止损启动(盈利达到x倍ATR)
input double TrailingStepATR = 0.75; // 追踪步长(ATR倍数)
input int MagicNumber = 202421; // 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;

//+------------------------------------------------------------------+
//| 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("");
}

//+------------------------------------------------------------------+
//| 获取H4宏观趋势方向(EMA200) |
//+------------------------------------------------------------------+
int GetMacroTrend()
{
double closeH4 = iClose(Symbol(), PERIOD_H4, 1);
double emaH4 = iMA(Symbol(), PERIOD_H4, H4TrendPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
if(closeH4 > emaH4) return 1; // 看涨
if(closeH4 < emaH4) return -1; // 看跌
return 0;
}

//+------------------------------------------------------------------+
//| 检查H1趋势是否与宏观趋势对齐 |
//+------------------------------------------------------------------+
bool IsTrendAligned(int macroTrend)
{
if(macroTrend == 0) return false;
double closeH1 = iClose(Symbol(), PERIOD_H1, 1);
double emaH1 = iMA(Symbol(), PERIOD_H1, H1TrendPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);

if(macroTrend == 1) return (closeH1 > emaH1);
else return (closeH1 < emaH1);
}

//+------------------------------------------------------------------+
//| 检查区间突破入场条件 |
//+------------------------------------------------------------------+
bool CheckBreakoutEntry(int direction, double &entryPrice, double &sl, double &tp, double atr)
{
if(direction == 0) return false;

double highBreakout = iHigh(Symbol(), PERIOD_H1, iHighest(Symbol(), PERIOD_H1, MODE_HIGH, BreakoutPeriod, 2));
double lowBreakout = iLow(Symbol(), PERIOD_H1, iLowest(Symbol(), PERIOD_H1, MODE_LOW, BreakoutPeriod, 2));
double close1 = iClose(Symbol(), PERIOD_H1, 1);
double open1 = iOpen(Symbol(), PERIOD_H1, 1);

// 向上突破:收盘价高于区间高点且收阳线
if(direction == 1 && close1 > highBreakout && close1 > open1)
{
entryPrice = Ask;
sl = entryPrice - (atr * ATRStopMultiplier);
tp = entryPrice + (atr * ATRTakeMultiplier);
return true;
}
// 向下突破:收盘价低于区间低点且收阴线
else if(direction == -1 && close1 < lowBreakout && close1 < open1)
{
entryPrice = Bid;
sl = entryPrice + (atr * ATRStopMultiplier);
tp = entryPrice - (atr * ATRTakeMultiplier);
return true;
}
return false;
}

//+------------------------------------------------------------------+
//| 管理自适应追踪止损 |
//+------------------------------------------------------------------+
void ManageAdaptiveTrailing(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);
}
}
break;
}
}
}
}

//+------------------------------------------------------------------+
//| 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];

// 管理现有持仓
int posCount = CountPositions();
if(posCount > 0)
{
double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(atr > 0) ManageAdaptiveTrailing(atr);
return;
}

// 获取指标
double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);
double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);

// 更新平均ATR用于波动率过滤
if(atr > 0) avgATR = (avgATR * 0.95) + (atr * 0.05);

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

// 获取宏观趋势方向
int macroTrend = GetMacroTrend();
if(macroTrend == 0)
{
Comment("H4无明确宏观趋势方向");
return;
}

// 检查H1趋势对齐
if(!IsTrendAligned(macroTrend))
{
Comment("H1趋势与宏观趋势方向不一致");
return;
}

// RSI动量过滤
if(macroTrend == 1 && rsi < 50)
{
Comment("RSI低于50,无上涨动能");
return;
}
if(macroTrend == -1 && rsi > 50)
{
Comment("RSI高于50,无下跌动能");
return;
}

// 检查突破入场
double entryPrice = 0, sl = 0, tp = 0;
if(CheckBreakoutEntry(macroTrend, entryPrice, sl, tp, atr))
{
int cmd = (macroTrend == 1) ? OP_BUY : OP_SELL;
int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Gold Macro Align", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("开仓失败,错误码:", GetLastError());
else
Print("突破开仓成功,方向:", cmd==OP_BUY?"买入":"卖出");
}
}

//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
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代码,策略架构参考了2025-2026年黄金EA的多时间框架宏观对齐原理。
免责声明: 黄金交易因高波动性具有重大风险。本EA按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
```