Discussion of article "MQL5 Cookbook: The History of Deals And Function Library for Getting Position Properties"

 

New article MQL5 Cookbook: The History of Deals And Function Library for Getting Position Properties is published:

It is time to briefly summarize the information provided in the previous articles on position properties. In this article, we will create a few additional functions to get the properties that can only be obtained after accessing the history of deals. We will also get familiar with data structures that will allow us to access position and symbol properties in a more convenient way.

MQL5 Cookbook: The History of Deals And Function Library for Getting Position Properties

Author: Anatoli Kazharski

 
The article is good, I liked it. It often happens that the current position consists of several deals, and besides the first and the last one, I would like to know the parameters of 2, 3, etc. deals. Such a function would enrich the article.
 
paladin800:
The article is good, I liked it. It often happens that the current position consists of several deals, and besides the first and the last one, I would like to know the parameters of 2, 3, etc. deals. Such a feature would enrich the article.
Let it be a homework for now. But I will try to write something more on this topic later.
 

Question to the author of the article.

How can I get the ID of the position in which the trade was involved immediately after the position was opened?



 
denkir:

Question to the author of the article.

How to get the identifier of the position, in which the trade was involved, immediately after the position was opened?

Select the position using the PositionSelect() function and get its identifier using the PositionGetInteger() function and the mql5 identifier - POSITION_IDENTIFIER.

Example from Help:

//+------------------------------------------------------------------+
//| Trade function|
//+------------------------------------------------------------------+
void OnTrade()
  {
//--- check if the position is available and print the time of its change
   if(PositionSelect(_Symbol))
     {     
//--- get position identifier for further work with it
      ulong position_ID=PositionGetInteger(POSITION_IDENTIFIER);
      Print(_Symbol," postion #",position_ID);
//--- get the time of position formation in milliseconds since 01.01.1970
      long create_time_msc=PositionGetInteger(POSITION_TIME_MSC);
      PrintFormat("Position #%d  POSITION_TIME_MSC = %i64 milliseconds => %s",position_ID,
                  create_time_msc,TimeToString(create_time_msc/1000));
//--- get the time of the last position change in seconds since 01.01.1970
      long update_time_sec=PositionGetInteger(POSITION_TIME_UPDATE);
      PrintFormat("Position #%d  POSITION_TIME_UPDATE = %i64 seconds => %s",
                  position_ID,update_time_sec,TimeToString(update_time_sec));
//--- get the time of the last position change in milliseconds since 01.01.1970
      long update_time_msc=PositionGetInteger(POSITION_TIME_UPDATE_MSC);
      PrintFormat("Position #%d  POSITION_TIME_UPDATE_MSC = %i64 milliseconds => %s",
                  position_ID,update_time_msc,TimeToString(update_time_msc/1000));
     }
//---
  }
 
tol64:

Select a position using the PositionSelect() function and get its identifier using the PositionGetInteger() function and the mql5 identifier - POSITION_IDENTIFIER.

Example from Help:

Thanks, tol64! In principle, you are right.

And I apologise that the first question was brief...despite the fact that I gave a reference to the position identifier.

The problem is like this. It is necessary, after a position has been opened, to catch this opening in the history of transactions with the help of CDealInfo class using the method ::PositionId(). And preferably with the smallest time gap between the moment of opening and the moment when a DEAL_ENTRY_IN or DEAL_ENTRY_INOUT type deal appears in the history.

Is it possible to do it this way? Not through the list of active positions, but through the list of deals in the history...
 
denkir:

Thank you, tol64! In principle, the truth is yours.

And I apologise for the brevity of the first question...despite the fact that I gave a link to the position ID.

The problem is like this. After a position has been opened, it is necessary to catch this opening in the history of transactions using the CDealInfo class with the ::PositionId() method. And preferably with the smallest time gap between the moment of opening and the moment when a deal of DEAL_ENTRY_IN or DEAL_ENTRY_INOUT type appears in the history.

Is it possible to do it this way? Not through the list of active positions, but through the list of trades in history...

In my opinion, tracking the event in OnTrade(), as in the example above, is quite suitable for this. But I would think that suddenly a position can't be selected by a trade event, and the next one won't be soon. Then you need to track the environment by timer. Even milliseconds can be set now.

Well, or instead of tracking the open position, scan the history of trades (number of trades). We remember the previous one and compare it with the current one. You can also do it in the timer. There are a lot of variants. It all depends on the specific task.

 

Anatoly, I have encountered a problem here. Please share your experience. Either there is a bug when working with functions, or I am going "wrong". But I think I have done everything correctly....

I have a question about function variants where the second one does not "work".

For example:

1)

double  HistoryOrderGetDouble(
   ulong                       ticket_number,     // ticket
   ENUM_ORDER_PROPERTY_DOUBLE  property_id        // property identifier
   );

2)

bool  HistoryOrderGetDouble(
   ulong                       ticket_number,     // ticket
   ENUM_ORDER_PROPERTY_DOUBLE  property_id,       // property identifier
   double&                     double_var         // here we take the value of the property
   );

I have attached the source code.

Files:
 
denkir:

Anatoly, I have encountered a problem here. Please share your experience. Either there is a bug when working with functions, or I am going "wrong". But I think I have done everything correctly....

The question is about function variants where the second one does not "work".

...

What do you see in the log? I have no problems when running the script.
 
tol64:
What do you see in the log? I don't see any problems when running the script.

I see that with the default settings of the script there is an entry like "Failure to return HistoryOrderGetDouble() function".

I.e. the boolean version of the function does not get the property value.

 
denkir:

I see that with default script settings there is a record like "Failure to return HistoryOrderGetDouble() function".

That is, the boolean version of the function does not get the property value.

Everything passes successfully for me. Look at everything in more detail (output to the log) to find out the reason. Number of orders, tickets, error number, etc.