
外部指标的提醒和注释(第二部分)
简介
第一篇文章“外部指标的提醒和注释”讨论了从使用图表中显示的 Wingdings 符号作为信息源的指标获得数据的方法。
这里,我们将看到如何从指标缓冲区获得数据以及利用它们将某些指标事件通知用户。
但是,尽管有一组用于在读取图形绘制的参数时获得所需特征的运算符,指标缓冲区的参数只能进行设置。 比如,这里我们无法得到颜色。 我们所能得到的只有价格和时间特征。 作为示例,我们将取一个指标,基于的原则是在指标中显示信息时颜色发生变化。
在这种指标的指标缓冲区中存储信息的选项之一是:使用两个指标缓冲区显示一条线。 为每个缓冲区分配一个静态色。 信息的输出方式是:在任一时点的信息显示中只涉及两个缓冲区中的一个。 活跃的缓冲区的参数值大于零。 不活跃的缓冲区的参数值为零。
这样,我们得到可以进行分析的值,以输出指标中的变化信息。
指标缓冲区数据输出
MQL 资源允许我们输入/输出数据到八个指标缓冲区。
因此,我们将对所有的八个缓冲区进行分析。
分析中采用的条件如下: 当指标值从零变为有效值,或者是逆向改变,即从有效值变为零时,必须通知用户。 我们取第一个变化的指标缓冲区的值,并从第一个柱或用户在外部变量中指定的其他任何柱开始逐个检查所有缓冲区的值。
extern int StartBar=1; // Start bar
我们以 Slope Trend_mtf 指标作为示例,其输出信息如下:
图 1
检测到变化之后,由指标记录变化,指标运行停止。
MQL4 有多个可用于输出数据的功能:
- 提醒窗口;
- 在指标窗口中的注释;
- 播放声音文件。
下面显示了一个输出数据的子程序。
//+------------------------------------------------------------------+ //| AlertSignal_v1 indicator Subfunction | //+------------------------------------------------------------------+ void AlertComment (string text) { if (SignalAlert == true) Alert (text); if (SignalComment == true) Comment (text); if (SignalPlay == true) PlaySound(Signal); }
我们添加由用户决定进行控制的必要变量。
我们也提供了输出由用户指定的用户注释的可能性。
extern int StartBar=1; // Start bar extern string indicator = "Insert Indicator Name"; // Custom indicator extern bool VLine = true; // Vertical line output switch extern bool SignalAlert=false; // Alert window output switch extern bool SignalComment=true; // Comment output switch extern bool SignalPlay= false; // Sound play switch extern string Signal = "alert.wav"; // Custom sound file extern string UserText = "direction changed"; // User comment
其中:
StartBar
表明计算开始的变量:
indicator
我们在这里输入希望获得数据的指标名称。
其后跟随了一系列的逻辑参数,用户可以用来处理相关程序参数的输出。
VLine
表明是否在指标参数的变化点显示(true)或不显示(false)垂直线。
SignalAlert
表明是否在指标参数变化时显示(true)或不显示(false)信息窗口。
SignalComment
表明是否在指标参数变化时在图表输出(true)或不输出(false)数据。
SignalPlay
表明是否在指标参数变化时播放(true)或不播放(false)声音文件。
Signal
我们在这里输入当指标参数变化时要播放的声音文件的名称。
UserText
如果我们不喜欢标准文本,在这里输入当指标参数变化时我们希望在图表中看到的文本。
类似的,你可以添加一个发送相关信息到电子邮件的参数。
现在,我们只需读取指标数据并使用我们的设置进行处理。
处理程序块的代码如下:
for(int ii=StartBar;ii<ExtBars;ii++) for(int i=0;i<8;i++) { First =iCustom(NULL,0,indicator,i,ii); Second=iCustom(NULL,0,indicator,i,ii+1); if((Second==0 && First>0) || (Second>0 && First==0)) { text=StringConcatenate(Str+" - On ",ii+1," bar ",Symbol()," - ",indicator," "); AlertComment(text+UserText); if(VLine==true) ObjectCreate(STRING_ID+"VL_"+i,0,0,Time[ii],Bid); return; } }
在实现了指定的方法和设置了显示垂直线的相关参数后,图表呈现如下:
图 2
总结
尽管有很多关于 MQL4 存在瑕疵的看法,但该语言提供了丰富的功能,可以广泛的用于编写各种交易和信息应用程序。 另外,它支持直觉学习,允许对大型的现成应用程序库进行访问。
信息指标的完整代码如下:
//+------------------------------------------------------------------+ //| AlertSignal_v1.mq4 | //| Copyright © 2009, WWW.FIBOOK.RU | //| http://fibook.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, FIBOOK.RU" #property link "http://fibook.ru" #define Str "http://fibook.ru" #define STRING_ID "AlertSignal_" #property indicator_chart_window extern int StartBar=1; // Start bar extern string indicator="Insert Indicator Name"; // Custom indicator extern bool VLine=true; // Vertical line output switch extern bool SignalAlert=false; // Alert window output switch extern bool SignalComment=true; // Comment output switch extern bool SignalPlay=false; // Sound play switch extern string Signal="alert.wav"; // Custom sound file extern string UserText="direction changed"; // User comment IndicatorShortName("AlertSignal_v1 "); //+------------------------------------------------------------------+ //| AlertSignal_v1 indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- for(int i=0;i<8;i++) ObjectDelete(STRING_ID+"VL_"+i); //---- return(0); } //+------------------------------------------------------------------+ //| AlertSignal_v1 indicator iteration function | //+------------------------------------------------------------------+ int start() { string text; int ExtBars=150; // Number of the last bars for calculation double First; // Internal variable double Second; // Internal variable int per; // Internal variable if(indicator=="Insert Indicator Name") { Alert(indicator); return; } if(per==Time[0]) return; // Adjust once in a period per=Time[0]; //---- for(int ii=StartBar;ii<ExtBars;ii++) for(int i=0;i<8;i++) { First =iCustom(NULL,0,indicator,i,ii); Second=iCustom(NULL,0,indicator,i,ii+1); if((Second==0 && First>0) || (Second>0 && First==0)) { text=StringConcatenate(Str+" - On ",ii+1," bar ",Symbol()," - ",indicator," "); AlertComment(text+UserText); if(VLine==true) ObjectCreate(STRING_ID+"VL_"+i,0,0,Time[ii],Bid); return; } } //---- return(0); } //+------------------------------------------------------------------+ //| AlertSignal_v1 indicator Subfunction | //+------------------------------------------------------------------+ void AlertComment(string text) { if(SignalAlert==true) Alert(text); if(SignalComment==true) Comment(text); if(SignalPlay==true) PlaySound(Signal); } //+------------------------------------------------------------------+
本文由MetaQuotes Ltd译自俄文
原文地址: https://www.mql5.com/ru/articles/1372
注意: MetaQuotes Ltd.将保留所有关于这些材料的权利。全部或部分复制或者转载这些材料将被禁止。
This article was written by a user of the site and reflects their personal views. MetaQuotes Ltd is not responsible for the accuracy of the information presented, nor for any consequences resulting from the use of the solutions, strategies or recommendations described.
