![DoEasy 函数库中的时间序列(第五十二部分):多周期、多品种单缓冲区标准指标的跨平台性质](https://c.mql5.com/2/49/doeasy_052_600x314.jpg)
DoEasy 函数库中的时间序列(第五十二部分):多周期、多品种单缓冲区标准指标的跨平台性质
内容目录
概述
从文章第三十九部分开始,我们逐步创立了构造自己的多品种、多周期指标的功能。 在所创建的基础上,很自然还应让标准指标能在多模式下操作。 且从文章第四十七部分开始,我们创立了这样的功能(存在的缺点会逐步定位并修复)。 但我们所做的一切都是针对 MetaTrader 5 平台完成的。
略微改进指标依托的函数库类,以便从老旧的 MetaTrader 4 平台切换到 MetaTrader 5 时,基于该函数库开发的程序均可正常运行。
与 MQL5 相反,在 MQL4 当中,我们的单一缓冲区不能拥有若干个绘图颜色。 在 MetaTrader 4 中,所有指标缓冲区都是单色的。 此限制也影响到 MetaTrader 4 的多缓冲区构造的概念。 在此,我们不能为特定的柱线指定绘制颜色。 而在 MetaTrader 5 里这很容易。 在此,我们将不得不为每种颜色单独定义一个单色指标缓冲区。 鉴于我们不必创建额外的计算缓冲区来存储图表的指定品种/周期中的指标数据 - MQL4 里针对指标的所有请求函数都会返回来自指定品种/周期的指定指标柱线的数值。 而在 MQL5 中,必须为指标创建句柄,并将必要数量的数据复制到目标数组,以便从该句柄请求数据。 对于指标,它是其计算缓冲区。 此后,按所需柱线索引从该数组获取指定指标的数据。 而在 MQL5 中,我们可以加快访问指标数据的速度。
故此,MQL4 中标准指标缓冲区对象的构造略有不同。 无需创建额外的计算缓冲区来存储要在当前图表上显示的指标数据信息。 但简化看来,我们失去了灵活性:要为每种颜色创建一个彩色缓冲区,我们必须拥有自己的单色指标缓冲区,现在,需要为柱线的显示颜色指定相应的缓冲区。 其余的缓冲区将被隐藏。 这是一个复杂的过程。
基于以上所述,MQL4 的多缓冲区构造概念如下:
- 我们将为一条指标曲线的每种颜色设置一个单独的指标缓冲区,
- 切换曲线颜色时,即显示与必要颜色相对应的那个指标曲线(缓冲区); 而同一指标指向所有其他颜色的曲线将被隐藏
综上所述,对于彩色多品种、多周期移动平均线指标,有三个显示颜色:
在 MQL5 中,我们会有三个数据数组(三个缓冲区):
- 绘制缓冲区(数据在“数据”窗口中显示)
- 颜色缓冲区(在“数据”窗口中不显示,但它指定第一条曲线上的柱线将使用哪个颜色缓冲区)
- 计算缓冲区,存储来自指定品种/周期的移动平均值(在“数据”窗口中不显示)
在 MQL4 中,我们会有三个数据数组(三个缓冲区):
- 颜色 1 的绘制缓冲区(在“数据”窗口中显示数据)
- 颜色 2 的绘制缓冲区(在“数据”窗口中显示数据)
- 颜色 3 的绘制缓冲区(在“数据”窗口中显示数据)
当减少颜色数量时,MQL4 的缓冲区数量将减少;若颜色数量增加,缓冲区数量也将增加。 在 MQL5 中,此示例的缓冲区数将始终等于 3。 而 MQL5 可以动态修改颜色数量,高达 64 种。 在 MQL4 中,指标曲线涂色并非那么简单。 原因是,必须为 64 种颜色创建 64 个缓冲区。 而这仅仅是为了一条曲线。 如果一个指标有 4 条曲线,则需要 256 个指标缓冲区数组。 若是八条曲线,则需要 512 个缓冲区,而这是极限。 犹如 MQL5,针对每根柱线指定了相应颜色的索引,并在该柱线上将曲线涂为指定的颜色。 对于 MQL4,与颜色对应的缓冲区会被显示,而其他则被隐藏。 在 MQL4 中,每种颜色的所有缓冲区都在终端数据窗口中可见。 在 MQL5 中,如同此例,将在终端数据窗口中看到一个对应的缓冲区:一条指示曲线 = 终端数据窗口中的一个值。
我们不会立即纠正已经完成的事情。 取而代之的是,逐步从简单到复杂,改进库类,令其某些方面的操作与 MQL4 函数库中的指标兼容。 今日,以建仓/派发指标为例,检查在 MQL4 中利用函数库创建的单缓冲区单色多品种、多周期标准指标的过程。
改进库类
如同往常,首先,添加必要的文本消息。 之前,在最终的指标程序的整个代码里,我们会立即检查由函数库创建的指标缓冲区与有关指标缓冲区所需数量的相关性:
#property indicator_buffers 3 #property indicator_plots 1
进而,在代码中,函数库创建了所有必需缓冲区之后,会执行检查:
//--- Check the number of buffers specified in the 'properties' block if(engine.BuffersPropertyPlotsTotal()!=indicator_plots) Alert(TextByLanguage("Attention! Value of \"indicator_plots\" should be "),engine.BuffersPropertyPlotsTotal()); if(engine.BuffersPropertyBuffersTotal()!=indicator_buffers) Alert(TextByLanguage("Attention! Value of \"indicator_buffers\" should be "),engine.BuffersPropertyBuffersTotal());
把针对 MQL4 的兼容性检查稍微改进,并将其移至函数库当中。 将检查时显示的文本放在函数库中的相应位置 - 在文件 \MQL5\Include\DoEasy\Data.mqh。 并加入新消息的索引:
//--- CEngine MSG_ENG_NO_TRADE_EVENTS, // There have been no trade events since the last launch of EA MSG_ENG_FAILED_GET_LAST_TRADE_EVENT_DESCR, // Failed to get description of the last trading event MSG_ENG_FAILED_GET_MARKET_POS_LIST, // Failed to get the list of open positions MSG_ENG_FAILED_GET_PENDING_ORD_LIST, // Failed to get the list of placed orders MSG_ENG_NO_OPEN_POSITIONS, // No open positions MSG_ENG_NO_PLACED_ORDERS, // No placed orders MSG_ENG_ERR_VALUE_PLOTS, // Attention! Value \"indicator_plots\" must be MSG_ENG_ERR_VALUE_ORDERS, // Attention! Value \"indicator_buffers\" must be
及与新添加的索引相对应的消息文本:
//--- CEngine {"There have been no trade events since the last launch of EA"}, {"Failed to get the description of the last trading event"}, {"Failed to get open positions list"}, {"Failed to get pending orders list"}, {"No open positions"}, {"No placed orders"}, {"Attention! Value of \"indicator_plots\" should be "}, {"Attention! Value of \"indicator_buffers\" should be "},
包含程序输入数据的文件称为 InpDatas.mqh... 遵循英语语法将其调整为正确的名称(我为文件命名时犯了一个错误)。 现在,该文件将称之为:\MQL5\Include\DoEasy\InpData.mqh。
简单地在其所在的文件夹中重命名它即可。
此文件已在文件 Data.mqh(我们正在编辑)中连接到函数库,调整包含代码:
//+------------------------------------------------------------------+ //| Data.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 "InpData.mqh" //+------------------------------------------------------------------+
我们开始实现跨平台特性。
如果尝试在 MetaTrader 4(文件 Engine.mqh 上的 F7)上的 MetaEditor 中编译该函数库,则会收到很多错误:
好吧,这并不令人惊讶。 很简单,按顺序开始。 首先,很明显,MQL4 还不知道某些常量和枚举。
在文件 \MQL5\Include\DoEasy\ToMQL4.mqh 里加入新的常量和枚举:
//+------------------------------------------------------------------+ //| ToMQL4.mqh | //| Copyright 2017, Artem A. Trishkin, Skype artmedia70 | //| https://www.mql5.com/en/users/artmedia70 | //+------------------------------------------------------------------+ #property copyright "Copyright 2017, Artem A. Trishkin, Skype artmedia70" #property link "https://www.mql5.com/en/users/artmedia70" #property strict #ifdef __MQL4__ //+------------------------------------------------------------------+ //| Error codes | //+------------------------------------------------------------------+ #define ERR_SUCCESS (ERR_NO_ERROR) #define ERR_MARKET_UNKNOWN_SYMBOL (ERR_UNKNOWN_SYMBOL) #define ERR_ZEROSIZE_ARRAY (ERR_ARRAY_INVALID) #define ERR_MAIL_SEND_FAILED (ERR_SEND_MAIL_ERROR) #define ERR_NOTIFICATION_SEND_FAILED (ERR_NOTIFICATION_ERROR) #define ERR_FTP_SEND_FAILED (ERR_FTP_ERROR) //+------------------------------------------------------------------+ //| Order types, filling policy, period, reasons | //+------------------------------------------------------------------+ #define ORDER_TYPE_CLOSE_BY (8) #define ORDER_TYPE_BUY_STOP_LIMIT (9) #define ORDER_TYPE_SELL_STOP_LIMIT (10) #define ORDER_REASON_EXPERT (3) #define ORDER_REASON_SL (4) #define ORDER_REASON_TP (5) #define ORDER_REASON_BALANCE (6) #define ORDER_REASON_CREDIT (7) //+------------------------------------------------------------------+ //| Flags of allowed order expiration modes | //+------------------------------------------------------------------+ #define SYMBOL_EXPIRATION_GTC (1) #define SYMBOL_EXPIRATION_DAY (2) #define SYMBOL_EXPIRATION_SPECIFIED (4) #define SYMBOL_EXPIRATION_SPECIFIED_DAY (8) //+------------------------------------------------------------------+ //| Flags of allowed order filling modes | //+------------------------------------------------------------------+ #define SYMBOL_FILLING_FOK (1) #define SYMBOL_FILLING_IOC (2) //+------------------------------------------------------------------+ //| The flags of allowed order types | //+------------------------------------------------------------------+ #define SYMBOL_ORDER_MARKET (1) #define SYMBOL_ORDER_LIMIT (2) #define SYMBOL_ORDER_STOP (4) #define SYMBOL_ORDER_STOP_LIMIT (8) #define SYMBOL_ORDER_SL (16) #define SYMBOL_ORDER_TP (32) #define SYMBOL_ORDER_CLOSEBY (64) //+------------------------------------------------------------------+ //| Indicator lines IDs | //+------------------------------------------------------------------+ #define TENKANSEN_LINE (0) #define KIJUNSEN_LINE (1) #define SENKOUSPANA_LINE (2) #define SENKOUSPANB_LINE (3) #define CHIKOUSPAN_LINE (4) //+------------------------------------------------------------------+ //| Deal types of MQL5 | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Position change method | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Open position direction | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Order status | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Margin calculation mode | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Prices on which basis chart by symbol is constructed | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Life span of pending orders and | //| set levels of StopLoss/TakeProfit | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Options types | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Right provided by an option | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Method of margin value calculation for an instrument | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Swap charge methods at position move | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Trading operations types | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Order filling policies | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Order life span | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Integer properties of selected position | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Real properties of selected position | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| String properties of selected position | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Technical indicator types | //+------------------------------------------------------------------+ enum ENUM_INDICATOR { IND_AC = 5, IND_AD = 6, IND_ALLIGATOR = 7, IND_ADX = 8, IND_ADXW = 9, IND_ATR = 10, IND_AO = 11, IND_BEARS = 12, IND_BANDS = 13, IND_BULLS = 14, IND_CCI = 15, IND_DEMARKER = 16, IND_ENVELOPES = 17, IND_FORCE = 18, IND_FRACTALS = 19, IND_GATOR = 20, IND_ICHIMOKU = 21, IND_BWMFI = 22, IND_MACD = 23, IND_MOMENTUM = 24, IND_MFI = 25, IND_MA = 26, IND_OSMA = 27, IND_OBV = 28, IND_SAR = 29, IND_RSI = 30, IND_RVI = 31, IND_STDDEV = 32, IND_STOCHASTIC = 33, IND_VOLUMES = 34, IND_WPR = 35, IND_DEMA = 36, IND_TEMA = 37, IND_TRIX = 38, IND_FRAMA = 39, IND_AMA = 40, IND_CHAIKIN = 41, IND_VIDYA = 42, IND_CUSTOM = 43, }; //+------------------------------------------------------------------+ //| Drawing style | //+------------------------------------------------------------------+ enum ENUM_DRAW_TYPE { DRAW_COLOR_LINE = DRAW_LINE, // MQL5 = 1, MQL4 = 0 DRAW_COLOR_HISTOGRAM = DRAW_HISTOGRAM, // MQL5 = 2, MQL4 = 2 DRAW_COLOR_ARROW = DRAW_ARROW, // MQL5 = 3, MQL4 = 3 DRAW_COLOR_SECTION = DRAW_SECTION, // MQL5 = 4, MQL4 = 1 DRAW_COLOR_HISTOGRAM2 = DRAW_NONE, // MQL5 = 0, MQL4 = 12 DRAW_COLOR_ZIGZAG = DRAW_ZIGZAG, // MQL5 = 6, MQL4 = 4 DRAW_COLOR_BARS = DRAW_NONE, // MQL5 = 0, MQL4 = 12 DRAW_COLOR_CANDLES = DRAW_NONE, // MQL5 = 0, MQL4 = 12 // DRAW_FILLING MQL4 = 5 DRAW_COLOR_NONE = DRAW_NONE, // MQL5 = 0, MQL4 = 12 }; //+------------------------------------------------------------------+ //| Volume type | //+------------------------------------------------------------------+ enum ENUM_APPLIED_VOLUME { VOLUME_TICK, VOLUME_REAL }; //+------------------------------------------------------------------+ //| Trade request structure | //+------------------------------------------------------------------+ //--- the code has been further removed for the sake of space #endif
进而,在随后的编译中,将得到 MQL4 中缺少 mql5 函数的错误。 特别是,BarsCalculated()。 此函数返回依据指标句柄得到的已计算柱线数量。 对于 MQL4 该函数未知。 mql4 函数 Bars() 是最接近 BarsCalculated() 的函数,就其含义而言,它返回指定时间序列中可用柱线的数量。
由于在 MQL4 中,在请求期间会考虑指标已进行过计算,因此我们可用时间序列的可用柱线数量(MQL4 Bars())代替指标(MQL5 BarsCalculated())的已计算数据量。 无论如何,当获取指标数据时,函数库方法将返回接收到的数据,并进行验证。 因此,我们研究一下可用时间序列柱线的规范,来代替 MQL4 中指标的已计算数据量。
调用函数 BarsCalculated() 的方法 IndicatorBarsCalculated() 位于文件 \MQL5\Include\DoEasy\Objects\Indicators\Buffer.mqh 当中。 同时,我们将不得不在同一位置对操控指标的其他方法进行大量改进。
早前,该方法整体编写在类主体中,其可立即返回已计算的数据量:
ENUM_INDICATOR IndicatorType(void) const { return (ENUM_INDICATOR)this.GetProperty(BUFFER_PROP_IND_TYPE); } string IndicatorName(void) const { return this.GetProperty(BUFFER_PROP_IND_NAME); } string IndicatorShortName(void) const { return this.GetProperty(BUFFER_PROP_IND_NAME_SHORT); } int IndicatorBarsCalculated(void) const { return ::BarsCalculated((int)this.GetProperty(BUFFER_PROP_IND_HANDLE));} int IndicatorLineAdditionalNumber(void) const { return (int)this.GetProperty(BUFFER_PROP_IND_LINE_ADDITIONAL_NUM); } int IndicatorLineMode(void) const { return (int)this.GetProperty(BUFFER_PROP_IND_LINE_MODE); }
现在,仅保留方法声明
ENUM_INDICATOR IndicatorType(void) const { return (ENUM_INDICATOR)this.GetProperty(BUFFER_PROP_IND_TYPE); } string IndicatorName(void) const { return this.GetProperty(BUFFER_PROP_IND_NAME); } string IndicatorShortName(void) const { return this.GetProperty(BUFFER_PROP_IND_NAME_SHORT); } int IndicatorLineAdditionalNumber(void) const { return (int)this.GetProperty(BUFFER_PROP_IND_LINE_ADDITIONAL_NUM); } int IndicatorLineMode(void) const { return (int)this.GetProperty(BUFFER_PROP_IND_LINE_MODE); } int IndicatorBarsCalculated(void);
... 并将其实现移到类主体之外:
//+------------------------------------------------------------------+ //| Return the number of standard indicator calculated bars | //+------------------------------------------------------------------+ int CBuffer::IndicatorBarsCalculated(void) { return(#ifdef __MQL5__ ::BarsCalculated((int)this.GetProperty(BUFFER_PROP_IND_HANDLE)) #else ::Bars(this.Symbol(),this.Timeframe()) #endif); } //+------------------------------------------------------------------+
此处,对于 MQL5,返回已计算的指标数据量;对于 MQL4,返回时间序列可用数据的数量。
将封闭的参数类构造函数切分为两部分。
第一部分 — 已用的那个,将仅针对 MQL5 保留;而对于 MQL4 复制 mql5 代码,并从中删除不必要的代码:
//+------------------------------------------------------------------+ //| Closed parametric constructor | //+------------------------------------------------------------------+ CBuffer::CBuffer(ENUM_BUFFER_STATUS buffer_status, ENUM_BUFFER_TYPE buffer_type, const uint index_plot, const uint index_base_array, const int num_datas, const uchar total_arrays, const int width, const string label) { #ifdef __MQL5__ this.m_type=COLLECTION_BUFFERS_ID; this.m_act_state_trigger=true; this.m_total_arrays=total_arrays; //--- Save integer properties this.m_long_prop[BUFFER_PROP_STATUS] = buffer_status; this.m_long_prop[BUFFER_PROP_TYPE] = buffer_type; this.m_long_prop[BUFFER_PROP_ID] = WRONG_VALUE; this.m_long_prop[BUFFER_PROP_IND_LINE_MODE] = INDICATOR_LINE_MODE_MAIN; this.m_long_prop[BUFFER_PROP_IND_HANDLE] = INVALID_HANDLE; this.m_long_prop[BUFFER_PROP_IND_TYPE] = WRONG_VALUE; this.m_long_prop[BUFFER_PROP_IND_LINE_ADDITIONAL_NUM] = WRONG_VALUE; ENUM_DRAW_TYPE type= ( !this.TypeBuffer() || !this.Status() ? DRAW_NONE : this.Status()==BUFFER_STATUS_FILLING ? DRAW_FILLING : ENUM_DRAW_TYPE(this.Status()+8) ); this.m_long_prop[BUFFER_PROP_DRAW_TYPE] = type; this.m_long_prop[BUFFER_PROP_TIMEFRAME] = PERIOD_CURRENT; this.m_long_prop[BUFFER_PROP_ACTIVE] = true; this.m_long_prop[BUFFER_PROP_ARROW_CODE] = 0x9F; this.m_long_prop[BUFFER_PROP_ARROW_SHIFT] = 0; this.m_long_prop[BUFFER_PROP_DRAW_BEGIN] = 0; this.m_long_prop[BUFFER_PROP_SHOW_DATA] = (buffer_type>BUFFER_TYPE_CALCULATE ? true : false); this.m_long_prop[BUFFER_PROP_SHIFT] = 0; this.m_long_prop[BUFFER_PROP_LINE_STYLE] = STYLE_SOLID; this.m_long_prop[BUFFER_PROP_LINE_WIDTH] = width; this.m_long_prop[BUFFER_PROP_COLOR_INDEXES] = (this.Status()>BUFFER_STATUS_NONE ? (this.Status()!=BUFFER_STATUS_FILLING ? 1 : 2) : 0); this.m_long_prop[BUFFER_PROP_COLOR] = clrRed; this.m_long_prop[BUFFER_PROP_NUM_DATAS] = num_datas; this.m_long_prop[BUFFER_PROP_INDEX_PLOT] = index_plot; this.m_long_prop[BUFFER_PROP_INDEX_BASE] = index_base_array; this.m_long_prop[BUFFER_PROP_INDEX_COLOR] = this.GetProperty(BUFFER_PROP_INDEX_BASE)+ (this.TypeBuffer()!=BUFFER_TYPE_CALCULATE ? this.GetProperty(BUFFER_PROP_NUM_DATAS) : 0); this.m_long_prop[BUFFER_PROP_INDEX_NEXT_BASE] = index_base_array+this.m_total_arrays; this.m_long_prop[BUFFER_PROP_INDEX_NEXT_PLOT] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? index_plot+1 : index_plot); //--- Save real properties this.m_double_prop[this.IndexProp(BUFFER_PROP_EMPTY_VALUE)] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? EMPTY_VALUE : 0); //--- Save string properties this.m_string_prop[this.IndexProp(BUFFER_PROP_SYMBOL)] = ::Symbol(); this.m_string_prop[this.IndexProp(BUFFER_PROP_LABEL)] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? label : NULL); this.m_string_prop[this.IndexProp(BUFFER_PROP_IND_NAME)] = NULL; this.m_string_prop[this.IndexProp(BUFFER_PROP_IND_NAME_SHORT)]=NULL; //--- If failed to change the size of the indicator buffer array, display the appropriate message indicating the string if(::ArrayResize(this.DataBuffer,(int)this.GetProperty(BUFFER_PROP_NUM_DATAS))==WRONG_VALUE) ::Print(DFUN_ERR_LINE,CMessage::Text(MSG_LIB_SYS_FAILED_DRAWING_ARRAY_RESIZE),". ",CMessage::Text(MSG_LIB_SYS_ERROR),": ",(string)::GetLastError()); //--- If failed to change the size of the color array (only for a non-calculated buffer), display the appropriate message indicating the string if(this.TypeBuffer()>BUFFER_TYPE_CALCULATE) if(::ArrayResize(this.ArrayColors,(int)this.ColorsTotal())==WRONG_VALUE) ::Print(DFUN_ERR_LINE,CMessage::Text(MSG_LIB_SYS_FAILED_COLORS_ARRAY_RESIZE),". ",CMessage::Text(MSG_LIB_SYS_ERROR),": ",(string)::GetLastError()); //--- For DRAW_FILLING, fill in the color array with two default colors if(this.Status()==BUFFER_STATUS_FILLING) { this.SetColor(clrBlue,0); this.SetColor(clrRed,1); } //--- Bind indicator buffers with arrays //--- In a loop by the number of indicator buffers int total=::ArraySize(DataBuffer); for(int i=0;i<total;i++) { //--- calculate the index of the next array and //--- bind the indicator buffer by the calculated index with the dynamic array //--- located by the i loop index in the DataBuffer array int index=(int)this.GetProperty(BUFFER_PROP_INDEX_BASE)+i; ::SetIndexBuffer(index,this.DataBuffer[i].Array,(this.TypeBuffer()==BUFFER_TYPE_DATA ? INDICATOR_DATA : INDICATOR_CALCULATIONS)); //--- Set indexation flag as in the timeseries to all buffer arrays ::ArraySetAsSeries(this.DataBuffer[i].Array,true); } //--- Bind the color buffer with the array (only for a non-calculated buffer and not for the filling buffer) if(this.Status()!=BUFFER_STATUS_FILLING && this.TypeBuffer()!=BUFFER_TYPE_CALCULATE) { ::SetIndexBuffer((int)this.GetProperty(BUFFER_PROP_INDEX_COLOR),this.ColorBufferArray,INDICATOR_COLOR_INDEX); ::ArraySetAsSeries(this.ColorBufferArray,true); } //--- Done if this is a calculated buffer if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; //--- Set integer parameters of the graphical series ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_TYPE,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_DRAW_TYPE)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_ARROW,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_ARROW_CODE)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_ARROW_SHIFT,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_ARROW_SHIFT)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_BEGIN,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_DRAW_BEGIN)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_SHOW_DATA,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_SHOW_DATA)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_SHIFT,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_SHIFT)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_STYLE,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_LINE_STYLE)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_WIDTH,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_LINE_WIDTH)); this.SetColor((color)this.GetProperty(BUFFER_PROP_COLOR)); //--- Set real parameters of the graphical series ::PlotIndexSetDouble((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_EMPTY_VALUE,this.GetProperty(BUFFER_PROP_EMPTY_VALUE)); //--- Set string parameters of the graphical series ::PlotIndexSetString((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LABEL,this.GetProperty(BUFFER_PROP_LABEL)); //--- MQL4 #else this.m_type=COLLECTION_BUFFERS_ID; this.m_act_state_trigger=true; this.m_total_arrays=1; //--- Save integer properties this.m_long_prop[BUFFER_PROP_STATUS] = buffer_status; this.m_long_prop[BUFFER_PROP_TYPE] = buffer_type; this.m_long_prop[BUFFER_PROP_ID] = WRONG_VALUE; this.m_long_prop[BUFFER_PROP_IND_LINE_MODE] = INDICATOR_LINE_MODE_MAIN; this.m_long_prop[BUFFER_PROP_IND_HANDLE] = INVALID_HANDLE; this.m_long_prop[BUFFER_PROP_IND_TYPE] = WRONG_VALUE; this.m_long_prop[BUFFER_PROP_IND_LINE_ADDITIONAL_NUM] = WRONG_VALUE; ENUM_DRAW_TYPE type=DRAW_COLOR_NONE; switch((int)this.Status()) { case BUFFER_STATUS_LINE : type=DRAW_COLOR_LINE; break; case BUFFER_STATUS_HISTOGRAM : type=DRAW_COLOR_HISTOGRAM; break; case BUFFER_STATUS_ARROW : type=DRAW_COLOR_ARROW; break; case BUFFER_STATUS_SECTION : type=DRAW_COLOR_SECTION; break; case BUFFER_STATUS_ZIGZAG : type=DRAW_COLOR_ZIGZAG; break; case BUFFER_STATUS_NONE : type=DRAW_COLOR_NONE; break; case BUFFER_STATUS_FILLING : type=DRAW_COLOR_NONE; break; case BUFFER_STATUS_HISTOGRAM2 : type=DRAW_COLOR_NONE; break; case BUFFER_STATUS_BARS : type=DRAW_COLOR_NONE; break; case BUFFER_STATUS_CANDLES : type=DRAW_COLOR_NONE; break; default : type=DRAW_COLOR_NONE; break; } this.m_long_prop[BUFFER_PROP_DRAW_TYPE] = type; this.m_long_prop[BUFFER_PROP_TIMEFRAME] = PERIOD_CURRENT; this.m_long_prop[BUFFER_PROP_ACTIVE] = true; this.m_long_prop[BUFFER_PROP_ARROW_CODE] = 0x9F; this.m_long_prop[BUFFER_PROP_ARROW_SHIFT] = 0; this.m_long_prop[BUFFER_PROP_DRAW_BEGIN] = 0; this.m_long_prop[BUFFER_PROP_SHOW_DATA] = (buffer_type>BUFFER_TYPE_CALCULATE ? true : false); this.m_long_prop[BUFFER_PROP_SHIFT] = 0; this.m_long_prop[BUFFER_PROP_LINE_STYLE] = STYLE_SOLID; this.m_long_prop[BUFFER_PROP_LINE_WIDTH] = width; this.m_long_prop[BUFFER_PROP_COLOR_INDEXES] = (this.Status()>BUFFER_STATUS_NONE ? (this.Status()!=BUFFER_STATUS_FILLING ? 1 : 2) : 0); this.m_long_prop[BUFFER_PROP_COLOR] = clrRed; this.m_long_prop[BUFFER_PROP_NUM_DATAS] = num_datas; this.m_long_prop[BUFFER_PROP_INDEX_PLOT] = index_plot; this.m_long_prop[BUFFER_PROP_INDEX_BASE] = index_base_array; this.m_long_prop[BUFFER_PROP_INDEX_COLOR] = this.GetProperty(BUFFER_PROP_INDEX_BASE); this.m_long_prop[BUFFER_PROP_INDEX_NEXT_BASE] = index_base_array+this.m_total_arrays; this.m_long_prop[BUFFER_PROP_INDEX_NEXT_PLOT] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? index_plot+1 : index_plot); //--- Save real properties this.m_double_prop[this.IndexProp(BUFFER_PROP_EMPTY_VALUE)] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? EMPTY_VALUE : 0); //--- Save string properties this.m_string_prop[this.IndexProp(BUFFER_PROP_SYMBOL)] = ::Symbol(); this.m_string_prop[this.IndexProp(BUFFER_PROP_LABEL)] = (this.TypeBuffer()>BUFFER_TYPE_CALCULATE ? label : NULL); this.m_string_prop[this.IndexProp(BUFFER_PROP_IND_NAME)] = NULL; this.m_string_prop[this.IndexProp(BUFFER_PROP_IND_NAME_SHORT)]=NULL; //--- If failed to change the size of the indicator buffer array, display the appropriate message indicating the string if(::ArrayResize(this.DataBuffer,(int)this.GetProperty(BUFFER_PROP_NUM_DATAS))==WRONG_VALUE) ::Print(DFUN_ERR_LINE,CMessage::Text(MSG_LIB_SYS_FAILED_DRAWING_ARRAY_RESIZE),". ",CMessage::Text(MSG_LIB_SYS_ERROR),": ",(string)::GetLastError()); //--- Bind indicator buffers with arrays //--- In a loop by the number of indicator buffers int total=::ArraySize(DataBuffer); for(int i=0;i<total;i++) { //--- calculate the index of the next array and //--- bind the indicator buffer by the calculated index with the dynamic array //--- located by the i loop index in the DataBuffer array int index=(int)this.GetProperty(BUFFER_PROP_INDEX_BASE)+i; ::SetIndexBuffer(index,this.DataBuffer[i].Array,(this.TypeBuffer()==BUFFER_TYPE_DATA ? INDICATOR_DATA : INDICATOR_CALCULATIONS)); //--- Set indexation flag as in the timeseries to all buffer arrays ::ArraySetAsSeries(this.DataBuffer[i].Array,true); } //--- Done if this is a calculated buffer if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; //--- Set integer parameters of the graphical series this.SetDrawType(type); ::SetIndexStyle((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT), (ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_DRAW_TYPE), (ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_LINE_STYLE), (ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_LINE_WIDTH), (ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_COLOR)); ::SetIndexArrow((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_ARROW_CODE)); ::SetIndexShift((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_ARROW_SHIFT)); ::SetIndexDrawBegin((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_DRAW_BEGIN)); ::SetIndexShift((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_SHIFT)); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_SHOW_DATA,(ENUM_PLOT_PROPERTY_INTEGER)this.GetProperty(BUFFER_PROP_SHOW_DATA)); //--- Set real parameters of the graphical series ::SetIndexEmptyValue((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),this.GetProperty(BUFFER_PROP_EMPTY_VALUE)); //--- Set string parameters of the graphical series ::SetIndexLabel((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),this.GetProperty(BUFFER_PROP_LABEL)); #endif } //+------------------------------------------------------------------+
此处,主要区别在于绘图类型的计算。 对于 MQL5,我们依据缓冲区类型(其状态)进行计算,在此设置相应的数值更容易。 为了设置指标缓冲区的必要值,需调用相应的 mql4 函数,因为尽管 mql5 函数 PlotIndexSetInteger(),PlotIndexSetDouble() 和 PlotIndexSetString() 不会引起编译错误,但同时,它们也不会为 MQL4 中的指标缓冲区设置必要的值。
依相同的方式,在为指标缓冲区设置某些属性的方法之中,依据每种语言的相应函数,将分为mql5-代码和 mql4-代码:
//+------------------------------------------------------------------+ //--- Set the graphical construction type by type and status | //+------------------------------------------------------------------+ void CBuffer::SetDrawType(void) { ENUM_DRAW_TYPE type=(!this.TypeBuffer() || !this.Status() ? (ENUM_DRAW_TYPE)DRAW_NONE : this.Status()==BUFFER_STATUS_FILLING ? (ENUM_DRAW_TYPE)DRAW_FILLING : ENUM_DRAW_TYPE(this.Status()+8)); this.SetProperty(BUFFER_PROP_DRAW_TYPE,type); #ifdef __MQL5__ ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_TYPE,type); #else ::SetIndexStyle((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),type,EMPTY,EMPTY,EMPTY); #endif } //+------------------------------------------------------------------+ //| Set the passed graphical construction type | //+------------------------------------------------------------------+ void CBuffer::SetDrawType(const ENUM_DRAW_TYPE draw_type) { if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; this.SetProperty(BUFFER_PROP_DRAW_TYPE,draw_type); #ifdef __MQL5__ ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_TYPE,draw_type); #else ::SetIndexStyle((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),draw_type,EMPTY,EMPTY,EMPTY); #endif } //+------------------------------------------------------------------+ //| Set the number of initial bars | //| without drawing and values in DataWindow | //+------------------------------------------------------------------+ void CBuffer::SetDrawBegin(const int value) { if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; this.SetProperty(BUFFER_PROP_DRAW_BEGIN,value); #ifdef __MQL5__ ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_DRAW_BEGIN,value); #else ::SetIndexDrawBegin((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),value); #endif } //+------------------------------------------------------------------+ //| Set the flag of displaying | //| construction values in DataWindow | //+------------------------------------------------------------------+ void CBuffer::SetShowData(const bool flag) { if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; this.SetProperty(BUFFER_PROP_SHOW_DATA,flag); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_SHOW_DATA,flag); } //+------------------------------------------------------------------+ //| Set the indicator graphical construction shift | //+------------------------------------------------------------------+ void CBuffer::SetShift(const int shift) { this.SetProperty(BUFFER_PROP_SHIFT,shift); if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; #ifdef __MQL5__ ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_SHIFT,shift); #else ::SetIndexShift((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),shift); #endif } //+------------------------------------------------------------------+ //| Set the line style | //+------------------------------------------------------------------+ void CBuffer::SetStyle(const ENUM_LINE_STYLE style) { if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; this.SetProperty(BUFFER_PROP_LINE_STYLE,style); #ifdef __MQL5__ ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_STYLE,style); #else ::SetIndexStyle((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),this.DrawType(),style,EMPTY,EMPTY); #endif } //+------------------------------------------------------------------+ //| Set the line width | //+------------------------------------------------------------------+ void CBuffer::SetWidth(const int width) { if(this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; this.SetProperty(BUFFER_PROP_LINE_WIDTH,width); #ifdef __MQL5__ ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_WIDTH,width); #else ::SetIndexStyle((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),this.DrawType(),EMPTY,width,EMPTY); #endif } //+------------------------------------------------------------------+ //| Set the number of colors | //+------------------------------------------------------------------+ void CBuffer::SetColorNumbers(const int number) { if(number>IND_COLORS_TOTAL || this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; int n=(this.Status()!=BUFFER_STATUS_FILLING ? number : 2); this.SetProperty(BUFFER_PROP_COLOR_INDEXES,n); ::ArrayResize(this.ArrayColors,n); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_COLOR_INDEXES,n); } //+------------------------------------------------------------------+ //| Set a single specified drawing color for the buffer | //+------------------------------------------------------------------+ void CBuffer::SetColor(const color colour) { if(this.Status()==BUFFER_STATUS_FILLING || this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; this.SetColorNumbers(1); this.SetProperty(BUFFER_PROP_COLOR,colour); this.ArrayColors[0]=colour; #ifdef __MQL5__ ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_COLOR,0,this.ArrayColors[0]); #else ::SetIndexStyle((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),this.DrawType(),EMPTY,EMPTY,this.ArrayColors[0]); #endif } //+------------------------------------------------------------------+ //| Set the drawing color to the specified color index | //+------------------------------------------------------------------+ void CBuffer::SetColor(const color colour,const uchar index) { #ifdef __MQL5__ if(index>IND_COLORS_TOTAL-1 || this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; if(index>this.ColorsTotal()-1) this.SetColorNumbers(index+1); this.ArrayColors[index]=colour; if(index==0) this.SetProperty(BUFFER_PROP_COLOR,(color)this.ArrayColors[0]); ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_COLOR,index,this.ArrayColors[index]); #else #endif } //+------------------------------------------------------------------+ //| Set drawing colors from the color array | //+------------------------------------------------------------------+ void CBuffer::SetColors(const color &array_colors[]) { #ifdef __MQL5__ //--- Exit if the passed array is empty if(::ArraySize(array_colors)==0 || this.TypeBuffer()==BUFFER_TYPE_CALCULATE) return; //--- Copy the passed array to the array of buffer object colors ::ArrayCopy(this.ArrayColors,array_colors,0,0,IND_COLORS_TOTAL); //--- Exit if the color array was empty and not copied for some reason int total=::ArraySize(this.ArrayColors); if(total==0) return; //--- If the drawing style is not DRAW_FILLING if(this.Status()!=BUFFER_STATUS_FILLING) { //--- if the new number of colors exceeds the currently set one, //--- set the new value for the number of colors if(total>this.ColorsTotal()) this.SetColorNumbers(total); } //--- If the drawing style is DRAW_FILLING, set the number of colors equal to 2 else total=2; //--- Set the very first color from the color array (for a single color) to the buffer object color property this.SetProperty(BUFFER_PROP_COLOR,(color)this.ArrayColors[0]); //--- Set the new number of colors for the indicator buffer ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_COLOR_INDEXES,total); //--- In the loop by the new number of colors, set all colors by their indices for the indicator buffer for(int i=0;i<total;i++) ::PlotIndexSetInteger((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LINE_COLOR,i,this.ArrayColors[i]); #else #endif } //+------------------------------------------------------------------+ //| Set the "empty" value for construction | //| without drawing | //+------------------------------------------------------------------+ void CBuffer::SetEmptyValue(const double value) { this.SetProperty(BUFFER_PROP_EMPTY_VALUE,value); if(this.TypeBuffer()!=BUFFER_TYPE_CALCULATE) #ifdef __MQL5__ ::PlotIndexSetDouble((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_EMPTY_VALUE,value); #else ::SetIndexEmptyValue((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),value); #endif } //+------------------------------------------------------------------+ //| Set the name for the graphical indicator series | //+------------------------------------------------------------------+ void CBuffer::SetLabel(const string label) { this.SetProperty(BUFFER_PROP_LABEL,label); if(this.TypeBuffer()!=BUFFER_TYPE_CALCULATE) #ifdef __MQL5__ ::PlotIndexSetString((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),PLOT_LABEL,label); #else ::SetIndexLabel((int)this.GetProperty(BUFFER_PROP_INDEX_PLOT),label); #endif } //+------------------------------------------------------------------
//+------------------------------------------------------------------+ //| Set the color index to the specified timeseries index | //| of the color buffer array | //+------------------------------------------------------------------+ void CBuffer::SetBufferColorIndex(const uint series_index,const uchar color_index) { #ifdef __MQL4__ return; #endif if(this.GetDataTotal(0)==0 || color_index>this.ColorsTotal()-1 || this.Status()==BUFFER_STATUS_FILLING || this.Status()==BUFFER_STATUS_NONE) return; int data_total=this.GetDataTotal(0); int data_index=((int)series_index<data_total ? (int)series_index : data_total-1); if(::ArraySize(this.ColorBufferArray)==0) ::Print(DFUN,CMessage::Text(MSG_LIB_SYS_ERROR),": ",CMessage::Text(MSG_LIB_TEXT_BUFFER_TEXT_INVALID_PROPERTY_BUFF)); if(data_index<0) return; this.ColorBufferArray[data_index]=color_index; } //+------------------------------------------------------------------+
在计算缓冲区中,在文件 \MQL5\Include\DoEasy\Objects\Indicators\BufferCalculate.mqh 中的类 CBufferCalculate 里,我们有三种方法,可将数据从指标句柄复制到计算缓冲区对象的数据数组当中。 方法返回已复制的数据量。 由于对于 MQL4 而言,不需要从指标句柄复制数据,因此我们将简单地利用相应的标准 mql4-函数依据指定品种、时间帧和柱线编号来获取它们,在这些方法中,我们必须返回某个哑元数值,表示复制成功。
在方法中,传递复制所需的柱线编号,并返回代表已复制数据等于该数值的标志。
对于 MQL4 则简单地返回它:
//+------------------------------------------------------------------+ //| Copy data of the specified indicator to the buffer object array | //+------------------------------------------------------------------+ int CBufferCalculate::FillAsSeries(const int indicator_handle,const int buffer_num,const int start_pos,const int count) { return(#ifdef __MQL5__ ::CopyBuffer(indicator_handle,buffer_num,-start_pos,count,this.DataBuffer[0].Array) #else count #endif ); } //+------------------------------------------------------------------+ int CBufferCalculate::FillAsSeries(const int indicator_handle,const int buffer_num,const datetime start_time,const int count) { return(#ifdef __MQL5__ ::CopyBuffer(indicator_handle,buffer_num,start_time,count,this.DataBuffer[0].Array) #else count #endif ); } //+------------------------------------------------------------------+ int CBufferCalculate::FillAsSeries(const int indicator_handle,const int buffer_num,const datetime start_time,const datetime stop_time) { return ( #ifdef __MQL5__ ::CopyBuffer(indicator_handle,buffer_num,start_time,stop_time,this.DataBuffer[0].Array) #else int(::fabs(start_time-stop_time)/::PeriodSeconds(this.Timeframe())+1) #endif ); } //+------------------------------------------------------------------+
对于最后一种方法,我们不指定复制的数据量,而是指定所需数据的开始时间和结束时间,
对于 MQL4,计算所需数据的开始时间和结束时间之间的柱线数量,并返回计算出的数值。
在指标缓冲区集合类中为标准指标创建所有缓冲区对象
文件位于 \MQL5\Include\DoEasy\Collections\BuffersCollection.mqh。
该类也为 MQL4 改进了兼容性。
在 MQL5 中,在创建标准指标对象的方法当中,首先创建所需指标的句柄,并在成功创建的情况下构造对象本身 。 在 MQL4 中,无需创建任何句柄,因此,在所有这些方法中添加所创建指标的哑元句柄:
//+------------------------------------------------------------------+ //| Create multi-symbol multi-period AC | //+------------------------------------------------------------------+ int CBuffersCollection::CreateAC(const string symbol,const ENUM_TIMEFRAMES timeframe,const int id=WRONG_VALUE) { //--- Create indicator handle and set default ID int handle= #ifdef __MQL5__ ::iAC(symbol,timeframe) #else 0 #endif ; int identifier=(id==WRONG_VALUE ? IND_AC : id); color array_colors[3]={clrGreen,clrRed,clrGreen}; CBuffer *buff=NULL; if(handle!=INVALID_HANDLE) { //--- Create histogram buffer from the zero line this.CreateHistogram();
期间,我们加零作为句柄值。 进而,我们可能要模拟指标句柄的创建,从而避免以相同输入创建两个相同标准指标对象的情况。 然而,实践将证明凡事是否要遵照事实进行。
创建模拟句柄的代码已添加到创建标准指标对象的所有方法当中。
我们不会在此详述它们。 代之,以 AD 指标创建方法为例,研究创建 MQL4 单缓冲区单色标准指标的必要修改, :
//+------------------------------------------------------------------+ //| Create multi-symbol multi-period AD | //+------------------------------------------------------------------+ int CBuffersCollection::CreateAD(const string symbol,const ENUM_TIMEFRAMES timeframe,const ENUM_APPLIED_VOLUME applied_volume,const int id=WRONG_VALUE) { //--- Create indicator handle and set default ID int handle= #ifdef __MQL5__ ::iAD(symbol,timeframe,applied_volume) #else 0 #endif ; int identifier=(id==WRONG_VALUE ? IND_AD : id); color array_colors[1]={clrLightSeaGreen}; CBuffer *buff=NULL; if(handle!=INVALID_HANDLE) { //--- Create line buffer this.CreateLine(); //--- Get the last created buffer object (drawn) and set to it all necessary parameters buff=this.GetLastCreateBuffer(); if(buff==NULL) return INVALID_HANDLE; buff.SetSymbol(symbol); buff.SetTimeframe(timeframe); buff.SetID(identifier); buff.SetIndicatorHandle(handle); buff.SetIndicatorType(IND_AD); buff.SetLineMode(INDICATOR_LINE_MODE_MAIN); buff.SetShowData(true); buff.SetLabel("A/D("+symbol+","+TimeframeDescription(timeframe)+")"); buff.SetIndicatorName("Accumulation/Distribution"); buff.SetIndicatorShortName("A/D("+symbol+","+TimeframeDescription(timeframe)+")"); #ifdef __MQL5__ buff.SetColors(array_colors); #else buff.SetColor(array_colors[0]); #endif //--- MQL5 #ifdef __MQL5__ //--- Create calculated buffer, in which standard indicator data will be stored this.CreateCalculate(); //--- Get the last created buffer object (calculated) and set to it all necessary parameters buff=this.GetLastCreateBuffer(); if(buff==NULL) return INVALID_HANDLE; buff.SetSymbol(symbol); buff.SetTimeframe(timeframe); buff.SetID(identifier); buff.SetIndicatorHandle(handle); buff.SetIndicatorType(IND_AD); buff.SetLineMode(INDICATOR_LINE_MODE_MAIN); buff.SetEmptyValue(EMPTY_VALUE); buff.SetLabel("A/D("+symbol+","+TimeframeDescription(timeframe)+")"); buff.SetIndicatorName("Accumulation/Distribution"); buff.SetIndicatorShortName("A/D("+symbol+","+TimeframeDescription(timeframe)+")"); #endif } return handle; } //+------------------------------------------------------------------+
在此,对于 MQL5 的设置,通过所传递对象的方法为利用颜色数组为颜色缓冲区设置一组颜色,而对于 MQL4 的设置,仅有一种颜色 — 颜色数组中的第一个颜色。 我们仅针对 MQL5 需要计算缓冲区:它存储按指定品种和图表周期创建的 AD 指标的数据。 对于 MQL4,不需要这样的缓冲区,因为我们将调用指标 iAD() 函数直接获取所有数据。
为指定标准指标准备数据,并在当前品种图表上设置数值的方法,对于 MQL5,它从计算缓冲区里读取数据。 对于 MQL4,它极其简单地从调用标准指标函数中获取数据:
//+------------------------------------------------------------------+ //| Prepare data of the specified standard indicator | //| for setting values on the current symbol chart | //+------------------------------------------------------------------+ int CBuffersCollection::PreparingSetDataStdInd(CBuffer *buffer_data0,CBuffer *buffer_data1,CBuffer *buffer_data2,CBuffer *buffer_data3,CBuffer *buffer_data4, CBuffer *buffer_calc0,CBuffer *buffer_calc1,CBuffer *buffer_calc2,CBuffer *buffer_calc3,CBuffer *buffer_calc4, const ENUM_INDICATOR ind_type, const int series_index, const datetime series_time, int &index_period, int &num_bars, double &value00, double &value01, double &value10, double &value11, double &value20, double &value21, double &value30, double &value31, double &value40, double &value41) { //--- Find bar index on a period which corresponds to the time of current bar start index_period=::iBarShift(buffer_data0.Symbol(),buffer_data0.Timeframe(),series_time,true); if(index_period==WRONG_VALUE || #ifdef __MQL5__ index_period>buffer_calc0.GetDataTotal()-1 #else index_period>buffer_data0.GetDataTotal()-1 #endif ) return WRONG_VALUE; //--- For MQL5 #ifdef __MQL5__ //--- Get the value by this index from indicator buffer if(buffer_calc0!=NULL) value00=buffer_calc0.GetDataBufferValue(0,index_period); if(buffer_calc1!=NULL) value10=buffer_calc1.GetDataBufferValue(0,index_period); if(buffer_calc2!=NULL) value20=buffer_calc2.GetDataBufferValue(0,index_period); if(buffer_calc3!=NULL) value30=buffer_calc3.GetDataBufferValue(0,index_period); if(buffer_calc4!=NULL) value40=buffer_calc4.GetDataBufferValue(0,index_period); //--- for MQL4 #else switch((int)ind_type) { //--- Single-buffer standard indicators case IND_AC : value00=::iAC(buffer_data0.Symbol(),buffer_data0.Timeframe(),index_period); break; case IND_AD : value00=::iAD(buffer_data0.Symbol(),buffer_data0.Timeframe(),index_period); break; case IND_AMA : break; case IND_AO : value00=::iAO(buffer_data0.Symbol(),buffer_data0.Timeframe(),index_period); break; case IND_ATR : break; case IND_BEARS : break; case IND_BULLS : break; case IND_BWMFI : value00=::iBWMFI(buffer_data0.Symbol(),buffer_data0.Timeframe(),index_period); break; case IND_CCI : break; case IND_CHAIKIN : break; case IND_DEMA : break; case IND_DEMARKER : break; case IND_FORCE : break; case IND_FRAMA : break; case IND_MA : break; case IND_MFI : break; case IND_MOMENTUM : break; case IND_OBV : break; case IND_OSMA : break; case IND_RSI : break; case IND_SAR : break; case IND_STDDEV : break; case IND_TEMA : break; case IND_TRIX : break; case IND_VIDYA : break; case IND_VOLUMES : value00=(double)::iVolume(buffer_data0.Symbol(),buffer_data0.Timeframe(),index_period); break; case IND_WPR : break; //--- Multi-buffer standard indicators case IND_ENVELOPES : break; case IND_FRACTALS : break; case IND_ADX : break; case IND_ADXW : break; case IND_BANDS : break; case IND_MACD : break; case IND_RVI : break; case IND_STOCHASTIC : break; case IND_ALLIGATOR : break; case IND_ICHIMOKU : break; case IND_GATOR : break; default: break; } #endif int series_index_start=series_index; //--- For the current chart we don’t need to calculate the number of bars processed - only one bar is available if(buffer_data0.Symbol()==::Symbol() && buffer_data0.Timeframe()==::Period()) { series_index_start=series_index; num_bars=1; } else { //--- Get the bar time which the bar with index_period index falls into on a period and symbol of calculated buffer datetime time_period=::iTime(buffer_data0.Symbol(),buffer_data0.Timeframe(),index_period); if(time_period==0) return false; //--- Get the current chart bar which corresponds to the time series_index_start=::iBarShift(::Symbol(),::Period(),time_period,true); if(series_index_start==WRONG_VALUE) return WRONG_VALUE; //--- Calculate the number of bars on the current chart which are to be filled in with calculated buffer data num_bars=::PeriodSeconds(buffer_data0.Timeframe())/::PeriodSeconds(PERIOD_CURRENT); if(num_bars==0) num_bars=1; } //--- Take values for color calculation if(buffer_data0!=NULL) value01=(series_index_start+num_bars>buffer_data0.GetDataTotal()-1 ? value00 : buffer_data0.GetDataBufferValue(0,series_index_start+num_bars)); if(buffer_data1!=NULL) value11=(series_index_start+num_bars>buffer_data1.GetDataTotal()-1 ? value10 : buffer_data1.GetDataBufferValue(0,series_index_start+num_bars)); if(buffer_data2!=NULL) value21=(series_index_start+num_bars>buffer_data2.GetDataTotal()-1 ? value20 : buffer_data2.GetDataBufferValue(0,series_index_start+num_bars)); if(buffer_data3!=NULL) value31=(series_index_start+num_bars>buffer_data3.GetDataTotal()-1 ? value30 : buffer_data3.GetDataBufferValue(0,series_index_start+num_bars)); if(buffer_data4!=NULL) value41=(series_index_start+num_bars>buffer_data4.GetDataTotal()-1 ? value40 : buffer_data4.GetDataBufferValue(0,series_index_start+num_bars)); return series_index_start; } //+------------------------------------------------------------------+
当下, 针对 MQL4,已实现了仅从单缓冲区标准指标获取数据,且无需除品种和图表周期之外的输入。 其余标准指标将在后续文章中实现。
在按照缓冲区对象品种/时间段,根据时间序列索引为当前图表上指定标准指标缓冲区设置值的方法里,针对 MQL4 进行了略微修改,剔除了检查:
//+------------------------------------------------------------------+ //| Set values for the current chart to buffers of the specified | //| standard indicator by the timeseries index in accordance | //| with buffer object symbol/period | //+------------------------------------------------------------------+ bool CBuffersCollection::SetDataBufferStdInd(const ENUM_INDICATOR ind_type,const int id,const int series_index,const datetime series_time,const char color_index=WRONG_VALUE) { //--- Get the list of buffer objects by type and ID CArrayObj *list=this.GetListBufferByTypeID(ind_type,id); if(list==NULL || list.Total()==0) { ::Print(DFUN,CMessage::Text(MSG_LIB_TEXT_BUFFER_TEXT_NO_BUFFER_OBJ)); return false; } //--- Get the list of drawn buffers with ID CArrayObj *list_data=CSelect::ByBufferProperty(list,BUFFER_PROP_TYPE,BUFFER_TYPE_DATA,EQUAL); list_data=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_TYPE,ind_type,EQUAL); //--- Get the list of calculated buffers with ID CArrayObj *list_calc=CSelect::ByBufferProperty(list,BUFFER_PROP_TYPE,BUFFER_TYPE_CALCULATE,EQUAL); list_calc=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_TYPE,ind_type,EQUAL); //--- Leave if any of the lists is empty if(list_data.Total()==0 #ifdef __MQL5__ || list_calc.Total()==0 #endif ) return false; //--- Declare necessary objects and variables CBuffer *buffer_data0=NULL,*buffer_data1=NULL,*buffer_data2=NULL,*buffer_data3=NULL,*buffer_data4=NULL,*buffer_tmp0=NULL,*buffer_tmp1=NULL; CBuffer *buffer_calc0=NULL,*buffer_calc1=NULL,*buffer_calc2=NULL,*buffer_calc3=NULL,*buffer_calc4=NULL; double value00=EMPTY_VALUE, value01=EMPTY_VALUE; double value10=EMPTY_VALUE, value11=EMPTY_VALUE; double value20=EMPTY_VALUE, value21=EMPTY_VALUE; double value30=EMPTY_VALUE, value31=EMPTY_VALUE; double value40=EMPTY_VALUE, value41=EMPTY_VALUE; double value_tmp0=EMPTY_VALUE,value_tmp1=EMPTY_VALUE; long vol0=0,vol1=0; int series_index_start=series_index,index_period=0, index=0,num_bars=1; uchar clr=0; //--- Depending on standard indicator type switch((int)ind_type) { //--- Single-buffer standard indicators case IND_AC : case IND_AD : case IND_AMA : case IND_AO : case IND_ATR : case IND_BEARS : case IND_BULLS : case IND_BWMFI : case IND_CCI : case IND_CHAIKIN : case IND_DEMA : case IND_DEMARKER : case IND_FORCE : case IND_FRAMA : case IND_MA : case IND_MFI : case IND_MOMENTUM : case IND_OBV : case IND_OSMA : case IND_RSI : case IND_SAR : case IND_STDDEV : case IND_TEMA : case IND_TRIX : case IND_VIDYA : case IND_VOLUMES : case IND_WPR : list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,0,EQUAL); buffer_data0=list.At(0); #ifdef __MQL5__ list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,0,EQUAL); buffer_calc0=list.At(0); #endif if(buffer_data0==NULL #ifdef __MQL5__ || buffer_calc0==NULL || buffer_calc0.GetDataTotal(0)==0 #endif ) return false; series_index_start=PreparingSetDataStdInd(buffer_data0,buffer_data1,buffer_data2,buffer_data3,buffer_data4, buffer_calc0,buffer_calc1,buffer_calc2,buffer_calc3,buffer_calc4, ind_type,series_index,series_time,index_period,num_bars, value00,value01,value10,value11,value20,value21,value30,value31,value40,value41); if(series_index_start==WRONG_VALUE) return false; //--- In a loop, by the number of bars in num_bars fill in the drawn buffer with a value from the calculated buffer taken by index_period index //--- and set the drawn buffer color depending on a proportion of value00 and value01 values for(int i=0;i<num_bars;i++) { index=series_index_start-i; buffer_data0.SetBufferValue(0,index,value00); if(ind_type!=IND_BWMFI) clr=(color_index==WRONG_VALUE ? uchar(value00>value01 ? 0 : value00<value01 ? 1 : 2) : color_index); else { vol0=::iVolume(buffer_calc0.Symbol(),buffer_calc0.Timeframe(),index_period); vol1=::iVolume(buffer_calc0.Symbol(),buffer_calc0.Timeframe(),index_period+1); clr= ( value00>value01 && vol0>vol1 ? 0 : value00<value01 && vol0<vol1 ? 1 : value00>value01 && vol0<vol1 ? 2 : value00<value01 && vol0>vol1 ? 3 : 4 ); } buffer_data0.SetBufferColorIndex(index,clr); } return true; //--- Multi-buffer standard indicators case IND_ENVELOPES : case IND_FRACTALS : list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,0,EQUAL); buffer_data0=list.At(0); list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,1,EQUAL); buffer_data1=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,0,EQUAL); buffer_calc0=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,1,EQUAL); buffer_calc1=list.At(0); if(buffer_calc0==NULL || buffer_data0==NULL || buffer_calc0.GetDataTotal(0)==0) return false; if(buffer_calc1==NULL || buffer_data1==NULL || buffer_calc1.GetDataTotal(0)==0) return false; series_index_start=PreparingSetDataStdInd(buffer_data0,buffer_data1,buffer_data2,buffer_data3,buffer_data4, buffer_calc0,buffer_calc1,buffer_calc2,buffer_calc3,buffer_calc4, ind_type,series_index,series_time,index_period,num_bars, value00,value01,value10,value11,value20,value21,value30,value31,value40,value41); if(series_index_start==WRONG_VALUE) return false; //--- In a loop, by the number of bars in num_bars fill in the drawn buffer with a value from the calculated buffer taken by index_period index //--- and set the drawn buffer color depending on a proportion of value00 and value01 values for(int i=0;i<num_bars;i++) { index=series_index_start-i; buffer_data0.SetBufferValue(0,index,value00); buffer_data1.SetBufferValue(1,index,value10); buffer_data0.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value00>value01 ? 0 : value00<value01 ? 1 : 2) : color_index); buffer_data1.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value10>value11 ? 0 : value10<value11 ? 1 : 2) : color_index); } return true; case IND_ADX : case IND_ADXW : case IND_BANDS : case IND_MACD : case IND_RVI : case IND_STOCHASTIC : case IND_ALLIGATOR : list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,0,EQUAL); buffer_data0=list.At(0); list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,1,EQUAL); buffer_data1=list.At(0); list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,2,EQUAL); buffer_data2=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,0,EQUAL); buffer_calc0=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,1,EQUAL); buffer_calc1=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,2,EQUAL); buffer_calc2=list.At(0); if(buffer_calc0==NULL || buffer_data0==NULL || buffer_calc0.GetDataTotal(0)==0) return false; if(buffer_calc1==NULL || buffer_data1==NULL || buffer_calc1.GetDataTotal(0)==0) return false; if(buffer_calc2==NULL || buffer_data2==NULL || buffer_calc2.GetDataTotal(0)==0) return false; series_index_start=PreparingSetDataStdInd(buffer_data0,buffer_data1,buffer_data2,buffer_data3,buffer_data4, buffer_calc0,buffer_calc1,buffer_calc2,buffer_calc3,buffer_calc4, ind_type,series_index,series_time,index_period,num_bars, value00,value01,value10,value11,value20,value21,value30,value31,value40,value41); if(series_index_start==WRONG_VALUE) return false; //--- In a loop, by the number of bars in num_bars fill in the drawn buffer with a value from the calculated buffer taken by index_period index //--- and set the drawn buffer color depending on a proportion of value00 and value01 values for(int i=0;i<num_bars;i++) { index=series_index_start-i; buffer_data0.SetBufferValue(0,index,value00); buffer_data1.SetBufferValue(0,index,value10); buffer_data2.SetBufferValue(0,index,value20); buffer_data0.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value00>value01 ? 0 : value00<value01 ? 1 : 2) : color_index); buffer_data1.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value10>value11 ? 0 : value10<value11 ? 1 : 2) : color_index); buffer_data2.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value20>value21 ? 0 : value20<value21 ? 1 : 2) : color_index); } return true; case IND_ICHIMOKU : list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_TENKAN_SEN,EQUAL); buffer_data0=list.At(0); list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_KIJUN_SEN,EQUAL); buffer_data1=list.At(0); list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_SENKOU_SPANA,EQUAL); buffer_data2=list.At(0); list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_SENKOU_SPANB,EQUAL); buffer_data3=list.At(0); list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_CHIKOU_SPAN,EQUAL); buffer_data4=list.At(0); //--- Get the list of buffer objects which have ID of auxiliary line, and from it - buffer object with line number as 0 list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_ADDITIONAL,EQUAL); list=CSelect::ByBufferProperty(list,BUFFER_PROP_IND_LINE_ADDITIONAL_NUM,0,EQUAL); buffer_tmp0=list.At(0); //--- Get the list of buffer objects which have ID of auxiliary line, and from it - buffer object with line number as 1 list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_ADDITIONAL,EQUAL); list=CSelect::ByBufferProperty(list,BUFFER_PROP_IND_LINE_ADDITIONAL_NUM,1,EQUAL); buffer_tmp1=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_TENKAN_SEN,EQUAL); buffer_calc0=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_KIJUN_SEN,EQUAL); buffer_calc1=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_SENKOU_SPANA,EQUAL); buffer_calc2=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_SENKOU_SPANB,EQUAL); buffer_calc3=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,INDICATOR_LINE_MODE_CHIKOU_SPAN,EQUAL); buffer_calc4=list.At(0); if(buffer_calc0==NULL || buffer_data0==NULL || buffer_calc0.GetDataTotal(0)==0) return false; if(buffer_calc1==NULL || buffer_data1==NULL || buffer_calc1.GetDataTotal(0)==0) return false; if(buffer_calc2==NULL || buffer_data2==NULL || buffer_calc2.GetDataTotal(0)==0) return false; if(buffer_calc3==NULL || buffer_data3==NULL || buffer_calc3.GetDataTotal(0)==0) return false; if(buffer_calc4==NULL || buffer_data4==NULL || buffer_calc4.GetDataTotal(0)==0) return false; series_index_start=PreparingSetDataStdInd(buffer_data0,buffer_data1,buffer_data2,buffer_data3,buffer_data4, buffer_calc0,buffer_calc1,buffer_calc2,buffer_calc3,buffer_calc4, ind_type,series_index,series_time,index_period,num_bars, value00,value01,value10,value11,value20,value21,value30,value31,value40,value41); if(series_index_start==WRONG_VALUE) return false; //--- In a loop, by the number of bars in num_bars fill in the drawn buffer with a value from the calculated buffer taken by index_period index //--- and set the drawn buffer color depending on a proportion of value00 and value01 values for(int i=0;i<num_bars;i++) { index=series_index_start-i; buffer_data0.SetBufferValue(0,index,value00); buffer_data1.SetBufferValue(0,index,value10); buffer_data2.SetBufferValue(0,index,value20); buffer_data3.SetBufferValue(0,index,value30); buffer_data4.SetBufferValue(0,index,value40); buffer_data0.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value00>value01 ? 0 : value00<value01 ? 1 : 2) : color_index); buffer_data1.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value10>value11 ? 0 : value10<value11 ? 1 : 2) : color_index); buffer_data2.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value20>value21 ? 0 : value20<value21 ? 1 : 2) : color_index); buffer_data3.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value30>value31 ? 0 : value30<value31 ? 1 : 2) : color_index); buffer_data4.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value40>value41 ? 0 : value40<value41 ? 1 : 2) : color_index); //--- Set values for indicator auxiliary lines depending on mutual position of Senkou Span A and Senkou Span B lines value_tmp0=buffer_data2.GetDataBufferValue(0,index); value_tmp1=buffer_data3.GetDataBufferValue(0,index); if(value_tmp0<value_tmp1) { buffer_tmp0.SetBufferValue(0,index,buffer_tmp0.EmptyValue()); buffer_tmp0.SetBufferValue(1,index,buffer_tmp0.EmptyValue()); buffer_tmp1.SetBufferValue(0,index,value_tmp0); buffer_tmp1.SetBufferValue(1,index,value_tmp1); } else { buffer_tmp0.SetBufferValue(0,index,value_tmp0); buffer_tmp0.SetBufferValue(1,index,value_tmp1); buffer_tmp1.SetBufferValue(0,index,buffer_tmp1.EmptyValue()); buffer_tmp1.SetBufferValue(1,index,buffer_tmp1.EmptyValue()); } } return true; case IND_GATOR : list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,0,EQUAL); buffer_data0=list.At(0); list=CSelect::ByBufferProperty(list_data,BUFFER_PROP_IND_LINE_MODE,1,EQUAL); buffer_data1=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,0,EQUAL); buffer_calc0=list.At(0); list=CSelect::ByBufferProperty(list_calc,BUFFER_PROP_IND_LINE_MODE,1,EQUAL); buffer_calc1=list.At(0); if(buffer_calc0==NULL || buffer_data0==NULL || buffer_calc0.GetDataTotal(0)==0) return false; if(buffer_calc1==NULL || buffer_data1==NULL || buffer_calc1.GetDataTotal(0)==0) return false; series_index_start=PreparingSetDataStdInd(buffer_data0,buffer_data1,buffer_data2,buffer_data3,buffer_data4, buffer_calc0,buffer_calc1,buffer_calc2,buffer_calc3,buffer_calc4, ind_type,series_index,series_time,index_period,num_bars, value00,value01,value10,value11,value20,value21,value30,value31,value40,value41); if(series_index_start==WRONG_VALUE) return false; //--- In a loop, by the number of bars in num_bars fill in the drawn buffer with a value from the calculated buffer taken by index_period index //--- and set the drawn buffer color depending on a proportion of value00 and value01 values for(int i=0;i<num_bars;i++) { index=series_index_start-i; buffer_data0.SetBufferValue(0,index,value00); buffer_data1.SetBufferValue(1,index,value10); buffer_data0.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value00>value01 ? 0 : value00<value01 ? 1 : 2) : color_index); buffer_data1.SetBufferColorIndex(index,color_index==WRONG_VALUE ? uchar(value10<value11 ? 0 : value10>value11 ? 1 : 2) : color_index); } return true; default: break; } return false; } //+------------------------------------------------------------------+
鉴于我们已决定把函数库创建的缓冲区对象数量的相关检查转换为程序的 #property 数值,因此将这个方法添加到文件 \MQL5\Include\DoEasy\Engine.mqh 中的函数库主对象 CEngine 类当中 。
在该类的公开部分声明方法:
//--- Display short description of all indicator buffers of the buffer collection void BuffersPrintShort(void); //--- Specify the required number of buffers for indicators void CheckIndicatorsBuffers(const int buffers,const int plots #ifdef __MQL4__ =1 #endif ); //--- Return the bar index on the specified timeframe chart by the current chart's bar index
在类主体外部编写其实现:
//+------------------------------------------------------------------+ //| Specify the required number of buffers for indicators | //+------------------------------------------------------------------+ void CEngine::CheckIndicatorsBuffers(const int buffers,const int plots #ifdef __MQL4__ =1 #endif ) { #ifdef __MQL5__ if(this.BuffersPropertyPlotsTotal()!=plots) ::Alert(CMessage::Text(MSG_ENG_ERR_VALUE_PLOTS),this.BuffersPropertyPlotsTotal()); if(this.BuffersPropertyBuffersTotal()!=buffers) ::Alert(CMessage::Text(MSG_ENG_ERR_VALUE_ORDERS),this.BuffersPropertyBuffersTotal()); #else if(buffers!=this.BuffersPropertyPlotsTotal()) ::Alert(CMessage::Text(MSG_ENG_ERR_VALUE_ORDERS),this.BuffersPropertyPlotsTotal()); ::IndicatorBuffers(this.BuffersPropertyBuffersTotal()); #endif } //+------------------------------------------------------------------+
对于 MQL5,只需显示有关警告,提示在指标程序的 #property 中有关创建指标缓冲区(绘制和计算)的数量与指定数值不匹配。
对于 MQL4,如果 #property indicator_buffers 中指定的数值不一致,则显示一个相关通知,并根据由函数库创建的所有缓冲区的总数设置所有指标缓冲区的总数(绘制和计算)。
现在,设置 MQL4 中指标显示的数据容量。 为此,在函数库服务函数 \MQL5\Include\DoEasy\Services\DELib.mqh 文件里改进标准指标设置小数位容量和级别的函数:
//+------------------------------------------------------------------+ //| Set capacity and levels for standard indicator | //+------------------------------------------------------------------+ void SetIndicatorLevels(const string symbol,const ENUM_INDICATOR ind_type) { int digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS); switch(ind_type) { case IND_AD : case IND_CHAIKIN : case IND_OBV : case IND_VOLUMES : digits=0; break; case IND_AO : case IND_BEARS : case IND_BULLS : case IND_FORCE : case IND_STDDEV : case IND_AMA : case IND_DEMA : case IND_FRAMA : case IND_MA : case IND_TEMA : case IND_VIDYA : case IND_BANDS : case IND_ENVELOPES : case IND_MACD : digits+=1; break; case IND_AC : case IND_OSMA : digits+=2; break; case IND_MOMENTUM : digits=2; break; case IND_CCI : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,100); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,-100); digits=2; break; case IND_DEMARKER : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,0.7); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,0.3); digits=3; break; case IND_MFI : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,80); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,20); break; case IND_RSI : IndicatorSetInteger(INDICATOR_LEVELS,3); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,70); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,50); IndicatorSetDouble(INDICATOR_LEVELVALUE,2,30); digits=2; break; case IND_STOCHASTIC : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,80); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,20); digits=2; break; case IND_WPR : IndicatorSetInteger(INDICATOR_LEVELS,2); IndicatorSetDouble(INDICATOR_LEVELVALUE,0,-80); IndicatorSetDouble(INDICATOR_LEVELVALUE,1,-20); digits=2; break; case IND_ATR : break; case IND_SAR : break; case IND_TRIX : break; default: IndicatorSetInteger(INDICATOR_LEVELS,0); break; } #ifdef __MQL5__ IndicatorSetInteger(INDICATOR_DIGITS,digits); #else IndicatorDigits(digits); #endif } //+------------------------------------------------------------------+
此处,对于 MQL4,为了设置指标显示小数位的容量,请用标准 mql4 函数 IndicatorDigits()。
创建单缓冲区多品种、多周期标准指标的库类改进至此完毕。
测试
为了执行测试,取上一篇文章的第二个指标(TestDoEasyPart51_2.mq5),并
将其保存在终端指标文件夹 MetaTrader 4 \MQL4\Indicators\TestDoEasy\Part52\ 之下,命名为 TestDoEasyPart52.mq4。
之前的测试指标已创建了鳄鱼振荡器多品种、多周期标准指标。 而此刻我们想要创建“建仓/派发”指标。
在文件头部针对 MQL4 设置指标所需缓冲区数量:
//+------------------------------------------------------------------+ //| TestDoEasyPart52.mq4 | //| 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> //--- properties #property indicator_separate_window #property indicator_buffers 1 #property indicator_plots 1 //--- classes //--- enums //--- defines //--- structures //--- input variables sinput string InpUsedSymbols = "EURUSD"; // Used symbol (one only) sinput ENUM_TIMEFRAMES InpPeriod = PERIOD_H4; // Used chart period //--- sinput bool InpUseSounds = true; // Use sounds //--- indicator buffers //--- global variables ENUM_SYMBOLS_MODE InpModeUsedSymbols= SYMBOLS_MODE_DEFINES; // Mode of used symbols list ENUM_TIMEFRAMES_MODE InpModeUsedTFs = TIMEFRAMES_MODE_LIST; // Mode of used timeframes list string InpUsedTFs; // List of used timeframes CEngine engine; // CEngine library main object string prefix; // Prefix of graphical object names int min_bars; // The minimum number of bars for the indicator calculation int used_symbols_mode; // Mode of working with symbols string array_used_symbols[]; // The array for passing used symbols to the library string array_used_periods[]; // The array for passing used timeframes to the library //+------------------------------------------------------------------+
在处理程序 OnInit() 中创建标准指标建仓派发的对象,并指定哪里需要 AD 指标类型:
//+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- Write the name of the working timeframe selected in the settings to InpUsedTFs variable InpUsedTFs=TimeframeDescription(InpPeriod); //--- Initialize DoEasy library OnInitDoEasy(); //--- Set indicator global variables prefix=engine.Name()+"_"; //--- Calculate the number of bars of the current period fitting in the maximum used period //--- Use the obtained value if it exceeds 2, otherwise use 2 int num_bars=NumberBarsInTimeframe(InpPeriod); min_bars=(num_bars>2 ? num_bars : 2); //--- Check and remove remaining indicator graphical objects if(IsPresentObectByPrefix(prefix)) ObjectsDeleteAll(0,prefix); //--- Create the button panel //--- Check playing a standard sound using macro substitutions engine.PlaySoundByDescription(SND_OK); //--- Wait for 600 milliseconds engine.Pause(600); engine.PlaySoundByDescription(SND_NEWS); //--- indicator buffers mapping //--- Create all the necessary buffer objects for constructing the selected standard indicator if(!engine.BufferCreateAD(InpUsedSymbols,InpPeriod,VOLUME_TICK,1)) { Print(TextByLanguage("Error. Indicator not created")); return INIT_FAILED; } //--- Check the number of buffers specified in the 'properties' block engine.CheckIndicatorsBuffers(indicator_buffers,indicator_plots); //--- Create the color array and set non-default colors to all buffers within the collection //--- (commented out since default colors are already set in methods of standard indicator creation) //--- (we can always set required colors either for all indicators like here or for each one individually) //color array_colors[]={clrGreen,clrRed,clrGray}; //engine.BuffersSetColors(array_colors); //--- Display short descriptions of created indicator buffers engine.BuffersPrintShort(); //--- Set a short name for the indicator, data capacity and levels string label=engine.BufferGetIndicatorShortNameByTypeID(IND_AD,1); IndicatorSetString(INDICATOR_SHORTNAME,label); SetIndicatorLevels(InpUsedSymbols,IND_AD); //--- Successful return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+
早前,在处理程序 OnInit() 中检查了指定指标缓冲区与创建指标缓冲区数量的对应关系:
//--- Check the number of buffers specified in the 'properties' block if(engine.BuffersPropertyPlotsTotal()!=indicator_plots) Alert(TextByLanguage("Attention! Value of \"indicator_plots\" should be "),engine.BuffersPropertyPlotsTotal()); if(engine.BuffersPropertyBuffersTotal()!=indicator_buffers) Alert(TextByLanguage("Attention! Value of \"indicator_buffers\" should be "),engine.BuffersPropertyBuffersTotal());
现在,它被替换为调用相应函数库方法。
在处理程序 OnCalculate() 中,在主程序循环中写入建仓/派发数据来代替写入鳄鱼振荡器数据即可:
//+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { //+------------------------------------------------------------------+ //| OnCalculate code block for working with the library: | //+------------------------------------------------------------------+ //--- Pass the current symbol data from OnCalculate() to the price structure and set the "as timeseries" flag to the arrays CopyDataAsSeries(rates_total,prev_calculated,time,open,high,low,close,tick_volume,volume,spread); //--- Check for the minimum number of bars for calculation if(rates_total<min_bars || Point()==0) return 0; //--- Handle the Calculate event in the library //--- If the OnCalculate() method of the library returns zero, not all timeseries are ready - leave till the next tick if(engine.OnCalculate(rates_data)==0) return 0; //--- If working in the tester if(MQLInfoInteger(MQL_TESTER)) { engine.OnTimer(rates_data); // Working in the library timer engine.EventsHandling(); // Working with library events } //+------------------------------------------------------------------+ //| OnCalculate code block for working with the indicator: | //+------------------------------------------------------------------+ //--- Check and calculate the number of calculated bars //--- If limit = 0, there are no new bars - calculate the current one //--- If limit = 1, a new bar has appeared - calculate the first and the current ones //--- If limit > 1 means the first launch or changes in history - the full recalculation of all data int limit=rates_total-prev_calculated; //--- Recalculate the entire history if(limit>1) { limit=rates_total-1; engine.BuffersInitPlots(); engine.BuffersInitCalculates(); } //--- Prepare data //--- Fill in calculated buffers of all created standard indicators with data int bars_total=engine.SeriesGetBarsTotal(InpUsedSymbols,InpPeriod); int total_copy=(limit<min_bars ? min_bars : fmin(limit,bars_total)); if(!engine.BufferPreparingDataAllBuffersStdInd()) return 0; //--- Calculate the indicator //--- Main calculation loop of the indicator for(int i=limit; i>WRONG_VALUE && !IsStopped(); i--) { engine.GetBuffersCollection().SetDataBufferStdInd(IND_AD,1,i,time[i]); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
对于指标的 mql5 版本,与 mql4 版本对比,我们需要更改 #property 中指定的绘制和计算缓冲区的数量:
//+------------------------------------------------------------------+ //| TestDoEasyPart52.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> //--- properties #property indicator_separate_window #property indicator_buffers 3 #property indicator_plots 1
编译指标,并在 MetaTrader 4 终端的 EURUSD H1 图表上启动它,并在指标输入中输入 EURUSD 品种和 H4 周期。 因此,在 MetaTrader 4 终端的 EURUSD 小时图上显示依据 EURUSD H4 计算的 AD 指标:
下一步是什么?
在下一篇文章中,继续操控 MetaTrader 5 中的指标,并可根据函数库的跨平台特性,毫不费力地操控。
函数库当前版本的所有文件,以及测试指标文件都随附于下。 您可以下载它们,并测试所有内容。
请在文章的评论中留下您的评论、问题和建议。
请注意,所有与先前平台的兼容性工作仅是为了支持该函数库的多平台特性,该特性最初是为 MQL5 创建的,且拥有更多优势和功能。.
没有计划为 MQL4 中运用该函数撰写单独的文章。 且不会写任何东西。 鉴于每位读者都可独立开发,在“量身定做”的基础上任何事情都可能发生,而在 MetaTrader 4 上运用该函数库时,似有不足。 我将继续令该函数库与这两个平台兼容,但仅出于以下原因:函数库用户能够轻松地移值其所有基于函数库的程序,以便能在 MetaTrader 4 中运行。
该系列中的先前文章:
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/8399
![神经网络变得轻松(第四部分):循环网络](https://c.mql5.com/2/48/Neural_networks_made_easy_004.png)