Discussion of article "Library for easy and quick development of MetaTrader programs (part V): Classes and collection of trading events, sending events to the program"

 

New article Library for easy and quick development of MetaTrader programs (part V): Classes and collection of trading events, sending events to the program has been published:

In the previous articles, we started creating a large cross-platform library simplifying the development of programs for MetaTrader 5 and MetaTrader 4 platforms. In the fourth part, we tested tracking trading events on the account. In this article, we will develop trading event classes and place them to the event collections. From there, they will be sent to the base object of the Engine library and the control program chart.

Now we can compile the EA and launch it in the tester. When clicking the buttons, short two-line messages about occurring account events are displayed in the tester journal.


Entries from the EA event handler are not displayed in the journal since they work outside of the tester. If click on the EA buttons on a demo account, three lines are displayed in the terminal journal: two lines from the method for displaying short messages of the CEvent class and another one — from the EA's OnChartEvent() handler.

Below is a sample of displaying a message in the journal when placing and removing a pending order:

- Pending order placed: 2019.04.05 23:19:55.248 -                                                              
EURUSD 0.10 Sell Limit #375419507 at price 1.14562                                                             
OnChartEvent: id=1001, event=TRADE_EVENT_PENDING_ORDER_PLASED, lparam=375419507, dparam=1.14562, sparam=EURUSD 
- Pending order removed: 2019.04.05 23:19:55.248 -                                                             
EURUSD 0.10 Sell Limit #375419507 at price 1.14562                                                             
OnChartEvent: id=1002, event=TRADE_EVENT_PENDING_ORDER_REMOVED, lparam=375419507, dparam=1.14562, sparam=EURUSD

Author: Artyom Trishkin

Library for easy and quick development of MetaTrader programs (part V): Classes and collection of trading events, sending events to the program
Library for easy and quick development of MetaTrader programs (part V): Classes and collection of trading events, sending events to the program
  • www.mql5.com
In the previous articles, we started creating a large cross-platform library simplifying the development of programs for MetaTrader 5 and MetaTrader 4 platforms. In the fourth part, we tested tracking trading events on the account. In this article, we will develop trading event classes and place them to the event collections. From there, they...
 

Hello Artyom, congratulations for the great job! Following the description in the text, it seems that function CHistoryCollection::OrderSearch(…) may have a break missing.

The for loop always complete all iterations from start-1 to 0, whether it finds the “lost order” or not.

Perhaps, it would be more efficient to include a break after finding the “lost order”:

 

ulong CHistoryCollection::OrderSearch(const int start,ENUM_ORDER_TYPE &order_type) 
  { 
   ulong order_ticket=0; 
   for(int i=start-1;i>=0;i--) 
     { 
      ulong ticket=::HistoryOrderGetTicket(i); 
      if(ticket==0) 
         continue; 
      ENUM_ORDER_TYPE type=(ENUM_ORDER_TYPE)::HistoryOrderGetInteger(ticket,ORDER_TYPE); 
      if(this.IsPresentOrderInList(ticket,type)) 
         continue; 
      order_ticket=ticket; 
      order_type=type; 
      break; 
     } 
   return order_ticket; 
  } 

 

What do you think?

 

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of...
 
Alvaro Arioni :

Hello Artyom, congratulations for the great job! Following the description in the text, it seems that function CHistoryCollection::OrderSearch(…) may have a break missing.

The for loop always complete all iterations from start-1 to 0 , whether it finds the “lost order” or not.

Perhaps, it would be more efficient to include a break after finding the “lost order”:

 

...

 

What do you think?

 

There may be more than one lost order

 
Alvaro Arioni:

Please edit your post and

use the code button (Alt+S) when pasting code

 
Artyom Trishkin:

There may be more than one lost order

OK, but the function is still returning just one of the lost orders, that which is closest to zero.

In this case, wouldn’t be the same iterating from 0 to start-1 with a break?


ulong CHistoryCollection::OrderSearch(const int start,ENUM_ORDER_TYPE &order_type) 
  { 
   ulong order_ticket=0; 
   for(int i=0; i < start ;i++) 
     { 
      ulong ticket=::HistoryOrderGetTicket(i); 
      if(ticket==0) 
         continue; 
      ENUM_ORDER_TYPE type=(ENUM_ORDER_TYPE)::HistoryOrderGetInteger(ticket,ORDER_TYPE); 
      if(this.IsPresentOrderInList(ticket,type)) 
         continue; 
      order_ticket=ticket; 
      order_type=type; 
      break; 
     } 
   return order_ticket; 
  } 

I know that it is just a detail, but I thought this could be a little improvement, especially in case of a too big history of orders.

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
Alvaro Arioni :

OK, but the function is still returning just one of the lost orders, that which is closest to zero.

In this case, wouldn’t be the same iterating from 0 to start-1 with a break?


I know that it is just a detail, but I thought this could be a little improvement, especially in case of a too big history of orders.

Ok, thanks, I'll check and test everything.

It may well be that "break" will come in handy.

Reason: