Summary: 黄金波动守护者EA是一款专为XAUUSD设计的MQL4智能交易系统。使用MACD趋势过滤、多层仓位进阶(最多3层)和基于ATR的动态风控,适合H4周期稳定运行。




黄金波动守护者EA专为黄金(XAUUSD)设计,核心聚焦于资金保全。与激进的网格系统不同,本EA采用纪律严明的趋势跟踪方法,使用MACD(12,26,9)确认趋势方向,并结合EMA交叉验证。EA采用多层仓位系统,每一层新增仓位必须满足更严格的入场条件。基于权益的追踪止损在保护利润的同时允许趋势延续。EA包含每日亏损限额、点差过滤和周五平仓机制。

推荐加载周期: H4
策略核心逻辑:
  • 趋势检测:MACD主线高于信号线为上涨趋势;低于则为下跌趋势。EMA(20)与EMA(50)交叉提供额外确认。

  • 入场信号:价格回撤至EMA(50)并在趋势方向收盘超越该线,同时MACD动量确认。

  • 多层进阶:仅当现有持仓盈利达到1.5倍ATR时才允许加仓(最多3层)。

  • 风险管理:ATR动态止损、基于权益的追踪止损、每日亏损上限5%。


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

    //--- 输入参数及注释
    input double BaseLotSize = 0.01; // 基础交易手数(首层仓位)
    input int FastEMAPeriod = 20; // 快EMA周期(趋势确认)
    input int SlowEMAPeriod = 50; // 慢EMA周期(趋势过滤)
    input int MACDFast = 12; // MACD快EMA周期
    input int MACDSlow = 26; // MACD慢EMA周期
    input int MACDSignal = 9; // MACD信号线周期
    input int ATRPeriod = 14; // ATR波动率周期
    input double ATRStopMultiplier = 1.5; // 止损倍数(ATR倍数)
    input double ATRTakeMultiplier = 2.8; // 止盈倍数(ATR倍数)
    input double TierProfitThreshold = 1.5; // 加仓所需盈利(x倍ATR)
    input int MaxTiers = 3; // 最大仓位层数
    input double TrailingStart = 0.8; // 追踪止损启动(盈利达到x倍ATR)
    input double TrailingStep = 0.4; // 追踪步长(ATR倍数)
    input int MagicNumber = 202421; // 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;
    int currentTier = 0;

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

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

    //+------------------------------------------------------------------+
    //| 获取MACD趋势方向 |
    //+------------------------------------------------------------------+
    int GetMACDTrend()
    {
    double macdMain = iMACD(Symbol(), PERIOD_H4, MACDFast, MACDSlow, MACDSignal, PRICE_CLOSE, MODE_MAIN, 1);
    double macdSignal = iMACD(Symbol(), PERIOD_H4, MACDFast, MACDSlow, MACDSignal, PRICE_CLOSE, MODE_SIGNAL, 1);
    double macdHist = macdMain - macdSignal;

    if(macdHist > 0) return 1; // 看涨
    if(macdHist < 0) return -1; // 看跌
    return 0;
    }

    //+------------------------------------------------------------------+
    //| 检查EMA对齐状态 |
    //+------------------------------------------------------------------+
    int GetEMAAlignment()
    {
    double emaFast = iMA(Symbol(), PERIOD_H4, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double emaSlow = iMA(Symbol(), PERIOD_H4, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double close = iClose(Symbol(), PERIOD_H4, 1);

    bool bullish = (close > emaSlow && emaFast > emaSlow);
    bool bearish = (close < emaSlow && emaFast < emaSlow);

    if(bullish) return 1;
    if(bearish) return -1;
    return 0;
    }

    //+------------------------------------------------------------------+
    //| 统计当前EA的持仓层数 |
    //+------------------------------------------------------------------+
    int GetCurrentTierCount()
    {
    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;
    }

    //+------------------------------------------------------------------+
    //| 获取每层平均盈利(点数) |
    //+------------------------------------------------------------------+
    double GetAverageProfitInATR(double atr)
    {
    if(atr <= 0) return 0;

    double totalProfitPoints = 0;
    int count = 0;

    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    double profitPoints = 0;
    if(OrderType() == OP_BUY)
    profitPoints = (Bid - OrderOpenPrice()) / Point;
    else if(OrderType() == OP_SELL)
    profitPoints = (OrderOpenPrice() - Ask) / Point;

    totalProfitPoints += profitPoints;
    count++;
    }
    }
    }

    if(count == 0) return 0;
    return (totalProfitPoints / count)
    Point;
    }

    //+------------------------------------------------------------------+
    //| 检查是否可以增加新层 |
    //+------------------------------------------------------------------+
    bool CanAddTier(double atr)
    {
    int currentCount = GetCurrentTierCount();
    if(currentCount >= MaxTiers) return false;
    if(currentCount == 0) return true;

    double avgProfit = GetAverageProfitInATR(atr);
    double requiredProfit = atr TierProfitThreshold;

    return (avgProfit >= requiredProfit);
    }

    //+------------------------------------------------------------------+
    //| 检查回撤入场条件 |
    //+------------------------------------------------------------------+
    bool CheckPullbackEntry(int direction, double &entryPrice, double &sl, double &tp, double atr)
    {
    double emaSlow = iMA(Symbol(), PERIOD_H4, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double close1 = iClose(Symbol(), PERIOD_H4, 1);
    double open1 = iOpen(Symbol(), PERIOD_H4, 1);
    double low1 = iLow(Symbol(), PERIOD_H4, 1);
    double high1 = iHigh(Symbol(), PERIOD_H4, 1);

    if(direction == 1) // 做多:回撤至EMA50后收盘在其上方
    {
    bool pulledToEMA = (low1 <= emaSlow && close1 > emaSlow);
    bool bullishClose = (close1 > open1);
    bool belowPreviousHigh = (high1 < iHigh(Symbol(), PERIOD_H4, 2));

    if(pulledToEMA && bullishClose && belowPreviousHigh)
    {
    entryPrice = Ask;
    sl = entryPrice - (atr
    ATRStopMultiplier);
    tp = entryPrice + (atr ATRTakeMultiplier);
    return true;
    }
    }
    else if(direction == -1) // 做空:回抽至EMA50后收盘在其下方
    {
    bool pulledToEMA = (high1 >= emaSlow && close1 < emaSlow);
    bool bearishClose = (close1 < open1);
    bool abovePreviousLow = (low1 > iLow(Symbol(), PERIOD_H4, 2));

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

    //+------------------------------------------------------------------+
    //| 管理所有持仓的追踪止损 |
    //+------------------------------------------------------------------+
    void ManageTrailingStop(double atr)
    {
    if(atr <= 0) atr = avgATR;
    double activate = atr
    TrailingStart;
    double step = atr TrailingStep;

    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    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);
    }
    }
    }
    }
    }
    }

    //+------------------------------------------------------------------+
    //| 计算新层的手数 |
    //+------------------------------------------------------------------+
    double GetTierLotSize()
    {
    int tier = GetCurrentTierCount();
    if(tier == 0) return BaseLotSize;
    return NormalizeDouble(BaseLotSize
    (1.0 + tier 0.2), 2); // 小幅递增但受控
    }

    //+------------------------------------------------------------------+
    //| 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线开始时检测入场(H4)
    if(Time[0] == lastBarTime)
    return;
    lastBarTime = Time[0];

    // 获取ATR用于波动率管理
    double atr = iATR(Symbol(), PERIOD_H4, ATRPeriod, 1);
    if(atr > 0) avgATR = (avgATR 0.95) + (atr 0.05);

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

    // 管理现有持仓(追踪止损)
    int posCount = GetCurrentTierCount();
    if(posCount > 0)
    {
    ManageTrailingStop(atr);
    }

    // 检查趋势方向
    int macdTrend = GetMACDTrend();
    int emaAlign = GetEMAAlignment();

    if(macdTrend == 0 || emaAlign == 0 || macdTrend != emaAlign)
    {
    Comment("趋势方向不一致。MACD: ", macdTrend, " EMA: ", emaAlign);
    return;
    }

    // 检查是否可以增加新层
    if(!CanAddTier(atr))
    {
    Comment("未满足加仓条件,盈利阈值未达");
    return;
    }

    // 检查入场条件
    double entryPrice = 0, sl = 0, tp = 0;
    if(CheckPullbackEntry(macdTrend, entryPrice, sl, tp, atr))
    {
    double lotSize = GetTierLotSize();
    int cmd = (macdTrend == 1) ? OP_BUY : OP_SELL;
    int ticket = OrderSend(Symbol(), cmd, lotSize, entryPrice, 5, sl, tp, "Volatility Guard", MagicNumber, 0, clrNONE);
    if(ticket < 0)
    Print("开仓失败,错误码:", GetLastError());
    else
    Print("第 ", GetCurrentTierCount(), " 层开仓成功,方向:", cmd==OP_BUY?"买入":"卖出");
    }
    }

    //+------------------------------------------------------------------+
    //| 平仓当前品种下所有属于该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代码,策略架构参考了2025-2026年最新黄金EA的结构性趋势跟踪方法。
    免责声明: 黄金交易因高波动性具有重大风险。本EA按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
    ``