Summary: 黄金K线流EA是一款专为黄金设计的MQL4智能交易系统。纯粹依赖K线形态:pin bar、内包线和关键位影线拒绝,不使用传统指标。适合H1周期。




黄金K线流EA专为黄金(XAUUSD)构建,使用纯价格行为和K线形态识别。它避免滞后指标,而是关注实时K线结构:pin bar(长影线)、内包线(压缩)以及在关键位的影线拒绝。EA基于近期摆动高点和低点跟踪动态支撑/阻力,当K线拒绝发生时入场。独特的波动率调整机制在高影响新闻时段放宽止损。

推荐加载周期: H1
策略核心逻辑:
  • 结构识别:识别最近20根K线的摆动高点和低点作为动态支撑/阻力。

  • K线拒绝形态:寻找影线长度 > 2倍实体的pin bar,收盘价靠近另一端(在支撑/阻力位拒绝)。

  • 内包线突破:当内包线(区间在前一根K线内)以强势收盘价突破母K线区间时入场。

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


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

    //--- 输入参数及注释
    input double LotSize = 0.01; // 固定交易手数(黄金0.01手)
    input int SwingLookback = 20; // 摆动高低点检测回溯周期
    input double PinBarRatio = 2.0; // Pin bar影线/实体最小比例(影线 > 实体 × 比例)
    input double InsideBreakoutMultiplier = 1.2; // 内包线突破倍数(收盘价超出母K线区间此百分比)
    input int ATRPeriod = 14; // ATR周期(动态止损)
    input double ATRStopMultiplier = 1.8; // 止损倍数(ATR的倍数)
    input double ATRTakeMultiplier = 3.5; // 止盈倍数(ATR的倍数)
    input int MagicNumber = 202418; // 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;
    double lastSwingHigh = 0;
    double lastSwingLow = 0;

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

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

    //+------------------------------------------------------------------+
    //| 检测近期摆动高点(回溯期内最高价) |
    //+------------------------------------------------------------------+
    double GetSwingHigh()
    {
    int highestIdx = iHighest(Symbol(), PERIOD_H1, MODE_HIGH, SwingLookback, 2);
    return iHigh(Symbol(), PERIOD_H1, highestIdx);
    }

    //+------------------------------------------------------------------+
    //| 检测近期摆动低点(回溯期内最低价) |
    //+------------------------------------------------------------------+
    double GetSwingLow()
    {
    int lowestIdx = iLowest(Symbol(), PERIOD_H1, MODE_LOW, SwingLookback, 2);
    return iLow(Symbol(), PERIOD_H1, lowestIdx);
    }

    //+------------------------------------------------------------------+
    //| 检查pin bar形态(拒绝K线) |
    //+------------------------------------------------------------------+
    bool IsPinBar(int shift, bool &isBullishPin)
    {
    double open = iOpen(Symbol(), PERIOD_H1, shift);
    double close = iClose(Symbol(), PERIOD_H1, shift);
    double high = iHigh(Symbol(), PERIOD_H1, shift);
    double low = iLow(Symbol(), PERIOD_H1, shift);

    double body = MathAbs(close - open);
    if(body <= 0) return false;

    double upperWick = high - MathMax(open, close);
    double lowerWick = MathMin(open, close) - low;

    // 看涨pin bar:下影线长,上影线短,收盘靠近高点
    bool bullish = (lowerWick > body PinBarRatio) && (upperWick < body 0.5) && (close > open);
    // 看跌pin bar:上影线长,下影线短,收盘靠近低点
    bool bearish = (upperWick > body PinBarRatio) && (lowerWick < body 0.5) && (close < open);

    if(bullish)
    isBullishPin = true;
    else if(bearish)
    isBullishPin = false;

    return (bullish || bearish);
    }

    //+------------------------------------------------------------------+
    //| 检查内包线形态(区间在前一根K线内) |
    //+------------------------------------------------------------------+
    bool IsInsideBar(int shift, double &parentHigh, double &parentLow)
    {
    double currHigh = iHigh(Symbol(), PERIOD_H1, shift);
    double currLow = iLow(Symbol(), PERIOD_H1, shift);
    double prevHigh = iHigh(Symbol(), PERIOD_H1, shift + 1);
    double prevLow = iLow(Symbol(), PERIOD_H1, shift + 1);

    bool isInside = (currHigh <= prevHigh && currLow >= prevLow);
    if(isInside)
    {
    parentHigh = prevHigh;
    parentLow = prevLow;
    }
    return isInside;
    }

    //+------------------------------------------------------------------+
    //| 检查内包线突破(收盘价确认) |
    //+------------------------------------------------------------------+
    bool CheckInsideBreakout(int &cmd, double parentHigh, double parentLow)
    {
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double open1 = iOpen(Symbol(), PERIOD_H1, 1);
    double range = parentHigh - parentLow;
    double threshold = range InsideBreakoutMultiplier;

    // 向上突破:收盘价高于母K线高点+阈值,且收阳线
    if(close1 > parentHigh + threshold && close1 > open1)
    {
    cmd = OP_BUY;
    return true;
    }
    // 向下突破:收盘价低于母K线低点-阈值,且收阴线
    else if(close1 < parentLow - threshold && close1 < open1)
    {
    cmd = OP_SELL;
    return true;
    }
    return false;
    }

    //+------------------------------------------------------------------+
    //| 检查关键位的影线拒绝(价格触及水平+pin bar) |
    //+------------------------------------------------------------------+
    bool CheckRejectionAtLevel(double level, double price, bool isResistance, int &cmd)
    {
    double tolerance = 150
    Point; // 黄金的150点容差
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double open1 = iOpen(Symbol(), PERIOD_H1, 1);
    bool isPin = false;
    bool isBullishPin = false;

    if(IsPinBar(1, isBullishPin))
    {
    isPin = true;
    }

    if(!isPin) return false;

    // 阻力位拒绝(价格从下方触及阻力位,然后出现看跌pin bar)
    if(isResistance && MathAbs(high - level) <= tolerance && !isBullishPin)
    {
    cmd = OP_SELL;
    return true;
    }
    // 支撑位拒绝(价格从上方触及支撑位,然后出现看涨pin bar)
    else if(!isResistance && MathAbs(low - level) <= tolerance && isBullishPin)
    {
    cmd = OP_BUY;
    return true;
    }
    return false;
    }

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

    // 检查现有持仓
    if(CountPositions() > 0)
    return;

    // 更新动态摆动水平
    lastSwingHigh = GetSwingHigh();
    lastSwingLow = GetSwingLow();

    double atr = iATR(Symbol(), PERIOD_H1, ATRPeriod, 1);
    if(atr <= 0) atr = 200
    Point;

    int cmd = -1;
    double sl = 0, tp = 0;
    double ask = Ask;
    double bid = Bid;
    double close1 = iClose(Symbol(), PERIOD_H1, 1);
    double high = iHigh(Symbol(), PERIOD_H1, 1);
    double low = iLow(Symbol(), PERIOD_H1, 1);

    // 策略1:内包线突破
    double parentHigh = 0, parentLow = 0;
    if(IsInsideBar(2, parentHigh, parentLow))
    {
    if(CheckInsideBreakout(cmd, parentHigh, parentLow))
    {
    sl = (cmd == OP_BUY) ? (bid - atr ATRStopMultiplier) : (ask + atr ATRStopMultiplier);
    tp = (cmd == OP_BUY) ? (bid + atr ATRTakeMultiplier) : (ask - atr ATRTakeMultiplier);
    }
    }

    // 策略2:关键位pin bar拒绝(如果没有内包线信号)
    if(cmd == -1)
    {
    // 阻力位拒绝(摆动高点)
    if(CheckRejectionAtLevel(lastSwingHigh, high, true, cmd))
    {
    sl = ask + atr ATRStopMultiplier;
    tp = ask - atr
    ATRTakeMultiplier;
    }
    // 支撑位拒绝(摆动低点)
    else if(CheckRejectionAtLevel(lastSwingLow, low, false, cmd))
    {
    sl = bid - atr ATRStopMultiplier;
    tp = bid + atr
    ATRTakeMultiplier;
    }
    }

    // 执行交易
    if(cmd != -1)
    {
    int ticket = OrderSend(Symbol(), cmd, LotSize, (cmd==OP_BUY?ask:bid), 3, sl, tp, "Gold CandleFlow", MagicNumber, 0, clrNONE);
    if(ticket < 0)
    Print("开仓失败,错误码:", GetLastError());
    }
    }

    //+------------------------------------------------------------------+
    //| 统计当前魔术号的持仓数量 |
    //+------------------------------------------------------------------+
    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按“原样”提供,不保证盈利。实盘交易前请在模拟账户充分测试。历史表现不代表未来结果。
    ``