MT5指标编译通过 · EX5 17KB 自动发布 4 0

均线回踩突破信号指标

18150169931·1 小时前发布

M60/M20 顺势过滤,MACD+ATR 确认回踩突破,箭头标进出场

均线回踩突破信号指标

MT5 真机运行截图 · 服务器把编译好的 EX5 挂到黄金 H1 图表上截的,你装上去看到的就是这个样子

MA60_20_Pullback_Signal.mq5245 行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#property copyright "XAUOF Indicator Workshop"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_plots   6

//============ 参数区 ============
input int      InpFastMA     = 20;      // 快线周期(默认20)
input int      InpSlowMA     = 60;      // 慢线周期(默认60)
input int      InpMAMethod   = 0;       // 均线类型 0=SMA 1=EMA
input int      InpMACDFast   = 12;      // MACD 快线
input int      InpMACDSlow   = 26;      // MACD 慢线
input int      InpMACDSignal = 9;       // MACD 信号线
input int      InpATRPeriod  = 14;      // ATR 周期
input double   InpATRFactor  = 0.8;     // ATR 过滤系数(越大越严格,建议0.6~1.2)
input int      InpArrowGap   = 5;       // 箭头离K线距离(点)
input color    InpFastColor  = clrDodgerBlue; // 快线颜色
input color    InpSlowColor  = clrOrangeRed;  // 慢线颜色
input color    InpBuyColor   = clrLime;       // 多信号箭头颜色
input color    InpSellColor  = clrRed;        // 空信号箭头颜色

//============ 缓冲区 ============
double FastBuf[];   // 快线数据
double SlowBuf[];   // 慢线数据
double BuyBuf[];    // 多箭头上
double SellBuf[];   // 空箭头下
double BuyLbl[];    // 多信号标签
double SellLbl[];   // 空信号标签

//============ 指标句柄 ============
int hFastMA = INVALID_HANDLE;
int hSlowMA = INVALID_HANDLE;
int hMACD   = INVALID_HANDLE;
int hATR    = INVALID_HANDLE;

//============ 初始化 ============
int OnInit()
{
   SetIndexBuffer(0, FastBuf, INDICATOR_DATA);
   SetIndexBuffer(1, SlowBuf, INDICATOR_DATA);
   SetIndexBuffer(2, BuyBuf,  INDICATOR_DATA);
   SetIndexBuffer(3, SellBuf, INDICATOR_DATA);
   SetIndexBuffer(4, BuyLbl,  INDICATOR_DATA);
   SetIndexBuffer(5, SellLbl, INDICATOR_DATA);

   // 快线
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, InpFastColor);
   PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 2);
   PlotIndexSetString(0, PLOT_LABEL, "MA快线");

   // 慢线
   PlotIndexSetInteger(1, PLOT_LINE_COLOR, InpSlowColor);
   PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 2);
   PlotIndexSetString(1, PLOT_LABEL, "MA慢线");

   // 多箭头(上箭头 233)
   PlotIndexSetInteger(2, PLOT_ARROW, 233);
   PlotIndexSetInteger(2, PLOT_ARROW_SHIFT, 10);
   PlotIndexSetInteger(2, PLOT_LINE_COLOR, InpBuyColor);
   PlotIndexSetInteger(2, PLOT_LINE_WIDTH, 3);
   PlotIndexSetString(2, PLOT_LABEL, "买入信号");
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);

   // 空箭头(下箭头 234)
   PlotIndexSetInteger(3, PLOT_ARROW, 234);
   PlotIndexSetInteger(3, PLOT_ARROW_SHIFT, -10);
   PlotIndexSetInteger(3, PLOT_LINE_COLOR, InpSellColor);
   PlotIndexSetInteger(3, PLOT_LINE_WIDTH, 3);
   PlotIndexSetString(3, PLOT_LABEL, "卖出信号");
   PlotIndexSetDouble(3, PLOT_EMPTY_VALUE, EMPTY_VALUE);

   // 文字标签(多)
   PlotIndexSetInteger(4, PLOT_ARROW, 167);
   PlotIndexSetInteger(4, PLOT_ARROW_SHIFT, 20);
   PlotIndexSetInteger(4, PLOT_LINE_COLOR, InpBuyColor);
   PlotIndexSetInteger(4, PLOT_LINE_WIDTH, 1);
   PlotIndexSetString(4, PLOT_LABEL, "多标签");
   PlotIndexSetDouble(4, PLOT_EMPTY_VALUE, EMPTY_VALUE);

   // 文字标签(空)
   PlotIndexSetInteger(5, PLOT_ARROW, 167);
   PlotIndexSetInteger(5, PLOT_ARROW_SHIFT, -20);
   PlotIndexSetInteger(5, PLOT_LINE_COLOR, InpSellColor);
   PlotIndexSetInteger(5, PLOT_LINE_WIDTH, 1);
   PlotIndexSetString(5, PLOT_LABEL, "空标签");
   PlotIndexSetDouble(5, PLOT_EMPTY_VALUE, EMPTY_VALUE);

   // 创建句柄
   ENUM_MA_METHOD maMethod = (InpMAMethod == 1) ? MODE_EMA : MODE_SMA;

   hFastMA = iMA(_Symbol, PERIOD_CURRENT, InpFastMA, 0, maMethod, PRICE_CLOSE);
   if(hFastMA == INVALID_HANDLE) { Print("快线句柄创建失败"); return INIT_FAILED; }

   hSlowMA = iMA(_Symbol, PERIOD_CURRENT, InpSlowMA, 0, maMethod, PRICE_CLOSE);
   if(hSlowMA == INVALID_HANDLE) { Print("慢线句柄创建失败"); return INIT_FAILED; }

   hMACD = iMACD(_Symbol, PERIOD_CURRENT, InpMACDFast, InpMACDSlow, InpMACDSignal, PRICE_CLOSE);
   if(hMACD == INVALID_HANDLE) { Print("MACD句柄创建失败"); return INIT_FAILED; }

   hATR = iATR(_Symbol, PERIOD_CURRENT, InpATRPeriod);
   if(hATR == INVALID_HANDLE) { Print("ATR句柄创建失败"); return INIT_FAILED; }

   IndicatorSetString(INDICATOR_SHORTNAME, "MA回踩信号系统");
   IndicatorSetInteger(INDICATOR_DIGITS, _Digits);

   return INIT_SUCCEEDED;
}

//============ 反初始化 ============
void OnDeinit(const int reason)
{
   if(hFastMA != INVALID_HANDLE) IndicatorRelease(hFastMA);
   if(hSlowMA != INVALID_HANDLE) IndicatorRelease(hSlowMA);
   if(hMACD   != INVALID_HANDLE) IndicatorRelease(hMACD);
   if(hATR    != INVALID_HANDLE) IndicatorRelease(hATR);
}

//============ 主计算 ============
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[])
{
   int minBars = MathMax(InpSlowMA, MathMax(InpMACDSlow + InpMACDSignal, InpATRPeriod)) + 5;
   if(rates_total < minBars) return 0;

   // 句柄准备好
   if(BarsCalculated(hFastMA) < rates_total) return 0;
   if(BarsCalculated(hSlowMA) < rates_total) return 0;
   if(BarsCalculated(hMACD)   < rates_total) return 0;
   if(BarsCalculated(hATR)    < rates_total) return 0;

   // 拷贝数据到缓冲区
   if(CopyBuffer(hFastMA, 0, 0, rates_total, FastBuf) < rates_total) return 0;
   if(CopyBuffer(hSlowMA, 0, 0, rates_total, SlowBuf) < rates_total) return 0;

   // MACD 主线和信号线
   double macdMain[], macdSig[];
   ArrayResize(macdMain, rates_total);
   ArrayResize(macdSig,  rates_total);
   if(CopyBuffer(hMACD, 0, 0, rates_total, macdMain) < rates_total) return 0;
   if(CopyBuffer(hMACD, 1, 0, rates_total, macdSig)  < rates_total) return 0;

   // ATR
   double atrBuf[];
   ArrayResize(atrBuf, rates_total);
   if(CopyBuffer(hATR, 0, 0, rates_total, atrBuf) < rates_total) return 0;

   // 起点
   int start = (prev_calculated > 0) ? prev_calculated - 1 : minBars;
   if(start < 1) start = 1;

   if(prev_calculated == 0)
   {
      ArrayInitialize(BuyBuf,  EMPTY_VALUE);
      ArrayInitialize(SellBuf, EMPTY_VALUE);
      ArrayInitialize(BuyLbl,  EMPTY_VALUE);
      ArrayInitialize(SellLbl, EMPTY_VALUE);
   }

   for(int i = start; i < rates_total && !IsStopped(); i++)
   {
      // 默认空值
      BuyBuf[i]  = EMPTY_VALUE;
      SellBuf[i] = EMPTY_VALUE;
      BuyLbl[i]  = EMPTY_VALUE;
      SellLbl[i] = EMPTY_VALUE;

      if(i < 3) continue;

      double fast   = FastBuf[i];
      double slow   = SlowBuf[i];
      double fast1  = FastBuf[i-1];
      double slow1  = SlowBuf[i-1];
      double atr    = atrBuf[i];
      double macd   = macdMain[i];
      double macdS  = macdSig[i];
      double macd1  = macdMain[i-1];
      double macdS1 = macdSig[i-1];

      // 数据合法性
      if(fast == EMPTY_VALUE || slow == EMPTY_VALUE) continue;
      if(atr <= 0) continue;

      // 波动过滤:K线实体小于 ATR 系数认为在震荡,信号不明确,跳过
      double body = MathAbs(close[i] - open[i]);
      if(body < atr * InpATRFactor * 0.3) continue;

      // 趋势定义
      bool upTrend   = (fast > slow) && (slow > SlowBuf[i-2]) && (close[i] > slow);
      bool downTrend = (fast < slow) && (slow < SlowBuf[i-2]) && (close[i] < slow);

      if(upTrend)
      {
         // 前一根 K 最低点触碰或接近快线,且前一根已收回快线上方 → 视为回踩完成
         bool touchedFast = (low[i-1] <= fast1 * 1.0005) && (low[i-1] >= slow1 * 0.9995);
         bool pulledBack  = (close[i-1] > fast1);

         // MACD 金叉或柱状体由负转正
         bool macdBull = (macd1 <= macdS1 && macd > macdS) || (macd > 0 && macd1 <= 0);

         // 当前为阳线,收在快线上
         bool candleUp = (close[i] > open[i]) && (close[i] > fast);

         // 要远离均线,突破幅度要够
         bool strongBreak = (close[i] - fast) > atr * 0.2;

         if(touchedFast && pulledBack && macdBull && candleUp && strongBreak)
         {
            BuyBuf[i] = low[i] - InpArrowGap * _Point;
            BuyLbl[i] = low[i] - (InpArrowGap + 15) * _Point;
         }
      }
      else if(downTrend)
      {
         // 前一根 K 最高点触碰或接近快线,且前一根已收回快线下方 → 视为回踩完成
         bool touchedFast = (high[i-1] >= fast1 * 0.9995) && (high[i-1] <= slow1 * 1.0005);
         bool pulledBack  = (close[i-1] < fast1);

         // MACD 死叉或柱状体由正转负
         bool macdBear = (macd1 >= macdS1 && macd < macdS) || (macd < 0 && macd1 >= 0);

         // 当前为阴线,收在快线下方
         bool candleDown = (close[i] < open[i]) && (close[i] < fast);

         // 突破幅度
         bool strongBreak = (fast - close[i]) > atr * 0.2;

         if(touchedFast && pulledBack && macdBear && candleDown && strongBreak)
         {
            SellBuf[i] = high[i] + InpArrowGap * _Point;
            SellLbl[i] = high[i] + (InpArrowGap + 15) * _Point;
         }
      }
   }

   return rates_total;
}
MT5 运行截图 · 完整视图带价格轴和时间轴的整张图表(约 150 根 H1 K 线),上面的封面是它最近 80 根 K 线的放大视图
均线回踩突破信号指标 在 MT5 上的真实截图

作品说明

思路:把 M60、M20 两条均线做趋势过滤,只在顺势方向回踩时给箭头信号;用 MACD 柱状体做动能确认,ATR 做波动过滤避开震荡假突破,KDJ 去掉避免接刀。信号只在回踩结束那根 K 线标记,箭头清晰。

使用说明

  1. 安装:把文件放到 MT5数据目录/MQL5/Indicators/,在 MT5 里按 F4 打开 MetaEditor,点"编译",然后拖到图表上。
  1. 参数怎么调
  • InpFastMA / InpSlowMA:M60/M20 是分别指两根均线的周期,默认 20 和 60,直接保持即可实现你说的 M20 快、M60 慢。
  • InpATRFactor:越大过滤越严,信号越少越精。0.6 温和,1.0 严格。
  • InpArrowGap:箭头离 K 线距离,屏幕太挤就调大。
  • InpMAMethod:0 是简单均线 SMA,1 是指数均线 EMA,做黄金建议 1。
  1. 信号怎么看
  • 绿色上箭头:上升趋势中,价格回踩快线(M20)后收回上方,同时 MACD 出现金叉或柱状体翻红→多。
  • 红色下箭头:下跌趋势中,价格回踩快线后收回下方,MACD 死叉或柱状体翻绿→空。
  • 只有同时满足"趋势方向 + 回踩完成 + MACD 共振 + 突破幅度足够 + 波动过滤"才画箭头,比单纯均线交叉干净得多。
  1. 接刀怎么规避的
  • 趋势方向由均线多空排列确定,逆势不画信号;
  • MACD 必须确认动能反转,单靠均线不触发;
  • ATR 过滤掉 K 线实体太小的震荡假突破(你说的"接刀"就是这种);
  • KDJ 故意去掉:KDJ 在趋势里反手频繁,容易在均线顺势中给出反方向误报,越加越乱。
  1. 平仓信号:反向箭头出现即视为平仓/反手信号。也可以用价格有效跌破慢线(M60)作为趋势结束的结束标志。
  1. 注意事项
  • 只在前一根 K 的回踩完成后当前 K 突破才画箭头,所以图表上信号会比较稀少但可靠;如果觉得太少,把 InpATRFactor 降到 0.5 试试。
  • 箭头用的是 MT5 内置的 Wingdings 233/234,在任何主题下都能显示。
  • 建议配合更高周期(H4)的同样指标判断大方向,避免小周期与主趋势打架。

AI 解读根据源码逐行分析写成,供参考,以实际运行为准

这是什么

这是一个跑在 MT5 图表主窗口上的指标。它画两条均线(默认 M20 快线、M60 慢线),然后用一组条件筛出「顺势回踩结束后的突破」那一刻,在 K 线上打一个箭头。

它不预测行情,只做筛选。目标是让箭头比单纯的均线交叉干净,把震荡里的假突破尽量滤掉。

逻辑原理

趋势方向:快线在慢线上方、慢线本身还在向上(slow > SlowBuf[i-2])、收盘价在慢线上方,才算上升趋势。下跌趋势反过来。逆势不画信号。

回踩完成:看前一根 K 线。上升趋势里,前一根的最低点要碰到快线附近(同时也得在慢线上方),并且收盘价收回了快线上方;下跌趋势里,前一根最高点碰快线,收盘收回快线下方。

MACD 动能确认:必须出现金叉(主线从下穿信号线到上方)或者柱状体由负转正,下跌方向则是死叉或柱状体由正转负。光有均线不触发。

波动过滤:当前 K 线实体如果小于 ATR × InpATRFactor × 0.3,认为在震荡,跳过。还有一条硬要求:收盘价离快线的距离要大于 ATR × 0.2,突破幅度不够也不画。

当前 K 线形态:上升要阳线且收在快线上方,下跌要阴线且收在快线下方。

五条全满足才画箭头。买入箭头画在最低点下方,卖出箭头画在最高点上方,另有文字标签跟着。

参数说明

  • InpFastMA(20):快线周期,也是回踩参照的那条线。
  • InpSlowMA(60):慢线周期,定趋势方向。
  • InpMAMethod(0):均线类型,0=SMA 简单均线,1=EMA 指数均线。
  • InpMACDFast(12):MACD 快线周期。
  • InpMACDSlow(26):MACD 慢线周期。
  • InpMACDSignal(9):MACD 信号线周期。
  • InpATRPeriod(14):ATR 周期,用于波动过滤。
  • InpATRFactor(0.8):ATR 过滤系数,越大越严、信号越少。作者建议 0.6~1.2。
  • InpArrowGap(5):箭头离 K 线的距离,单位是点,图太挤就调大。
  • InpFastColor(clrDodgerBlue):快线颜色。
  • InpSlowColor(clrOrangeRed):慢线颜色。
  • InpBuyColor(clrLime):多信号箭头颜色。
  • InpSellColor(clrRed):空信号箭头颜色。

怎么用

把文件放进 MT5 数据目录的 MQL5/Indicators/,按 F4 打开 MetaEditor 编译,然后从导航器拖到图表上。

它是主图指标,挂在要看的品种和周期上即可。图上你会看到两条均线,以及零散的绿色上箭头和红色下箭头。箭头只在回踩结束那根 K 线出现,不需要额外加载别的指标。

常见做法是配合更高周期(比如 H4)同款指标看大方向,大周期和小周期方向一致时再参考箭头。反向箭头可以当作平仓或反手的提示,也有人用价格有效跌破慢线作为趋势结束的标志——这两条是使用思路,代码本身只画箭头,不管仓位。

适用场景

它本质是趋势跟随逻辑,适合走出单边趋势、有明确回踩节奏的品种和周期。趋势越规整,箭头越有参考价值。

不适合来回震荡、没有方向的横盘行情——那正是它想过滤掉的部分,过滤后可能长时间不出信号。也不适合拿它做剥头皮,因为要等一根 K 回踩完成、下一根再确认,节奏偏慢。

风险提示

信号稀少:条件卡得比较死,图表上箭头不多。信号少不等于准,别把「少」当成「稳」。

有确认滞后:它用的是前一根的回踩加当前 K 的突破,等于要等行情走一段才确认,入场位置天然靠后。

概率不是必然:ATR 过滤和 MACD 共振只能减少震荡里的假信号,趋势里的回抽照样能打掉。箭头是提示,不是保证。

关于重绘:计算只读已收盘的历史 K 线数据用于判断,箭头画在当根 K 线上;当前未收盘 K 线的条件会随价格变动而变,最后一根上的箭头在收盘前可能来回出现和消失,收盘后才定下来。

滑点与点差:箭头给出的是信号位置,不是成交价。实盘要自己考虑点差和滑点。

参数依赖:换品种、换周期后,原来的 ATR 系数和均线周期未必还合适,需要自己重新看。

不涉及仓位:这是指标,代码里没有任何手数、止损、止盈的计算,资金管理要你自己做。

常见问题

这个指标会重绘吗?
历史 K 线上的箭头是固定的。但当前正在走的那根 K 线,条件会随价格变化,箭头可能在收盘前出现又消失,收盘后才定下来。
MT4 能用吗?
这是 MQL5 写的,只能用在 MT5。MT4 不识别这套指标句柄写法,需要转写。
可以用在 XAUUSD 以外的品种吗?
代码里没有限定品种,任何品种都能挂。但均线周期和 ATR 系数是按参数设的,换品种后要自己观察信号是否还合理。
默认参数适合什么周期?
代码没有限制周期,参数写的是 M20/M60,作者说明里建议做黄金时把均线类型改成 EMA(InpMAMethod=1)。具体周期要自己测试。
为什么图表上箭头这么少?
信号要同时满足趋势方向、回踩完成、MACD 共振、实体和幅度过滤五个条件,本来就少。想多一些可以把 InpATRFactor 从 0.8 降到 0.5 左右再试。

MA Pullback Breakout Signal Indicator for MT5

An MT5 chart indicator that plots two moving averages and marks pullback breakout arrows only when trend direction, MACD momentum and ATR volatility filters all agree. Buy and sell arrows appear when price pulls back to the fast MA and then breaks away in the trend direction.

Source code is shown above. Compiled EX5 files are available to VIP members.

评论

后可以留言,比如反馈用着怎么样、想加什么功能。

还没有人评论,来抢个沙发。