股票量化软件:赫兹量化中多周期、多品种单缓冲区标准指标的跨平台性质
改进库类
如同往常,首先,添加必要的文本消息。 之前,在最终的指标程序的整个代码里,我们会立即检查由函数库创建的指标缓冲区与有关指标缓冲区所需数量的相关性:
#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" //+------------------------------------------------------------------+
我们开始实现跨平台特性。
//+------------------------------------------------------------------+ //| 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 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 | //+--------------+ //| Margin calculation mode | //+------------------------------------------------------------------+ //--- the code has been removed for the sake of space //+------------------------------------------------------------------+ //| Pf 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, //+------------------------------------------------------------------+ //| Trade request structure | //+------------------------------------------------------------------+ //--- the code has been further removed for the sake of space #endif
早前,该方法整体编写在类主体中,其可立即返回已计算的数据量:
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); }
... 并将其实现移到类主体之外:
//+------------------------------------------------------------------+ //| 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); } //+------------------------------------------------------------------