Summary: 黄金顶点趋势EA是一款专为XAUUSD设计的MQL4智能交易系统。使用H4周期结构趋势确认、ADX趋势强度过滤、ATR动态止损和整体止盈机制,适合H1周期稳定运行。




黄金顶点趋势EA专为黄金的稳定交易而设计,核心聚焦于结构性趋势分析。与忽视市场方向的激进网格系统不同,本EA使用多周期趋势确认机制,确保交易与主导市场结构保持一致。EA在H4周期上识别关键摆动点,通过ADX确认趋势强度,然后在H1周期等待回调入场。每笔持仓都受ATR动态止损保护,整体止盈机制锁定组合收益。每日亏损限制和点差控制提供额外的安全层。

推荐加载周期: H1(执行周期),H4(趋势参考周期)
策略核心逻辑:
  • 趋势结构:在H4周期上识别过去40根K线的波段高低点,判断趋势方向。

  • 趋势强度:ADX(14) > 25,过滤低波动震荡行情。

  • 入场信号:价格回调至H1周期EMA20,方向与H4结构一致,伴随确认K线形态。

  • 风险管理:ATR动态止损(1.8倍ATR),止盈3倍ATR。最多同时持仓2单,每日亏损上限5%。


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

    //--- 输入参数及注释
    input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
    input int H4StructurePeriod = 40; // H4周期结构检测回溯K线数
    input int H1EntryMAPeriod = 20; // H1入场EMA周期(回调判断)
    input int ADXPeriod = 14; // ADX周期(趋势强度过滤)
    input int ADXThreshold = 25; // 最小ADX阈值(低于此值不开仓)
    input int ATRPeriod = 14; // ATR周期(动态止损)
    input double ATRStopMultiplier = 1.8; // 止损倍数(ATR倍数)
    input double ATRTakeMultiplier = 3.0; // 止盈倍数(ATR倍数)
    input int MaxPositions = 2; // 最大同时持仓数
    input double BasketProfitPoints = 200; // 整体止盈点数(组合目标)
    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 h4TrendDirection = 0; // 1=多头结构, -1=空头结构
    datetime trendConfirmedTime = 0;

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

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

    //+------------------------------------------------------------------+
    //| 检测H4周期趋势结构(基于波段高低点) |
    //+------------------------------------------------------------------+
    int DetectH4TrendStructure()
    {
    // 获取H4周期近期波段高点和低点
    int swingHighIdx = iHighest(Symbol(), PERIOD_H4, MODE_HIGH, H4StructurePeriod, 1);
    int swingLowIdx = iLowest(Symbol(), PERIOD_H4, MODE_LOW, H4StructurePeriod, 1);

    if(swingHighIdx < 0 || swingLowIdx < 0) return 0;

    double swingHigh = iHigh(Symbol(), PERIOD_H4, swingHighIdx);
    double swingLow = iLow(Symbol(), PERIOD_H4, swingLowIdx);
    double currentClose = iClose(Symbol(), PERIOD_H4, 1);
    double currentHigh = iHigh(Symbol(), PERIOD_H4, 1);
    double currentLow = iLow(Symbol(), PERIOD_H4, 1);

    // 基于更高的高点/更高的低点判断趋势结构
    bool bullishStructure = (currentHigh > swingHigh && currentLow > swingLow);
    bool bearishStructure = (currentLow < swingLow && currentHigh < swingHigh);

    // 额外使用H4 200 EMA确认
    double ema200H4 = iMA(Symbol(), PERIOD_H4, 200, 0, MODE_EMA, PRICE_CLOSE, 1);

    if(bullishStructure && currentClose > ema200H4) return 1;
    if(bearishStructure && currentClose < ema200H4) return -1;

    return 0;
    }

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

    //+------------------------------------------------------------------+
    //| 检查H1周期回调入场条件 |
    //+------------------------------------------------------------------+
    bool CheckPullbackEntry(int trendDir, double &entryPrice, double &sl, double &tp, double atr)
    {
    if(trendDir == 0) return false;

    double ema20H1 = iMA(Symbol(), PERIOD_H1, H1EntryMAPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double open1 = iOpen(Symbol(), PERIOD_H1, 1);
    double low1 = iLow(Symbol(), PERIOD_H1, 1);
    double high1 = iHigh(Symbol(), PERIOD_H1, 1);

    if(trendDir == 1) // 做多:价格回调至EMA20后反弹
    {
    bool pulledToEMA = (low1 <= ema20H1 && close1 > ema20H1);
    bool bullishCandle = (close1 > open1);
    bool confirmMomentum = (close1 > iClose(Symbol(), PERIOD_H1, 2));

    if(pulledToEMA && bullishCandle && confirmMomentum)
    {
    entryPrice = Ask;
    sl = entryPrice - (atr
    ATRStopMultiplier);
    tp = entryPrice + (atr ATRTakeMultiplier);
    return true;
    }
    }
    else if(trendDir == -1) // 做空:价格反弹至EMA20后回落
    {
    bool pulledToEMA = (high1 >= ema20H1 && close1 < ema20H1);
    bool bearishCandle = (close1 < open1);
    bool confirmMomentum = (close1 < iClose(Symbol(), PERIOD_H1, 2));

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

    //+------------------------------------------------------------------+
    //| 计算组合持仓浮动盈亏(点数近似) |
    //+------------------------------------------------------------------+
    double CalculateBasketProfitPoints()
    {
    double totalProfit = 0;
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    totalProfit += OrderProfit();
    }
    }
    }
    // 转换为点数近似值(黄金0.01手每点约1美元)
    double pointValue = LotSize
    100;
    if(pointValue > 0)
    return totalProfit / pointValue;
    return 0;
    }

    //+------------------------------------------------------------------+
    //| 检查并执行组合止盈 |
    //+------------------------------------------------------------------+
    bool CheckBasketTakeProfit()
    {
    double profitPoints = CalculateBasketProfitPoints();
    if(profitPoints >= BasketProfitPoints)
    {
    CloseAllOrders();
    Print("组合止盈触发,盈利点数:", profitPoints);
    return true;
    }
    return false;
    }

    //+------------------------------------------------------------------+
    //| 管理现有持仓:追踪止损+组合止盈 |
    //+------------------------------------------------------------------+
    void ManagePositions(double atr)
    {
    CheckBasketTakeProfit();

    // 单笔追踪止损管理
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
    {
    if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    double trailStep = atr 0.8;
    double newSL = 0;

    if(OrderType() == OP_BUY)
    {
    double profit = Bid - OrderOpenPrice();
    if(profit > atr
    1.2) // 盈利达到1.2倍ATR后启动追踪
    {
    newSL = Bid - trailStep;
    if(newSL > OrderStopLoss())
    OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
    }
    }
    else if(OrderType() == OP_SELL)
    {
    double profit = OrderOpenPrice() - Ask;
    if(profit > atr 1.2)
    {
    newSL = Ask + trailStep;
    if(newSL < OrderStopLoss() || OrderStopLoss() == 0)
    OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrNONE);
    }
    }
    }
    }
    }
    }

    //+------------------------------------------------------------------+
    //| 统计当前魔术号的持仓数量 |
    //+------------------------------------------------------------------+
    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);
    }
    }
    }
    }

    //+------------------------------------------------------------------+
    //| 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];

    // 获取ATR用于计算
    double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
    if(atr > 0) avgATR = (avgATR 0.95) + (atr 0.05);

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

    // 管理现有持仓
    int posCount = CountPositions();
    if(posCount > 0)
    {
    ManagePositions(atr);
    return;
    }

    // 检查最大持仓限制
    if(posCount >= MaxPositions)
    return;

    // 检测H4趋势结构(每4小时更新一次)
    static datetime lastTrendCheck = 0;
    if(TimeCurrent() - lastTrendCheck >= 14400)
    {
    h4TrendDirection = DetectH4TrendStructure();
    lastTrendCheck = TimeCurrent();
    }

    // 无明显趋势,跳过交易
    if(h4TrendDirection == 0)
    {
    Comment("H4周期无明显趋势结构");
    return;
    }

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

    // 检查回调入场
    double entryPrice = 0, sl = 0, tp = 0;
    if(CheckPullbackEntry(h4TrendDirection, entryPrice, sl, tp, atr))
    {
    int cmd = (h4TrendDirection == 1) ? OP_BUY : OP_SELL;
    int ticket = OrderSend(Symbol(), cmd, LotSize, entryPrice, 5, sl, tp, "Gold Apex", MagicNumber, 0, clrNONE);
    if(ticket < 0)
    Print("开仓失败,错误码:", GetLastError());
    else
    Print("开仓成功,方向:", cmd==OP_BUY?"买入":"卖出");
    }
    }
    //+------------------------------------------------------------------+
    `
    参考来源: 原创MQL4代码,策略框架基于2026年最新的结构性趋势分析方法设计。
    免责声明: 黄金交易因高波动性具有重大风险。本EA按“原样”提供,不保证盈利。实盘部署前请在模拟账户充分测试。历史表现不代表未来结果。
    ``