内容
- 删除未使用的属性
- 实现 MQL4 的平仓事件
- 测试
- 下一步是什么?
删除未使用的属性
在进行定义事件的工作时,我注意到在 MQL5 中,所有时间参数都以毫秒为单位进行设置。 MQL4 没有这样的订单和持仓属性,但没有什么能阻止我们为 MQL4 设置毫秒为单位的时间。 换句话说,任何以秒为单位的时间都可简单地用毫秒值覆盖,尽管不会在任何地方用到。 接收和显示以秒/毫秒为单位设置的时间完全相同,时间格式的“尾部”三位数表示所显示的毫秒数。
所以,我决定从订单属性中删除所有设置为秒的属性,并用毫秒为单位替换订单中的相同属性。
既然我们决定删除一些东西,那么添加一些东西也理所应当。 因此,我们为每个订单添加新的“自定义注释”属性。
它可以随时设置任何订单或持仓(开仓和平仓/删除)。 我们为什么需要这个? 例如,对于符合某些条件的订单的文本标签,或为了视觉显示(函数库稍后将提供其自己的图形外壳),这也许是必要的,以便可以使用各种图形构造轻易地显示用文本标签标记的订单。
打开 Defines.mqh 文件,按下 Ctrl+F 搜索所有包含以秒为单位的订单属性,以及以 "_MSC" 结尾的类似属性 (这些属性会以毫秒设置)。 删除“毫秒”订单属性,保留“秒”属性,并将整数型属性数量由 24 替换为 21:
//+------------------------------------------------------------------+ //| Order, deal, position integer properties | //+------------------------------------------------------------------+ enum ENUM_ORDER_PROP_INTEGER { ORDER_PROP_TICKET = 0, // Order ticket ORDER_PROP_MAGIC, // Order magic number ORDER_PROP_TIME_OPEN, // Open time (MQL5 Deal time) ORDER_PROP_TIME_CLOSE, // Close time (MQL5 Execution or removal time - ORDER_TIME_DONE) ORDER_PROP_TIME_OPEN_MSC, // Open time in milliseconds (MQL5 Deal time in msc) ORDER_PROP_TIME_CLOSE_MSC, // Close time in milliseconds (MQL5 Execution or removal time - ORDER_TIME_DONE_MSC) ORDER_PROP_TIME_EXP, // Order expiration date (for pending orders) ORDER_PROP_STATUS, // Order status (from the ENUM_ORDER_STATUS enumeration) ORDER_PROP_TYPE, // Order/deal type ORDER_PROP_REASON, // Deal/order/position reason or source ORDER_PROP_STATE, // Order status (from the ENUM_ORDER_STATE enumeration) ORDER_PROP_POSITION_ID, // Position ID ORDER_PROP_POSITION_BY_ID, // Opposite position ID ORDER_PROP_DEAL_ORDER_TICKET, // Ticket of the order that triggered a deal ORDER_PROP_DEAL_ENTRY, // Deal direction – IN, OUT or IN/OUT ORDER_PROP_TIME_UPDATE, // Position change time in seconds ORDER_PROP_TIME_UPDATE_MSC, // Position change time in milliseconds ORDER_PROP_TICKET_FROM, // Parent order ticket ORDER_PROP_TICKET_TO, // Derived order ticket ORDER_PROP_PROFIT_PT, // Profit in points ORDER_PROP_CLOSE_BY_SL, // Flag of closing by StopLoss ORDER_PROP_CLOSE_BY_TP, // Flag of closing by TakeProfit ORDER_PROP_GROUP_ID, // Order/position group ID ORDER_PROP_DIRECTION, // Direction type (Buy, Sell) }; #define ORDER_PROP_INTEGER_TOTAL (24) // Total number of integer properties #define ORDER_PROP_INTEGER_SKIP (0) // Number of order properties not used in sorting //+------------------------------------------------------------------+
修改后,订单的整数属性列表将如下所示:
//+------------------------------------------------------------------+ //| Order, deal, position integer properties | //+------------------------------------------------------------------+ enum ENUM_ORDER_PROP_INTEGER { ORDER_PROP_TICKET = 0, // Order ticket ORDER_PROP_MAGIC, // Order magic number ORDER_PROP_TIME_OPEN, // Open time in milliseconds (MQL5 Deal time) ORDER_PROP_TIME_CLOSE, // Close time in milliseconds (MQL5 Execution or removal time - ORDER_TIME_DONE) ORDER_PROP_TIME_EXP, // Order expiration date (for pending orders) ORDER_PROP_STATUS, // Order status (from the ENUM_ORDER_STATUS enumeration) ORDER_PROP_TYPE, // Order/deal type ORDER_PROP_REASON, // Deal/order/position reason or source ORDER_PROP_STATE, // Order status (from the ENUM_ORDER_STATE enumeration) ORDER_PROP_POSITION_ID, // Position ID ORDER_PROP_POSITION_BY_ID, // Opposite position ID ORDER_PROP_DEAL_ORDER_TICKET, // Ticket of the order that triggered a deal ORDER_PROP_DEAL_ENTRY, // Deal direction – IN, OUT or IN/OUT ORDER_PROP_TIME_UPDATE, // Position change time in milliseconds ORDER_PROP_TICKET_FROM, // Parent order ticket ORDER_PROP_TICKET_TO, // Derived order ticket ORDER_PROP_PROFIT_PT, // Profit in points ORDER_PROP_CLOSE_BY_SL, // Flag of closing by StopLoss ORDER_PROP_CLOSE_BY_TP, // Flag of closing by TakeProfit ORDER_PROP_GROUP_ID, // Order/position group ID ORDER_PROP_DIRECTION, // Direction type (Buy, Sell) }; #define ORDER_PROP_INTEGER_TOTAL (21) // Total number of integer properties #define ORDER_PROP_INTEGER_SKIP (0) // Number of order properties not used in sorting //+------------------------------------------------------------------+
我们按时间搜索可选项的枚举,并删除含有毫秒为单位的选项常量:
//+------------------------------------------------------------------+ //| Possible selection options by time | //+------------------------------------------------------------------+ enum ENUM_SELECT_BY_TIME { SELECT_BY_TIME_OPEN, // By open time SELECT_BY_TIME_CLOSE, // By close time SELECT_BY_TIME_OPEN_MSC, // By open time in milliseconds SELECT_BY_TIME_CLOSE_MSC, // By close time in milliseconds }; //+------------------------------------------------------------------+
枚举将只包含两个常量:
//+------------------------------------------------------------------+ //| Possible selection options by time | //+------------------------------------------------------------------+ enum ENUM_SELECT_BY_TIME { SELECT_BY_TIME_OPEN, // By open time (in milliseconds) SELECT_BY_TIME_CLOSE, // By close time (in milliseconds) }; //+------------------------------------------------------------------+
现在,按时间设置选项时,MQL5 的时间选项以毫秒为单位,MQL4 的时间选项以秒为单位。
将新的“自定义注释”属性添加到订单字符串型属性,并将字符串型属性的总数增加到 4:
//+------------------------------------------------------------------+ //| Order, deal, position string properties | //+------------------------------------------------------------------+ enum ENUM_ORDER_PROP_STRING { ORDER_PROP_SYMBOL = (ORDER_PROP_INTEGER_TOTAL+ORDER_PROP_DOUBLE_TOTAL), // Order symbol ORDER_PROP_COMMENT, // Order comment ORDER_PROP_COMMENT_EXT, // Order custom comment ORDER_PROP_EXT_ID // Order ID in the external trading system }; #define ORDER_PROP_STRING_TOTAL (4) // Total number of string properties //+------------------------------------------------------------------+
我们在可能的排序条件枚举中删除所有以毫秒为单位的引用(现在按时间排序时默认使用它们),并添加按自定义注释排序的条件:
//+------------------------------------------------------------------+ //| Possible criteria of orders and deals sorting | //+------------------------------------------------------------------+ #define FIRST_ORD_DBL_PROP (ORDER_PROP_INTEGER_TOTAL-ORDER_PROP_INTEGER_SKIP) #define FIRST_ORD_STR_PROP (ORDER_PROP_INTEGER_TOTAL+ORDER_PROP_DOUBLE_TOTAL-ORDER_PROP_INTEGER_SKIP) enum ENUM_SORT_ORDERS_MODE { //--- Sort by integer properties SORT_BY_ORDER_TICKET = 0, // Sort by order ticket SORT_BY_ORDER_MAGIC = 1, // Sort by order magic number SORT_BY_ORDER_TIME_OPEN = 2, // Sort by order open time in milliseconds SORT_BY_ORDER_TIME_CLOSE = 3, // Sort by order close time in milliseconds SORT_BY_ORDER_TIME_EXP = 4, // Sort by order expiration date SORT_BY_ORDER_STATUS = 5, // Sort by order status (market order/pending order/deal/balance, credit operation) SORT_BY_ORDER_TYPE = 6, // Sort by order type SORT_BY_ORDER_REASON = 7, // Sort by order/position reason/source SORT_BY_ORDER_STATE = 8, // Sort by order status SORT_BY_ORDER_POSITION_ID = 9, // Sort by position ID SORT_BY_ORDER_POSITION_BY_ID = 10, // Sort by opposite position ID SORT_BY_ORDER_DEAL_ORDER = 11, // Sort by order a deal is based on SORT_BY_ORDER_DEAL_ENTRY = 12, // Sort by deal direction – IN, OUT or IN/OUT SORT_BY_ORDER_TIME_UPDATE = 13, // Sort by position change time in seconds SORT_BY_ORDER_TICKET_FROM = 14, // Sort by parent order ticket SORT_BY_ORDER_TICKET_TO = 15, // Sort by derived order ticket SORT_BY_ORDER_PROFIT_PT = 16, // Sort by order profit in points SORT_BY_ORDER_CLOSE_BY_SL = 17, // Sort by order closing by StopLoss flag SORT_BY_ORDER_CLOSE_BY_TP = 18, // Sort by order closing by TakeProfit flag SORT_BY_ORDER_GROUP_ID = 19, // Sort by order/position group ID SORT_BY_ORDER_DIRECTION = 20, // Sort by direction (Buy, Sell) //--- Sort by real properties SORT_BY_ORDER_PRICE_OPEN = FIRST_ORD_DBL_PROP, // Sort by open price SORT_BY_ORDER_PRICE_CLOSE = FIRST_ORD_DBL_PROP+1, // Sort by close price SORT_BY_ORDER_SL = FIRST_ORD_DBL_PROP+2, // Sort by StopLoss price SORT_BY_ORDER_TP = FIRST_ORD_DBL_PROP+3, // Sort by TakeProfit price SORT_BY_ORDER_PROFIT = FIRST_ORD_DBL_PROP+4, // Sort by profit SORT_BY_ORDER_COMMISSION = FIRST_ORD_DBL_PROP+5, // Sort by commission SORT_BY_ORDER_SWAP = FIRST_ORD_DBL_PROP+6, // Sort by swap SORT_BY_ORDER_VOLUME = FIRST_ORD_DBL_PROP+7, // Sort by volume SORT_BY_ORDER_VOLUME_CURRENT = FIRST_ORD_DBL_PROP+8, // Sort by unexecuted volume SORT_BY_ORDER_PROFIT_FULL = FIRST_ORD_DBL_PROP+9, // Sort by profit+commission+swap criterion SORT_BY_ORDER_PRICE_STOP_LIMIT= FIRST_ORD_DBL_PROP+10, // Sort by Limit order when StopLimit order is activated //--- Sort by string properties SORT_BY_ORDER_SYMBOL = FIRST_ORD_STR_PROP, // Sort by symbol SORT_BY_ORDER_COMMENT = FIRST_ORD_STR_PROP+1, // Sort by comment SORT_BY_ORDER_COMMENT_EXT = FIRST_ORD_STR_PROP+2, // Sort by custom comment SORT_BY_ORDER_EXT_ID = FIRST_ORD_STR_PROP+3 // Sort by order ID in an external trading system }; //+------------------------------------------------------------------+
Defines.mqh 中的修改已完毕。 现在我们需要从函数库文件中剔除所有已删除订单属性的引用:
函数库文件中所有排序模式实例
SORT_BY_ORDER_TIME_OPEN_MSC
和
SORT_BY_ORDER_TIME_CLOSE_MSC
替换为
SORT_BY_ORDER_TIME_OPEN
和
SORT_BY_ORDER_TIME_CLOSE
在 HistoryDeal.mqh,HistoryOrder.mqh,HistoryPending.mqh,MarketOrder.mqh,MarketPending.mqh 和 MarketPosition.mqh 抽象订单继承类文件里,删除所有以毫秒为单位的订单属性 (它们现在的默认值为毫秒):
ORDER_PROP_TIME_CLOSE_MSC
和
ORDER_PROP_TIME_UPDATE_MSC
在 COrder 抽象订单类的 Order.mqh文件中,从私有部分删除返回时间的方法:
datetime OrderOpenTime(void) const; datetime OrderCloseTime(void) const; datetime OrderExpiration(void) const; datetime PositionTimeUpdate(void) const; datetime PositionTimeUpdateMSC(void) const;
从类的公有部分中删除以毫秒为单位返回时间的简化访问方法。 它将由返回秒值时间的方法替代:
//+------------------------------------------------------------------+ //| Methods of a simplified access to the order object properties | //+------------------------------------------------------------------+ //--- Return (1) ticket, (2) parent order ticket, (3) derived order ticket, (4) magic number, (5) order reason, //--- (6) position ID, (7) opposite position ID, (8) group ID, (9) type, (10) flag of closing by StopLoss, //--- (11) flag of closing by TakeProfit (12) open time, (13) close time, (14) open time in milliseconds, //--- (15) close time in milliseconds, (16) expiration date, (17) state, (18) status, (19) order type by direction long Ticket(void) const { return this.GetProperty(ORDER_PROP_TICKET); } long TicketFrom(void) const { return this.GetProperty(ORDER_PROP_TICKET_FROM); } long TicketTo(void) const { return this.GetProperty(ORDER_PROP_TICKET_TO); } long Magic(void) const { return this.GetProperty(ORDER_PROP_MAGIC); } long Reason(void) const { return this.GetProperty(ORDER_PROP_REASON); } long PositionID(void) const { return this.GetProperty(ORDER_PROP_POSITION_ID); } long PositionByID(void) const { return this.GetProperty(ORDER_PROP_POSITION_BY_ID); } long GroupID(void) const { return this.GetProperty(ORDER_PROP_GROUP_ID); } long TypeOrder(void) const { return this.GetProperty(ORDER_PROP_TYPE); } bool IsCloseByStopLoss(void) const { return (bool)this.GetProperty(ORDER_PROP_CLOSE_BY_SL); } bool IsCloseByTakeProfit(void) const { return (bool)this.GetProperty(ORDER_PROP_CLOSE_BY_TP); } datetime TimeOpen(void) const { return (datetime)this.GetProperty(ORDER_PROP_TIME_OPEN); } datetime TimeClose(void) const { return (datetime)this.GetProperty(ORDER_PROP_TIME_CLOSE); } datetime TimeOpenMSC(void) const { return (datetime)this.GetProperty(ORDER_PROP_TIME_OPEN_MSC); } datetime TimeCloseMSC(void) const { return (datetime)this.GetProperty(ORDER_PROP_TIME_CLOSE_MSC); } datetime TimeExpiration(void) const { return (datetime)this.GetProperty(ORDER_PROP_TIME_EXP); } ENUM_ORDER_STATE State(void) const { return (ENUM_ORDER_STATE)this.GetProperty(ORDER_PROP_STATE); } ENUM_ORDER_STATUS Status(void) const { return (ENUM_ORDER_STATUS)this.GetProperty(ORDER_PROP_STATUS); } ENUM_ORDER_TYPE TypeByDirection(void) const { return (ENUM_ORDER_TYPE)this.GetProperty(ORDER_PROP_DIRECTION); } //--- Return (1) open price, (2) close price, (3) profit, (4) commission, (5) swap, (6) volume,
另外,添加返回和设置订单自定义注释的方法:
//--- Return (1) symbol, (2) comment, (3) ID at an exchange string Symbol(void) const { return this.GetProperty(ORDER_PROP_SYMBOL); } string Comment(void) const { return this.GetProperty(ORDER_PROP_COMMENT); } string CommentExt(void) const { return this.GetProperty(ORDER_PROP_COMMENT_EXT); } string ExternalID(void) const { return this.GetProperty(ORDER_PROP_EXT_ID); } //--- Get the full order profit double ProfitFull(void) const { return this.Profit()+this.Comission()+this.Swap(); } //--- Get order profit in points int ProfitInPoints(void) const; //--- Set (1) group ID and (2) custom comment void SetGroupID(const long group_id) { this.SetProperty(ORDER_PROP_GROUP_ID,group_id); } void SetCommentExt(const string comment_ext) { this.SetProperty(ORDER_PROP_COMMENT_EXT,comment_ext); }
在 COrder 类的平仓构造函数中,删除保存第二个时间的属性,将毫秒属性替换为秒,以后会在其内保存毫秒时间。
添加
将自定义注释保存为空字符串:
//+------------------------------------------------------------------+ //| Closed parametric constructor | //+------------------------------------------------------------------+ COrder::COrder(ENUM_ORDER_STATUS order_status,const ulong ticket) { //--- Save integer properties this.m_ticket=ticket; this.m_long_prop[ORDER_PROP_STATUS] = order_status; this.m_long_prop[ORDER_PROP_MAGIC] = this.OrderMagicNumber(); this.m_long_prop[ORDER_PROP_TICKET] = this.OrderTicket(); this.m_long_prop[ORDER_PROP_TIME_EXP] = this.OrderExpiration(); this.m_long_prop[ORDER_PROP_TYPE] = this.OrderType(); this.m_long_prop[ORDER_PROP_STATE] = this.OrderState(); this.m_long_prop[ORDER_PROP_DIRECTION] = this.OrderTypeByDirection(); this.m_long_prop[ORDER_PROP_POSITION_ID] = this.OrderPositionID(); this.m_long_prop[ORDER_PROP_REASON] = this.OrderReason(); this.m_long_prop[ORDER_PROP_DEAL_ORDER_TICKET] = this.DealOrderTicket(); this.m_long_prop[ORDER_PROP_DEAL_ENTRY] = this.DealEntry(); this.m_long_prop[ORDER_PROP_POSITION_BY_ID] = this.OrderPositionByID(); this.m_long_prop[ORDER_PROP_TIME_OPEN] = this.OrderOpenTimeMSC(); this.m_long_prop[ORDER_PROP_TIME_CLOSE] = this.OrderCloseTimeMSC(); this.m_long_prop[ORDER_PROP_TIME_UPDATE] = this.PositionTimeUpdateMSC(); //--- Save real properties this.m_double_prop[this.IndexProp(ORDER_PROP_PRICE_OPEN)] = this.OrderOpenPrice(); this.m_double_prop[this.IndexProp(ORDER_PROP_PRICE_CLOSE)] = this.OrderClosePrice(); this.m_double_prop[this.IndexProp(ORDER_PROP_PROFIT)] = this.OrderProfit(); this.m_double_prop[this.IndexProp(ORDER_PROP_COMMISSION)] = this.OrderCommission(); this.m_double_prop[this.IndexProp(ORDER_PROP_SWAP)] = this.OrderSwap(); this.m_double_prop[this.IndexProp(ORDER_PROP_VOLUME)] = this.OrderVolume(); this.m_double_prop[this.IndexProp(ORDER_PROP_SL)] = this.OrderStopLoss(); this.m_double_prop[this.IndexProp(ORDER_PROP_TP)] = this.OrderTakeProfit(); this.m_double_prop[this.IndexProp(ORDER_PROP_VOLUME_CURRENT)] = this.OrderVolumeCurrent(); this.m_double_prop[this.IndexProp(ORDER_PROP_PRICE_STOP_LIMIT)] = this.OrderPriceStopLimit(); //--- Save string properties this.m_string_prop[this.IndexProp(ORDER_PROP_SYMBOL)] = this.OrderSymbol(); this.m_string_prop[this.IndexProp(ORDER_PROP_COMMENT)] = this.OrderComment(); this.m_string_prop[this.IndexProp(ORDER_PROP_EXT_ID)] = this.OrderExternalID(); //--- Save additional integer properties this.m_long_prop[ORDER_PROP_PROFIT_PT] = this.ProfitInPoints(); this.m_long_prop[ORDER_PROP_TICKET_FROM] = this.OrderTicketFrom(); this.m_long_prop[ORDER_PROP_TICKET_TO] = this.OrderTicketTo(); this.m_long_prop[ORDER_PROP_CLOSE_BY_SL] = this.OrderCloseByStopLoss(); this.m_long_prop[ORDER_PROP_CLOSE_BY_TP] = this.OrderCloseByTakeProfit(); this.m_long_prop[ORDER_PROP_GROUP_ID] = 0; //--- Save additional real properties this.m_double_prop[this.IndexProp(ORDER_PROP_PROFIT_FULL)] = this.ProfitFull(); //--- Save additional string properties this.m_string_prop[this.IndexProp(ORDER_PROP_COMMENT_EXT)] = ""; } //+------------------------------------------------------------------+
在 MQL4返回持仓 ID 的方法中,执行以下操作:如果此为持仓,则返回其票证,否则返回零。 在 MQL5 中,开仓票证充当持仓 ID。 它在整个持仓生存期内保持不变。
因此,在 MQL4 中,只有仓位票证可以作为持仓 ID。 MQL4 中的挂单没有此类 ID。 如果删除订单,则不会据其开立任何仓位。 如果订单已激活,则 MQL4 订单历史记录中没有此类订单,但该持仓接收其票证,并用此票证充当持仓 ID。//+------------------------------------------------------------------+ //| Return the position ID | //+------------------------------------------------------------------+ long COrder::OrderPositionID(void) const { #ifdef __MQL4__ return(this.Status()==ORDER_STATUS_MARKET_POSITION ? this.Ticket() : 0); #else long id=0; switch((ENUM_ORDER_STATUS)this.GetProperty(ORDER_PROP_STATUS)) { case ORDER_STATUS_MARKET_POSITION : id=::PositionGetInteger(POSITION_IDENTIFIER); break; case ORDER_STATUS_MARKET_ORDER : case ORDER_STATUS_MARKET_PENDING : id=::OrderGetInteger(ORDER_POSITION_ID); break; case ORDER_STATUS_HISTORY_PENDING : case ORDER_STATUS_HISTORY_ORDER : id=::HistoryOrderGetInteger(m_ticket,ORDER_POSITION_ID); break; case ORDER_STATUS_DEAL : id=::HistoryDealGetInteger(m_ticket,DEAL_POSITION_ID); break; default : id=0; break; } return id; #endif } //+------------------------------------------------------------------+
为 MQL4 补充返回逆向仓位 ID 方法:
//+------------------------------------------------------------------+ //| Return the opposite position ID | //+------------------------------------------------------------------+ long COrder::OrderPositionByID(void) const { long ticket=0; #ifdef __MQL4__ string order_comment=::OrderComment(); if(::StringFind(order_comment,"close hedge by #")>WRONG_VALUE) ticket=::StringToInteger(::StringSubstr(order_comment,16)); #else switch((ENUM_ORDER_STATUS)this.GetProperty(ORDER_PROP_STATUS)) { case ORDER_STATUS_MARKET_ORDER : case ORDER_STATUS_MARKET_PENDING : ticket=::OrderGetInteger(ORDER_POSITION_BY_ID); break; case ORDER_STATUS_HISTORY_PENDING : case ORDER_STATUS_HISTORY_ORDER : ticket=::HistoryOrderGetInteger(m_ticket,ORDER_POSITION_BY_ID); break; default : ticket=0; break; } #endif return ticket; } //+------------------------------------------------------------------+
此处,如果这是 MQL4,且如果订单注释内含 "close hedge by #",则计算注释里所含的逆向订单票证的索引,并将方法返回的值分配给它。
因为我们不想再以秒为单位获取时间,故此从类清单中删除两个不再需要的方法实现:
//+------------------------------------------------------------------+ //| Return open time | //+------------------------------------------------------------------+ datetime COrder::OrderOpenTime(void) const { #ifdef __MQL4__ return ::OrderOpenTime(); #else datetime res=0; switch((ENUM_ORDER_STATUS)this.GetProperty(ORDER_PROP_STATUS)) { case ORDER_STATUS_MARKET_POSITION : res=(datetime)::PositionGetInteger(POSITION_TIME); break; case ORDER_STATUS_MARKET_ORDER : case ORDER_STATUS_MARKET_PENDING : res=(datetime)::OrderGetInteger(ORDER_TIME_SETUP); break; case ORDER_STATUS_HISTORY_PENDING : case ORDER_STATUS_HISTORY_ORDER : res=(datetime)::HistoryOrderGetInteger(m_ticket,ORDER_TIME_SETUP); break; case ORDER_STATUS_DEAL : res=(datetime)::HistoryDealGetInteger(m_ticket,DEAL_TIME); break; default : res=0; break; } return res; #endif } //+------------------------------------------------------------------+ //| Return close time | //+------------------------------------------------------------------+ datetime COrder::OrderCloseTime(void) const { #ifdef __MQL4__ return ::OrderCloseTime(); #else datetime res=0; switch((ENUM_ORDER_STATUS)this.GetProperty(ORDER_PROP_STATUS)) { case ORDER_STATUS_HISTORY_PENDING : case ORDER_STATUS_HISTORY_ORDER : res=(datetime)::HistoryOrderGetInteger(m_ticket,ORDER_TIME_DONE); break; case ORDER_STATUS_DEAL : res=(datetime)::HistoryDealGetInteger(m_ticket,DEAL_TIME); break; default : res=0; break; } return res; #endif } //+------------------------------------------------------------------+
为了更有意义地显示 MQL4 中的订单状态,在返回订单状态描述的方法中进行小幅修改:
//+------------------------------------------------------------------+ //| Return the order status name | //+------------------------------------------------------------------+ string COrder::StatusDescription(void) const { ENUM_ORDER_STATUS status=this.Status(); ENUM_ORDER_TYPE type=(ENUM_ORDER_TYPE)this.TypeOrder(); return ( status==ORDER_STATUS_BALANCE ? TextByLanguage("Балансовая операция","Balance operation") : #ifdef __MQL5__ status==ORDER_STATUS_MARKET_ORDER || status==ORDER_STATUS_HISTORY_ORDER ? ( type==ORDER_TYPE_CLOSE_BY ? TextByLanguage("Закрывающий ордер","Order for closing by") : TextByLanguage("Ордер на ","The order to ")+(type==ORDER_TYPE_BUY ? TextByLanguage("покупку","buy") : TextByLanguage("продажу","sell")) ) : #else status==ORDER_STATUS_HISTORY_ORDER ? TextByLanguage("Исторический ордер","History order") : #endif status==ORDER_STATUS_DEAL ? TextByLanguage("Сделка","Deal") : status==ORDER_STATUS_MARKET_POSITION ? TextByLanguage("Позиция","Active position") : status==ORDER_STATUS_MARKET_PENDING ? TextByLanguage("Установленный отложенный ордер","Active pending order") : status==ORDER_STATUS_HISTORY_PENDING ? TextByLanguage("Отложенный ордер","Pending order") : EnumToString(status) ); } //+------------------------------------------------------------------+
此处,对于 MQL4 中的删除挂单和平仓,我们将状态描述返回为“历史订单”。
在返回订单整数型属性描述的方法中,修改包含 ORDER_PROP_TIME_OPEN,ORDER_PROP_TIME_CLOSE 和 ORDER_PROP_TIME_UPDATE 属性描述的字符串,以便令它们返回毫秒属性:
//+------------------------------------------------------------------+ //| Return description of an order's integer property | //+------------------------------------------------------------------+ string COrder::GetPropertyDescription(ENUM_ORDER_PROP_INTEGER property) { return ( //--- General properties property==ORDER_PROP_MAGIC ? TextByLanguage("Магик","Magic number")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+(string)this.GetProperty(property) ) : property==ORDER_PROP_TICKET ? TextByLanguage("Тикет","Ticket")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : " #"+(string)this.GetProperty(property) ) : property==ORDER_PROP_TICKET_FROM ? TextByLanguage("Тикет родительского ордера","Ticket of parent order")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : " #"+(string)this.GetProperty(property) ) : property==ORDER_PROP_TICKET_TO ? TextByLanguage("Тикет наследуемого ордера","Inherited order ticket")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : " #"+(string)this.GetProperty(property) ) : property==ORDER_PROP_TIME_EXP ? TextByLanguage("Дата экспирации","Date of expiration")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : (this.GetProperty(property)==0 ? TextByLanguage(": Не задана",": Not set") : ": "+::TimeToString(this.GetProperty(property),TIME_DATE|TIME_MINUTES|TIME_SECONDS)) ) : property==ORDER_PROP_TYPE ? TextByLanguage("Тип","Type")+": "+this.TypeDescription() : property==ORDER_PROP_DIRECTION ? TextByLanguage("Тип по направлению","Type by direction")+": "+this.DirectionDescription() : property==ORDER_PROP_REASON ? TextByLanguage("Причина","Reason")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+this.GetReasonDescription(this.GetProperty(property)) ) : property==ORDER_PROP_POSITION_ID ? TextByLanguage("Идентификатор позиции","Position identifier")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": #"+(string)this.GetProperty(property) ) : property==ORDER_PROP_DEAL_ORDER_TICKET ? TextByLanguage("Сделка на основании ордера с тикетом","Deal by order ticket")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": #"+(string)this.GetProperty(property) ) : property==ORDER_PROP_DEAL_ENTRY ? TextByLanguage("Направление сделки","Deal entry")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+this.GetEntryDescription(this.GetProperty(property)) ) : property==ORDER_PROP_POSITION_BY_ID ? TextByLanguage("Идентификатор встречной позиции","Opposite position identifier")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+(string)this.GetProperty(property) ) : property==ORDER_PROP_TIME_OPEN ? TextByLanguage("Время открытия в милисекундах","Opening time in milliseconds")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+TimeMSCtoString(this.GetProperty(property))+" ("+(string)this.GetProperty(property)+")" ) : property==ORDER_PROP_TIME_CLOSE ? TextByLanguage("Время закрытия в милисекундах","Closing time in milliseconds")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+TimeMSCtoString(this.GetProperty(property))+" ("+(string)this.GetProperty(property)+")" ) : property==ORDER_PROP_TIME_UPDATE ? TextByLanguage("Время изменения позиции в милисекундах","Time to change the position in milliseconds")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+(this.GetProperty(property)!=0 ? TimeMSCtoString(this.GetProperty(property))+" ("+(string)this.GetProperty(property)+")" : "0") ) : property==ORDER_PROP_STATE ? TextByLanguage("Состояние","Statе")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": \""+this.StateDescription()+"\"" ) : //--- Additional property property==ORDER_PROP_STATUS ? TextByLanguage("Статус","Status")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": \""+this.StatusDescription()+"\"" ) : property==ORDER_PROP_PROFIT_PT ? ( this.Status()==ORDER_STATUS_MARKET_PENDING ? TextByLanguage("Дистанция от цены в пунктах","Distance from price in points") : TextByLanguage("Прибыль в пунктах","Profit in points") )+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+(string)this.GetProperty(property) ) : property==ORDER_PROP_CLOSE_BY_SL ? TextByLanguage("Закрытие по StopLoss","Close by StopLoss")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+(this.GetProperty(property) ? TextByLanguage("Да","Yes") : TextByLanguage("Нет","No")) ) : property==ORDER_PROP_CLOSE_BY_TP ? TextByLanguage("Закрытие по TakeProfit","Close by TakeProfit")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+(this.GetProperty(property) ? TextByLanguage("Да","Yes") : TextByLanguage("Нет","No")) ) : property==ORDER_PROP_GROUP_ID ? TextByLanguage("Идентификатор группы","Group identifier")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : ": "+(string)this.GetProperty(property) ) : "" ); } //+------------------------------------------------------------------+
并添加返回自定义注释描述符串型属性的方法:
//+------------------------------------------------------------------+ //| Return description of the order's string property | //+------------------------------------------------------------------+ string COrder::GetPropertyDescription(ENUM_ORDER_PROP_STRING property) { return ( property==ORDER_PROP_SYMBOL ? TextByLanguage("Символ","Symbol")+": \""+this.GetProperty(property)+"\"" : property==ORDER_PROP_COMMENT ? TextByLanguage("Комментарий","Comment")+ (this.GetProperty(property)=="" ? TextByLanguage(": Отсутствует",": Not set"):": \""+this.GetProperty(property)+"\"") : property==ORDER_PROP_COMMENT_EXT ? TextByLanguage("Пользовательский комментарий","Custom comment")+ (this.GetProperty(property)=="" ? TextByLanguage(": Не задан",": Not set"):": \""+this.GetProperty(property)+"\"") : property==ORDER_PROP_EXT_ID ? TextByLanguage("Идентификатор на бирже","Exchange identifier")+ (!this.SupportProperty(property) ? TextByLanguage(": Свойство не поддерживается",": Property not supported") : (this.GetProperty(property)=="" ? TextByLanguage(": Отсутствует",": Not set"):": \""+this.GetProperty(property)+"\"")): "" ); } //+------------------------------------------------------------------+
COrder 抽象订单类的修改完毕。
在 DELib.mqh 服务函数文件中,对函数进行小幅改进,按订单类型返回订单/持仓名称:
//+------------------------------------------------------------------+ //| Return order name | //+------------------------------------------------------------------+ string OrderTypeDescription(const ENUM_ORDER_TYPE type,bool as_order=true) { string pref=(#ifdef __MQL5__ "Market order" #else (as_order ? "Market order" : "Position") #endif ); return ( type==ORDER_TYPE_BUY_LIMIT ? "Buy Limit" : type==ORDER_TYPE_BUY_STOP ? "Buy Stop" : type==ORDER_TYPE_SELL_LIMIT ? "Sell Limit" : type==ORDER_TYPE_SELL_STOP ? "Sell Stop" : #ifdef __MQL5__ type==ORDER_TYPE_BUY_STOP_LIMIT ? "Buy Stop Limit" : type==ORDER_TYPE_SELL_STOP_LIMIT ? "Sell Stop Limit" : type==ORDER_TYPE_CLOSE_BY ? TextByLanguage("Закрывающий ордер","Order for closing by") : #else type==ORDER_TYPE_BALANCE ? TextByLanguage("Балансовая операция","Balance operation") : type==ORDER_TYPE_CREDIT ? TextByLanguage("Кредитная операция","Credit operation") : #endif type==ORDER_TYPE_BUY ? pref+" Buy" : type==ORDER_TYPE_SELL ? pref+" Sell" : TextByLanguage("Неизвестный тип ордера","Unknown order type") ); } //+------------------------------------------------------------------+
在此,我们针对 for MQL4 添加标志来管控订单名称的显示,即可为订单,亦或为持仓。
针对 MQL4 显示默认设置 “作为订单”。 为什么要这样做? 假设发送开仓事件至日志时,导致开仓的订单显示在方括号中。 在此情况下,由单据为123 的市价(非挂单)订单开立的空头仓位会以更有意义的 [Market order Sell #123] 替换
[Position Sell #123] 消息作为导致开仓的标注。
我们改进市价订单和持仓集合类的 AddToListMarket() 方法。 我们现在使用 ORDER_PROP_TIME_UPDATE
仓位更新时间(默认设置为毫秒),替代以毫秒为单位的 ORDER_PROP_TIME_UPDATE_MSC 仓位更新时间:
//+--------------------------------------------------------------------------------+ //| Add an order or a position to the list of orders and positions on the account | //+--------------------------------------------------------------------------------+ bool CMarketCollection::AddToListMarket(COrder *order) { if(order==NULL) return false; ENUM_ORDER_STATUS status=order.Status(); if(this.m_list_all_orders.InsertSort(order)) { if(status==ORDER_STATUS_MARKET_POSITION) { this.m_struct_curr_market.hash_sum_acc+=order.GetProperty(ORDER_PROP_TIME_UPDATE)+this.ConvertToHS(order); this.m_struct_curr_market.total_volumes+=order.Volume(); this.m_struct_curr_market.total_positions++; return true; } if(status==ORDER_STATUS_MARKET_PENDING) { this.m_struct_curr_market.hash_sum_acc+=this.ConvertToHS(order); this.m_struct_curr_market.total_volumes+=order.Volume(); this.m_struct_curr_market.total_pending++; return true; } } else { ::Print(DFUN,order.TypeDescription()," #",order.Ticket()," ",TextByLanguage("не удалось добавить в список","failed to add to the list")); delete order; } return false; } //+------------------------------------------------------------------+
在创建控制订单的方法中将订单时间替换为以毫秒为单位的订单时间,并将其添加到列表中(出于同样的原因):
//+------------------------------------------------------------------+ //| Create and add an order to the list of control orders | //+------------------------------------------------------------------+ bool CMarketCollection::AddToListControl(COrder *order) { if(order==NULL) return false; COrderControl* order_control=new COrderControl(order.PositionID(),order.Ticket(),order.Magic(),order.Symbol()); if(order_control==NULL) return false; order_control.SetTime(order.TimeOpen()); order_control.SetTimePrev(order.TimeOpen()); order_control.SetVolume(order.Volume()); order_control.SetTime(order.TimeOpen()); order_control.SetTypeOrder(order.TypeOrder()); order_control.SetTypeOrderPrev(order.TypeOrder()); order_control.SetPrice(order.PriceOpen()); order_control.SetPricePrev(order.PriceOpen()); order_control.SetStopLoss(order.StopLoss()); order_control.SetStopLossPrev(order.StopLoss()); order_control.SetTakeProfit(order.TakeProfit()); order_control.SetTakeProfitPrev(order.TakeProfit()); if(!this.m_list_control.Add(order_control)) { delete order_control; return false; } return true; } //+------------------------------------------------------------------+
在 HistoryCollection.mqh 文件的历史订单和成交集合类里按时间选择订单的方法当中, 改进属性比较的选择。
由于我们已经选择了四个属性(以毫秒为单位的开单时间,以毫秒为单位的平单时间,以秒为单位的开单时间,以及以秒为单位的平单时间),且我们现已删除了其中两个属性,并简化了选择:
//+------------------------------------------------------------------+ //| Select orders from the collection with time | //| from begin_time to end_time | //+------------------------------------------------------------------+ CArrayObj *CHistoryCollection::GetListByTime(const datetime begin_time=0,const datetime end_time=0, const ENUM_SELECT_BY_TIME select_time_mode=SELECT_BY_TIME_CLOSE) { ENUM_ORDER_PROP_INTEGER property=(select_time_mode==SELECT_BY_TIME_CLOSE ? ORDER_PROP_TIME_CLOSE : ORDER_PROP_TIME_OPEN); CArrayObj *list=new CArrayObj(); if(list==NULL) { ::Print(DFUN+TextByLanguage("Ошибка создания временного списка","Error creating temporary list")); return NULL; } datetime begin=begin_time,end=(end_time==0 ? END_TIME : end_time); if(begin_time>end_time) begin=0; list.FreeMode(false); ListStorage.Add(list); //--- this.m_order_instance.SetProperty(property,begin); int index_begin=this.m_list_all_orders.SearchGreatOrEqual(&m_order_instance); if(index_begin==WRONG_VALUE) return list; this.m_order_instance.SetProperty(property,end); int index_end=this.m_list_all_orders.SearchLessOrEqual(&m_order_instance); if(index_end==WRONG_VALUE) return list; for(int i=index_begin; i<=index_end; i++) list.Add(this.m_list_all_orders.At(i)); return list; } //+------------------------------------------------------------------+
在CEngine.mqh 文件中,使用 SORT_BY_ORDER_TIME_OPEN 时间常量替换 SORT_BY_ORDER_TIME_OPEN_MSC 时间常量的所有实例(以毫秒为单位)。
现在,默认情况下,此常量使用以毫秒为单位的时间。
如此,从函数库文件里删除有关以秒为时间单位的改进就完毕了。
实现 MQL4 的平仓事件
在 MQL4 中为寻找识别平仓和订单移除事件发生的可能选项时,进行了各种实验和测试,但结果令人沮丧。 与 MQL5 不同,MQL4 提供的可用于判断事件标识的数据更少。
在 MQL5
中,我们可以轻松地利用属于某笔持仓的订单数据来定义事件,而在 MQL4 中,仓位和挂单都被视为订单。 如果我们在 MetaTrader 4 中有挂单,那么在删除后,我们将面临:
- 历史订单数量增加
- 总交易量减少
- 在场订单数量保持不变。
若要确保事件属于删除挂单(MQL4 中的仓位也是订单),则需检查持仓的数量。 如果未有变化,则动作是针对挂单执行。 在 MetaTrader 4 中,直至我们部分平仓(一笔订单)之前,一切似乎都是正常且合乎逻辑的。 当部分平仓时,我们与删除挂单时的情况相同:
- 历史订单数量增加(部分平仓(订单)已进入历史),
- 自我们部分平仓以来,账户交易量减少,
- 持仓的数量没有减少 — 部分平仓时保持不变。
这与删除挂单时的状态相同。 我们可以在测试器中启动上一篇文章里的测试 EA,并观察到这一点。 开仓,将其部分平仓,设置挂单并将其删除。
在最后一个操作期间,两条消息同时出现在日志中:部分平仓和挂单删除。 我所提及的标准一致性,正是用来定义上述两个事件。 该程序只是简单地定义了两个事件,其中一个是不正确的。
在此,我们可以利用检查已修改挂单数量 — 在定义部分平仓时,我们还应检查在场挂单的数量变化。 如果它没有变化,则该事件是部分平仓。
一切好似都合乎逻辑,但这种方法对 MetaTrader 4 中允许的交易订单施加了限制。 换句话说,我们无法在单次循环中删除挂单并部分平仓,因为这违反了上面提到的事件定义逻辑。 我们可以实现一个解决方案来绕过这个限制,但它需要重新设计在场/历史订单和持仓集合类。
若要定义场内环境的变化,我们需要使用帐户内变化的临时订单和持仓列表,而非管控所发生的变化量。 应根据这些列表的数据处理事件。
在此情况下,每种事件类型都有自己的列表,创建事件并将它们发送到程序,然后根据已变化的订单和持仓列表执行操作。
若要定义部分平仓,我们需要管理帐户的总交易量。 这意味着我们需要将其变化值传递给 CEventsCollection 类的 Refresh() 方法。 像往常一样,一切都从函数库的基本对象开始。 为了调用事件集合类的更新方法,我们需添加必要的内容。
在 CEngine::TradeEventsControl() 类方法中,将附加的可转移参数添加到
CEventsCollection 类的 Refresh 方法中:
//+------------------------------------------------------------------+ //| Check trading events | //+------------------------------------------------------------------+ void CEngine::TradeEventsControl(void) { //--- Initialize the trading events code and flags this.m_is_market_trade_event=false; this.m_is_history_trade_event=false; //--- Update the lists this.m_market.Refresh(); this.m_history.Refresh(); //--- First launch actions if(this.IsFirstStart()) { this.m_acc_trade_event=TRADE_EVENT_NO_EVENT; return; } //--- Check the changes in the market status and account history this.m_is_market_trade_event=this.m_market.IsTradeEvent(); this.m_is_history_trade_event=this.m_history.IsTradeEvent(); //--- If there is any event, send the lists, the flags and the number of new orders and deals to the event collection, and update it int change_total=0; CArrayObj* list_changes=this.m_market.GetListChanges(); if(list_changes!=NULL) change_total=list_changes.Total(); if(this.m_is_history_trade_event || this.m_is_market_trade_event || change_total>0) { this.m_events.Refresh(this.m_history.GetList(),this.m_market.GetList(),list_changes,this.m_market.GetListControl(), this.m_is_history_trade_event,this.m_is_market_trade_event, this.m_history.NewOrders(),this.m_market.NewPendingOrders(), this.m_market.NewPositions(),this.m_history.NewDeals(), this.m_market.ChangedVolumeValue()); //--- Get the account's last trading event this.m_acc_trade_event=this.m_events.GetLastTradeEvent(); } } //+------------------------------------------------------------------+
现在我们需要通过为它实现另一个参数(交易量变化值)来更改 CEventsCollection 类本身的 Refresh() 方法。
将新参数添加到
EventsCollection.mqh 文件中 Refresh() 方法的定义中:
public: //--- Select events from the collection with time within the range from begin_time to end_time CArrayObj *GetListByTime(const datetime begin_time=0,const datetime end_time=0); //--- Return the full event collection list "as is" CArrayObj *GetList(void) { return &this.m_list_events; } //--- Return the list by selected (1) integer, (2) real and (3) string properties meeting the compared criterion CArrayObj *GetList(ENUM_EVENT_PROP_INTEGER property,long value,ENUM_COMPARER_TYPE mode=EQUAL) { return CSelect::ByEventProperty(this.GetList(),property,value,mode); } CArrayObj *GetList(ENUM_EVENT_PROP_DOUBLE property,double value,ENUM_COMPARER_TYPE mode=EQUAL) { return CSelect::ByEventProperty(this.GetList(),property,value,mode); } CArrayObj *GetList(ENUM_EVENT_PROP_STRING property,string value,ENUM_COMPARER_TYPE mode=EQUAL) { return CSelect::ByEventProperty(this.GetList(),property,value,mode); } //--- Update the list of events void Refresh(CArrayObj* list_history, CArrayObj* list_market, CArrayObj* list_changes, CArrayObj* list_control, const bool is_history_event, const bool is_market_event, const int new_history_orders, const int new_market_pendings, const int new_market_positions, const int new_deals, const double changed_volume); //--- Set the control program chart ID void SetChartID(const long id) { this.m_chart_id=id; } //--- Return the last trading event on the account ENUM_TRADE_EVENT GetLastTradeEvent(void) const { return this.m_trade_event; } //--- Reset the last trading event void ResetLastTradeEvent(void) { this.m_trade_event=TRADE_EVENT_NO_EVENT; } //--- Constructor CEventsCollection(void); };
我们需要在 Refresh() 方法的实现中添加:
//+------------------------------------------------------------------+ //| Update the event list | //+------------------------------------------------------------------+ void CEventsCollection::Refresh(CArrayObj* list_history, CArrayObj* list_market, CArrayObj* list_changes, CArrayObj* list_control, const bool is_history_event, const bool is_market_event, const int new_history_orders, const int new_market_pendings, const int new_market_positions, const int new_deals, const double changed_volume) {
现在我们需要创建平仓和部分平仓事件处理程序,并解决在部分平仓期间错误定义挂单删除事件的问题。
在类的私有部分中,修改调用新事件创建方法的第一种形式,将控制订单列表传递给方法。
我们需要它来识别参与平仓的订单。 此外,添加
返回历史(已平)仓位列表的方法,和按仓位票证返回控制订单指针的方法:
class CEventsCollection : public CListObj { private: CListObj m_list_events; // List of events bool m_is_hedge; // Hedge account flag long m_chart_id; // Control program chart ID int m_trade_event_code; // Trading event code ENUM_TRADE_EVENT m_trade_event; // Account trading event CEvent m_event_instance; // Event object for searching by property MqlTick m_tick; // Last tick structure ulong m_position_id; // Position ID (MQL4) ENUM_ORDER_TYPE m_type_first; // Opening order type (MQL4) //--- Create a trading event depending on the order (1) status and (2) change type void CreateNewEvent(COrder* order,CArrayObj* list_history,CArrayObj* list_market,CArrayObj* list_control); void CreateNewEvent(COrderControl* order); //--- Create an event for a (1) hedging account, (2) netting account void NewDealEventHedge(COrder* deal,CArrayObj* list_history,CArrayObj* list_market); void NewDealEventNetto(COrder* deal,CArrayObj* list_history,CArrayObj* list_market); //--- Select from the list and return the list of (1) market pending orders, (2) open positions CArrayObj* GetListMarketPendings(CArrayObj* list); CArrayObj* GetListPositions(CArrayObj* list); //--- Select from the list and return the list of historical (1) closed orders, //--- (2) removed pending orders, (3) deals, (4) all closing orders CArrayObj* GetListHistoryPositions(CArrayObj* list); CArrayObj* GetListHistoryPendings(CArrayObj* list); CArrayObj* GetListDeals(CArrayObj* list); CArrayObj* GetListCloseByOrders(CArrayObj* list); //--- Return the list of (1) all position orders by its ID, (2) all position deals by its ID //--- (3) all market entry deals by position ID, (4) all market exit deals by position ID, //--- (5) all position reversal deals by position ID CArrayObj* GetListAllOrdersByPosID(CArrayObj* list,const ulong position_id); CArrayObj* GetListAllDealsByPosID(CArrayObj* list,const ulong position_id); CArrayObj* GetListAllDealsInByPosID(CArrayObj* list,const ulong position_id); CArrayObj* GetListAllDealsOutByPosID(CArrayObj* list,const ulong position_id); CArrayObj* GetListAllDealsInOutByPosID(CArrayObj* list,const ulong position_id); //--- Return the total volume of all deals (1) IN, (2) OUT of the position by its ID double SummaryVolumeDealsInByPosID(CArrayObj* list,const ulong position_id); double SummaryVolumeDealsOutByPosID(CArrayObj* list,const ulong position_id); //--- Return the (1) first, (2) last and (3) closing order from the list of all position orders, //--- (4) an order by ticket, (5) market position by ID, //--- (6) the last and (7) penultimate InOut deal by position ID COrder* GetFirstOrderFromList(CArrayObj* list,const ulong position_id); COrder* GetLastOrderFromList(CArrayObj* list,const ulong position_id); COrder* GetCloseByOrderFromList(CArrayObj* list,const ulong position_id); COrder* GetHistoryOrderByTicket(CArrayObj* list,const ulong order_ticket); COrder* GetPositionByID(CArrayObj* list,const ulong position_id); //--- Return the (1) control order by ticket, (2) opening order type by position ticket (MQL4) COrderControl* GetOrderControlByTicket(CArrayObj* list,const ulong ticket); ENUM_ORDER_TYPE GetTypeFirst(CArrayObj* list,const ulong ticket); //--- Return the flag of the event object presence in the event list bool IsPresentEventInList(CEvent* compared_event); //--- Existing order/position change event handler void OnChangeEvent(CArrayObj* list_changes,const int index); public:
在类的实体之外实现返回已平仓列表的方法:
//+------------------------------------------------------------------+ //| Select only closed positions from the list | //+------------------------------------------------------------------+ CArrayObj* CEventsCollection::GetListHistoryPositions(CArrayObj *list) { if(list.Type()!=COLLECTION_HISTORY_ID) { Print(DFUN,TextByLanguage("Ошибка. Список не является списком исторической коллекции","Error. The list is not a list of the history collection")); return NULL; } CArrayObj* list_orders=CSelect::ByOrderProperty(list,ORDER_PROP_STATUS,ORDER_STATUS_HISTORY_ORDER,EQUAL); return list_orders; } //+------------------------------------------------------------------+
该方法对我们来说没什么新意。 我已经在函数论述的前面部分中阐述了类似的方法。
我们来实现按票证返回控制订单的方法,并修改按票证返回控制订单类型的方法:
//+------------------------------------------------------------------+ //| Return a control order by a position ticket (MQL4) | //+------------------------------------------------------------------+ COrderControl* CEventsCollection::GetOrderControlByTicket(CArrayObj *list,const ulong ticket) { if(list==NULL) return NULL; int total=list.Total(); for(int i=0;i<total;i++) { COrderControl* ctrl=list.At(i); if(ctrl==NULL) continue; if(ctrl.Ticket()==ticket) return ctrl; } return NULL; } //+------------------------------------------------------------------+ //| Return an opening order type by a position ticket (MQL4) | //+------------------------------------------------------------------+ ENUM_ORDER_TYPE CEventsCollection::GetTypeFirst(CArrayObj* list,const ulong ticket) { if(list==NULL) return WRONG_VALUE; COrderControl* ctrl=this.GetOrderControlByTicket(list,ticket); if(ctrl==NULL) return WRONG_VALUE; return (ENUM_ORDER_TYPE)ctrl.TypeOrder(); } //+------------------------------------------------------------------+
与先前研究的类似方法一样,按票证返回控制订单的方法很简单。 它接收控制订单列表,和我们想要获得控制订单的持仓的票据。
从列表中获取订单,并在列表范围的循环中将其与传递给方法的票证进行比较。
如果票证相等,则返回指向控制订单的指针,否则返回 NULL。
GetTypeFirst() 方法返回控制订单类型,该控制订单类型由先前循环中发现的与所传递参数匹配的订单组成。
当检测到这样的订单时,返回其类型。
现在我们有了按常委票据返回控制订单的方法,我们可以从 GetTypeFirst() 方法中删除搜索循环。 这正是我刚完成的。 现在,在方法中,我们利用
GetOrderControlByTicket() 方法按仓位票据获取控制订单。 如果成功获得(非
NULL),则返回获取订单的类型,否则返回
-1。
现在我们可以将 MQL4 的平仓处理逻辑添加到事件集合更新方法中:
//+------------------------------------------------------------------+ //| Update the event list | //+------------------------------------------------------------------+ void CEventsCollection::Refresh(CArrayObj* list_history, CArrayObj* list_market, CArrayObj* list_changes, CArrayObj* list_control, const bool is_history_event, const bool is_market_event, const int new_history_orders, const int new_market_pendings, const int new_market_positions, const int new_deals, const double changed_volume) { //--- Exit if the lists are empty if(list_history==NULL || list_market==NULL) return; //--- If the event is in the market environment if(is_market_event) { //--- if the order properties were changed int total_changes=list_changes.Total(); if(total_changes>0) { for(int i=total_changes-1;i>=0;i--) { this.OnChangeEvent(list_changes,i); } } //--- if the number of placed pending orders increased (MQL5, MQL4) if(new_market_pendings>0) { //--- Receive the list of the newly placed pending orders CArrayObj* list=this.GetListMarketPendings(list_market); if(list!=NULL) { //--- Sort the new list by order placement time list.Sort(SORT_BY_ORDER_TIME_OPEN); //--- Take the number of orders equal to the number of newly placed ones from the end of the list in a loop (the last N events) int total=list.Total(), n=new_market_pendings; for(int i=total-1; i>=0 && n>0; i--,n--) { //--- Receive an order from the list, if this is a pending order, set a trading event COrder* order=list.At(i); if(order!=NULL && order.Status()==ORDER_STATUS_MARKET_PENDING) this.CreateNewEvent(order,list_history,list_market,list_control); } } } #ifdef __MQL4__ //--- If the number of positions increased (MQL4) if(new_market_positions>0) { //--- Get the list of open positions CArrayObj* list=this.GetListPositions(list_market); if(list!=NULL) { //--- Sort the new list by a position open time list.Sort(SORT_BY_ORDER_TIME_OPEN); //--- Take the number of positions equal to the number of newly placed open positions from the end of the list in a loop (the last N events) int total=list.Total(), n=new_market_positions; for(int i=total-1; i>=0 && n>0; i--,n--) { //--- Receive a position from the list. If this is a position, search for opening order data and set a trading event COrder* position=list.At(i); if(position!=NULL && position.Status()==ORDER_STATUS_MARKET_POSITION) { //--- Find an order and set (1) a type of an order that led to opening a position and a (2) position ID this.m_type_first=this.GetTypeFirst(list_control,position.Ticket()); this.m_position_id=position.Ticket(); this.CreateNewEvent(position,list_history,list_market,list_control); } } } } //--- If the number of positions decreased or a position is closed partially (MQL4) else if(new_market_positions<0 || (new_market_positions==0 && changed_volume<0 && new_history_orders>0 && new_market_pendings>WRONG_VALUE)) { //--- Get the list of closed positions CArrayObj* list=this.GetListHistoryPositions(list_history); if(list!=NULL) { //--- Sort the new list by position close time list.Sort(SORT_BY_ORDER_TIME_CLOSE); //--- Take the number of positions equal to the number of newly closed positions from the end of the list in a loop (the last N events) int total=list.Total(), n=new_history_orders; for(int i=total-1; i>=0 && n>0; i--,n--) { //--- Receive an order from the list. If this is a position, look for data of an opening order and set a trading event COrder* position=list.At(i); if(position!=NULL && position.Status()==ORDER_STATUS_HISTORY_ORDER) { //--- If there is a control order of a closed position COrderControl* ctrl=this.GetOrderControlByTicket(list_control,position.Ticket()); if(ctrl!=NULL) { //--- Set an (1) order type that led to a position opening, (2) position ID and create a position closure event this.m_type_first=(ENUM_ORDER_TYPE)ctrl.TypeOrder(); this.m_position_id=position.Ticket(); this.CreateNewEvent(position,list_history,list_market,list_control); } } } } } #endif } //--- If an event in an account history if(is_history_event) { //--- If the number of historical orders increased (MQL5, MQL4) if(new_history_orders>0) { //--- Get the list of newly removed pending orders CArrayObj* list=this.GetListHistoryPendings(list_history); if(list!=NULL) { //--- Sort the new list by order removal time list.Sort(SORT_BY_ORDER_TIME_CLOSE); //--- Take the number of orders equal to the number of newly removed ones from the end of the list in a loop (the last N events) int total=list.Total(), n=new_history_orders; for(int i=total-1; i>=0 && n>0; i--,n--) { //--- Receive an order from the list. If this is a removed pending order without a position ID, //--- this is an order removal - set a trading event COrder* order=list.At(i); if(order!=NULL && order.Status()==ORDER_STATUS_HISTORY_PENDING && order.PositionID()==0) this.CreateNewEvent(order,list_history,list_market,list_control); } } } //--- If the number of deals increased (MQL5) #ifdef __MQL5__ if(new_deals>0) { //--- Receive the list of deals only CArrayObj* list=this.GetListDeals(list_history); if(list!=NULL) { //--- Sort the new list by deal time list.Sort(SORT_BY_ORDER_TIME_OPEN); //--- Take the number of deals equal to the number of new ones from the end of the list in a loop (the last N events) int total=list.Total(), n=new_deals; for(int i=total-1; i>=0 && n>0; i--,n--) { //--- Receive a deal from the list and set a trading event COrder* order=list.At(i); if(order!=NULL) this.CreateNewEvent(order,list_history,list_market); } } } #endif } } //+------------------------------------------------------------------+
处理平仓非常清晰透明。 它的详述在清单中进行了注释,故此没有必要对其逻辑进行详细说明,代码注释已经足够详尽。
由于现在又将另一个列表传递给调用创建新事件方法的第一种形式, 针对 MQL5,也需在 CEventsCollection
事件集合类的 Refresh() 方法中
添加传递控制订单列表来调用方法:
//--- If the number of deals increased (MQL5) #ifdef __MQL5__ if(new_deals>0) { //--- Receive the list of deals only CArrayObj* list=this.GetListDeals(list_history); if(list!=NULL) { //--- Sort the new list by deal time list.Sort(SORT_BY_ORDER_TIME_OPEN); //--- Take the number of deals equal to the number of new ones from the end of the list in a loop (the last N events) int total=list.Total(), n=new_deals; for(int i=total-1; i>=0 && n>0; i--,n--) { //--- Receive a deal from the list and set a trading event COrder* order=list.At(i); if(order!=NULL) this.CreateNewEvent(order,list_history,list_market,list_control); } } } #endif
现在,我们在事件创建方法中为第一种调用形式添加创建平仓事件的代码:
CEventsCollection::CreateNewEvent(COrder* order,CArrayObj* list_history,CArrayObj* list_market,CArrayObj* list_control).
由于该方法非常庞大,我们只需查看为 MQL4 创建平仓事件的代码:
//--- Position closed (__MQL4__) if(status==ORDER_STATUS_HISTORY_ORDER) { //--- Set the "position closed" trading event code this.m_trade_event_code=TRADE_EVENT_FLAG_POSITION_CLOSED; //--- Set the "request executed in full" reason ENUM_EVENT_REASON reason=EVENT_REASON_DONE; //--- If the closure by StopLoss flag is set for an order, a position is closed by StopLoss if(order.IsCloseByStopLoss()) { //--- set the "closure by StopLoss" reason reason=EVENT_REASON_DONE_SL; //--- add the StopLoss closure flag to the event code this.m_trade_event_code+=TRADE_EVENT_FLAG_SL; } //--- If the closure by TakeProfit flag is set for an order, a position is closed by TakeProfit if(order.IsCloseByTakeProfit()) { //--- set the "closure by TakeProfit" reason reason=EVENT_REASON_DONE_TP; //--- add the TakeProfit closure flag to the event code this.m_trade_event_code+=TRADE_EVENT_FLAG_TP; } //--- If an order has the property with an inherited order filled, a position is closed partially if(order.TicketTo()>0) { //--- set the "partial closure" reason reason=EVENT_REASON_DONE_PARTIALLY; //--- add the partial closure flag to the event code this.m_trade_event_code+=TRADE_EVENT_FLAG_PARTIAL; } //--- Check closure by an opposite position COrder* order_close_by=this.GetCloseByOrderFromList(list_history,order.Ticket()); //--- Declare the variables of the opposite order properties and initialize them with the values of the current closed position ENUM_ORDER_TYPE close_by_type=this.m_type_first; double close_by_volume=order.Volume(); ulong close_by_ticket=order.Ticket(); long close_by_magic=order.Magic(); string close_by_symbol=order.Symbol(); //--- If the list of historical orders features an order with a closed position ID, the position is closed by an opposite one if(order_close_by!=NULL) { //--- Fill in the properties of an opposite closing order using data on the opposite position properties close_by_type=(ENUM_ORDER_TYPE)order_close_by.TypeOrder(); close_by_ticket=order_close_by.Ticket(); close_by_magic=order_close_by.Magic(); close_by_symbol=order_close_by.Symbol(); close_by_volume=order_close_by.Volume(); //--- set the "close by" reason reason=EVENT_REASON_DONE_BY_POS; //--- add the close by flag to the event code this.m_trade_event_code+=TRADE_EVENT_FLAG_BY_POS; //--- Take data on (1) closed and (2) opposite positions from the list of control orders //--- (in this list, the properties of two opposite positions remain the same as before the closure) COrderControl* ctrl_closed=this.GetOrderControlByTicket(list_control,order.Ticket()); COrderControl* ctrl_close_by=this.GetOrderControlByTicket(list_control,close_by_ticket); double vol_closed=0; double vol_close_by=0; //--- If no errors detected when receiving these two opposite orders if(ctrl_closed!=NULL && ctrl_close_by!=NULL) { //--- Calculate closed volumes of a (1) closed and (2) an opposite positions vol_closed=ctrl_closed.Volume()-order.Volume(); vol_close_by=vol_closed-close_by_volume; //--- If a position is closed partially (the previous volume exceeds the currently closed one) if(ctrl_closed.Volume()>order.Volume()) { //--- add the partial closure flag to an event code this.m_trade_event_code+=TRADE_EVENT_FLAG_PARTIAL; //--- set the "partial closure" reason reason=EVENT_REASON_DONE_PARTIALLY_BY_POS; } } } //--- Create the position closure event CEvent* event=new CEventPositionClose(this.m_trade_event_code,order.Ticket()); if(event!=NULL && order.PositionByID()==0) { event.SetProperty(EVENT_PROP_TIME_EVENT,order.TimeClose()); // Event time event.SetProperty(EVENT_PROP_REASON_EVENT,reason); // Event reason (from the ENUM_EVENT_REASON enumeration) event.SetProperty(EVENT_PROP_TYPE_DEAL_EVENT,close_by_type); // Event deal type event.SetProperty(EVENT_PROP_TICKET_DEAL_EVENT,order.Ticket()); // Event deal ticket event.SetProperty(EVENT_PROP_TYPE_ORDER_EVENT,close_by_type); // Type of the order that triggered an event deal (the last position order) event.SetProperty(EVENT_PROP_TYPE_ORDER_POSITION,this.m_type_first); // Type of an order that triggered a position deal (the first position order) event.SetProperty(EVENT_PROP_TICKET_ORDER_EVENT,close_by_ticket); // Ticket of an order, based on which an event deal is opened (the last position order) event.SetProperty(EVENT_PROP_TICKET_ORDER_POSITION,order.Ticket()); // Ticket of an order, based on which a position deal is opened (the first position order) event.SetProperty(EVENT_PROP_POSITION_ID,this.m_position_id); // Position ID event.SetProperty(EVENT_PROP_POSITION_BY_ID,close_by_ticket); // Opposite position ID event.SetProperty(EVENT_PROP_MAGIC_BY_ID,close_by_magic); // Opposite position magic number event.SetProperty(EVENT_PROP_TYPE_ORD_POS_BEFORE,order.TypeOrder()); // Position order type before direction changed event.SetProperty(EVENT_PROP_TICKET_ORD_POS_BEFORE,order.Ticket()); // Position order ticket before direction changed event.SetProperty(EVENT_PROP_TYPE_ORD_POS_CURRENT,order.TypeOrder()); // Current position order type event.SetProperty(EVENT_PROP_TICKET_ORD_POS_CURRENT,order.Ticket()); // Current position order ticket event.SetProperty(EVENT_PROP_PRICE_OPEN_BEFORE,order.PriceOpen()); // Order price before modification< event.SetProperty(EVENT_PROP_PRICE_SL_BEFORE,order.StopLoss()); // StopLoss before modification event.SetProperty(EVENT_PROP_PRICE_TP_BEFORE,order.TakeProfit()); // TakeProfit before modification event.SetProperty(EVENT_PROP_PRICE_EVENT_ASK,this.m_tick.ask); // Ask price during an event event.SetProperty(EVENT_PROP_PRICE_EVENT_BID,this.m_tick.bid); // Bid price during an event event.SetProperty(EVENT_PROP_MAGIC_ORDER,order.Magic()); // Order/deal/position magic number event.SetProperty(EVENT_PROP_TIME_ORDER_POSITION,order.TimeOpen()); // Time of an order, based on which a position deal is opened (the first position order) event.SetProperty(EVENT_PROP_PRICE_EVENT,order.PriceOpen()); // Event price event.SetProperty(EVENT_PROP_PRICE_OPEN,order.PriceOpen()); // Order/deal/position open price event.SetProperty(EVENT_PROP_PRICE_CLOSE,order.PriceClose()); // Order/deal/position close price event.SetProperty(EVENT_PROP_PRICE_SL,order.StopLoss()); // StopLoss position price event.SetProperty(EVENT_PROP_PRICE_TP,order.TakeProfit()); // TakeProfit position price event.SetProperty(EVENT_PROP_VOLUME_ORDER_INITIAL,order.Volume()); // Requested order volume event.SetProperty(EVENT_PROP_VOLUME_ORDER_EXECUTED,order.Volume()-order.VolumeCurrent()); // Executed order volume event.SetProperty(EVENT_PROP_VOLUME_ORDER_CURRENT,order.VolumeCurrent()); // Remaining (unexecuted) order volume event.SetProperty(EVENT_PROP_VOLUME_POSITION_EXECUTED,order.Volume()); // Executed position volume event.SetProperty(EVENT_PROP_PROFIT,order.Profit()); // Profit event.SetProperty(EVENT_PROP_SYMBOL,order.Symbol()); // Order symbol event.SetProperty(EVENT_PROP_SYMBOL_BY_ID,close_by_symbol); // Opposite position symbol //--- Set control program chart ID, decode the event code and set the event type event.SetChartID(this.m_chart_id); event.SetTypeEvent(); //--- Add the event object if it is not in the list if(!this.IsPresentEventInList(event)) { this.m_list_events.InsertSort(event); //--- Send a message about the event and set the value of the last trading event event.SendEvent(); this.m_trade_event=event.TradeEvent(); } //--- If the event is already present in the list, remove a new event object and display a debugging message else { ::Print(DFUN_ERR_LINE,TextByLanguage("Такое событие уже есть в списке","This event is already in the list.")); delete event; } } } #endif
鉴于我们已经在前面的文章中论述了创建事件的逻辑,因此我们仅需简要地提一下:首先,将事件代码设置为“平仓”,将事件原因设置为“请求完全执行”。 接着查看已平订单的各种属性,根据这些属性向事件代码添加必要的标志,并在必要时更改事件原因。 在定义由逆向仓位平仓时,我们使用平仓的控制订单数据。
这些数据为我们提供了在合并平仓之前逆向订单的完整信息,可令我们定义订单类型及其交易量,以及找出是哪一笔发起了平仓。
然后,所有收集的数据都记录在事件属性中,并创建一个新的平仓事件。
我已改进了返回 MQL4 所有仓位平仓单列表的方法:
//+------------------------------------------------------------------+ //| Return the list of all closing CloseBy orders from the list | //+------------------------------------------------------------------+ CArrayObj* CEventsCollection::GetListCloseByOrders(CArrayObj *list) { if(list.Type()!=COLLECTION_HISTORY_ID) { Print(DFUN,TextByLanguage("Ошибка. Список не является списком исторической коллекции","Error. The list is not a list of history collection")); return NULL; } #ifdef __MQL5__ CArrayObj* list_orders=CSelect::ByOrderProperty(list,ORDER_PROP_TYPE,ORDER_TYPE_CLOSE_BY,EQUAL); #else CArrayObj* list_orders=CSelect::ByOrderProperty(list,ORDER_PROP_POSITION_BY_ID,0,NO_EQUAL); #endif return list_orders; } //+------------------------------------------------------------------+
对于 MQL5,我们只从历史订单列表中选择 ORDER_TYPE_CLOSE_BY 类型的订单。 鉴于 MQL4
中没有此类订单,我们仅选择逆向仓位 ID 属性的订单,更具体地说,订单的此属性不等于零。
针对 MQL4,返回最后平仓订单的方法也得以改进:
//+------------------------------------------------------------------+ //| Return the last closing order | //| from the list of all position orders | //+------------------------------------------------------------------+ COrder* CEventsCollection::GetCloseByOrderFromList(CArrayObj *list,const ulong position_id) { #ifdef __MQL5__ CArrayObj* list_orders=this.GetListAllOrdersByPosID(list,position_id); list_orders=CSelect::ByOrderProperty(list_orders,ORDER_PROP_TYPE,ORDER_TYPE_CLOSE_BY,EQUAL); if(list_orders==NULL || list_orders.Total()==0) return NULL; list_orders.Sort(SORT_BY_ORDER_TIME_OPEN); #else CArrayObj* list_orders=CSelect::ByOrderProperty(list,ORDER_PROP_POSITION_BY_ID,position_id,EQUAL); if(list_orders==NULL || list_orders.Total()==0) return NULL; list_orders.Sort(SORT_BY_ORDER_TIME_CLOSE); #endif COrder* order=list_orders.At(list_orders.Total()-1); return(order!=NULL ? order : NULL); } //+------------------------------------------------------------------+
在 MQL5 中,我们可以随时获取属于某笔仓位的最后一笔订单。 然而,MQL4 不是这种情况。 所以,针对 MQL4 的情况下,我决定返回 逆向仓位订单(如果存在),或 NULL(如果某笔仓位未由逆向仓位平仓)。 当仓位由逆向仓位平仓时,这可令我们部分实现获取平仓订单。
所有这些都是为 MQL4 定义平仓所需的更改和改进。
在下面附带的文件中可找到所有类的完整清单。
测试
为了执行测试,我们将利用来自前一篇文章的测试 EA TestDoEasyPart10.mq4,其位于 \MQL4\Experts\TestDoEasy\Part10,将其保存至新文件夹 \MQL4\Experts\TestDoEasy\Part11 之下,并命名为 TestDoEasyPart11.mq4。
由于我们从可能的订单和成交排序标准的 ENUM_SORT_ORDERS_MODE 枚举中删除了时间常数(以毫秒为单位),因此我们需要在 EA 的按下挂单删除按钮的处理程序中将
list.Sort(SORT_BY_ORDER_TIME_OPEN_MSC);
替换为按时间来排序:
//--- If the BUTT_DELETE_PENDING button is pressed: Remove the first pending order else if(button==EnumToString(BUTT_DELETE_PENDING)) { //--- Get the list of all orders CArrayObj* list=engine.GetListMarketPendings(); if(list!=NULL) { //--- Sort the list by placement time list.Sort(SORT_BY_ORDER_TIME_OPEN); int total=list.Total(); //--- In the loop from the position with the most amount of time for(int i=total-1;i>=0;i--) { COrder* order=list.At(i); if(order==NULL) continue; //--- delete the order by its ticket #ifdef __MQL5__ trade.OrderDelete(order.Ticket()); #else PendingOrderDelete(order.Ticket()); #endif } } }
编译 EA 并将 止损点数 和 止盈点数 输入设置为零,以便在没有停止订单的情况下平仓。 然后在测试器中启动 EA,开仓,然后将其部分平仓。
接着,下一笔挂单并将其删除:
现在,部分平仓和挂单删除的事件被定义为单独的事件。
再次启动 EA 并单击观察事件定义的按钮:
正如我们所见,事件定义正确。 定义了一个平仓事件,止损价位和挂单价格的修改也一并进行了跟踪。
下一步是什么?
在本文中,我们完成了现有函数库功能的转换,以便与 MQL4 兼容。 在即将发表的文章中,我们将创建新的“帐户”和“品种”对象,它们的集合和事件。
下面附有当前版本函数库的所有文件,以及测试 EA 文件供您测试和下载。
请在评论中留下您的问题、意见和建议。
返回目录
系列中的前几篇文章:
第一部分 概念,数据管理。
第二部分
历史订单和成交集合。
第三部分 在场订单和持仓集合,安排搜索。
第四部分
交易事件, 概念。
第五部分 交易事件类和集合。 将事件发送至程序。
第六部分
净持帐户事件。
第七部分 StopLimit 挂单激活事件,为订单和持仓修改事件准备功能。
第八部分
订单和持仓修改事件。
第九部分 与 MQL4 的兼容性 - 准备数据。
第十部分
与 MQL4 的兼容性 - 开仓和激活挂单事件。