Summary: 黄金地平线EA是一款专为XAUUSD设计的MQL4智能交易系统。使用多时间框架EMA趋势确认、ATR动态止损和每日亏损限额,适合H1周期稳定运行。




黄金地平线EA专门为黄金(XAUUSD)设计,通过聚焦趋势方向和波动率感知实现稳定、低风险的运营。与激进的网格或马丁格尔系统不同,本EA将信号质量置于交易频率之上。它使用多时间框架趋势确认(H1和H4 EMA)确保与更广泛的市场方向一致。基于ATR的波动率过滤器防止在市场噪音过大时入场,而动态ATR止损则适应黄金当前的波动特性。EA包含每日净值保护、点差控制和周五平仓机制,以规避周末缺口风险。

推荐加载周期: H1
策略核心逻辑:
  • 多时间框架趋势:H4 EMA(50)决定主要趋势方向,H1必须同向交易。

  • 动量过滤:RSI(14)必须确认动量(做多>50,做空<50)。

  • 入场触发:价格回撤至H1 EMA(20)并收盘超越该线,方向与趋势一致。

  • 波动率保护:当前ATR(14)不得超过20周期平均ATR的1.8倍。

  • 风险管理:动态止损为1.5倍ATR,止盈为3倍ATR。盈利达到1倍ATR后启动追踪止损。


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

    //--- 输入参数及注释
    input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
    input int H1TrendMAPeriod = 50; // H1 EMA周期(趋势过滤)
    input int H4TrendMAPeriod = 50; // H4 EMA周期(主趋势方向)
    input int EntryMAPeriod = 20; // 入场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 int MagicNumber = 202419; // 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;

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

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

    //+------------------------------------------------------------------+
    //| 获取更高时间框架趋势方向(H4) |
    //+------------------------------------------------------------------+
    int GetHTFTrend()
    {
    double closeH4 = iClose(Symbol(), PERIOD_H4, 1);
    double emaH4 = iMA(Symbol(), PERIOD_H4, H4TrendMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    if(closeH4 > emaH4) return 1; // 看涨
    if(closeH4 < emaH4) return -1; // 看跌
    return 0;
    }

    //+------------------------------------------------------------------+
    //| 检查回撤入场条件 |
    //+------------------------------------------------------------------+
    bool CheckPullbackEntry(int direction, double &entryPrice, double &sl, double &tp, double atr)
    {
    double emaFast = iMA(Symbol(), PERIOD_H1, EntryMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double open1 = iOpen(Symbol(), PERIOD_H1, 1);

    if(direction == 1) // 做多:价格回撤至EMA20,然后收盘在其上方
    {
    double low1 = iLow(Symbol(), PERIOD_H1, 1);
    bool pulledToEMA = (low1 <= emaFast && close1 > emaFast);
    bool bullishClose = (close1 > open1);

    if(pulledToEMA && bullishClose)
    {
    entryPrice = Ask;
    sl = entryPrice - (atr
    ATRStopMultiplier);
    tp = entryPrice + (atr ATRTakeMultiplier);
    return true;
    }
    }
    else if(direction == -1) // 做空:价格回抽至EMA20,然后收盘在其下方
    {
    double high1 = iHigh(Symbol(), PERIOD_H1, 1);
    bool pulledToEMA = (high1 >= emaFast && close1 < emaFast);
    bool bearishClose = (close1 < open1);

    if(pulledToEMA && bearishClose)
    {
    entryPrice = Bid;
    sl = entryPrice + (atr
    ATRStopMultiplier);
    tp = entryPrice - (atr ATRTakeMultiplier);
    return true;
    }
    }
    return false;
    }

    //+------------------------------------------------------------------+
    //| 管理持仓的追踪止损 |
    //+------------------------------------------------------------------+
    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()
    {
    // 每日净值保护
    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];

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

    // 获取指标
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double emaH1 = iMA(Symbol(), PERIOD_H1, H1TrendMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);
    double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);

    // 更新平均ATR用于波动率过滤
    if(atr > 0) avgATR = (avgATR 0.95) + (atr 0.05);

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

    // 获取H4趋势方向
    int h4Trend = GetHTFTrend();
    if(h4Trend == 0)
    {
    Comment("H4无明确趋势方向");
    return;
    }

    // H1趋势对齐检查
    bool h1Bullish = (close1 > emaH1);
    bool h1Bearish = (close1 < emaH1);

    int allowedDirection = 0;
    if(h4Trend == 1 && h1Bullish) allowedDirection = 1;
    else if(h4Trend == -1 && h1Bearish) allowedDirection = -1;

    if(allowedDirection == 0)
    {
    Comment("H1与H4趋势方向不一致");
    return;
    }

    // RSI动量过滤
    if(allowedDirection == 1 && rsi < 50)
    {
    Comment("RSI低于50,无上涨动能");
    return;
    }
    if(allowedDirection == -1 && rsi > 50)
    {
    Comment("RSI高于50,无下跌动能");
    return;
    }

    // 检查回撤入场
    double entryPrice = 0, sl = 0, tp = 0;
    if(CheckPullbackEntry(allowedDirection, entryPrice, sl, tp, atr))
    {
    int cmd = (allowedDirection == 1) ? OP_BUY : OP_SELL;
    int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Gold Horizon", 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代码,策略架构参考了黄金趋势跟踪与结构性分析方法。
    免责声明: 黄金交易因高波动性具有重大风险。本EA按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
    ``