Summary: 黄金冰川EA是一款专为XAUUSD设计的MQL4智能交易系统。结合多时间框架结构方向确认、ATR动态止损和自适应风险管理,适合H1周期稳定运行。




黄金冰川EA专为黄金稳定交易而设计,采用结构分析方法,模仿机构订单流检测逻辑。EA在考虑入场前,先识别更高时间框架(H4和H1)的关键结构突破,确保交易顺应宏观方向。独特的“市场记忆”模块追踪连续亏损次数,并相应调整暂停计时器。EA使用基于ATR的动态止损,在高波动时放宽止损,波动减弱时收紧止损。所有交易均包含固定止盈目标,并在达到最低盈利后启动追踪止损。

推荐加载周期: H1
策略核心逻辑:
1. 结构方向:H4 EMA(200)提供宏观方向偏好。H1 EMA(50)必须同向才考虑入场。
2. 突破检测:识别最近20根K线的波段高低点。价格必须收盘超越这些水平。
3. 趋势强度过滤:ADX(14)必须超过23,确保市场处于趋势状态。
4. 波动率保护:当前ATR(14)不得超过50周期平均ATR的2.0倍。
5. 风险管理:固定止损1.6倍ATR(14),止盈2.8倍ATR。盈利达到1.2倍ATR后启动追踪止损。
6. 市场记忆:连续亏损达到设定次数后自动暂停交易指定时长。

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

//--- 输入参数及注释
input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
input int H4BiasPeriod = 200; // H4 EMA周期(宏观方向)
input int H1TrendPeriod = 50; // H1 EMA周期(趋势对齐)
input int StructureLookback = 20; // 结构高低点回溯K线数
input int ADXPeriod = 14; // ADX周期(趋势强度)
input int ADXThreshold = 23; // 最小ADX阈值(趋势市场要求)
input int ATRPeriod = 14; // ATR波动率周期
input double ATRMaxMultiplier = 2.0; // 最大ATR倍数(波动率保护)
input double ATRStopMultiplier = 1.6; // 止损倍数(ATR倍数)
input double ATRTakeMultiplier = 2.8; // 止盈倍数(ATR倍数)
input double TrailingStartATR = 1.2; // 追踪止损启动(盈利达到x倍ATR)
input double TrailingStepATR = 0.5; // 追踪步长(ATR倍数)
input int MaxConsecutiveLosses = 3; // 最大连续亏损次数(超限后暂停)
input int PauseMinutes = 60; // 亏损后暂停时长(分钟)
input int MagicNumber = 202512; // 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 structureHigh = 0;
double structureLow = 0;
int consecutiveLosses = 0;
datetime lastLossTime = 0;

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

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

//+------------------------------------------------------------------+
//| 获取H4时间框架结构方向 |
//+------------------------------------------------------------------+
int GetMacroBias()
{
double closeH4 = iClose(Symbol(), PERIOD_H4, 1);
double emaH4 = iMA(Symbol(), PERIOD_H4, H4BiasPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
if(closeH4 > emaH4) return 1;
if(closeH4 < emaH4) return -1;
return 0;
}

//+------------------------------------------------------------------+
//| 检查H1趋势是否与宏观方向一致 |
//+------------------------------------------------------------------+
bool IsH1Aligned(int macroBias)
{
if(macroBias == 0) return false;
double closeH1 = iClose(Symbol(), PERIOD_H1, 1);
double emaH1 = iMA(Symbol(), PERIOD_H1, H1TrendPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);

if(macroBias == 1) return (closeH1 > emaH1);
if(macroBias == -1) return (closeH1 < emaH1);
return false;
}

//+------------------------------------------------------------------+
//| 检测结构突破水平(波段高低点) |
//+------------------------------------------------------------------+
void DetectStructureLevels()
{
int highestIdx = iHighest(Symbol(), PERIOD_H1, MODE_HIGH, StructureLookback, 1);
int lowestIdx = iLowest(Symbol(), PERIOD_H1, MODE_LOW, StructureLookback, 1);

if(highestIdx > 0)
structureHigh = iHigh(Symbol(), PERIOD_H1, highestIdx);
if(lowestIdx > 0)
structureLow = iLow(Symbol(), PERIOD_H1, lowestIdx);
}

//+------------------------------------------------------------------+
//| 检查ADX趋势强度 |
//+------------------------------------------------------------------+
bool IsTrendStrong()
{
double adx = iADX(Symbol(), PERIOD_H1, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
return (adx >= ADXThreshold);
}

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

double close1 = iClose(Symbol(), PERIOD_H1, 1);

// 向上突破:收盘价高于结构高点,且看涨动量
if(direction == 1 && structureHigh > 0)
{
if(close1 > structureHigh)
{
entryPrice = Ask;
sl = entryPrice - (atr * ATRStopMultiplier);
tp = entryPrice + (atr * ATRTakeMultiplier);
return true;
}
}
// 向下突破:收盘价低于结构低点,且看跌动量
else if(direction == -1 && structureLow > 0)
{
if(close1 < structureLow)
{
entryPrice = Bid;
sl = entryPrice + (atr * ATRStopMultiplier);
tp = entryPrice - (atr * ATRTakeMultiplier);
return true;
}
}
return false;
}

//+------------------------------------------------------------------+
//| 追踪连续亏损(市场记忆功能) |
//+------------------------------------------------------------------+
void TrackTradeResult()
{
static int prevTotalOrders = 0;
static double prevEquity = 0;

int currentOrders = CountPositions();
double currentEquity = AccountEquity();

// 检测交易刚刚平仓
if(prevTotalOrders > 0 && currentOrders == 0)
{
double equityChange = currentEquity - prevEquity;
if(equityChange < -0.01) // 亏损
{
consecutiveLosses++;
lastLossTime = TimeCurrent();
}
else if(equityChange > 0.01) // 盈利
{
consecutiveLosses = 0;
}
}

if(currentOrders > 0)
prevEquity = currentEquity;

prevTotalOrders = currentOrders;
}

//+------------------------------------------------------------------+
//| 管理持仓的追踪止损 |
//+------------------------------------------------------------------+
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);
}
}
break;
}
}
}
}

//+------------------------------------------------------------------+
//| EA主循环函数(每Tick执行) |
//+------------------------------------------------------------------+
void OnTick()
{
// 追踪交易结果(市场记忆)
TrackTradeResult();

// 市场记忆:连续亏损后暂停
if(consecutiveLosses >= MaxConsecutiveLosses)
{
if(TimeCurrent() - lastLossTime < PauseMinutes * 60)
{
Comment("市场记忆激活:连续", consecutiveLosses, "次亏损,暂停 ",
(PauseMinutes * 60 - (TimeCurrent() - lastLossTime)) / 60, " 分钟");
return;
}
else
{
consecutiveLosses = 0;
}
}

// 每日净值保护
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];

// 更新结构水平
DetectStructureLevels();

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

// 获取ATR用于波动率过滤
double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(atr > 0) avgATR = (avgATR * 0.95) + (atr * 0.05);

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

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

// 获取H4宏观方向
int macroBias = GetMacroBias();
if(macroBias == 0)
{
Comment("H4无明确宏观方向");
return;
}

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

// 检查结构突破
double entryPrice = 0, sl = 0, tp = 0;
if(CheckStructuralBreakout(macroBias, entryPrice, sl, tp, atr))
{
int cmd = (macroBias == 1) ? OP_BUY : OP_SELL;
int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Gold Glacier", 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的结构化原理,包括Gold EA Macro Trend v1的多时间框架对齐逻辑和TRetBO Pro的无敌模式思想。
免责声明: 黄金交易因高波动性具有重大风险。本EA按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
```