Summary: 黄金融合EA是一款专为XAUUSD设计的低风险MQL4智能交易系统。使用H4趋势过滤、H1入场信号和自适应ATR止损,适合黄金的高波动特性。建议加载H1图表。
黄金融合EA专为黄金(XAUUSD)设计,通过适应黄金的高波动和频繁反转特性,实现低风险、稳定收益。EA采用多周期方法:H4图表确定主趋势方向(EMA50),H1图表寻找回调入场信号(RSI背离+K线确认)。基于ATR的自适应止损和追踪止损在波动扩大时锁定利润。EA还包含点差保护、每日净值回撤限制和周五平仓功能,规避周末跳空风险。
推荐加载周期: H1(图表加载在H1,EA会自动读取H4数据)
策略核心逻辑:
1. 趋势过滤(H4):H4周期EMA50定义多空方向。价格高于EMA只做多,低于EMA只做空。
2. 波动率状态(H1):H1周期ATR(14)必须在20周期平均ATR的0.8~1.6倍区间内,超出则不开仓。
3. 入场信号(H1):H1周期RSI(14)顶底背离 + 吞没K线形态确认。
4. 风险管理:固定止损250点或1.2倍H1 ATR(取较大值)。盈利350点后启动追踪止损,步长80点。每日亏损上限4%,最大点差35点。
```mql4
//+------------------------------------------------------------------+
//| GoldFusionEA.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| 输入参数及详细注释 |
//+------------------------------------------------------------------+
input double LotSize = 0.01; // 固定手数(建议0.01)
input int H4TrendMAPeriod = 50; // H4趋势过滤EMA周期
input int H1RSIPeriod = 14; // H1 RSI背离检测周期
input int H1ATRPeriod = 14; // H1 ATR波动率过滤周期
input double VolatilityMinRatio = 0.8; // 最小ATR比率(当前/平均)允许交易
input double VolatilityMaxRatio = 1.6; // 最大ATR比率(当前/平均)允许交易
input int StopLossPips = 250; // 固定止损点数(250点=25美元黄金)
input double StopLossATRMultiplier = 1.2; // 动态止损倍数(ATR × 此值,取固定/动态较大者)
input int TakeProfitPips = 500; // 止盈点数(50美元黄金)
input int TrailingStartPips = 350; // 追踪止损启动盈利(点数)
input int TrailingStepPips = 80; // 追踪步长(点数)
input double DailyLossLimit = 4.0; // 每日亏损限额(账户余额百分比)
input int MaxSpread = 35; // 最大允许点差(单位:点)
input bool CloseOnFriday = true; // 周五20:00 GMT前平仓
input int MagicNumber = 202419; // EA魔术号
//+------------------------------------------------------------------+
//| 全局变量 |
//+------------------------------------------------------------------+
double dailyStartBalance = 0;
datetime lastH1BarTime = 0;
bool fridayCloseDone = false;
double avgATR_H1 = 0;
//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastH1BarTime = 0;
fridayCloseDone = false;
avgATR_H1 = iATR(Symbol(), PERIOD_H1, H1ATRPeriod, 1);
if(avgATR_H1 <= 0) avgATR_H1 = 200 * Point;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| EA退出函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| 更新滚动平均ATR |
//+------------------------------------------------------------------+
double UpdateAvgATR()
{
double currentATR = iATR(Symbol(), PERIOD_H1, H1ATRPeriod, 1);
if(currentATR > 0)
avgATR_H1 = (avgATR_H1 * 0.95) + (currentATR * 0.05);
return currentATR;
}
//+------------------------------------------------------------------+
//| 检测RSI背离(看涨或看跌) |
//+------------------------------------------------------------------+
int DetectRSIDivergence()
{
// 获取H1最近3根K线的RSI和收盘价
double rsi[3];
double price[3];
for(int i = 1; i <= 3; i++)
{
rsi[i-1] = iRSI(Symbol(), PERIOD_H1, H1RSIPeriod, PRICE_CLOSE, i);
price[i-1] = iClose(Symbol(), PERIOD_H1, i);
}
// 看涨背离:价格创更低低点,RSI创更高低点
if(price[0] < price[1] && price[1] < price[2] && rsi[0] > rsi[1] && rsi[1] > rsi[2])
return 1;
// 看跌背离:价格创更高高点,RSI创更低高点
if(price[0] > price[1] && price[1] > price[2] && rsi[0] < rsi[1] && rsi[1] < rsi[2])
return -1;
return 0; // 无背离
}
//+------------------------------------------------------------------+
//| 检测H1吞没K线形态 |
//+------------------------------------------------------------------+
bool IsEngulfing(int direction)
{
double open1 = iOpen(Symbol(), PERIOD_H1, 1);
double close1 = iClose(Symbol(), PERIOD_H1, 1);
double open2 = iOpen(Symbol(), PERIOD_H1, 2);
double close2 = iClose(Symbol(), PERIOD_H1, 2);
if(direction == 1) // 看涨吞没
{
return (close2 < open2) && (close1 > open1) && (close1 > open2) && (open1 < close2);
}
else if(direction == -1) // 看跌吞没
{
return (close2 > open2) && (close1 < open1) && (close1 < open2) && (open1 > close2);
}
return false;
}
//+------------------------------------------------------------------+
//| H4趋势方向 (1=上涨, -1=下跌, 0=震荡) |
//+------------------------------------------------------------------+
int GetH4Trend()
{
double ema50 = iMA(Symbol(), PERIOD_H4, H4TrendMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H4, 1);
if(close1 > ema50) return 1;
if(close1 < ema50) return -1;
return 0;
}
//+------------------------------------------------------------------+
//| EA主循环函数(每Tick执行) |
//+------------------------------------------------------------------+
void OnTick()
{
// 每日净值保护
double equity = AccountEquity();
double lossPct = (dailyStartBalance - equity) / dailyStartBalance * 100;
if(lossPct >= DailyLossLimit)
{
Comment("已达每日亏损上限 ", DailyLossLimit, "%,暂停交易");
return;
}
// 周五收盘前平仓(规避周末跳空)
if(CloseOnFriday && !fridayCloseDone)
{
datetime now = TimeCurrent();
if(TimeDayOfWeek(now) == 5 && TimeHour(now) >= 20)
{
CloseAllOrders();
fridayCloseDone = true;
Comment("周末平仓完成");
return;
}
if(TimeDayOfWeek(now) != 5)
fridayCloseDone = false;
}
// 点差过滤
int spread = (int)MarketInfo(Symbol(), MODE_SPREAD);
if(spread > MaxSpread)
{
Comment("当前点差过大:", spread);
return;
}
// 新H1 K线检测
if(Time[0] == lastH1BarTime)
return;
lastH1BarTime = Time[0];
// 管理现有持仓(追踪止损)
if(CountPositions() > 0)
{
ManageTrailingStop();
return;
}
// 更新ATR并执行波动率过滤
double currentATR = UpdateAvgATR();
double atrRatio = currentATR / avgATR_H1;
if(atrRatio < VolatilityMinRatio || atrRatio > VolatilityMaxRatio)
{
Comment("波动率过滤:ATR比率 = ", DoubleToString(atrRatio,2), " 不在区间 [",VolatilityMinRatio,",",VolatilityMaxRatio,"] 内");
return;
}
// 获取H4趋势方向
int trendDir = GetH4Trend();
if(trendDir == 0)
{
Comment("H4无明显趋势,等待");
return;
}
// 检测RSI背离
int divDir = DetectRSIDivergence();
if(divDir == 0)
{
Comment("未检测到RSI背离");
return;
}
// 检查背离方向是否与趋势一致
if((trendDir == 1 && divDir == -1) || (trendDir == -1 && divDir == 1))
{
Comment("背离方向与H4趋势相反,跳过");
return;
}
// 吞没K线确认
if(!IsEngulfing(divDir))
{
Comment("无确认的吞没K线形态");
return;
}
// 计算动态止损(取固定止损与动态止损的较大值)
int fixedStop = StopLossPips;
int dynamicStop = (int)(currentATR / Point * StopLossATRMultiplier);
int finalStop = MathMax(fixedStop, dynamicStop);
double sl = 0, tp = 0;
int cmd = -1;
double ask = Ask;
double bid = Bid;
if(divDir == 1) // 买入信号
{
cmd = OP_BUY;
sl = bid - finalStop * Point;
tp = bid + TakeProfitPips * Point;
}
else if(divDir == -1) // 卖出信号
{
cmd = OP_SELL;
sl = ask + finalStop * Point;
tp = ask - TakeProfitPips * Point;
}
if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?ask:bid), 3, sl, tp, "Gold Fusion", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("开仓失败,错误码:", GetLastError());
}
}
//+------------------------------------------------------------------+
//| 追踪止损管理 |
//+------------------------------------------------------------------+
void ManageTrailingStop()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
double newSL = 0;
if(OrderType() == OP_BUY)
{
double profitPoints = (Bid - OrderOpenPrice()) / Point;
if(profitPoints >= TrailingStartPips)
{
newSL = Bid - TrailingStepPips * Point;
if(newSL > OrderStopLoss())
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
else if(OrderType() == OP_SELL)
{
double profitPoints = (OrderOpenPrice() - Ask) / Point;
if(profitPoints >= TrailingStartPips)
{
newSL = Ask + TrailingStepPips * Point;
if(newSL < OrderStopLoss() || OrderStopLoss() == 0)
OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
}
}
break;
}
}
}
}
//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
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, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
}
}
}
}
//+------------------------------------------------------------------+
```
参考来源: 原创MQL4代码,仅供学习参考。
免责声明: 黄金交易因高波动性具有显著风险。本EA按“原样”提供,不保证盈利。实盘部署前请务必在模拟账户测试至少2个月。历史表现不代表未来结果。
```