Summary: 黄金结构流EA是一款专为XAUUSD设计的MQL4智能交易系统。使用波段高低点识别结构突破,结合ADX趋势强度过滤和ATR波动率保护,配合多时间框架确认,适合H4周期稳定运行。




黄金结构流EA专为黄金(XAUUSD)设计,采用结构分析方法,模仿机构订单流检测逻辑。与盲目摊平亏损的网格或马丁格尔系统不同,本EA识别关键结构突破——当价格在较高时间框架上突破已确立的波段高点或低点时——沿着突破方向入场。策略使用三重过滤机制:结构确认(突破近期20根K线高低点)、趋势强度验证(ADX > 25)、波动率适配(ATR与50周期平均值比较)。EA限制同一时间仅持有一个方向的头寸,使用固定手数无仓位递进,并包含每日净值回撤限制和周五平仓保护。

推荐加载周期: H4
策略核心逻辑:
  • 结构突破检测:识别最近20根K线的最高高点和最低低点。当价格收盘超越这些水平时视为突破。

  • 趋势强度过滤:ADX(14)必须超过可配置阈值(默认25),确保市场处于趋势状态。

  • 波动率保护:当前ATR(14)不应超过50周期平均ATR的2.0倍。

  • 多时间框架确认(可选):更高时间框架EMA(H4周期200)提供额外方向偏好。

  • 风险管理:基于ATR的固定止损(1.5倍ATR),止盈2.5倍ATR,盈利达到1倍ATR后启动追踪止损。


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

    //--- 输入参数及注释
    input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
    input int StructureLookback = 20; // 结构高低点回溯K线数
    input int ADXPeriod = 14; // ADX周期(趋势强度)
    input int ADXThreshold = 25; // 最小ADX阈值(趋势市场要求)
    input int ATRPeriod = 14; // ATR波动率周期
    input double ATRMaxMultiplier = 2.0; // 最大ATR倍数(波动率保护)
    input double ATRStopMultiplier = 1.5; // 止损倍数(ATR倍数)
    input double ATRTakeMultiplier = 2.5; // 止盈倍数(ATR倍数)
    input double TrailingStartATR = 1.0; // 追踪止损启动(盈利达到x倍ATR)
    input double TrailingStepATR = 0.5; // 追踪步长(ATR倍数)
    input bool UseHTFTrendFilter = true; // 启用H4 200EMA趋势过滤
    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;
    double structureHigh = 0;
    double structureLow = 0;

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

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

    //+------------------------------------------------------------------+
    //| 检测结构突破水平(波段高低点) |
    //+------------------------------------------------------------------+
    void DetectStructureLevels()
    {
    int highestIdx = iHighest(Symbol(), PERIOD_H4, MODE_HIGH, StructureLookback, 1);
    int lowestIdx = iLowest(Symbol(), PERIOD_H4, MODE_LOW, StructureLookback, 1);

    if(highestIdx > 0)
    structureHigh = iHigh(Symbol(), PERIOD_H4, highestIdx);
    if(lowestIdx > 0)
    structureLow = iLow(Symbol(), PERIOD_H4, lowestIdx);
    }

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

    //+------------------------------------------------------------------+
    //| 检查ADX趋势强度 |
    //+------------------------------------------------------------------+
    bool IsTrendStrong()
    {
    double adx = iADX(Symbol(), PERIOD_H4, ADXPeriod, PRICE_CLOSE, MODE_MAIN, 1);
    return (adx >= ADXThreshold);
    }

    //+------------------------------------------------------------------+
    //| 检查结构突破入场条件 |
    //+------------------------------------------------------------------+
    bool CheckStructuralBreakout(int direction, double &entryPrice, double &sl, double &tp, double atr)
    {
    if(direction == 0) return false;

    double close1 = iClose(Symbol(), PERIOD_H4, 1);

    // 向上突破:收盘价高于结构高点
    if(direction == 1 && structureHigh > 0)
    {
    if(close1 > structureHigh)
    {
    entryPrice = Ask;
    sl = entryPrice - (atr
    ATRStopMultiplier);
    tp = entryPrice + (atr ATRTakeMultiplier);
    return true;
    }
    }
    // 向下突破:收盘价低于结构低点
    else if(direction == -1 && structureLow > 0)
    {
    if(close1 < structureLow)
    {
    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线开始时检测入场(H4)
    if(Time[0] == lastBarTime)
    return;
    lastBarTime = Time[0];

    // 更新结构水平
    DetectStructureLevels();

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

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

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

    // 检查ADX趋势强度
    if(!IsTrendStrong())
    {
    Comment("ADX低于阈值,无明确趋势");
    return;
    }

    // 确定允许的交易方向
    int allowedDirection = 0;
    int htfTrend = GetHTFTrend();

    // 无HTF过滤时,结构突破决定方向
    if(!UseHTFTrendFilter)
    {
    double close1 = iClose(Symbol(), PERIOD_H4, 1);
    if(close1 > structureHigh) allowedDirection = 1;
    else if(close1 < structureLow) allowedDirection = -1;
    }
    else
    {
    if(htfTrend == 1 && structureHigh > 0)
    allowedDirection = 1;
    else if(htfTrend == -1 && structureLow > 0)
    allowedDirection = -1;
    }

    if(allowedDirection == 0)
    {
    Comment("无有效结构突破信号");
    return;
    }

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