Summary: 黄金趋势脉冲EA是一款适用于XAUUSD的MQL4智能交易系统,使用EMA交叉判断趋势方向,RSI过滤入场点,适合H1周期,内置固定止损止盈。




黄金趋势脉冲EA专门为黄金(XAUUSD)交易设计。它结合两条指数移动平均线(EMA)判断整体趋势方向,并使用RSI指标过滤最佳入场点。EA仅在趋势明确且动量确认时开仓,减少震荡行情中的假信号。每笔交易带有固定止损止盈,并具备可选追踪止损功能以锁定利润。EA还包含每日亏损限额和点差控制。

推荐加载周期: H1
策略核心逻辑:
1. 趋势过滤:快EMA(12)在慢EMA(26)之上表示上涨趋势;反之表示下跌趋势。
2. 入场确认:上涨趋势中,RSI(14)需处于50-70之间(未超买);下跌趋势中,RSI需处于30-50之间(未超卖)。
3. 入场信号:收盘价高于快EMA且RSI>50时做多;收盘价低于快EMA且RSI<50时做空。
4. 风险控制:固定止损350点,止盈700点,可选追踪止损(盈利200点后启动)。

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

//--- 输入参数及注释
input double LotSize = 0.01; // 固定手数(黄金0.01手)
input int FastEMAPeriod = 12; // 快EMA周期(趋势判断)
input int SlowEMAPeriod = 26; // 慢EMA周期(趋势判断)
input int RSIPeriod = 14; // RSI周期(动量过滤)
input int StopLossPoints = 350; // 止损点数(黄金350点)
input int TakeProfitPoints = 700; // 止盈点数(盈亏比2:1)
input bool UseTrailingStop = true; // 启用追踪止损
input int TrailingStart = 200; // 追踪启动点数(盈利达到此值后启动)
input int TrailingStep = 50; // 追踪止损距离(点数)
input int MagicNumber = 202415; // EA魔术号(区分不同EA)
input int MaxSpread = 40; // 最大允许点差(单位:点)
input double DailyLossLimit = 5.0; // 每日亏损限额(账户余额百分比)

//--- 全局变量
double dailyStartBalance = 0;
datetime lastBarTime = 0;

//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
dailyStartBalance = AccountBalance();
lastBarTime = 0;
return(INIT_SUCCEEDED);
}

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

//+------------------------------------------------------------------+
//| EA主循环函数(每Tick执行) |
//+------------------------------------------------------------------+
void OnTick()
{
// 每日净值保护:亏损超限则停止交易
double currentEquity = AccountEquity();
double lossPercent = (dailyStartBalance - currentEquity) / dailyStartBalance * 100;
if(lossPercent >= DailyLossLimit)
{
Comment("已达每日亏损上限,停止开新仓");
return;
}

// 点差过滤:过大点差不交易
if(MarketInfo(Symbol(), MODE_SPREAD) > MaxSpread)
{
Comment("当前点差过大:", MarketInfo(Symbol(), MODE_SPREAD));
return;
}

// 仅在新K线开始时检测入场(H1周期)
if(Time[0] == lastBarTime)
return;
lastBarTime = Time[0];

// 检查是否已有持仓
if(CountPositions() > 0)
{
if(UseTrailingStop)
ApplyTrailingStop();
return;
}

// 获取指标数值
double fastEMA = iMA(Symbol(), PERIOD_H1, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double slowEMA = iMA(Symbol(), PERIOD_H1, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);
double close1 = iClose(Symbol(), PERIOD_H1, 1);

int cmd = -1;
double sl = 0, tp = 0;

// 做多条件:快EMA高于慢EMA,RSI在50-70之间,收盘价高于快EMA
if(fastEMA > slowEMA && rsi > 50 && rsi < 70 && close1 > fastEMA)
{
cmd = OP_BUY;
sl = SymbolInfoDouble(Symbol(), SYMBOL_BID) - StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_BID) + TakeProfitPoints * Point;
}
// 做空条件:快EMA低于慢EMA,RSI在30-50之间,收盘价低于快EMA
else if(fastEMA < slowEMA && rsi < 50 && rsi > 30 && close1 < fastEMA)
{
cmd = OP_SELL;
sl = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + StopLossPoints * Point;
tp = SymbolInfoDouble(Symbol(), SYMBOL_ASK) - TakeProfitPoints * Point;
}

if(cmd != -1)
{
int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?Ask:Bid), 3, sl, tp, "Gold Trend Pulse", MagicNumber, 0, clrNONE);
if(ticket < 0)
Print("开仓失败,错误码:", GetLastError());
}
}

//+------------------------------------------------------------------+
//| 对现有持仓应用追踪止损 |
//+------------------------------------------------------------------+
void ApplyTrailingStop()
{
for(int i = OrdersTotal()-1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
double currentStop = OrderStopLoss();
double newStop = 0;

if(OrderType() == OP_BUY)
{
double profitPoints = (Bid - OrderOpenPrice()) / Point;
if(profitPoints >= TrailingStart)
{
newStop = Bid - TrailingStep * Point;
if(newStop > currentStop)
{
if(OrderModify(OrderTicket(), OrderOpenPrice(), newStop, OrderTakeProfit(), 0, clrNONE))
Print("买入追踪止损已更新 #", OrderTicket());
}
}
}
else if(OrderType() == OP_SELL)
{
double profitPoints = (OrderOpenPrice() - Ask) / Point;
if(profitPoints >= TrailingStart)
{
newStop = Ask + TrailingStep * Point;
if(newStop < currentStop || currentStop == 0)
{
if(OrderModify(OrderTicket(), OrderOpenPrice(), newStop, OrderTakeProfit(), 0, clrNONE))
Print("卖出追踪止损已更新 #", OrderTicket());
}
}
}
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;
}
//+------------------------------------------------------------------+
```
参考来源: 原创MQL4代码,仅供学习参考。
免责声明: 外汇及黄金交易具有较高风险。本EA按“原样”提供,不保证盈利。实盘交易前请在模拟账户充分测试。历史表现不代表未来结果。
```