大家好!今天我们来聊聊MetaTrader 4中的水平指标。这个指标可以帮助我们设置触发水平,并在价格穿越这些水平时发送提醒通知,非常适合那些喜欢及时掌握市场动态的交易者。
首先,当你在设置中指定的触发水平(trigLv)被价格穿越时,如果开启了通知功能,指标会将消息推送到你的手机。同时,如果开启了警报(alert)功能,系统会发出声音警告。这种设计让你可以在图表上添加多个不同水平的指标,及时接收信号。
需要注意的是,触发水平在每根K线中只会被触发一次,下一根K线开盘后才会重新开始计算。这是为了避免在每个价格波动中触发过于频繁的信号。

//+------------------------------------------------------------------+ //| LevelIndicator.mq4 | //| Copyright 2022, © Cyberdev | //| https://www.mql5.com/en/users/cyberdev/seller | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, © Cyberdev" #property link "https://www.mql5.com/en/users/cyberdev/seller" #property version "1.00" #property strict #property indicator_chart_window #property indicator_plots 0 #include <ChartObjects\ChartObjectsLines.mqh> input bool alert = true; // 使用警报 input bool notification = true; // 启用推送通知 input double trigLv = 0.0; // 触发水平 input int deviation = 30; // 触发水平的偏差(点数) input int lineWidth = 1; // 线宽 input ENUM_LINE_STYLE lineStyle = STYLE_SOLID; // 线样式 input color lineColor = clrMediumSpringGreen; // 线颜色 input color inactivityColor = clrLightGray; // 非活动状态颜色 CChartObjectHLine lv, dvH, dvL; bool equal(double _v1, double _v2, double _epsilon) { return fabs(_v1 - _v2) <= fabs(_epsilon); } //+------------------------------------------------------------------+ //| 自定义指标初始化函数 | //+------------------------------------------------------------------+ int OnInit() { string name; double dv; color color_; name = "alert.lv-"; dv = deviation * SymbolInfoDouble(NULL, SYMBOL_POINT); color_ = (alert || notification) ? lineColor : inactivityColor; for (int n = 0; n <= INT_MAX && !IsStopped(); n++) { if (ObjectFind(0, name + (string)n) != 0) { if (!lv.Create(0, name + (string)n, 0, trigLv)) return INIT_FAILED; lv.Width(lineWidth); lv.Style(lineStyle); lv.Color(color_); dvH.Create(0, "alert.dvH-" + (string)n, 0, trigLv + dv); dvH.Width(1); dvH.Style(STYLE_DOT); dvH.Color(color_); dvL.Create(0, "alert.dvL-" + (string)n, 0, trigLv - dv); dvL.Width(1); dvL.Style(STYLE_DOT); dvL.Color(color_); break; } } if (!alert && !notification) Print("Level Indicator. Level ", lv.Price(0), " is inactive!"); if (trigLv == 0.0) Alert("Level Indicator. Set parameter \"trigLv\" to the desired value!"); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { //lv.Delete(); //dvH.Delete(); //dvL.Delete(); } //+------------------------------------------------------------------+ //| 自定义指标迭代函数 | //+------------------------------------------------------------------+ 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[] ) { static bool triggered = false; static datetime time_ = 0; if (!alert && !notification) return rates_total; if (equal(lv.Price(0), close[0], deviation * SymbolInfoDouble(NULL, SYMBOL_POINT))) { if (time_ != time[0]) time_ = time[0]; else return rates_total; if (!triggered) { if (alert) Alert("Level Indicator. Level ", NormalizeDouble(lv.Price(0), (int)SymbolInfoInteger(NULL, SYMBOL_DIGITS)), " triggered!"); if (notification) SendNotification("Level Indicator. Level " + (string)NormalizeDouble(lv.Price(0), (int)SymbolInfoInteger(NULL, SYMBOL_DIGITS)) + " triggered!"); } triggered = true; } else triggered = false; return(rates_total); } //+------------------------------------------------------------------+