MT4 ATR波动突破指标:收盘确认信号与完整MQL4源码

ATR(平均真实波幅)衡量价格波动幅度,并不预测涨跌方向。这个原创MT4指标以上一根已收盘K线为基准绘制波动上下轨,只有后一根K线真正收盘突破对应阈值时才标出箭头。它用于学习波动扩张信号,并非自动交易系统。

精确计算规则

对于K线i,上轨=Close[i+1]+1.6×ATR(14)[i+1];下轨=Close[i+1]-1.6×ATR(14)[i+1]。真实波幅TR取最高价减最低价、最高价与前收盘价差的绝对值、最低价与前收盘价差的绝对值三者中的最大值,ATR通过平台内置iATR平滑计算。若Close[i]高于上轨,出现看涨箭头;若低于下轨,出现看跌箭头。可选的实体确认要求相应的阳线或阴线。基准收盘价与ATR均来自前一根已经完成的K线,因此无需未来数据。

波动通道示意图

MT4 ATR波动突破指标:收盘确认信号与完整MQL4源码
示意图:深蓝线为收盘价,绿线为ATR上轨,橙线为ATR下轨;不代表真实市场行情。

数值示例

假设上一根已收盘K线收于1.1000,ATR(14)为0.0020,倍数为1.6,则上轨为1.1032,下轨为1.0968。下一根阳线若收于1.1036,收盘后才出现绿色箭头。如果盘中上冲至1.1032以上、最终收于1.1029,则不算突破。这样可以避免把盘中的短暂冲高误判为已确认信号。

完整MT4自定义指标源码

将代码保存为MT4_ATR_Band_Breakout.mq4,放入MQL4/Indicators。四个缓冲区分别保存上轨、下轨、看涨箭头和看跌箭头;箭头不是EA交易指令。

#property strict
 #property indicator_chart_window
 #property indicator_buffers 4
 #property indicator_color1 clrDodgerBlue
 #property indicator_color2 clrOrange
 #property indicator_color3 clrLimeGreen
 #property indicator_color4 clrTomato
 
 input int ATRPeriod = 14;
 input double ATRMultiplier = 1.6;
 input int MaxBarsToCalculate = 1500; // 0 = all eligible history
 input bool RequireBodyConfirmation = true;
 
 double UpperBand[], LowerBand[], BuyArrow[], SellArrow[];
 
 int OnInit()
 {
    if(ATRPeriod < 1 || ATRMultiplier <= 0 || MaxBarsToCalculate < 0)
       return(INIT_PARAMETERS_INCORRECT);
    SetIndexBuffer(0, UpperBand);
    SetIndexBuffer(1, LowerBand);
    SetIndexBuffer(2, BuyArrow);
    SetIndexBuffer(3, SellArrow);
    ArraySetAsSeries(UpperBand, true);
    ArraySetAsSeries(LowerBand, true);
    ArraySetAsSeries(BuyArrow, true);
    ArraySetAsSeries(SellArrow, true);
    SetIndexStyle(0, DRAW_LINE, STYLE_DASH, 1, clrDodgerBlue);
    SetIndexStyle(1, DRAW_LINE, STYLE_DASH, 1, clrOrange);
    SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, 1, clrLimeGreen);
    SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, 1, clrTomato);
    SetIndexArrow(2, 233);
    SetIndexArrow(3, 234);
    for(int j=0; j<4; j++) SetIndexEmptyValue(j, EMPTY_VALUE);
    SetIndexLabel(0, "Previous close + ATR multiple");
    SetIndexLabel(1, "Previous close - ATR multiple");
    SetIndexLabel(2, "Confirmed bullish breakout");
    SetIndexLabel(3, "Confirmed bearish breakout");
    IndicatorShortName("Confirmed ATR Breakout");
    return(INIT_SUCCEEDED);
 }
 
 int OnCalculate(const int rates_total, const int prev_calculated,
                 const datetime &time[], const double &open[],
                 const double &high[], const double &low[],
                 const double &close[], const long &tick_volume[],
                 const long &volume[], const int &spread[])
 {
    if(rates_total < ATRPeriod + 4) return(0);
    ArraySetAsSeries(open, true);
    ArraySetAsSeries(high, true);
    ArraySetAsSeries(low, true);
    ArraySetAsSeries(close, true);
    // Never plot on the unfinished candle.
    UpperBand[0] = LowerBand[0] = EMPTY_VALUE;
    BuyArrow[0] = SellArrow[0] = EMPTY_VALUE;
    int oldest = rates_total - ATRPeriod - 2;
    if(MaxBarsToCalculate > 0) oldest = MathMin(oldest, MaxBarsToCalculate);
    for(int i=oldest; i>=1; i--)
    {
       UpperBand[i] = LowerBand[i] = EMPTY_VALUE;
       BuyArrow[i] = SellArrow[i] = EMPTY_VALUE;
       // The threshold uses ONLY the previous closed candle.
       double a = iATR(NULL, 0, ATRPeriod, i+1);
       if(a <= 0) continue;
       double top = close[i+1] + ATRMultiplier*a;
       double bottom = close[i+1] - ATRMultiplier*a;
       UpperBand[i] = top;
       LowerBand[i] = bottom;
       bool bullish = close[i] > top &&
             (!RequireBodyConfirmation || close[i] > open[i]);
       bool bearish = close[i] < bottom &&
             (!RequireBodyConfirmation || close[i] < open[i]);
       if(bullish) BuyArrow[i] = low[i] - 0.2*a;
       if(bearish) SellArrow[i] = high[i] + 0.2*a;
    }
    return(rates_total);
 }

参数与调节

参数 默认值 含义
ATRPeriod 14 ATR计算周期
ATRMultiplier 1.6 距离前收盘价的ATR倍数
MaxBarsToCalculate 1500 历史计算上限;0表示所有符合条件的K线
RequireBodyConfirmation true 要求K线实体方向与突破方向一致

倍数越小通常信号越多,但噪声也可能增加;ATR周期越长,波动估计越平滑,但反应更慢。默认值仅用于教学,并非优化结果。

安装与验证步骤

  1. 在MT4选择File → Open Data Folder → MQL4 → Indicators。
  2. 保存源码,用MetaEditor编译并检查警告。
  3. 刷新Navigator,加载到历史数据充足的图表,并检查参数。
  4. 确认正在形成的索引0 K线不绘制箭头或上下轨;指标只显示已收盘K线。
  5. 在不同品种、周期、历史缺口和经纪商数据上测试,结合前向记录核对历史信号。

局限与风险

波动突破不代表趋势一定延续,更不保证交易盈利。震荡行情、消息冲击与跳空都可能产生假信号。指标不下单、不设置止损、不计算仓位,也未计入点差、手续费和滑点。经纪商修正历史数据后,历史图形可能改变,但代码并未主动引用未来K线。考虑交易前仍应检查支撑阻力及真实盈亏比。

如需EA集成、正式环境提醒或自定义指标缓冲区,可了解交易软件定制开发服务。

风险声明:本文仅供技术教育,不构成投资建议或收益承诺。代码结构已参照官方MQL4文档核对,但尚未在实际终端中编译或回测。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。