Summary: 黄金地平线EA是一款专为XAUUSD设计的MQL4智能交易系统,融合EMA趋势检测、RSI动量确认与ATR波动率过滤,适合H1周期稳定运行并配备每日风控。




黄金地平线EA是一款专为黄金(XAUUSD)设计的稳定、低风险的自动化交易系统。与那些在持续趋势中可能导致账户爆仓的激进网格或马丁格尔系统不同,该EA遵循严谨的趋势确认方法。它结合了三个核心过滤条件:EMA趋势方向、RSI动量以及ATR波动率防护,确保只在有利的市场条件下开仓。每笔交易都设有固定的止损和2:1的风险回报比止盈。EA还包含每日亏损限额、点差控制和周五收盘平仓保护,以规避周末跳空风险。

推荐加载周期: H1

策略核心逻辑:
1. 趋势过滤:50周期EMA判断方向(价格高于EMA只做多,低于EMA只做空)。
2. 动量确认:RSI(14)做多需高于55,做空需低于45,以避开低动量区域。
3. 波动率防护:ATR(14)需低于其20周期均值的1.8倍,以规避极端波动行情。
4. 入场执行:当上述条件同时满足时,在下一根K线开盘价入场,配合固定止损止盈。
5. 风险管理:固定止损350点,止盈700点。每日亏损限额5%,最大点差35点。

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

//+------------------------------------------------------------------+
//| 输入参数及注释 |
//+------------------------------------------------------------------+

input double InpLotSize = 0.01; // 固定交易手数(黄金0.01手)
input int InpStopLossPoints = 350; // 止损点数(5位报价平台为3500小点)
input int InpTakeProfitPoints = 700; // 止盈点数(2:1风险回报比)
input int InpTrendMAPeriod = 50; // EMA趋势过滤周期
input int InpRSIPeriod = 14; // RSI动量确认周期
input double InpRSIThresholdLong = 55.0; // 做多所需RSI阈值(需高于此值)
input double InpRSIThresholdShort = 45.0; // 做空所需RSI阈值(需低于此值)
input int InpATRPeriod = 14; // ATR波动率过滤周期
input double InpATRMaxMultiplier = 1.8; // ATR最大倍数(超过avgATR倍数则跳过)
input int InpMagicNumber = 202418; // EA魔术号
input int InpMaxSpread = 35; // 最大允许点差(单位:点)
input double InpDailyLossLimit = 5.0; // 每日亏损限额(账户余额百分比)
input bool InpUseFridayClose = true; // 周五21:00 GMT前平仓

//+------------------------------------------------------------------+
//| 全局变量 |
//+------------------------------------------------------------------+

double g_dailyStartBalance = 0;
datetime g_lastBarTime = 0;
bool g_fridayCloseDone = false;
double g_avgATR = 0;

//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
g_dailyStartBalance = AccountBalance();
g_lastBarTime = 0;
g_fridayCloseDone = false;
g_avgATR = iATR(_Symbol, PERIOD_H1, InpATRPeriod, 1);
if(g_avgATR <= 0) g_avgATR = 250 * Point;
return(INIT_SUCCEEDED);
}

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

//+------------------------------------------------------------------+
//| 每日亏损检测 |
//+------------------------------------------------------------------+
bool IsDailyLossExceeded()
{
double equity = AccountEquity();
double lossPercent = (g_dailyStartBalance - equity) / g_dailyStartBalance * 100;
return (lossPercent >= InpDailyLossLimit);
}

//+------------------------------------------------------------------+
//| 周五平仓处理 |
//+------------------------------------------------------------------+
void HandleFridayClose()
{
if(!InpUseFridayClose) return;
if(g_fridayCloseDone) return;

datetime now = TimeCurrent();
if(TimeDayOfWeek(now) == 5 && TimeHour(now) >= 21)
{
CloseAllOrders();
g_fridayCloseDone = true;
}
else if(TimeDayOfWeek(now) != 5)
{
g_fridayCloseDone = false;
}
}

//+------------------------------------------------------------------+
//| 点差验证 |
//+------------------------------------------------------------------+
bool IsSpreadValid()
{
int currentSpread = (int)MarketInfo(_Symbol, MODE_SPREAD);
if(currentSpread > InpMaxSpread)
{
Comment("点差过大: ", currentSpread);
return false;
}
return true;
}

//+------------------------------------------------------------------+
//| 统计当前魔术号的持仓数量 |
//+------------------------------------------------------------------+
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() == InpMagicNumber)
count++;
}
}
return count;
}

//+------------------------------------------------------------------+
//| 平仓所有订单 |
//+------------------------------------------------------------------+
void CloseAllOrders()
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == _Symbol && OrderMagicNumber() == InpMagicNumber)
{
if(OrderType() == OP_BUY)
OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrNONE);
else if(OrderType() == OP_SELL)
OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrNONE);
}
}
}
}

//+------------------------------------------------------------------+
//| EA主循环函数(每Tick执行) |
//+------------------------------------------------------------------+
void OnTick()
{
// 每日亏损保护
if(IsDailyLossExceeded())
{
Comment("已达每日亏损上限,停止开新仓");
return;
}

// 周五平仓处理
HandleFridayClose();

// 点差验证
if(!IsSpreadValid())
return;

// 新K线检测(H1周期)
if(Time[0] == g_lastBarTime)
return;
g_lastBarTime = Time[0];

// 检查现有持仓
if(CountPositions() > 0)
return;

// 计算已收盘K线(shift=1)的指标值
double ema = iMA(_Symbol, PERIOD_H1, InpTrendMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
double close1 = iClose(_Symbol, PERIOD_H1, 1);
double rsi = iRSI(_Symbol, PERIOD_H1, InpRSIPeriod, PRICE_CLOSE, 1);
double atr = iATR(_Symbol, PERIOD_H1, InpATRPeriod, 1);

// 更新滚动平均ATR
if(atr > 0)
g_avgATR = (g_avgATR * 0.95) + (atr * 0.05);

// 波动率过滤:当前ATR过高则跳过
if(atr > g_avgATR * InpATRMaxMultiplier)
{
Comment("当前波动率过高,ATR: ", atr);
return;
}

int cmd = -1;
double sl = 0;
double tp = 0;
double ask = Ask;
double bid = Bid;

// 做多条件:价格高于EMA,RSI高于阈值
if(close1 > ema && rsi > InpRSIThresholdLong)
{
cmd = OP_BUY;
sl = bid - InpStopLossPoints * Point;
tp = bid + InpTakeProfitPoints * Point;
}
// 做空条件:价格低于EMA,RSI低于阈值
else if(close1 < ema && rsi < InpRSIThresholdShort)
{
cmd = OP_SELL;
sl = ask + InpStopLossPoints * Point;
tp = ask - InpTakeProfitPoints * Point;
}

// 执行交易
if(cmd != -1)
{
double price = (cmd == OP_BUY) ? ask : bid;
int ticket = OrderSend(_Symbol, cmd, InpLotSize, price, 3, sl, tp, "Gold Horizon", InpMagicNumber, 0, clrNONE);
if(ticket < 0)
Print("开仓失败,错误码:", GetLastError());
}
}

//+------------------------------------------------------------------+
```
参考来源: 原创MQL4代码,策略逻辑参考了当前黄金市场主流机构交易系统的趋势过滤与风险管理思路。

免责声明: 外汇及黄金交易具有重大亏损风险。本EA仅供学习参考,不保证任何盈利能力。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
```