Summary: GoldStabilityPro EA是一款专为XAUUSD设计的MQL4智能交易系统。将ADX趋势过滤与基于ATR的动态止损相结合,适合H1周期长期稳定运行。




GoldStabilityPro EA专门为XAUUSD设计,专注于长期稳定性和资金保全。与激进的高频系统不同,本EA将交易质量置于数量之上,仅在趋势强度和波动率条件同时满足时执行交易。策略结合ADX进行趋势过滤,ATR进行动态仓位管理。每笔交易都基于当前市场波动率设有固定止损和止盈,确保风险可控。EA包含每日净值保护、点差控制和周五平仓机制。

推荐加载周期: H1
策略核心逻辑:
  • 趋势强度过滤:ADX(14)必须高于ADX最小阈值(默认25),确保处于趋势市场。

  • 方向过滤:DI+(看涨)或DI-(看跌)决定交易方向,选择占优的一方。

  • 波动率评估:计算ATR(14)并用于动态止损设置。

  • 入场确认:价格必须收盘于H1 EMA(50)之外,且方向与占优DI一致。

  • 风险管理:固定止损(默认300点)和止盈(默认600点),可选ATR缩放功能。


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

    //--- 输入参数及注释
    input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
    input int ADXPeriod = 14; // ADX周期(趋势强度检测)
    input int ADXMinThreshold = 25; // 最小ADX阈值(趋势市要求)
    input int EMAPeriod = 50; // EMA周期(趋势方向过滤)
    input int ATRPeriod = 14; // ATR周期(波动率参考)
    input int StopLossPoints = 300; // 止损点数(300点)
    input int TakeProfitPoints = 600; // 止盈点数(盈亏比2:1)
    input bool UseATRScaling = false; // 启用ATR动态缩放止损止盈
    input double ATRMultiplier = 1.5; // 启用缩放时的ATR止损倍数
    input int MagicNumber = 202420; // 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;

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

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

    //+------------------------------------------------------------------+
    //| 获取ADX趋势方向与强度 |
    //+------------------------------------------------------------------+
    int GetADXDirection(double &adxValue, double &diPlus, double &diMinus)
    {
    adxValue = iADX(Symbol(), PERIOD_H1, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
    diPlus = iADX(Symbol(), PERIOD_H1, ADXPeriod, PRICE_CLOSE, MODE_PLUSDI, 1);
    diMinus = iADX(Symbol(), PERIOD_H1, ADXPeriod, PRICE_CLOSE, MODE_MINUSDI, 1);

    if(adxValue < ADXMinThreshold) return 0; // 无趋势
    if(diPlus > diMinus) return 1; // 看涨趋势
    if(diMinus > diPlus) return -1; // 看跌趋势
    return 0;
    }

    //+------------------------------------------------------------------+
    //| 计算基于ATR的动态止损止盈 |
    //+------------------------------------------------------------------+
    void CalculateDynamicSLTP(int direction, double entryPrice, double &sl, double &tp, double atr)
    {
    if(UseATRScaling && atr > 0)
    {
    double dynamicSL = atr ATRMultiplier / Point;
    double dynamicTP = dynamicSL
    2;

    if(direction == 1) // 买入
    {
    sl = entryPrice - dynamicSL Point;
    tp = entryPrice + dynamicTP
    Point;
    }
    else // 卖出
    {
    sl = entryPrice + dynamicSL Point;
    tp = entryPrice - dynamicTP
    Point;
    }
    }
    else
    {
    if(direction == 1) // 买入
    {
    sl = entryPrice - StopLossPoints Point;
    tp = entryPrice + TakeProfitPoints
    Point;
    }
    else // 卖出
    {
    sl = entryPrice + StopLossPoints Point;
    tp = entryPrice - TakeProfitPoints
    Point;
    }
    }
    }

    //+------------------------------------------------------------------+
    //| 检查EMA方向对齐确认 |
    //+------------------------------------------------------------------+
    bool CheckEMAAlignment(int direction)
    {
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double ema = iMA(Symbol(), PERIOD_H1, EMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);

    if(direction == 1) return (close1 > ema);
    if(direction == -1) return (close1 < ema);
    return false;
    }

    //+------------------------------------------------------------------+
    //| 管理持仓的追踪止损 |
    //+------------------------------------------------------------------+
    void ManageTrailingStop()
    {
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
    double trailStep = (UseATRScaling && atr > 0) ? (atr 0.5) : (100 Point);
    double newSL = 0;

    if(OrderType() == OP_BUY)
    {
    double profit = Bid - OrderOpenPrice();
    if(profit >= 150 Point) // 最低盈利门槛启动追踪
    {
    newSL = Bid - trailStep;
    if(newSL > OrderStopLoss())
    OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
    }
    }
    else if(OrderType() == OP_SELL)
    {
    double profit = OrderOpenPrice() - Ask;
    if(profit >= 150
    Point)
    {
    newSL = Ask + trailStep;
    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];

    // 管理现有持仓
    if(CountPositions() > 0)
    {
    ManageTrailingStop();
    return;
    }

    // 获取ADX趋势方向和强度
    double adxValue = 0, diPlus = 0, diMinus = 0;
    int direction = GetADXDirection(adxValue, diPlus, diMinus);

    if(direction == 0)
    {
    Comment("未检测到强趋势。ADX: ", adxValue);
    return;
    }

    // 检查EMA方向对齐
    if(!CheckEMAAlignment(direction))
    {
    Comment("EMA方向对齐失败");
    return;
    }

    // 获取ATR用于动态计算
    double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);

    // 确定入场价格并计算止损止盈
    double entryPrice = (direction == 1) ? Ask : Bid;
    double sl = 0, tp = 0;
    CalculateDynamicSLTP(direction, entryPrice, sl, tp, atr);

    int cmd = (direction == 1) ? OP_BUY : OP_SELL;
    int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "GoldStabilityPro", MagicNumber, 0, clrNONE);

    if(ticket < 0)
    {
    Print("开仓失败,错误码:", GetLastError());
    }
    else
    {
    Print("开仓成功。方向:", cmd==OP_BUY?"买入":"卖出",
    " | ADX: ", adxValue, " | DI+: ", diPlus, " | DI-: ", diMinus);
    }
    }

    //+------------------------------------------------------------------+
    //| 统计当前魔术号的持仓数量 |
    //+------------------------------------------------------------------+
    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代码,ADX+ATR趋势跟踪策略架构。
    免责声明: 黄金交易因高波动性具有重大风险。本EA按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
    ``