Download MetaTrader 5

Measuring the execution time & Get results from OrderSendAsync

To add comments, please log in or register
Wjack07
69
Wjack07 2014.05.21 16:40 

Hi:

I have two questions here, and cannot find the solution (here and other places), hope some people can provide related information.
It is about both MT4 & MT5

[1].
Basically, I want to monitor the slippage of every deals

In MT4, only one order at one time can be placed (single thread, and no OrderSendAsync function), so we can wait until the result retcode becomes "DONE", and then check its deal price, so I can compare with my order price.
In MT5, to speed up the order placing for multiple orders, 
OrderSendAsync can be used, but I found other information in result from OrderSendAsync (such as deal price etc) is always empty.
Does that mean OrderSendAsync doesnt send the feedback ??

If so, one can only monitor the results by checking HistoryDeal, but it is still unknown when to check.
The deal retcode is always 10008, and any new deal shown in the HistoryDeal might be contributed by other EA, it is not accurate to simply check new deal, and MT5 has no "deal label".

Overall I found it is difficult to track orders placed by OrderSendAsync. Is there any better solution?

[2]
I also want to measure the execution time to milliseconds order.

In MT4, when a order is placed, one can find in the "Journal" to see when the order is sent and when it is done on the server.
(well, actually it seems to be the time received on the user computer, so it is relatively not accurate.) 
In MT5, one can find the execution time in the "Journal" about the execution time for deals to milisecond.

My question is, how MT5 calculated it? and how can I code that measuring in the code, so I can do the statistic for many orders.

Many thanks
Wjack07

Alain Verleyen
Moderator
31134
Alain Verleyen 2014.05.21 16:54  

See this post of Renat, CEO of Metaquotes.

I know it's not exactly what you asked for, but maybe it can help.

Wjack07
69
Wjack07 2014.05.21 20:44  
angevoyageur:

See this post of Renat, CEO of Metaquotes.

I know it's not exactly what you asked for, but maybe it can help.

Hi, thank you for the reply.
In the post you quoted, they were trying to test MT4 & MT5 execution time.
I am happy with the sync part, and I know how to do that.

As for the Async part, they also did the tests, but those provided code (on page 3) just tested OrderSendAsync execution time on the client end, it doesnt include the time for the broker and liquidity provider's server execution.
Also, it doesnt show how to track the deal done by OrderSendAsync.

On the MT5 manual, the OnTradeTransaction is mentioned, and seems it can help to track the deal. But it does describe how in detail.
Also,(I quote from the manual) ""
A trade transaction description does not deliver all available information concerning orders, deals and positions (e.g., comments). OrderGet*, HistoryOrderGet*, HistoryDealGet* and PositionGet* functions should be used to get extended information.""

It seems one still need to use the HistoryDealGet to track the deal's slippage.
But as I mentioned earlier, it is hard to tell if that deal is the one we want to track. (There is no labelling in MT5, and this deal could came from other EAs.)

The question still remained unanswered.

Many thanks
Wjack07 

Alain Verleyen
Moderator
31134
Alain Verleyen 2014.05.21 20:53  

Is this article help you to understand how to "track" a deal ?

https://www.mql5.com/en/articles/125

The Optimal Method for Calculation of Total Position Volume by Specified Magic Number
The Optimal Method for Calculation of Total Position Volume by Specified Magic Number
  • 2010.07.27
  • Dmitry Fedoseev
  • www.mql5.com
The problem of calculation of the total position volume of the specified symbol and magic number is considered in this article. The proposed method requests only the minimum necessary part of the history of deals, finds the closest time when the total position was equal to zero, and performs the calculations with the recent deals. Working with global variables of the client terminal is also considered.
Wjack07
69
Wjack07 2014.05.22 12:23  
angevoyageur:

Is this article help you to understand how to "track" a deal ?

https://www.mql5.com/en/articles/125

OK I see. I didnt think of using magic number, which is essentially to label each order/deal
So what I should do is to implant a specific magic number to each of my order, and then check it when it is done.

Here I have two further question.

(1) when I use OrderSendAsync, the result retcode will only show ""order placed", rather than deal done.
So even if I can track the deal after by using magic number, I still dont know when to check.

Seems I can only wait for a certain time (maybe using Sleep), and check the deal again and again until it is done.
It is inefficient.
Is there any better solution?

(2)
is "ORDER_TIME_DONE_MSC - ORDER_TIME_SETUP_MSC" equal to "DEAL_TIME_MSC" ?
by their definition on the MT5 reference, it seems so, but I am not sure. 

Many thanks
Regards, Jhensi
Alain Verleyen
Moderator
31134
Alain Verleyen 2014.05.22 12:36  
Wjack07:
OK I see. I didnt think of using magic number, which is essentially to label each order/deal
So what I should do is to implant a specific magic number to each of my order, and then check it when it is done.

Here I have two further question.

(1) when I use OrderSendAsync, the result retcode will only show ""order placed", rather than deal done.
So even if I can track the deal after by using magic number, I still dont know when to check.

Seems I can only wait for a certain time (maybe using Sleep), and check the deal again and again until it is done.
It is inefficient.
Is there any better solution?

(2)
is "ORDER_TIME_DONE_MSC - ORDER_TIME_SETUP_MSC" equal to "DEAL_TIME_MSC" ?
by their definition on the MT5 reference, it seems so, but I am not sure. 

Many thanks
Regards, Jhensi

You can either check periodically (with OnTimer() rather than sleep), or use OnTrade() or OnTradeTransaction().

I don't know about your question (2), you have to check, and maybe let us know what you found.

Wjack07
69
Wjack07 2014.05.22 22:45  
angevoyageur:

You can either check periodically (with OnTimer() rather than sleep), or use OnTrade() or OnTradeTransaction().

I don't know about your question (2), you have to check, and maybe let us know what you found.

Hi 

I just bump into a weird problem.

I try to use OnTrade, which will be ignited every time when one trade is changed.

And then I use HistoryDealsTotal(), and then check those History deal's magic number, to track my order sent out by OrderSendAsync.

But I surprisingly found out my HistoryDealsTotal() and HistoryOrdersTotal() are always zero, even after the execution is done (the position is shown up, the journal has shown the deal is done, and the history column also show new data, but just the HistoryDealsTotal() is always 0).

Following is the code: 

 

void OnTarde(void){
      
      Print("check HistoryDealsTotal  = ",HistoryDealsTotal(), "  check  order total",HistoryOrdersTotal());  // => it turns out to be always 0
      for (int i = HistoryDealsTotal()-1;i>HistoryDealsTotal()-4;i--)                                         // => to check the latest few deals
      {    
           ticket=HistoryDealGetTicket(i);
           Print(HistoryDealGetString(ticket,DEAL_SYMBOL),"  ",HistoryDealGetInteger(ticket,DEAL_MAGIC));      // => check for the magic number
           
      }
}

 Many thanks
Wjack

Alain Verleyen
Moderator
31134
Alain Verleyen 2014.05.22 23:04  
Wjack07:

Hi 

I just bump into a weird problem.

I try to use OnTrade, which will be ignited every time when one trade is changed.

And then I use HistoryDealsTotal(), and then check those History deal's magic number, to track my order sent out by OrderSendAsync.

But I surprisingly found out my HistoryDealsTotal() and HistoryOrdersTotal() are always zero, even after the execution is done (the position is shown up, the journal has shown the deal is done, and the history column also show new data, but just the HistoryDealsTotal() is always 0).

Following is the code: 

 

 Many thanks
Wjack

Did you ever try to read the documentation ?

I also suggest you this article https://www.mql5.com/en/articles/211

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • 2011.02.01
  • MetaQuotes Software Corp.
  • www.mql5.com
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
Wjack07
69
Wjack07 2014.05.23 01:06  
angevoyageur:

Did you ever try to read the documentation ?

I also suggest you this article https://www.mql5.com/en/articles/211

Hi Thank you for your advice.

I did read it, but very briefly, so missed some important details.

My mistake was I didnt use "HistorySelect(0,TimeCurrent()); "  to get the data into the cache first.
(I still in the MT4 mind-set, which just take the results straight)

So the problem is solved now. I hope this is also helpful for other people.

Many thanks
Wjack
Wjack07
69
Wjack07 2014.05.23 13:28  

Hi:

Just to follow up one un-answer question:   "ORDER_TIME_DONE_MSC / ORDER_TIME_SETUP_MSC / DEAL_TIME_MSC".


After correctly using HistorySelect, now I can track my placed order (and wait until it is done and becomes a deal).

So I checked their ORDER_TIME_DONE_MSC / ORDER_TIME_SETUP_MSC / DEAL_TIME_MSC.

But they show the exact number, so I cannot use them to calculate how much time it took for executing.

I am using Market execution**, and I am not sure if that does matter for ORDER_TIME_DONE_MSC / ORDER_TIME_SETUP_MSC / DEAL_TIME_MSC.

Also, it is a demo account, so I am not sure if that does matter too. (maybe for the live account, the timing is more correct?)

Another related question is, when an order is placed, the Journal column will show how long it takes to be done.
What does it come from (how MT5 calculated it) ? Can we extract that number for statistic use (not by hand, but automatically recording)?

**As for why I know it is Market execution: (here is an easy way to check)
I didn't add any price to the request (I leave it 0), and it is executed successfully, so it is either Market execution or exchange execution.
(on the MT5 which my broker provide, it doesnt support exchange execution, so it should be market execution)

 

 Many thanks
Wjack

Wjack07
69
Wjack07 2014.05.24 19:18  
(In case that this post is forgotten...)
(Well, I made some mistakes about the market execution in my last post, and this post seems to be related, but actually it is another question, so even if my another post was solved, this one hasn't !)


I should simplify my question, to make it readable.

When every time a trade is executed on MT5, it will show how much time it took in the Journal, and how does MT5 calculate it?
I tried to check ORDER_TIME_DONE_MSC / ORDER_TIME_SETUP_MSC / DEAL_TIME_MSC, but they turn out to be the same number.
So basically I dont know how to get the execution time on the user side.
(without using Journal. The shown execution time in the Journal cannot be processed in the EA, or Can it?)

Many thanks
Wjack
To add comments, please log in or register