MT5指标 二创编译通过 · EX5 14KB 4 1

结构高低点 + 趋势线(MT5)(二创)

11111111·1 小时前发布
原作结构高低点 + 趋势线(MT4)XAUOF 官方二创本作

相对上一版改了:这个指标给我生成 mt5的呗

结构高低点 + 趋势线(MT5)(二创)

效果示意图 · AI 根据源码绘制

结构高低点_趋势线_MT5.mq5302
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//+------------------------------------------------------------------+

//|                                            SwingPointTrendLine.mq5 |

//|                                       量化交易摆动点趋势线指标      |

//| 基于 XAUOF 社区作品 #5《结构高低点 + 趋势线(MT4)》(作者 XAUOF 官方)二次创作 |

//| 改动:从 MQL4 转换为 MQL5 语法                                      |

//+------------------------------------------------------------------+

#property copyright "Swing Point Trend Line Indicator - MT5"
#property link      ""
#property version   "2.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

//---- 可调参数

input int    SwingBars = 5;              // 左右比较的K线数量(推荐3-10)

input bool   ShowTrendLines = true;      // 是否显示趋势线

input color  UpTrendColor = clrLime;     // 上升趋势线颜色

input color  DownTrendColor = clrRed;    // 下降趋势线颜色

input int    LineWidth = 2;              // 趋势线粗细(1-5)


//---- 指标缓冲区

double HighDots[];    // 摆动高点标记

double LowDots[];     // 摆动低点标记


//---- 全局变量

datetime lastHighTime1 = 0, lastHighTime2 = 0;  // 最近两个高点时间

double   lastHighPrice1 = 0, lastHighPrice2 = 0; // 最近两个高点价格

datetime lastLowTime1 = 0, lastLowTime2 = 0;    // 最近两个低点时间

double   lastLowPrice1 = 0, lastLowPrice2 = 0;   // 最近两个低点价格


string upTrendLineName = "UpTrendLine_MT5";
string downTrendLineName = "DownTrendLine_MT5";

//+------------------------------------------------------------------+

//| 指标初始化函数                                                      |

//+------------------------------------------------------------------+

int OnInit()
{
   // 设置指标缓冲区 0:摆动高点

   SetIndexBuffer(0, HighDots, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(0, PLOT_ARROW, 159);  // 小圆点符号

   PlotIndexSetString(0, PLOT_LABEL, "摆动高点");
   PlotIndexSetInteger(0, PLOT_ARROW_SHIFT, 0);
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrRed);
   
   // 设置指标缓冲区 1:摆动低点

   SetIndexBuffer(1, LowDots, INDICATOR_DATA);
   PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(1, PLOT_ARROW, 159);
   PlotIndexSetString(1, PLOT_LABEL, "摆动低点");
   PlotIndexSetInteger(1, PLOT_ARROW_SHIFT, 0);
   PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrLime);
   
   // 设置空值

   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   
   // 清空之前的趋势线

   ObjectDelete(0, upTrendLineName);
   ObjectDelete(0, downTrendLineName);
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+

//| 指标清理函数                                                        |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)
{
   // 删除趋势线对象

   ObjectDelete(0, upTrendLineName);
   ObjectDelete(0, downTrendLineName);
   Comment("");  // 清空提示文字

}

//+------------------------------------------------------------------+

//| 主计算函数                                                          |

//+------------------------------------------------------------------+

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 < SwingBars * 2 + 1)
      return(0);
   
   // 将数组设置为时间序列(索引 0 = 最新 K 线)

   ArraySetAsSeries(time, true);
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);
   ArraySetAsSeries(close, true);
   ArraySetAsSeries(HighDots, true);
   ArraySetAsSeries(LowDots, true);
   
   // 确定计算起点

   int start = SwingBars;
   if(prev_calculated > SwingBars)
      start = 1;  // 只计算最新的几根

   else
      start = rates_total - SwingBars - 1;  // 首次全量计算

   
   // 初始化缓冲区

   ArrayInitialize(HighDots, EMPTY_VALUE);
   ArrayInitialize(LowDots, EMPTY_VALUE);
   
   // 遍历K线识别摆动点(从旧到新)

   for(int i = start; i < rates_total - SwingBars; i++)
   {
      // 检查是否为摆动高点

      bool isSwingHigh = true;
      for(int j = 1; j <= SwingBars; j++)
      {
         // 检查索引范围

         if(i - j < 0 || i + j >= rates_total)
         {
            isSwingHigh = false;
            break;
         }
         // 左右都不能有更高的高点(注意时间序列方向)

         if(high[i - j] >= high[i] || high[i + j] >= high[i])
         {
            isSwingHigh = false;
            break;
         }
      }
      
      // 如果是摆动高点,标记并记录

      if(isSwingHigh)
      {
         HighDots[i] = high[i] + 10 * _Point;  // 在高点上方显示

         
         // 更新最近两个高点

         if(time[i] > lastHighTime1)
         {
            lastHighTime2 = lastHighTime1;
            lastHighPrice2 = lastHighPrice1;
            lastHighTime1 = time[i];
            lastHighPrice1 = high[i];
         }
         else if(time[i] > lastHighTime2)
         {
            lastHighTime2 = time[i];
            lastHighPrice2 = high[i];
         }
      }
      
      // 检查是否为摆动低点

      bool isSwingLow = true;
      for(int j = 1; j <= SwingBars; j++)
      {
         // 检查索引范围

         if(i - j < 0 || i + j >= rates_total)
         {
            isSwingLow = false;
            break;
         }
         // 左右都不能有更低的低点

         if(low[i - j] <= low[i] || low[i + j] <= low[i])
         {
            isSwingLow = false;
            break;
         }
      }
      
      // 如果是摆动低点,标记并记录

      if(isSwingLow)
      {
         LowDots[i] = low[i] - 10 * _Point;  // 在低点下方显示

         
         // 更新最近两个低点

         if(time[i] > lastLowTime1)
         {
            lastLowTime2 = lastLowTime1;
            lastLowPrice2 = lastLowPrice1;
            lastLowTime1 = time[i];
            lastLowPrice1 = low[i];
         }
         else if(time[i] > lastLowTime2)
         {
            lastLowTime2 = time[i];
            lastLowPrice2 = low[i];
         }
      }
   }
   
   // 绘制趋势线

   if(ShowTrendLines)
   {
      DrawTrendLines();
   }
   
   // 检查价格突破

   CheckBreakout(close[0]);
   
   return(rates_total);
}

//+------------------------------------------------------------------+

//| 绘制趋势线                                                          |

//+------------------------------------------------------------------+

void DrawTrendLines()
{
   // 删除旧的趋势线

   ObjectDelete(0, upTrendLineName);
   ObjectDelete(0, downTrendLineName);
   
   // 绘制下降趋势线(连接两个高点)

   if(lastHighTime1 > 0 && lastHighTime2 > 0 && lastHighTime1 != lastHighTime2)
   {
      ObjectCreate(0, downTrendLineName, OBJ_TREND, 0, lastHighTime2, lastHighPrice2, lastHighTime1, lastHighPrice1);
      ObjectSetInteger(0, downTrendLineName, OBJPROP_COLOR, DownTrendColor);
      ObjectSetInteger(0, downTrendLineName, OBJPROP_WIDTH, LineWidth);
      ObjectSetInteger(0, downTrendLineName, OBJPROP_RAY_RIGHT, true);  // 延长到右边

      ObjectSetInteger(0, downTrendLineName, OBJPROP_SELECTABLE, true);
      ObjectSetInteger(0, downTrendLineName, OBJPROP_BACK, false);  // 前景显示

   }
   
   // 绘制上升趋势线(连接两个低点)

   if(lastLowTime1 > 0 && lastLowTime2 > 0 && lastLowTime1 != lastLowTime2)
   {
      ObjectCreate(0, upTrendLineName, OBJ_TREND, 0, lastLowTime2, lastLowPrice2, lastLowTime1, lastLowPrice1);
      ObjectSetInteger(0, upTrendLineName, OBJPROP_COLOR, UpTrendColor);
      ObjectSetInteger(0, upTrendLineName, OBJPROP_WIDTH, LineWidth);
      ObjectSetInteger(0, upTrendLineName, OBJPROP_RAY_RIGHT, true);
      ObjectSetInteger(0, upTrendLineName, OBJPROP_SELECTABLE, true);
      ObjectSetInteger(0, upTrendLineName, OBJPROP_BACK, false);
   }
}

//+------------------------------------------------------------------+

//| 检查价格突破                                                        |

//+------------------------------------------------------------------+

void CheckBreakout(double currentPrice)
{
   bool breakoutDetected = false;
   string breakoutMsg = "";
   
   // 计算当前时间对应的趋势线价格

   datetime currentTime = TimeCurrent();
   
   // 检查上升趋势线突破(价格跌破)

   if(lastLowTime1 > 0 && lastLowTime2 > 0 && lastLowTime1 != lastLowTime2)
   {
      // 计算趋势线斜率

      double timeDiff = (double)(lastLowTime1 - lastLowTime2);
      if(timeDiff > 0)
      {
         double priceDiff = lastLowPrice1 - lastLowPrice2;
         double slope = priceDiff / timeDiff;
         
         // 计算当前时间点的趋势线价格

         double trendLinePrice = lastLowPrice1 + slope * (currentTime - lastLowTime1);
         
         // 价格跌破上升趋势线

         if(currentPrice < trendLinePrice)
         {
            breakoutDetected = true;
            breakoutMsg = "结构已破:跌破上升趋势线";
         }
      }
   }
   
   // 检查下降趋势线突破(价格突破向上)

   if(lastHighTime1 > 0 && lastHighTime2 > 0 && lastHighTime1 != lastHighTime2)
   {
      // 计算趋势线斜率

      double timeDiff = (double)(lastHighTime1 - lastHighTime2);
      if(timeDiff > 0)
      {
         double priceDiff = lastHighPrice1 - lastHighPrice2;
         double slope = priceDiff / timeDiff;
         
         // 计算当前时间点的趋势线价格

         double trendLinePrice = lastHighPrice1 + slope * (currentTime - lastHighTime1);
         
         // 价格突破下降趋势线

         if(currentPrice > trendLinePrice)
         {
            breakoutDetected = true;
            breakoutMsg = "结构已破:突破下降趋势线";
         }
      }
   }
   
   // 显示提示信息

   if(breakoutDetected)
   {
      Comment("\n", breakoutMsg, "\n当前价格: ", DoubleToString(currentPrice, _Digits));
   }
   else
   {
      Comment("");  // 清空提示

   }
}
真实 MT5 运行截图服务器把编译好的 EX5 挂到黄金 H1 图表上截的,上面的封面就是照着它画的
结构高低点 + 趋势线(MT5)(二创) 在 MT5 上的真实截图

作品说明

好的,我帮你把这个 MT4 指标转成 MT5 版本。核心逻辑完全保留,只是语法适配到 MQL5。


相对原作改动:

  • 从 MQL4 语法转换为 MQL5 语法
  • 去掉 #property strict(MQL5 默认严格)
  • 增加 #property indicator_buffersindicator_plots 声明
  • SetIndexStyle / SetIndexArrow / SetIndexLabel 改为 PlotIndexSetInteger / PlotIndexSetString
  • 数组用 ArraySetAsSeries 设置为时间序列模式,保持 MT4 习惯的索引方向
  • 对象操作函数参数微调(MT5 的 ObjectCreate 等接口略有不同)

使用说明

  1. 安装方法
  • 将代码保存为 结构高低点_趋势线_MT5.mq5
  • 放到 MT5 数据文件夹的 MQL5\Indicators\ 目录
  • 在 MetaEditor 中编译,或重启 MT5 自动编译
  • 在图表上右键「技术指标」→「Custom」→选择本指标
  1. 参数调整
  • SwingBars:摆动点识别灵敏度,数值越小识别的点越多(3-10 推荐)
  • ShowTrendLines:开关趋势线显示
  • UpTrendColor / DownTrendColor:上升/下降趋势线颜色
  • LineWidth:趋势线粗细
  1. 信号说明
  • 红色圆点标记摆动高点,绿色圆点标记摆动低点
  • 红色趋势线连接最近两个高点(下降趋势线)
  • 绿色趋势线连接最近两个低点(上升趋势线)
  • 图表左上角会提示「结构已破」信息:价格突破趋势线时触发
  1. 注意事项
  • 摆动点识别有滞后性,确认需要等待 SwingBars 根 K 线走完
  • 趋势线会自动延长到右侧,可手动拖动调整
  • 仅识别结构,不构成交易建议,需结合其他指标确认

评论

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

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