Summary: 黄金信号脉冲EA是一款专为XAUUSD设计的MQL4智能交易系统。结合EMA50趋势过滤、RSI(14)动量确认、ATR波动率保护和动态仓位管理,适合H1周期稳定运行。
黄金信号脉冲EA专为黄金(XAUUSD)设计,采用保守的多条件共振方法。与激进的网格或马丁格尔系统不同,本EA要求三个独立条件同时满足才会开仓:趋势方向(EMA50)、动量(RSI)和波动率状态(ATR)。EA仅在主趋势方向上进行交易,并避免在高波动期入场。动态仓位管理系统根据当前ATR水平调整手数,在黄金波动率飙升时降低风险敞口。额外的安全功能包括每日净值回撤保护、最大点差控制、周五平仓机制以及亏损交易后的冷却期。
推荐加载周期: H1
策略核心逻辑:
1. 趋势过滤:EMA(50)决定主要方向。价格高于EMA只做多;低于EMA只做空。
2. 动量确认:RSI(14)必须确认趋势(做多需要RSI>50,做空需要RSI<50)。
3. 入场信号:趋势确认后,等待价格回撤至EMA50区域并出现确认K线(收盘价沿趋势方向超越EMA)。
4. 波动率保护:当前ATR(14)不得超过30周期平均ATR的1.8倍。
5. 风险管理:基于1.5倍ATR的固定止损,3倍ATR止盈,盈利达到1倍ATR后启动追踪止损。
```mql4
//+------------------------------------------------------------------+
//| GoldSignalPulseEA.mq4 |
//| |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
//--- 输入参数及注释
input double BaseLotSize = 0.01; // 基础交易手数(黄金0.01手)
input int TrendEMAPeriod = 50; // EMA周期(趋势过滤)
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.0; // 追踪止损启动(盈利达到x倍ATR)
input double TrailingStepATR = 0.5; // 追踪步长(ATR倍数)
input double RiskPerTradePercent = 1.0; // 动态手数:每笔风险百分比
input int MaxSpread = 35; // 最大允许点差(黄金单位:点)
input double DailyLossLimit = 5.0; // 每日亏损限额(账户余额百分比)
input int CooldownMinutes = 30; // 亏损后冷却时间(分钟)
input bool UseFridayClose = true; // 周五21:00 GMT前平仓
input int MagicNumber = 202422; // EA魔术号
//--- 全局变量
double dailyStartBalance = 0;
datetime lastBarTime = 0;
bool fridayCloseExecuted = false;
double avgATR = 0;
datetime lastLossTime = 0;
double currentATR = 0;
//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
fridayCloseExecuted = false;
lastLossTime = 0;
avgATR = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(avgATR <= 0) avgATR = 250 * Point;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| EA退出函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}
//+------------------------------------------------------------------+
//| 获取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;
}
//+------------------------------------------------------------------+
//| 检查RSI动量确认 |
//+------------------------------------------------------------------+
bool CheckRSIMomentum(int direction)
{
double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);
if(rsi <= 0) return false;
if(direction == 1 && rsi > 50) return true;
if(direction == -1 && rsi < 50) return true;
return false;
}
//+------------------------------------------------------------------+
//| 检查回撤入场条件 |
//+------------------------------------------------------------------+
bool CheckPullbackEntry(int direction, double &entryPrice, double &sl, double &tp, double atr)
{
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);
if(direction == 1) // 做多:价格回撤至EMA区域,然后收盘于其上
{
bool pulledToEMA = (low1 <= ema + (atr * 0.3)) && (close1 > ema);
bool bullishClose = (close1 > iOpen(Symbol(), PERIOD_H1, 1));
if(pulledToEMA && bullishClose)
{
entryPrice = Ask;
sl = entryPrice - (atr * ATRStopMultiplier);
tp = entryPrice + (atr * ATRTakeMultiplier);
return true;
}
}
else if(direction == -1) // 做空:价格反弹至EMA区域,然后收盘于其下
{
bool pulledToEMA = (high1 >= ema - (atr * 0.3)) && (close1 < ema);
bool bearishClose = (close1 < iOpen(Symbol(), PERIOD_H1, 1));
if(pulledToEMA && bearishClose)
{
entryPrice = Bid;
sl = entryPrice + (atr * ATRStopMultiplier);
tp = entryPrice - (atr * ATRTakeMultiplier);
return true;
}
}
return false;
}
//+------------------------------------------------------------------+
//| 基于ATR波动率计算动态手数 |
//+------------------------------------------------------------------+
double CalculateDynamicLotSize(double atr)
{
double lot = BaseLotSize;
// 当ATR高于平均值时减小手数(高波动)
if(avgATR > 0 && atr > avgATR)
{
double reduction = avgATR / atr;
lot = BaseLotSize * reduction;
}
// 基于风险的仓位计算(使用RiskPerTradePercent)
if(RiskPerTradePercent > 0 && RiskPerTradePercent <= 5)
{
double accountBalance = AccountBalance();
double riskAmount = accountBalance * RiskPerTradePercent / 100.0;
double slDistance = atr * ATRStopMultiplier;
double riskBasedLot = riskAmount / (slDistance * 100);
if(riskBasedLot > 0 && riskBasedLot < lot)
lot = riskBasedLot;
}
lot = NormalizeDouble(lot, 2);
if(lot < 0.01) lot = 0.01;
if(lot > 1.0) lot = 1.0;
return lot;
}
//+------------------------------------------------------------------+
//| 管理持仓的追踪止损 |
//+------------------------------------------------------------------+
void ManageTrailingStop()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
double atr = currentATR;
if(atr <= 0) atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
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;
}
}
}
}
//+------------------------------------------------------------------+
//| 检查是否处于亏损后冷却期 |
//+------------------------------------------------------------------+
bool IsInCooldown()
{
if(lastLossTime == 0) return false;
return (TimeCurrent() - lastLossTime < CooldownMinutes * 60);
}
//+------------------------------------------------------------------+
//| 记录亏损交易时间 |
//+------------------------------------------------------------------+
void CheckAndRecordLoss()
{
static datetime lastCheck = 0;
if(TimeCurrent() - lastCheck < 60) return;
lastCheck = TimeCurrent();
for(int i = OrdersHistoryTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(OrderCloseTime() > lastLossTime && OrderProfit() < 0)
{
lastLossTime = OrderCloseTime();
Print("亏损交易已记录,冷却期 ", CooldownMinutes, " 分钟");
}
}
}
}
}
//+------------------------------------------------------------------+
//| 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和平均值
currentATR = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
if(currentATR > 0) avgATR = (avgATR * 0.95) + (currentATR * 0.05);
// 检查并记录亏损交易(用于冷却期)
CheckAndRecordLoss();
// 管理现有持仓
if(CountPositions() > 0)
{
ManageTrailingStop();
return;
}
// 亏损后冷却期检查
if(IsInCooldown())
{
Comment("冷却期进行中,", (CooldownMinutes * 60 - (TimeCurrent() - lastLossTime)) / 60, "分钟后可开新仓");
return;
}
// 波动率保护
if(avgATR > 0 && currentATR > avgATR * ATRMaxMultiplier)
{
Comment("当前波动率过高,ATR: ", currentATR);
return;
}
// 获取趋势方向
int trendDirection = GetTrendDirection();
if(trendDirection == 0)
{
Comment("无明确趋势方向");
return;
}
// RSI动量确认
if(!CheckRSIMomentum(trendDirection))
{
Comment("RSI动量未确认");
return;
}
// 检查回撤入场
double entryPrice = 0, sl = 0, tp = 0;
if(CheckPullbackEntry(trendDirection, entryPrice, sl, tp, currentATR))
{
double lot = CalculateDynamicLotSize(currentATR);
int cmd = (trendDirection == 1) ? OP_BUY : OP_SELL;
int ticket = OrderSend(Symbol(), cmd, lot, entryPrice, 5, sl, tp, "Signal Pulse", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("开仓失败,错误码:", GetLastError());
else
Print("开仓成功,方向:", cmd==OP_BUY?"买入":"卖出", " 手数:", lot);
}
}
//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
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按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
```