
DoEasy 函数库中的时间序列(第五十六部分):自定义指标对象,从集合中的指标对象获取数据
内容目录
概述
继续创建功能,从而在 EA 中处理基于函数库数据的指标。
我们已创建了标准指标对象类,并开始将它们安置在集合列表中。 为了拥有完整的“设置”,仅缺少自定义指标对象。 今天,即将创建此类对象。 其构造概念与创建标准指标对象的概念不同。
因为我们在终端中已拥有最终的标准指标集合,所以我可以基于每个指标的绝对且事先已知的数据来创建标准指标对象。 那就是我要做的:指定创建指标相对应的严格设置参数组合。 期间,自定义指标可以具有任何事先不知道的参数集合。
出于此原因,自定义指标对象的创建概念与标准指标对象的创建不同。
如果要创建标准指标对象,调用每个指标自己的创建方法就足够了,且在输入中包含其所有必要的属性,与此同时,为了可以采用初期未知数量和参数类型来创建自定义指标,必须将已初始填充的指标输入结构数组传递给其创建方法。 创建指标所需的所有参数和属性都将从该方法中获取。 不幸的是,因此,函数库用户将不得不单独填充这个输入结构数组,从而在程序中创建自定义指标。
鉴于我正试图创建一个函数库来简化图形的创建,因此我选择了以下几种在程序中调用已创建指标的若干变体:所创建的每个指标都将为其自身标记唯一的 ID。 我能够通过该 ID 调用每个已创建的指标。 幸运的是,我创建了它们,为它们设置了 ID,且我很清楚其中哪个 ID 与程序中创建的某个或另一个指标相对应。
除了 ID 外,还有通过其所有参数调用指标的方法。 即,可依据创建该指标对象时指定的相同参数获取指标对象,然后处理得到的对象 - 从指标中获取数据(今天将创建数据接收方法),并将其复制到数组中,用于统计研究(在后续文章中)。
改进库类
与往常一样,首先,在文件 \MQL5\Include\DoEasy\Data.mqh 里输入新的函数库消息。
添加新消息的索引:
MSG_LIB_TEXT_IND_TEXT_GROUP, // Indicator group MSG_LIB_TEXT_IND_TEXT_GROUP_TREND, // Trend indicator MSG_LIB_TEXT_IND_TEXT_GROUP_OSCILLATOR, // Oscillator MSG_LIB_TEXT_IND_TEXT_GROUP_VOLUMES, // Volumes MSG_LIB_TEXT_IND_TEXT_GROUP_ARROWS, // Arrow indicator MSG_LIB_TEXT_IND_TEXT_ID, // Indicator ID MSG_LIB_TEXT_IND_TEXT_EMPTY_VALUE, // Empty value for plotting where nothing will be drawn MSG_LIB_TEXT_IND_TEXT_SYMBOL, // Indicator symbol MSG_LIB_TEXT_IND_TEXT_NAME, // Indicator name MSG_LIB_TEXT_IND_TEXT_SHORTNAME, // Indicator short name MSG_LIB_TEXT_IND_TEXT_IND_PARAMETERS, // Indicator parameters MSG_LIB_TEXT_IND_TEXT_APPLIED_VOLUME, // Volume type for calculation MSG_LIB_TEXT_IND_TEXT_PERIOD, // Averaging period MSG_LIB_TEXT_IND_TEXT_FAST_PERIOD, // Fast MA period MSG_LIB_TEXT_IND_TEXT_SLOW_PERIOD, // Slow MA period MSG_LIB_TEXT_IND_TEXT_SIGNAL, // Difference averaging period MSG_LIB_TEXT_IND_TEXT_TENKAN_PERIOD, // Tenkan-sen period MSG_LIB_TEXT_IND_TEXT_KIJUN_PERIOD, // Kijun-sen period MSG_LIB_TEXT_IND_TEXT_SPANB_PERIOD, // Senkou Span B period MSG_LIB_TEXT_IND_TEXT_JAW_PERIOD, // Period for jaw line calculation MSG_LIB_TEXT_IND_TEXT_TEETH_PERIOD, // Period for teeth line calculation MSG_LIB_TEXT_IND_TEXT_LIPS_PERIOD, // Period for lips line calculation MSG_LIB_TEXT_IND_TEXT_JAW_SHIFT, // Horizontal shift of jaws line MSG_LIB_TEXT_IND_TEXT_TEETH_SHIFT, // Horizontal shift of teeth line MSG_LIB_TEXT_IND_TEXT_LIPS_SHIFT, // Horizontal shift of lips line MSG_LIB_TEXT_IND_TEXT_SHIFT, // Horizontal shift of the indicator MSG_LIB_TEXT_IND_TEXT_MA_METHOD, // Smoothing type MSG_LIB_TEXT_IND_TEXT_APPLIED_PRICE, // Price type or handle MSG_LIB_TEXT_IND_TEXT_STD_DEVIATION, // Number of standard deviations MSG_LIB_TEXT_IND_TEXT_DEVIATION, // Deviation of channel borders from the central line MSG_LIB_TEXT_IND_TEXT_STEP, // Price change step — acceleration factor MSG_LIB_TEXT_IND_TEXT_MAXIMUM, // Maximum step MSG_LIB_TEXT_IND_TEXT_KPERIOD, // K-period (number of bars for calculation) MSG_LIB_TEXT_IND_TEXT_DPERIOD, // D-period (primary smoothing period) MSG_LIB_TEXT_IND_TEXT_SLOWING, // Final smoothing MSG_LIB_TEXT_IND_TEXT_PRICE_FIELD, // Stochastic calculation method MSG_LIB_TEXT_IND_TEXT_CMO_PERIOD, // Chande Momentum period MSG_LIB_TEXT_IND_TEXT_SMOOTHING_PERIOD, // Smoothing factor period MSG_LIB_TEXT_IND_TEXT_CUSTOM_PARAM, // Input parameter //--- CIndicatorsCollection MSG_LIB_SYS_FAILED_ADD_IND_TO_LIST, // Error. Failed to add indicator object to the list MSG_LIB_SYS_INVALID_IND_POINTER, // Error. Invalid pointer to indicator object is passed MSG_LIB_SYS_IND_ID_EXIST, // Error. Indicator object with ID already exists }; //+------------------------------------------------------------------+
以及与新添加的索引相对应的文本消息:
{"Arrow indicator"}, {"Indicator ID"}, {"Empty value for plotting, for which there is no drawing"}, {"Indicator symbol"}, {"Indicator name"}, {"Indicator shortname"}, {"Indicator parameters"}, {"Volume type for calculation"}, {"Averaging period"}, {"Fast MA period"}, {"Slow MA period"}, {"Averaging period for their difference"}, {"Tenkan-sen period"}, {"Kijun-sen period"}, {"Senkou Span B period"}, {"Period for the calculation of jaws"}, {"Period for the calculation of teeth"}, {"Period for the calculation of lips"}, {"Horizontal shift of jaws"}, {"Horizontal shift of teeth"}, {"Horizontal shift of lips"}, {"Horizontal shift of the indicator"}, {"Smoothing type"}, {"Price type or handle"}, {"Number of standard deviations"}, {"Deviation of boundaries from the midline"}, {"Price increment step - acceleration factor"}, {"Maximum value of step"}, {,"K-period (number of bars for calculations)"}, {"D-period (period of first smoothing)"}, {"Final smoothing"}, {"Stochastic calculation method"}, {"Chande Momentum period"}, {"Smoothing factor period"}, {"Input parameter"}, {"Error. Failed to add indicator object to list"}, {"Error. Invalid pointer to indicator object passed"}, {"Error. There is already exist an indicator object with ID"}, }; //+---------------------------------------------------------------------+
创建标准指标对象时,在其构造函数中一次性设置与指标类型相对应的分组:趋势、箭头、震荡指标或交易量指标。 由于创建的指标类型是预先已知的,因此我可以针对标准指标这样做。 对于自定义指标,您无法事先知道其类型(指标所属的分组),且无法在对象类构造函数中进行设置。 因此,我会再创建一个指标组 “any”,该指标分组将自动为新创建的对象设置。 创建对象后,将为客户提供设置自定义指标所属分组的函数。
在文件 \MQL5\Include\DoEasy\Defines.mqh 的指标分组枚举里加入一个新的常量:
//+------------------------------------------------------------------+ //| Indicator group | //+------------------------------------------------------------------+ enum ENUM_INDICATOR_GROUP { INDICATOR_GROUP_TREND, // Trend indicator INDICATOR_GROUP_OSCILLATOR, // Oscillator INDICATOR_GROUP_VOLUMES, // Volumes INDICATOR_GROUP_ARROWS, // Arrow indicator INDICATOR_GROUP_ANY, // Any indicator }; //+------------------------------------------------------------------+
由于我们决定通过唯一的 ID 调用已创建指标,因此我们应该为指标添加一个新的整数型属性枚举:
//+------------------------------------------------------------------+ //| Indicator integer properties | //+------------------------------------------------------------------+ enum ENUM_INDICATOR_PROP_INTEGER { INDICATOR_PROP_STATUS = 0, // Indicator status (from enumeration ENUM_INDICATOR_STATUS) INDICATOR_PROP_TYPE, // Indicator type (from enumeration ENUM_INDICATOR) INDICATOR_PROP_TIMEFRAME, // Indicator timeframe INDICATOR_PROP_HANDLE, // Indicator handle INDICATOR_PROP_GROUP, // Indicator group INDICATOR_PROP_ID, // Indicator ID }; #define INDICATOR_PROP_INTEGER_TOTAL (6) // Total number of indicator integer properties #define INDICATOR_PROP_INTEGER_SKIP (0) // Number of indicator properties not used in sorting //+------------------------------------------------------------------+
并分别将指标的整数型属性的数量从 5 增加到 6。
为了能够依据 ID 在列表中搜索指标,加入新的依据 ID 指标排序标准:
//+------------------------------------------------------------------+ //| Possible indicator sorting criteria | //+------------------------------------------------------------------+ #define FIRST_INDICATOR_DBL_PROP (INDICATOR_PROP_INTEGER_TOTAL-INDICATOR_PROP_INTEGER_SKIP) #define FIRST_INDICATOR_STR_PROP (INDICATOR_PROP_INTEGER_TOTAL-INDICATOR_PROP_INTEGER_SKIP+INDICATOR_PROP_DOUBLE_TOTAL-INDICATOR_PROP_DOUBLE_SKIP) enum ENUM_SORT_INDICATOR_MODE { //--- Sort by integer properties SORT_BY_INDICATOR_INDEX_STATUS = 0, // Sort by indicator status SORT_BY_INDICATOR_TYPE, // Sort by indicator type SORT_BY_INDICATOR_TIMEFRAME, // Sort by indicator timeframe SORT_BY_INDICATOR_HANDLE, // Sort by indicator handle SORT_BY_INDICATOR_GROUP, // Sort by indicator group SORT_BY_INDICATOR_ID, // Sort by indicator ID //--- Sort by real properties SORT_BY_INDICATOR_EMPTY_VALUE = FIRST_INDICATOR_DBL_PROP,// Sort by the empty value for plotting where nothing will be drawn //--- Sort by string properties SORT_BY_INDICATOR_SYMBOL = FIRST_INDICATOR_STR_PROP, // Sort by indicator symbol SORT_BY_INDICATOR_NAME, // Sort by indicator name SORT_BY_INDICATOR_SHORTNAME, // Sort by indicator short name }; //+------------------------------------------------------------------+
鉴于已为指标对象添加了新属性,因此现在可以稍微改善 \MQL5\Include\DoEasy\Objects\Indicators\IndicatorDE.mqh 中的抽象指标对象类。
在类的公开部分中,编写两个设置和获取 “indicator ID” 属性的方法:
//--- Set indicator’s (1) group, (2) empty value of buffers, (3) name, (4) short name, (5) indicator ID void SetGroup(const ENUM_INDICATOR_GROUP group) { this.SetProperty(INDICATOR_PROP_GROUP,group); } void SetEmptyValue(const double value) { this.SetProperty(INDICATOR_PROP_EMPTY_VALUE,value); } void SetName(const string name) { this.SetProperty(INDICATOR_PROP_NAME,name); } void SetShortName(const string shortname) { this.SetProperty(INDICATOR_PROP_SHORTNAME,shortname); } void SetID(const int id) { this.SetProperty(INDICATOR_PROP_ID,id); } //--- Return indicator’s (1) status, (2) group, (3) timeframe, (4) type, (5) handle, (6) ID, //--- (7) empty value of buffers, (8) name, (9) short name, (10) symbol ENUM_INDICATOR_STATUS Status(void) const { return (ENUM_INDICATOR_STATUS)this.GetProperty(INDICATOR_PROP_STATUS);} ENUM_INDICATOR_GROUP Group(void) const { return (ENUM_INDICATOR_GROUP)this.GetProperty(INDICATOR_PROP_GROUP); } ENUM_TIMEFRAMES Timeframe(void) const { return (ENUM_TIMEFRAMES)this.GetProperty(INDICATOR_PROP_TIMEFRAME); } ENUM_INDICATOR TypeIndicator(void) const { return (ENUM_INDICATOR)this.GetProperty(INDICATOR_PROP_TYPE); } int Handle(void) const { return (int)this.GetProperty(INDICATOR_PROP_HANDLE); } int ID(void) const { return (int)this.GetProperty(INDICATOR_PROP_ID); } double EmptyValue(void) const { return this.GetProperty(INDICATOR_PROP_EMPTY_VALUE); } string Name(void) const { return this.GetProperty(INDICATOR_PROP_NAME); } string ShortName(void) const { return this.GetProperty(INDICATOR_PROP_SHORTNAME); } string Symbol(void) const { return this.GetProperty(INDICATOR_PROP_SYMBOL); }
在标准指标对象中,我们可以添加显示每个指标参数说明的方法,因为我们肯定知道特定指标的哪个参数需要说明。 而对于显示自定义指标参数的说明,我们无法确切事先了解未知指标的每个参数的用途。 因此,只需针对每个顺序参数显示来自 MqlParam 指标参数数组的说明。
在抽象指标对象类的公开部分中,声明一个方法来显示 MqlParam 结构参数的说明,还有另外两个方法:依据索引从指标对象获取数据和柱线时间:
//--- Return description of (1) type, (2) status, (3) group, (4) timeframe, (5) empty value of indicator, (6) parameter of m_mql_param array string GetTypeDescription(void) const { return m_ind_type_description; } string GetStatusDescription(void) const; string GetGroupDescription(void) const; string GetTimeframeDescription(void) const; string GetEmptyValueDescription(void) const; string GetMqlParamDescription(const int index) const; //--- Display the description of indicator object properties in the journal (full_prop=true - all properties, false - supported ones only) void Print(const bool full_prop=false); //--- Display (1) a short description, (2) description of indicator object parameters in the journal (implementation in the descendants) virtual void PrintShort(void) {;} virtual void PrintParameters(void) {;} //--- Return data of specified buffer from specified bar (1) by index, (2) by bar time double GetDataBuffer(const int buffer_num,const int index); double GetDataBuffer(const int buffer_num,const datetime time); }; //+------------------------------------------------------------------+
我们在类主体之外实现这些方法:
返回 MqlParam 结构数组参数说明的方法:
//+------------------------------------------------------------------+ //| Return the description of parameter of m_mql_param array | //+------------------------------------------------------------------+ string CIndicatorDE::GetMqlParamDescription(const int index) const { return "["+(string)index+"] "+MqlParameterDescription(this.m_mql_param[index]); } //+------------------------------------------------------------------+
将数组中的参数索引传递给方法,然后根据指定的数组索引从存储在结构中的数据里提取参数说明并创建一个字符串。 下面,我将编写 MqlParameterDescription() 函数,从而返回 MqlParam 结构的数据说明。
依据指标和柱线时间返回指标数据的方法:
//+------------------------------------------------------------------+ //| Return data of specified buffer from specified bar by index | //+------------------------------------------------------------------+ double CIndicatorDE::GetDataBuffer(const int buffer_num,const int index) { double array[1]={EMPTY_VALUE}; int copied=::CopyBuffer(this.Handle(),buffer_num,index,1,array); return(copied==1 ? array[0] : this.EmptyValue()); } //+------------------------------------------------------------------+ //| Return data of specified buffer from specified bar by time | //+------------------------------------------------------------------+ double CIndicatorDE::GetDataBuffer(const int buffer_num,const datetime time) { double array[1]={EMPTY_VALUE}; int copied=::CopyBuffer(this.Handle(),buffer_num,time,1,array); return(copied==1 ? array[0] : this.EmptyValue()); } //+------------------------------------------------------------------+
该方法接收指标或柱线时间,而数据必须从指标接收。 利用 CopyBuffer() 函数依据索引或时间请求一根柱线,并返回写在数组里的结果。 如果由于某种原因未收到数据,则这些方法将返回为指标对象设置的空值。
在类的构造函数中,设置默认指标 ID(-1):
//+------------------------------------------------------------------+ //| Closed parametric constructor | //+------------------------------------------------------------------+ CIndicatorDE::CIndicatorDE(ENUM_INDICATOR ind_type, string symbol, ENUM_TIMEFRAMES timeframe, ENUM_INDICATOR_STATUS status, ENUM_INDICATOR_GROUP group, string name, string shortname, MqlParam &mql_params[]) { //--- Set collection ID for the object this.m_type=COLLECTION_INDICATORS_ID; //--- Write description of indicator type this.m_ind_type_description=IndicatorTypeDescription(ind_type); //--- If parameter array size passed to constructor is more than zero //--- fill in the array of object parameters with data from the array passed to constructor int count=::ArrayResize(this.m_mql_param,::ArraySize(mql_params)); for(int i=0;i<count;i++) { this.m_mql_param[i].type = mql_params[i].type; this.m_mql_param[i].double_value = mql_params[i].double_value; this.m_mql_param[i].integer_value= mql_params[i].integer_value; this.m_mql_param[i].string_value = mql_params[i].string_value; } //--- Create indicator handle int handle=::IndicatorCreate(symbol,timeframe,ind_type,count,this.m_mql_param); //--- Save integer properties this.m_long_prop[INDICATOR_PROP_STATUS] = status; this.m_long_prop[INDICATOR_PROP_TYPE] = ind_type; this.m_long_prop[INDICATOR_PROP_GROUP] = group; this.m_long_prop[INDICATOR_PROP_TIMEFRAME] = timeframe; this.m_long_prop[INDICATOR_PROP_HANDLE] = handle; this.m_long_prop[INDICATOR_PROP_ID] = WRONG_VALUE; //--- Save real properties this.m_double_prop[this.IndexProp(INDICATOR_PROP_EMPTY_VALUE)]=EMPTY_VALUE; //--- Save string properties this.m_string_prop[this.IndexProp(INDICATOR_PROP_SYMBOL)] = (symbol==NULL || symbol=="" ? ::Symbol() : symbol); this.m_string_prop[this.IndexProp(INDICATOR_PROP_NAME)] = name; this.m_string_prop[this.IndexProp(INDICATOR_PROP_SHORTNAME)]= shortname; } //+------------------------------------------------------------------+
鉴于每个指标的句柄以及其 ID(遵照我们意愿设置)的唯一性,因此在比较两个指标的识别参数时,必须跳过上述两个参数。 否则,进行标识时,每个指标都会被标识为唯一,且我们将无法正确比较两个指标的参数。
为避免这种情况,在比较两个指标对象的方法中跳过 “handle” 和 “ID” 属性:
//+------------------------------------------------------------------+ //| Compare CIndicatorDE objects with each other by all properties | //+------------------------------------------------------------------+ bool CIndicatorDE::IsEqual(CIndicatorDE *compared_obj) const { if(!IsEqualMqlParamArrays(compared_obj.m_mql_param)) return false; int beg=0, end=INDICATOR_PROP_INTEGER_TOTAL; for(int i=beg; i<end; i++) { ENUM_INDICATOR_PROP_INTEGER prop=(ENUM_INDICATOR_PROP_INTEGER)i; if(prop==INDICATOR_PROP_HANDLE || prop==INDICATOR_PROP_ID) continue; if(this.GetProperty(prop)!=compared_obj.GetProperty(prop)) return false; } beg=end; end+=INDICATOR_PROP_DOUBLE_TOTAL; for(int i=beg; i<end; i++) { ENUM_INDICATOR_PROP_DOUBLE prop=(ENUM_INDICATOR_PROP_DOUBLE)i; if(this.GetProperty(prop)!=compared_obj.GetProperty(prop)) return false; } beg=end; end+=INDICATOR_PROP_STRING_TOTAL; for(int i=beg; i<end; i++) { ENUM_INDICATOR_PROP_STRING prop=(ENUM_INDICATOR_PROP_STRING)i; if(this.GetProperty(prop)!=compared_obj.GetProperty(prop)) return false; } return true; } //+------------------------------------------------------------------+
在返回指标整数型属性说明的方法中添加显示 “ID” 指标属性说明的代码:
//+------------------------------------------------------------------+ //| Return description of indicator's integer property | //+------------------------------------------------------------------+ string CIndicatorDE::GetPropertyDescription(ENUM_INDICATOR_PROP_INTEGER property) { return ( property==INDICATOR_PROP_STATUS ? CMessage::Text(MSG_LIB_TEXT_IND_TEXT_STATUS)+ (!this.SupportProperty(property) ? ": "+CMessage::Text(MSG_LIB_PROP_NOT_SUPPORTED) : ": "+this.GetStatusDescription() ) : property==INDICATOR_PROP_TYPE ? CMessage::Text(MSG_LIB_TEXT_IND_TEXT_TYPE)+ (!this.SupportProperty(property) ? ": "+CMessage::Text(MSG_LIB_PROP_NOT_SUPPORTED) : ": "+this.GetTypeDescription() ) : property==INDICATOR_PROP_GROUP ? CMessage::Text(MSG_LIB_TEXT_IND_TEXT_GROUP)+ (!this.SupportProperty(property) ? ": "+CMessage::Text(MSG_LIB_PROP_NOT_SUPPORTED) : ": "+this.GetGroupDescription() ) : property==INDICATOR_PROP_ID ? CMessage::Text(MSG_LIB_TEXT_IND_TEXT_ID)+ (!this.SupportProperty(property) ? ": "+CMessage::Text(MSG_LIB_PROP_NOT_SUPPORTED) : ": "+(string)this.GetProperty(property) ) : property==INDICATOR_PROP_TIMEFRAME ? CMessage::Text(MSG_LIB_TEXT_IND_TEXT_TIMEFRAME)+ (!this.SupportProperty(property) ? ": "+CMessage::Text(MSG_LIB_PROP_NOT_SUPPORTED) : ": "+this.GetTimeframeDescription() ) : property==INDICATOR_PROP_HANDLE ? CMessage::Text(MSG_LIB_TEXT_IND_TEXT_HANDLE)+ (!this.SupportProperty(property) ? ": "+CMessage::Text(MSG_LIB_PROP_NOT_SUPPORTED) : ": "+(string)this.GetProperty(property) ) : "" ); } //+------------------------------------------------------------------+
现在,在服务函数文件 \MQL5\Include\DoEasy\Services\DELib.mqh 中,添加显示 MqlParam 结构说明的函数:
//+------------------------------------------------------------------+ //| Return the description of parameter MqlParam array | //+------------------------------------------------------------------+ string MqlParameterDescription(const MqlParam &mql_param) { int type=mql_param.type; string res=CMessage::Text(MSG_ORD_TYPE)+" "+typename(type)+": "; //--- type of parameter string if(type==TYPE_STRING) res+=mql_param.string_value; //--- type of parameter datetime else if(type==TYPE_DATETIME) res+=TimeToString(mql_param.integer_value,TIME_DATE|TIME_MINUTES|TIME_SECONDS); //--- type of parameter color else if(type==TYPE_COLOR) res+=ColorToString((color)mql_param.integer_value,true); //--- type of parameter bool else if(type==TYPE_BOOL) res+=(string)(bool)mql_param.integer_value; //--- integer types else if(type>TYPE_BOOL && type<TYPE_FLOAT) res+=(string)mql_param.integer_value; //--- real types else res+=DoubleToString(mql_param.double_value,8); return res; } //+------------------------------------------------------------------+
MqlParam 结构包括若干字段。 其中之一是包含存储在结构中的数据类型。 依据这个数据类型,我可知晓从哪个结构字段里接收数据,以及哪个函数可用来在日志里显示数据的字符串说明。
获取数据类型并开始形成字串,其中包含单词 "Type " +" "+ 数据类型字符串说明 + ": "。
进而,根据数据类型,为已创建字符串加入结构字段的数值说明,针对相应的类型,利用标准函数显示所需数据类型的字符串说明。
最后,利用抽象指标对象类的 MqlParam 结构参数的显示方法,以及上面曾分析过的终端日志服务函数,显示自定义指标四个参数的说明如下所示:
--- Indicator parameters --- - [1] Type int: 13 - [2] Type int: 0 - [3] Type int: 0 - [4] Type int: 2
由于新属性已添加到指标对象 - 其 ID,因此 \MQL5\Include\DoEasy\Objects\Indicators\Standart\ 文件夹中的所有指标对象的所有类,都要在其指标名称简述显示方法里略作补充。 简单地将 ID 值添加到简述里:
//+------------------------------------------------------------------+ //| Display a short description of indicator object in the journal | //+------------------------------------------------------------------+ void CIndAC::PrintShort(void) { string id=(this.ID()>WRONG_VALUE ? ", id #"+(string)this.ID()+"]" : "]"); ::Print(GetStatusDescription()," ",this.Name()," ",this.Symbol()," ",TimeframeDescription(this.Timeframe())," [handle ",this.Handle(),id); } //+------------------------------------------------------------------+
在此: 创建 ID 说明。 于此,如果 ID 值大于 -1,则会显示 ID;否则,如果 ID 缺失(其值为 -1),则不会在说明中显示(只有一对括号)。 进而,将接收到的字符串添加到指标简述当中。
在所有指标对象类里已加入了这些改进。
自定义指标对象
现在,我们来添加自定义指标对象类。 将其放置到函数库的标准指标文件夹当中 \MQL5\Include\DoEasy\Objects\Indicators\Standart\。 我之所以这样做,只是因为终端指标列表也包含自定义指标。 这意味着它也属于终端指标列表。
如常分析整个类:
//+------------------------------------------------------------------+ //| IndCustom.mqh | //| Copyright 2020, MetaQuotes Software Corp. | //| https://mql5.com/en/users/artmedia70 | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://mql5.com/en/users/artmedia70" //+------------------------------------------------------------------+ //| Include files | //+------------------------------------------------------------------+ #include "..\\IndicatorDE.mqh" //+------------------------------------------------------------------+ //| Custom indicator | //+------------------------------------------------------------------+ class CIndCustom : public CIndicatorDE { private: public: //--- Constructor CIndCustom(const string symbol,const ENUM_TIMEFRAMES timeframe,MqlParam &mql_param[]) : CIndicatorDE(IND_CUSTOM,symbol,timeframe, INDICATOR_STATUS_CUSTOM, INDICATOR_GROUP_ANY, mql_param[0].string_value, mql_param[0].string_value+"("+(symbol==NULL || symbol=="" ? ::Symbol() : symbol)+ ","+TimeframeDescription(timeframe)+")",mql_param) {} //--- Supported indicator properties (1) real, (2) integer virtual bool SupportProperty(ENUM_INDICATOR_PROP_DOUBLE property); virtual bool SupportProperty(ENUM_INDICATOR_PROP_INTEGER property); //--- Display (1) a short description, (2) description of indicator object parameters in the journal virtual void PrintShort(void); virtual void PrintParameters(void); }; //+------------------------------------------------------------------+ //| Return 'true' if indicator supports a passed | //| integer property, otherwise return 'false' | //+------------------------------------------------------------------+ bool CIndCustom::SupportProperty(ENUM_INDICATOR_PROP_INTEGER property) { return true; } //+------------------------------------------------------------------+ //| Return 'true' if indicator supports a passed | //| real property, otherwise return 'false' | //+------------------------------------------------------------------+ bool CIndCustom::SupportProperty(ENUM_INDICATOR_PROP_DOUBLE property) { return true; } //+------------------------------------------------------------------+ //| Display a short description of indicator object in the journal | //+------------------------------------------------------------------+ void CIndCustom::PrintShort(void) { string id=(this.ID()>WRONG_VALUE ? ", id #"+(string)this.ID()+"]" : "]"); ::Print(GetStatusDescription()," ",this.Name()," ",this.Symbol()," ",TimeframeDescription(this.Timeframe())," [handle ",this.Handle(),id); } //+------------------------------------------------------------------+ //| Display parameter description of indicator object in the journal | //+------------------------------------------------------------------+ void CIndCustom::PrintParameters(void) { ::Print(" --- ",CMessage::Text(MSG_LIB_TEXT_IND_TEXT_IND_PARAMETERS)," --- "); int total=::ArraySize(this.m_mql_param); for(int i=1;i<total;i++) { ::Print(" - ",this.GetMqlParamDescription(i)); } } //+------------------------------------------------------------------+
由于是标准指标对象类,故我们已经知道所有方法。 但与它们对比,于此在类构造函数里,我们获取创建指标时,在 MqlParam 输入结构数组中初创的参数,而不是输入中的。 对于抽象指标对象类的封闭构造函数,需传递 “custom indicator” 状态,“any indicator” 分组,并把名称作为传递参数的第一个数组元素,在创建自定义指标时必须具有 TYPE_STRING 类型,且字段 string_value 的值包含自定义指标名称。 以相同的方式创建指标简称,但同时附带品种和时间帧的描述。 此外,可利用父类 SetName() 和 SetShortName() 方法来更改指标名称和简称。 但指标名称已经包含其路径。 因此(至少在函数库开发的这个阶段)不能更改已创建自定义指标的名称。
在日志中显示指标对象参数说明的方法中会执行以下操作:首先,显示标题,然后利用先前分析过的方法(尤其是父类 GetMqlParamDescription() 方法),按指标参数数组循环显示以下每个参数。
所有指标对象都存储在 CIndicatorsCollection 类的集合列表中,该类实现位于 \MQL5\Include\DoEasy\Collections\IndicatorsCollection.mqh。
将指标添加到集合列表时,必须附加检查其 ID 的唯一性。 并指定自定义指标类可见,以便指标集合类可以处理它们。
在到指标对象集合类中包含自定义指标类的文件:
//+------------------------------------------------------------------+ //| IndicatorsCollection.mqh | //| Copyright 2020, MetaQuotes Software Corp. | //| https://mql5.com/en/users/artmedia70 | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://mql5.com/en/users/artmedia70" #property version "1.00" //+------------------------------------------------------------------+ //| Include files | //+------------------------------------------------------------------+ #include "ListObj.mqh" #include "..\Objects\Indicators\Standart\IndAC.mqh" #include "..\Objects\Indicators\Standart\IndAD.mqh" #include "..\Objects\Indicators\Standart\IndADX.mqh" #include "..\Objects\Indicators\Standart\IndADXW.mqh" #include "..\Objects\Indicators\Standart\IndAlligator.mqh" #include "..\Objects\Indicators\Standart\IndAMA.mqh" #include "..\Objects\Indicators\Standart\IndAO.mqh" #include "..\Objects\Indicators\Standart\IndATR.mqh" #include "..\Objects\Indicators\Standart\IndBands.mqh" #include "..\Objects\Indicators\Standart\IndBears.mqh" #include "..\Objects\Indicators\Standart\IndBulls.mqh" #include "..\Objects\Indicators\Standart\IndBWMFI.mqh" #include "..\Objects\Indicators\Standart\IndCCI.mqh" #include "..\Objects\Indicators\Standart\IndChaikin.mqh" #include "..\Objects\Indicators\Standart\IndCustom.mqh" #include "..\Objects\Indicators\Standart\IndDEMA.mqh" #include "..\Objects\Indicators\Standart\IndDeMarker.mqh" #include "..\Objects\Indicators\Standart\IndEnvelopes.mqh" #include "..\Objects\Indicators\Standart\IndForce.mqh" #include "..\Objects\Indicators\Standart\IndFractals.mqh" #include "..\Objects\Indicators\Standart\IndFRAMA.mqh" #include "..\Objects\Indicators\Standart\IndGator.mqh" #include "..\Objects\Indicators\Standart\IndIchimoku.mqh" #include "..\Objects\Indicators\Standart\IndMA.mqh" #include "..\Objects\Indicators\Standart\IndMACD.mqh" #include "..\Objects\Indicators\Standart\IndMFI.mqh" #include "..\Objects\Indicators\Standart\IndMomentum.mqh" #include "..\Objects\Indicators\Standart\IndOBV.mqh" #include "..\Objects\Indicators\Standart\IndOsMA.mqh" #include "..\Objects\Indicators\Standart\IndRSI.mqh" #include "..\Objects\Indicators\Standart\IndRVI.mqh" #include "..\Objects\Indicators\Standart\IndSAR.mqh" #include "..\Objects\Indicators\Standart\IndStDev.mqh" #include "..\Objects\Indicators\Standart\IndStoch.mqh" #include "..\Objects\Indicators\Standart\IndTEMA.mqh" #include "..\Objects\Indicators\Standart\IndTRIX.mqh" #include "..\Objects\Indicators\Standart\IndVIDYA.mqh" #include "..\Objects\Indicators\Standart\IndVolumes.mqh" #include "..\Objects\Indicators\Standart\IndWPR.mqh" //+------------------------------------------------------------------+
在类的私密部分里,声明将指标对象添加到集合的方法,以及一个检查列表中指定 ID 指标对象是否存在的方法:
//+------------------------------------------------------------------+ //| Indicator collection | //+------------------------------------------------------------------+ class CIndicatorsCollection : public CObject { private: CListObj m_list; // Indicator object list MqlParam m_mql_param[]; // Array of indicator parameters //--- (1) Create, (2) add to collection list a new indicator object and set an ID for it CIndicatorDE *CreateIndicator(const ENUM_INDICATOR ind_type,MqlParam &mql_param[],const string symbol_name=NULL,const ENUM_TIMEFRAMES period=PERIOD_CURRENT); int AddIndicatorToList(CIndicatorDE *indicator,const int id); //--- Return the indicator index in the list int Index(CIndicatorDE *compared_obj); //--- Check presence of indicator object with specified id in the list bool CheckID(const int id); public:
在类的公开部分中,声明一个方法返回指向自定义指标对象的指针,该方法依据其分组和在数组 MqlParam 中指定的参数(与标准指标相比,自定义指标参数只能通过在此类数组中传递来指定):
CIndicatorDE *GetIndCCI(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const ENUM_APPLIED_PRICE applied_price); CIndicatorDE *GetIndCustom(const string symbol,const ENUM_TIMEFRAMES timeframe,ENUM_INDICATOR_GROUP group,MqlParam ¶m[]); CIndicatorDE *GetIndDEMA(const string symbol,const ENUM_TIMEFRAMES timeframe, const int ma_period, const int ma_shift, const ENUM_APPLIED_PRICE applied_price);
在指标声明模块中创建方法,首先,将每个指标的标识 ID 添加到每个方法的参数之中;其次,添加创建自定义指标的方法声明。 我不会提供这些方法的完整声明清单。 取而代之,我仅提供三个方法(所有方法均已改进,且您可在附件中找到它们):
int CreateCCI(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id, const int ma_period=14, const ENUM_APPLIED_PRICE applied_price=PRICE_TYPICAL); int CreateCustom(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id, ENUM_INDICATOR_GROUP group, MqlParam &mql_param[]); int CreateDEMA(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id, const int ma_period=14, const int ma_shift=0, const ENUM_APPLIED_PRICE applied_price=PRICE_CLOSE);
在输入品种和时间帧之后的所有方法中,在其创建时强制指定指标 ID。
在自定义指标创建方法中,另外指定了指标分组,和指标参数数组,并在传递之前预先填充。 自定义指标将基于它们来创建。
在类主体清单的末尾,声明为指标设置指定 ID 的方法,以及依据指定 ID 返回指标对象的方法:
//--- Set ID for the specified indicator void SetID(CIndicatorDE *indicator,const int id); //--- Return indicator object by its ID CIndicatorDE *GetIndByID(const uint id); //--- Display (1) the complete and (2) short collection description in the journal void Print(void); void PrintShort(void); //--- Constructor CIndicatorsCollection(); }; //+------------------------------------------------------------------+
现在,我们来分析所有声明的方法。
在创建新指标对象的私密方法中,添加创建新对象 - 自定义指标:
//+------------------------------------------------------------------+ //| Create new indicator object | //+------------------------------------------------------------------+ CIndicatorDE *CIndicatorsCollection::CreateIndicator(const ENUM_INDICATOR ind_type,MqlParam &mql_param[],const string symbol_name=NULL,const ENUM_TIMEFRAMES period=PERIOD_CURRENT) { string symbol=(symbol_name==NULL || symbol_name=="" ? ::Symbol() : symbol_name); ENUM_TIMEFRAMES timeframe=(period==PERIOD_CURRENT ? ::Period() : period); CIndicatorDE *indicator=NULL; switch(ind_type) { case IND_AC : indicator=new CIndAC(symbol,timeframe,mql_param); break; case IND_AD : indicator=new CIndAD(symbol,timeframe,mql_param); break; case IND_ADX : indicator=new CIndADX(symbol,timeframe,mql_param); break; case IND_ADXW : indicator=new CIndADXW(symbol,timeframe,mql_param); break; case IND_ALLIGATOR : indicator=new CIndAlligator(symbol,timeframe,mql_param); break; case IND_AMA : indicator=new CIndAMA(symbol,timeframe,mql_param); break; case IND_AO : indicator=new CIndAO(symbol,timeframe,mql_param); break; case IND_ATR : indicator=new CIndATR(symbol,timeframe,mql_param); break; case IND_BANDS : indicator=new CIndBands(symbol,timeframe,mql_param); break; case IND_BEARS : indicator=new CIndBears(symbol,timeframe,mql_param); break; case IND_BULLS : indicator=new CIndBulls(symbol,timeframe,mql_param); break; case IND_BWMFI : indicator=new CIndBWMFI(symbol,timeframe,mql_param); break; case IND_CCI : indicator=new CIndCCI(symbol,timeframe,mql_param); break; case IND_CHAIKIN : indicator=new CIndCHO(symbol,timeframe,mql_param); break; case IND_DEMA : indicator=new CIndDEMA(symbol,timeframe,mql_param); break; case IND_DEMARKER : indicator=new CIndDeMarker(symbol,timeframe,mql_param); break; case IND_ENVELOPES : indicator=new CIndEnvelopes(symbol,timeframe,mql_param); break; case IND_FORCE : indicator=new CIndForce(symbol,timeframe,mql_param); break; case IND_FRACTALS : indicator=new CIndFractals(symbol,timeframe,mql_param); break; case IND_FRAMA : indicator=new CIndFRAMA(symbol,timeframe,mql_param); break; case IND_GATOR : indicator=new CIndGator(symbol,timeframe,mql_param); break; case IND_ICHIMOKU : indicator=new CIndIchimoku(symbol,timeframe,mql_param); break; case IND_MA : indicator=new CIndMA(symbol,timeframe,mql_param); break; case IND_MACD : indicator=new CIndMACD(symbol,timeframe,mql_param); break; case IND_MFI : indicator=new CIndMFI(symbol,timeframe,mql_param); break; case IND_MOMENTUM : indicator=new CIndMomentum(symbol,timeframe,mql_param); break; case IND_OBV : indicator=new CIndOBV(symbol,timeframe,mql_param); break; case IND_OSMA : indicator=new CIndOsMA(symbol,timeframe,mql_param); break; case IND_RSI : indicator=new CIndRSI(symbol,timeframe,mql_param); break; case IND_RVI : indicator=new CIndRVI(symbol,timeframe,mql_param); break; case IND_SAR : indicator=new CIndSAR(symbol,timeframe,mql_param); break; case IND_STDDEV : indicator=new CIndStDev(symbol,timeframe,mql_param); break; case IND_STOCHASTIC : indicator=new CIndStoch(symbol,timeframe,mql_param); break; case IND_TEMA : indicator=new CIndTEMA(symbol,timeframe,mql_param); break; case IND_TRIX : indicator=new CIndTRIX(symbol,timeframe,mql_param); break; case IND_VIDYA : indicator=new CIndVIDYA(symbol,timeframe,mql_param); break; case IND_VOLUMES : indicator=new CIndVolumes(symbol,timeframe,mql_param); break; case IND_WPR : indicator=new CIndWPR(symbol,timeframe,mql_param); break; case IND_CUSTOM : indicator=new CIndCustom(symbol,timeframe,mql_param); break; default: break; } return indicator; } //+------------------------------------------------------------------+
修改方法,将新指标对象添加到集合列表:
//+------------------------------------------------------------------+ //| Add a new indicator object to collection list | //+------------------------------------------------------------------+ int CIndicatorsCollection::AddIndicatorToList(CIndicatorDE *indicator,const int id) { //--- If invalid indicator is passed to the object - return INVALID_HANDLE if(indicator==NULL) return INVALID_HANDLE; //--- If such indicator is already in the list int index=this.Index(indicator); if(index!=WRONG_VALUE) { //--- Remove the earlier created object, get indicator object from the list and return indicator handle delete indicator; indicator=this.m_list.At(index); } //--- If indicator object is not in the list yet else { //--- If failed to add indicator object to the list - display a corresponding message, //--- remove object and return INVALID_HANDLE if(!this.m_list.Add(indicator)) { ::Print(CMessage::Text(MSG_LIB_SYS_FAILED_ADD_IND_TO_LIST)); delete indicator; return INVALID_HANDLE; } } //--- If indicator is successfully added to the list or is already there... //--- If indicator with specified ID (not -1) is not in the list - set ID if(id>WRONG_VALUE && !this.CheckID(id)) indicator.SetID(id); //--- Return handle of a new indicator added to the list return indicator.Handle(); } //+------------------------------------------------------------------+
现在,把指标 ID 也传递给该方法。 如果含有该 ID 的指标在集合列表中不存在,则为指标对象设置指定的 ID。 否则,默认设置指标 ID 将为 -1。
现在,所有创建指标对象的方法都变得更简洁了。
我们以创建 AC 和鳄嘴指标对象为例进行分析:
//+------------------------------------------------------------------+ //| Create a new indicator object Accelerator Oscillator | //| and place it to the collection list | //+------------------------------------------------------------------+ int CIndicatorsCollection::CreateAC(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id) { //--- AC indicator possesses no parameters - resize the array of parameter structures ::ArrayResize(this.m_mql_param,0); //--- Create indicator object CIndicatorDE *indicator=this.CreateIndicator(IND_AC,this.m_mql_param,symbol,timeframe); //--- Return indicator handle received as a result of adding the object to collection list return this.AddIndicatorToList(indicator,id); } //+------------------------------------------------------------------+
//+------------------------------------------------------------------+ //| Create new indicator object Alligator | //| and place it to the collection list | //+------------------------------------------------------------------+ int CIndicatorsCollection::CreateAlligator(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id, const int jaw_period=13, const int jaw_shift=8, const int teeth_period=8, const int teeth_shift=5, const int lips_period=5, const int lips_shift=3, const ENUM_MA_METHOD ma_method=MODE_SMMA, const ENUM_APPLIED_PRICE applied_price=PRICE_MEDIAN) { //--- Add required indicator parameters to the array of parameter structures ::ArrayResize(this.m_mql_param,8); this.m_mql_param[0].type=TYPE_INT; this.m_mql_param[0].integer_value=jaw_period; this.m_mql_param[1].type=TYPE_INT; this.m_mql_param[1].integer_value=jaw_shift; this.m_mql_param[2].type=TYPE_INT; this.m_mql_param[2].integer_value=teeth_period; this.m_mql_param[3].type=TYPE_INT; this.m_mql_param[3].integer_value=teeth_shift; this.m_mql_param[4].type=TYPE_INT; this.m_mql_param[4].integer_value=lips_period; this.m_mql_param[5].type=TYPE_INT; this.m_mql_param[5].integer_value=lips_shift; this.m_mql_param[6].type=TYPE_INT; this.m_mql_param[6].integer_value=ma_method; this.m_mql_param[7].type=TYPE_INT; this.m_mql_param[7].integer_value=applied_price; //--- Create indicator object CIndicatorDE *indicator=this.CreateIndicator(IND_ALLIGATOR,this.m_mql_param,symbol,timeframe); //--- Return indicator handle received as a result of adding the object to collection list return this.AddIndicatorToList(indicator,id); } //+------------------------------------------------------------------+
现在,仅需填充输入结构,创建指标,并调用方法将其添加到集合列表就足够了。 在指标对象创建的所有方法中均执行了此类更改。 除了自定义指标创建方法之外,我不再对其进行分析:
//+------------------------------------------------------------------+ //| Create a new object - custom indicator | //| and place it to the collection list | //+------------------------------------------------------------------+ int CIndicatorsCollection::CreateCustom(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id, ENUM_INDICATOR_GROUP group, MqlParam &mql_param[]) { //--- Create indicator object CIndicatorDE *indicator=this.CreateIndicator(IND_CUSTOM,mql_param,symbol,timeframe); if(indicator==NULL) return INVALID_HANDLE; //--- Set a group for indicator object indicator.SetGroup(group); //--- Return indicator handle received as a result of adding the object to collection list return this.AddIndicatorToList(indicator,id); } //+------------------------------------------------------------------+
此处,略有不同。 在这里,除了 ID 之外,创建方法还获得指标分组。 而且,由于我们无法事先知晓所创建自定义指标的参数,因此会在 MqlParam 参数数组中一次性传递所创建指标的所有参数。
而所有标准指标的每个输入参数的标准默认值则以绝对方式添加到创建方法之中。 因此,若要创建默认参数的标准指标,只需指定品种、时间帧和 ID 就足够了。
返回自定义指标对象的指针的方法实现:
//+------------------------------------------------------------------+ //| Return pointer to object- custom indicator | //+------------------------------------------------------------------+ CIndicatorDE *CIndicatorsCollection::GetIndCustom(const string symbol,const ENUM_TIMEFRAMES timeframe,ENUM_INDICATOR_GROUP group,MqlParam ¶m[]) { CIndicatorDE *tmp=new CIndCustom(symbol,timeframe,param); if(tmp==NULL) return NULL; tmp.SetGroup(group); int index=this.Index(tmp); delete tmp; return(index>WRONG_VALUE ? this.m_list.At(index) : NULL); } //+------------------------------------------------------------------+
此处:创建一个临时指标对象,以便在集合列表中搜索相同的对象,为其设置一个分组,并获取集合列表中找到的相应对象的索引。 然后,删除临时对象,并依据找到的列表索引返回指向该对象的指针,或 NULL(如果在列表中未找到该对象)。
检查列表中含有指定标识的指标对象是否存在的方法:
//+------------------------------------------------------------------+ //| Check presence of indicator object with specified id in the list | //+------------------------------------------------------------------+ bool CIndicatorsCollection::CheckID(const int id) { CArrayObj *list=CSelect::ByIndicatorProperty(this.GetList(),INDICATOR_PROP_ID,id,EQUAL); return(list!=NULL && list.Total()!=0); } //+------------------------------------------------------------------+
获取含有指定 ID 指标对象的列表,检查列表是否有效且不为空(列表必须包含一个对象),并返回结果标志。
为指定指标设置 ID 的方法:
//+------------------------------------------------------------------+ //| Set ID for the specified indicator | //+------------------------------------------------------------------+ void CIndicatorsCollection::SetID(CIndicatorDE *indicator,const int id) { if(indicator==NULL) { ::Print(DFUN,CMessage::Text(MSG_LIB_SYS_INVALID_IND_POINTER)); return; } if(id>WRONG_VALUE) { if(CheckID(id)) { ::Print(DFUN,CMessage::Text(MSG_LIB_SYS_IND_ID_EXIST)," #",(string)id); return; } } indicator.SetID(id); } //+------------------------------------------------------------------+
该方法接收指标对象的指针,而传递给该方法的 ID 参数必须要设置。
如果传递了无效的指针,则通知并离开方法。
如果 ID 值大于 -1,检查含有相同 ID 的指标对象是否存在,且是否可用 - 通知并离开。
如果所有检查均成功,则为对象设置 ID。 如果传递给该方法的 ID 值小于零,则无需进行任何检查即可为该对象设置该 ID,而指标对象的这个 ID 表示它不存在。
依据指定的 ID 返回指标对象的方法:
//+------------------------------------------------------------------+ //| Return indicator object by its ID | //+------------------------------------------------------------------+ CIndicatorDE *CIndicatorsCollection::GetIndByID(const uint id) { CArrayObj *list=CSelect::ByIndicatorProperty(this.GetList(),INDICATOR_PROP_ID,id,EQUAL); return(list==NULL || list.Total()==0 ? NULL : list.At(list.Total()-1)); } //+------------------------------------------------------------------+
此处:获取含有指定 ID 的指标对象列表,返回 NULL(如果无法获取列表或列表为空),或指向含有所需 ID 的对象指针。 由于只会有一个含有指定 ID 的对象,因此在接收列表中指定哪个索引都没有关系:第一个或最后一个。 此处指定了最后一个。
在 EA 中测试指标创建存在一个问题:更改时间帧时,会创建另一个相同的指标,但时间帧不同。 这是真实的:输入相同,但计算不同时间帧的指标其实是两个不同的指标。 为了简单地避免此问题,在程序析构期间清除已创建的指标列表就足够了。 为此,在 CEngine 函数库主对象所在的文件 \MQL5\Include\DoEasy\Engine.mqh 中,声明一个新的处理程序 OnDeinit():
//--- (1) Timer, event handler (2) NewTick, (3) Calculate, Deinit void OnTimer(SDataCalculate &data_calculate); void OnTick(SDataCalculate &data_calculate,const uint required=0); int OnCalculate(SDataCalculate &data_calculate,const uint required=0); void OnDeinit(void);
并在类主体外部编写其实现:
//+------------------------------------------------------------------+ //| Deinitialize library | //+------------------------------------------------------------------+ void CEngine::OnDeinit(void) { this.m_indicators.GetList().Clear(); } //+------------------------------------------------------------------+
该类的方法将从程序的 OnDeinit() 处理程序中调用。 我们在此已调用了指标集对象中清除集合列表的方法。
测试
我们取用上一篇文章的 EA 测试各种指标的创建,以及从创建的指标对象里获取数据。
将其保存在新的文件夹 \MQL5\Experts\TestDoEasy\Part56\ 之下,新命名为 TestDoEasyPart56.mq5。
在 EA 中,创建两个自定义移动平均线指标,但参数不同(指标取自终端标准发行版 \MQL5\Indicators\Examples\ 指标样本文件夹)。 并创建两个含有不同输入的标准自适应移动平均线指标。
在全局领域,设置宏替换来简化依据 ID 调用指标,并为要创建的两个自定义指标声明两个参数数组:
//+------------------------------------------------------------------+ //| TestDoEasyPart56.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://mql5.com/en/users/artmedia70 | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://mql5.com/en/users/artmedia70" #property version "1.00" //--- includes #include <DoEasy\Engine.mqh> //--- enums enum ENUM_BUTTONS { BUTT_BUY, BUTT_BUY_LIMIT, BUTT_BUY_STOP, BUTT_BUY_STOP_LIMIT, BUTT_CLOSE_BUY, BUTT_CLOSE_BUY2, BUTT_CLOSE_BUY_BY_SELL, BUTT_SELL, BUTT_SELL_LIMIT, BUTT_SELL_STOP, BUTT_SELL_STOP_LIMIT, BUTT_CLOSE_SELL, BUTT_CLOSE_SELL2, BUTT_CLOSE_SELL_BY_BUY, BUTT_DELETE_PENDING, BUTT_CLOSE_ALL, BUTT_SET_STOP_LOSS, BUTT_SET_TAKE_PROFIT, BUTT_PROFIT_WITHDRAWAL, BUTT_TRAILING_ALL }; #define TOTAL_BUTT (20) #define MA1 (1) #define MA2 (2) #define AMA1 (3) #define AMA2 (4) //--- structures struct SDataButt { string name; string text; }; //--- input variables input ushort InpMagic = 123; // Magic number input double InpLots = 0.1; // Lots input uint InpStopLoss = 150; // StopLoss in points input uint InpTakeProfit = 150; // TakeProfit in points input uint InpDistance = 50; // Pending orders distance (points) input uint InpDistanceSL = 50; // StopLimit orders distance (points) input uint InpDistancePReq = 50; // Distance for Pending Request's activate (points) input uint InpBarsDelayPReq = 5; // Bars delay for Pending Request's activate (current timeframe) input uint InpSlippage = 5; // Slippage in points input uint InpSpreadMultiplier = 1; // Spread multiplier for adjusting stop-orders by StopLevel input uchar InpTotalAttempts = 5; // Number of trading attempts sinput double InpWithdrawal = 10; // Withdrawal funds (in tester) sinput uint InpButtShiftX = 0; // Buttons X shift sinput uint InpButtShiftY = 10; // Buttons Y shift input uint InpTrailingStop = 50; // Trailing Stop (points) input uint InpTrailingStep = 20; // Trailing Step (points) input uint InpTrailingStart = 0; // Trailing Start (points) input uint InpStopLossModify = 20; // StopLoss for modification (points) input uint InpTakeProfitModify = 60; // TakeProfit for modification (points) sinput ENUM_SYMBOLS_MODE InpModeUsedSymbols = SYMBOLS_MODE_CURRENT; // Mode of used symbols list sinput string InpUsedSymbols = "EURUSD,AUDUSD,EURAUD,EURCAD,EURGBP,EURJPY,EURUSD,GBPUSD,NZDUSD,USDCAD,USDJPY"; // List of used symbols (comma - separator) sinput ENUM_TIMEFRAMES_MODE InpModeUsedTFs = TIMEFRAMES_MODE_LIST; // Mode of used timeframes list sinput string InpUsedTFs = "M1,M5,M15,M30,H1,H4,D1,W1,MN1"; // List of used timeframes (comma - separator) sinput bool InpUseSounds = true; // Use sounds //--- global variables CEngine engine; SDataButt butt_data[TOTAL_BUTT]; string prefix; double lot; double withdrawal=(InpWithdrawal<0.1 ? 0.1 : InpWithdrawal); ushort magic_number; uint stoploss; uint takeprofit; uint distance_pending; uint distance_stoplimit; uint distance_pending_request; uint bars_delay_pending_request; uint slippage; bool trailing_on; bool pressed_pending_buy; bool pressed_pending_buy_limit; bool pressed_pending_buy_stop; bool pressed_pending_buy_stoplimit; bool pressed_pending_close_buy; bool pressed_pending_close_buy2; bool pressed_pending_close_buy_by_sell; bool pressed_pending_sell; bool pressed_pending_sell_limit; bool pressed_pending_sell_stop; bool pressed_pending_sell_stoplimit; bool pressed_pending_close_sell; bool pressed_pending_close_sell2; bool pressed_pending_close_sell_by_buy; bool pressed_pending_delete_all; bool pressed_pending_close_all; bool pressed_pending_sl; bool pressed_pending_tp; double trailing_stop; double trailing_step; uint trailing_start; uint stoploss_to_modify; uint takeprofit_to_modify; int used_symbols_mode; string array_used_symbols[]; string array_used_periods[]; bool testing; uchar group1; uchar group2; double g_point; int g_digits; //--- Arrays of custom indicator parameters MqlParam param_ma1[]; MqlParam param_ma2[]; //+------------------------------------------------------------------+
实质上,声明的宏替换是针对指标数值的描述。 通过 ID 名称调用指标比通过其值调用指标更简单。
在 EA 的 OnInit() 处理程序里,创建所有四个指标,并立即在日志里显示所有已创建指标的数据:
//+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Calling the function displays the list of enumeration constants in the journal, //--- (the list is set in strings 22 and 25 of DELib.mqh file) for checking the constants validity //EnumNumbersTest(); //--- Set EA global variables prefix=MQLInfoString(MQL_PROGRAM_NAME)+"_"; testing=engine.IsTester(); for(int i=0;i<TOTAL_BUTT;i++) { butt_data[i].name=prefix+EnumToString((ENUM_BUTTONS)i); butt_data[i].text=EnumToButtText((ENUM_BUTTONS)i); } lot=NormalizeLot(Symbol(),fmax(InpLots,MinimumLots(Symbol())*2.0)); magic_number=InpMagic; stoploss=InpStopLoss; takeprofit=InpTakeProfit; distance_pending=InpDistance; distance_stoplimit=InpDistanceSL; slippage=InpSlippage; trailing_stop=InpTrailingStop*Point(); trailing_step=InpTrailingStep*Point(); trailing_start=InpTrailingStart; stoploss_to_modify=InpStopLossModify; takeprofit_to_modify=InpTakeProfitModify; distance_pending_request=(InpDistancePReq<5 ? 5 : InpDistancePReq); bars_delay_pending_request=(InpBarsDelayPReq<1 ? 1 : InpBarsDelayPReq); g_point=SymbolInfoDouble(NULL,SYMBOL_POINT); g_digits=(int)SymbolInfoInteger(NULL,SYMBOL_DIGITS); //--- Initialize random group numbers group1=0; group2=0; srand(GetTickCount()); //--- Initialize DoEasy library OnInitDoEasy(); //--- Create indicators ArrayResize(param_ma1,4); //--- Name of indicator 1 param_ma1[0].type=TYPE_STRING; param_ma1[0].string_value="Examples\\Custom Moving Average.ex5"; //--- Calculation period param_ma1[1].type=TYPE_INT; param_ma1[1].integer_value=13; //--- Horizontal shift param_ma1[2].type=TYPE_INT; param_ma1[2].integer_value=0; //--- Smoothing method param_ma1[3].type=TYPE_INT; param_ma1[3].integer_value=MODE_SMA; //--- Create indicator 1 engine.GetIndicatorsCollection().CreateCustom(NULL,PERIOD_CURRENT,MA1,INDICATOR_GROUP_TREND,param_ma1); ArrayResize(param_ma2,5); //--- Name of indicator 2 param_ma2[0].type=TYPE_STRING; param_ma2[0].string_value="Examples\\Custom Moving Average.ex5"; //--- Calculation period param_ma2[1].type=TYPE_INT; param_ma2[1].integer_value=13; //--- Horizontal shift param_ma2[2].type=TYPE_INT; param_ma2[2].integer_value=0; //--- Smoothing method param_ma2[3].type=TYPE_INT; param_ma2[3].integer_value=MODE_SMA; //--- Calculation price param_ma2[4].type=TYPE_INT; param_ma2[4].integer_value=PRICE_OPEN; //--- Create indicator 2 engine.GetIndicatorsCollection().CreateCustom(NULL,PERIOD_CURRENT,MA2,INDICATOR_GROUP_TREND,param_ma2); //--- Create indicator 3 engine.GetIndicatorsCollection().CreateAMA(NULL,PERIOD_CURRENT,AMA1); //--- Create indicator 4 engine.GetIndicatorsCollection().CreateAMA(NULL,PERIOD_CURRENT,AMA2,14); //--- Display descriptions of created indicators engine.GetIndicatorsCollection().Print(); engine.GetIndicatorsCollection().PrintShort(); //--- Check and remove remaining EA graphical objects if(IsPresentObectByPrefix(prefix)) ObjectsDeleteAll(0,prefix); //--- Create the button panel if(!CreateButtons(InpButtShiftX,InpButtShiftY)) return INIT_FAILED; //--- Set trailing activation button status ButtonState(butt_data[TOTAL_BUTT-1].name,trailing_on); //--- Reset states of the buttons for working using pending requests for(int i=0;i<14;i++) { ButtonState(butt_data[i].name+"_PRICE",false); ButtonState(butt_data[i].name+"_TIME",false); } //--- Check playing a standard sound by macro substitution and a custom sound by description engine.PlaySoundByDescription(SND_OK); //--- Wait for 600 milliseconds engine.Pause(600); engine.PlaySoundByDescription(TextByLanguage("The sound of a falling coin 2")); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+
为第二个自定义 MA 指标设置计算价格 PRICE_OPEN。 如果未明确指定计算价格,则在默认情况下(在第一个 MA 指标中)计算指标采用 PRICE_CLOSE 价格。
创建 AMA 指标时,第一个指标的计算周期为 9(默认设置)。 第二个则显式设置接收值 14。
因此,创建的所有四个指标都含有不同的参数输入值。
在 EA 的处理程序 OnDeinit() 中,调用函数库的处理程序 OnDeinit():
//+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- Remove EA graphical objects by an object name prefix ObjectsDeleteAll(0,prefix); Comment(""); //--- Deinitialize library engine.OnDeinit(); } //+------------------------------------------------------------------+
当切换时间帧时,EA 析构期间会清除指标集合列表。 这可避免创建其他不必要的指标对象。
在 EA 的 OnTick() 处理程序里,访问每个已创建的指标对象,并在图表注释中显示每个出现的指标的当前柱线数据:
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- Handle the NewTick event in the library engine.OnTick(rates_data); //--- If work in tester if(MQLInfoInteger(MQL_TESTER)) { engine.OnTimer(rates_data); // Work in timer PressButtonsControl(); // Button press control engine.EventsHandling(); // Work with events } //--- Get custom indicator objects CIndicatorDE *ma1=engine.GetIndicatorsCollection().GetIndByID(MA1); CIndicatorDE *ma2=engine.GetIndicatorsCollection().GetIndByID(MA2); CIndicatorDE *ama1=engine.GetIndicatorsCollection().GetIndByID(AMA1); CIndicatorDE *ama2=engine.GetIndicatorsCollection().GetIndByID(AMA2); Comment ( "ma1=",DoubleToString(ma1.GetDataBuffer(0,0),6), ", ma2=",DoubleToString(ma2.GetDataBuffer(0,0),6), "\nama1=",DoubleToString(ama1.GetDataBuffer(0,0),6), ", ama2=",DoubleToString(ama2.GetDataBuffer(0,0),6) ); //--- If the trailing flag is set if(trailing_on) { TrailingPositions(); // Trailing positions TrailingOrders(); // Trailing of pending orders } } //+------------------------------------------------------------------+
在上一篇文章中,我们在函数库初始化函数 OnInitDoEasy() 中创建了临时指标对象。 从函数里删除这些代码:
//--- Create timeseries of all symbols used engine.SeriesCreateAll(array_used_periods); //--- Check created timeseries - display descriptions of all created timeseries in the journal //--- (true - only created ones, false - created and declared ones) engine.GetTimeSeriesCollection().PrintShort(false); // Short descriptions //engine.GetTimeSeriesCollection().Print(true); // Full descriptions //--- Create indicators engine.GetIndicatorsCollection().CreateAMA(Symbol(),Period(),9,2,30,0,PRICE_CLOSE); engine.GetIndicatorsCollection().CreateAMA(Symbol(),Period(),10,3,32,5,PRICE_CLOSE); engine.GetIndicatorsCollection().Print(); engine.GetIndicatorsCollection().PrintShort(); //--- Create resource text files
编译 EA,并在图表上启动它,对其进行初步设置,仅采用当前品种和时间帧。
日志里将显示所有已创建指标的参数说明:
--- Initialize "DoEasy" library --- Work with the current symbol only: "EURUSD" Work with the current timeframe only: H1 EURUSD symbol timeseries: - "EURUSD" H1 timeseries: Requested: 1000, Actually: 1000, Created: 1000, On the server: 6284 Library initialize time: 00:00:00.141 ============= Parameter list start: “Custom indicator" ============= Indicator status: Custom indicator Indicator type: CUSTOM Indicator timeframe: H1 Indicator handle: 10 Indicator group: Trend indicator Indicator ID: 1 ------ Empty value for plotting where nothing will be drawn: EMPTY_VALUE ------ Indicator symbol: EURUSD Indicator name: "Examples\Custom Moving Average.ex5" Indicator short name: "Examples\Custom Moving Average.ex5(EURUSD,H1)" --- Indicator parameters --- - [1] Type int: 13 - [2] Type int: 0 - [3] Type int: 0 ================== Parameter list end: "Custom indicator" ================== ============= Parameter list start: “Custom indicator" ============= Indicator status: Custom indicator Indicator type: CUSTOM Indicator timeframe: H1 Indicator handle: 11 Indicator group: Trend indicator Indicator ID: 2 ------ Empty value for plotting where nothing will be drawn: EMPTY_VALUE ------ Indicator symbol: EURUSD Indicator name: "Examples\Custom Moving Average.ex5" Indicator short name: "Examples\Custom Moving Average.ex5(EURUSD,H1)" --- Indicator parameters --- - [1] Type int: 13 - [2] Type int: 0 - [3] Type int: 0 - [4] Type int: 2 ================== Parameter list end: "Custom indicator" ================== ============= Parameter list start: "Standard indicator" ============= Indicator status: Standard indicator Indicator type: AMA Indicator timeframe: H1 Indicator handle: 12 Indicator group: Trend indicator Indicator ID: 3 ------ Empty value for plotting where nothing will be drawn: EMPTY_VALUE ------ Indicator symbol: EURUSD Indicator name: "Adaptive Moving Average" Indicator short name: "AMA(EURUSD,H1)" --- Indicator parameters --- - Averaging period: 9 - Fast MA period: 2 - Slow MA period: 30 - Horizontal shift of the indicator: 0 - Price type or handle: CLOSE ================== Parameter list end: "Standard indicator" ================== ============= Parameter list start: "Standard indicator" ============= Indicator status: Standard indicator Indicator type: AMA Indicator timeframe: H1 Indicator handle: 13 Indicator group: Trend indicator Indicator ID: 4 ------ Empty value for plotting where nothing will be drawn: EMPTY_VALUE ------ Indicator symbol: EURUSD Indicator name: "Adaptive Moving Average" Indicator short name: "AMA(EURUSD,H1)" --- Indicator parameters --- - Averaging period: 14 - Fast MA period: 2 - Slow MA period: 30 - Horizontal shift of the indicator: 0 - Price type or handle: CLOSE ================== Parameter list end: "Standard indicator" ================== Custom indicator Examples\Custom Moving Average.ex5 EURUSD H1 [handle 10, id #1] Custom indicator Examples\Custom Moving Average.ex5 EURUSD H1 [handle 11, id #2] Standard indicator Adaptive Moving Average EURUSD H1 [handle 12, id #3] Standard indicator Adaptive Moving Average EURUSD H1 [handle 13, id #4]
品种图表将显示所有已创建指标缓冲区中的数据:
可在图表上添加必要的指标,这些指标的参数与 EA 中创建的参数相对应。 您可以在图表注释和数据窗口中检查这些指标的匹配情况 - 它们应该匹配。
下一步是什么?
在后续的文章中,我将继续创建在 EA 中处理指标的功能。 在最近的文章中,我计划将指标数据与时间序列类柱线绑定,并从指标中获取数据,以便进行各种统计研究。
下面附有当前函数库版本的所有文件,以及 MQL5 的测试 EA 文件。 您可以下载它们,并测试所有内容。
请注意,目前指标集合类正在开发当中,因此,强烈建议不要在程序中使用它。
请在文章的评论中留下您的评论、问题和建议。
该系列中的先前文章:
DoEasy 函数库中的时间序列(第三十五部分):柱线对象和品种时间序列列表
DoEasy 函数库中的时间序列(第三十六部分):所有用到的品种周期的时间序列对象
DoEasy 函数库中的时间序列(第三十七部分):时间序列集合 - 按品种和周期的时间序列数据库
DoEasy 函数库中的时间序列(第三十八部分):时间序列集合 - 实时更新以及从程序访问数据
DoEasy 函数库中的时间序列(第三十九部分):基于函数库的指标 - 准备数据和时间序列事件
DoEasy 函数库中的时间序列(第四十部分):基于函数库的指标 - 实时刷新数据
DoEasy 函数库中的时间序列(第四十一部分):多品种多周期指标样品
DoEasy 函数库中的时间序列(第四十二部分):抽象指标缓冲区对象类
DoEasy 函数库中的时间序列(第四十三部分):指标缓冲区对象类
DoEasy 函数库中的时间序列(第四十四部分):指标缓冲区对象集合类
DoEasy 函数库中的时间序列(第四十五部分):多周期指标缓冲区
DoEasy 函数库中的时间序列(第四十六部分):多周期、多品种指标缓冲区
DoEasy 函数库中的时间序列(第四十七部分):多周期、多品种标准指标
DoEasy 函数库中的时间序列(第四十八部分):在单一子窗口里基于一个缓冲区的多周期、多品种指标
DoEasy 函数库中的时间序列(第四十九部分):多周期、多品种、多缓冲区标准指标
DoEasy 函数库中的时间序列(第五十部分):多周期、多品种带位移的标准指标
DoEasy 函数库中的时间序列(第五十一部分):复合多周期、多品种标准指标
DoEasy 函数库中的时间序列(第五十二部分):多周期、多品种单缓冲区标准指标的跨平台性质
DoEasy 函数库中的时间序列(第五十三部分):抽象基准指标类
DoEasy 函数库中的时间序列(第五十四部分):抽象基准指标类的衍生类
DoEasy 函数库中的时间序列(第五十五部分):指标集合类
本文由MetaQuotes Ltd译自俄文
原文地址: https://www.mql5.com/ru/articles/8646
注意: 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.



感谢您分享这些宝贵的 PROJECT;我将研究所有代码,学习更多深层次的编码技巧... :):)