Summary: 金色地平线EA是一款专为XAUUSD设计的稳定型MQL4智能交易系统。使用双EMA金叉死叉配合RSI过滤和ATR波动保护,适合H1周期。




金色地平线EA专门为黄金(XAUUSD)交易设计,采用稳定运营思路,充分尊重黄金强趋势和突然反转的特性。EA采用双EMA交叉系统(快线20,慢线50),结合RSI(14)确认来过滤假信号。ATR(14)作为波动率阀门,在极端市场噪声时阻止入场。每笔交易带有固定止损和追踪止损,随着趋势延伸锁定利润。EA还包含每日亏损限制和最大点差控制。

推荐加载周期: H1
策略核心逻辑:
  • 趋势检测:快EMA20上穿慢EMA50 → 潜在上升趋势;下穿则为下降趋势。

  • 信号确认:RSI(14)必须大于50(上升趋势)或小于50(下降趋势)以与趋势对齐。

  • 波动率阀门:ATR(14)必须低于ATR阈值(ATR < ATRMaxLimit × 200点参考值),避免无序波动。

  • 出场策略:固定止盈600点,或盈利达到300点后激活追踪止损(追踪步长200点)。


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

    //--- 输入参数及注释
    input double LotSize = 0.01; // 固定手数(黄金0.01手)
    input int FastMAPeriod = 20; // 快线均线周期(EMA)
    input int SlowMAPeriod = 50; // 慢线均线周期(EMA)
    input int RSIPeriod = 14; // RSI确认指标周期
    input int ATRPeriod = 14; // ATR波动率过滤周期
    input double ATRMaxLimit = 1.2; // 最大ATR倍数(ATR > 1.2×200点=不开仓)
    input int StopLossPoints = 300; // 固定止损点数(300点)
    input int TakeProfitPoints = 600; // 固定止盈点数(盈亏比2:1)
    input int TrailingStart = 300; // 启动追踪止损所需盈利点数
    input int TrailingStep = 200; // 追踪止损距离(点数)
    input int MagicNumber = 202415; // EA魔术号
    input int MaxSpread = 35; // 最大允许点差(单位:点)
    input double DailyLossLimit = 5.0; // 每日亏损限额(账户余额百分比)
    input bool UseFridayClose = true; // 周五20:00 GMT前平仓

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

    //+------------------------------------------------------------------+
    //| EA初始化函数 |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    dailyStartBalance = AccountBalance();
    lastBarTime = 0;
    fridayCloseExecuted = false;
    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;
    }

    // 周五收盘前平仓(20:00 GMT)
    if(UseFridayClose && !fridayCloseExecuted)
    {
    datetime currentTime = TimeCurrent();
    int hourGMT = TimeHour(currentTime);
    if(TimeDayOfWeek(currentTime) == 5 && hourGMT >= 20)
    {
    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 positions = CountPositions();
    if(positions > 0)
    {
    ManageTrailingStop();
    return;
    }

    // 获取前一根K线的指标值
    double fastEMA = iMA(Symbol(), PERIOD_H1, FastMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double slowEMA = iMA(Symbol(), PERIOD_H1, SlowMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double rsi = iRSI(Symbol(), PERIOD_H1, RSIPeriod, PRICE_CLOSE, 1);
    double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);

    // 波动率过滤
    double atrThreshold = ATRMaxLimit
    200; // 黄金参考200点基准
    if(atr > atrThreshold)
    {
    Comment("当前波动率过高,暂停交易。ATR: ", atr);
    return;
    }

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

    // 做多条件:EMA金叉 + RSI > 50
    if(fastEMA > slowEMA && rsi > 50)
    {
    double fastEMA_prev = iMA(Symbol(), PERIOD_H1, FastMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 2);
    double slowEMA_prev = iMA(Symbol(), PERIOD_H1, SlowMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 2);

    if(fastEMA_prev <= slowEMA_prev)
    {
    cmd = OP_BUY;
    sl = SymbolInfoDouble(Symbol(), SYMBOL_BID) - StopLossPoints Point;
    tp = SymbolInfoDouble(Symbol(), SYMBOL_BID) + TakeProfitPoints
    Point;
    }
    }
    // 做空条件:EMA死叉 + RSI < 50
    else if(fastEMA < slowEMA && rsi < 50)
    {
    double fastEMA_prev = iMA(Symbol(), PERIOD_H1, FastMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 2);
    double slowEMA_prev = iMA(Symbol(), PERIOD_H1, SlowMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 2);

    if(fastEMA_prev >= slowEMA_prev)
    {
    cmd = OP_SELL;
    sl = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + StopLossPoints Point;
    tp = SymbolInfoDouble(Symbol(), SYMBOL_ASK) - TakeProfitPoints
    Point;
    }
    }

    if(cmd != -1)
    {
    double price = (cmd == OP_BUY) ? Ask : Bid;
    int ticket = OrderSend(Symbol(), cmd, LotSize, price, 3, sl, tp, "Golden Horizon", 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;
    double currentStopLoss = OrderStopLoss();

    if(OrderType() == OP_BUY)
    {
    double profitPoints = (Bid - OrderOpenPrice()) / Point;
    if(profitPoints >= TrailingStart)
    {
    newSL = Bid - TrailingStep Point;
    if(newSL > currentStopLoss)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE))
    Print("买入追踪止损更新 #", OrderTicket());
    }
    }
    }
    else if(OrderType() == OP_SELL)
    {
    double profitPoints = (OrderOpenPrice() - Ask) / Point;
    if(profitPoints >= TrailingStart)
    {
    newSL = Ask + TrailingStep
    Point;
    if(newSL < currentStopLoss || currentStopLoss == 0)
    {
    if(OrderModify(OrderTicket(), OrderOpenPrice(), newSL, 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;
    }

    //+------------------------------------------------------------------+
    //| 平仓当前品种下所有属于该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按“原样”提供,不保证盈利。实盘交易前请在模拟账户充分测试。历史表现不代表未来结果。
    ``