OrderSend vs OrderSendAsynch - page 2

 
Narek Kamalyan:
So I general it is going to Sleep() until respond from server will confirm position and the code will run forward. I am right?

If you wanna take the risk that your EA freezes - yes. 

No!

 
Alain Verleyen:
It doesn't make a difference because there is no synchronous function in mql5. OrderSend is NOT synchronous even if Metaquotes doesn't want to recognize it.

Hi Alain,

In fact, the two functions have differences. The OrderSend() function will always return 'true' when the trade server has received the order for processing, and as result, the trader server fills the order ticket (field 'order') in the MqlTradeResult structure of the function. The mql programs have to use this order ticket to keep track of its processing. The OrderSendAsync() function will always return 'true' when the terminal successfully sent the request (through the internet port of the client terminal), at this point, there is no guarantee that the trade server has received the order, the positive result only indicates that the request was sent by the thread of the EA, and as result, the terminal client (not the trade server) will fill the field 'request_id' of the MqlTradeResult structure of the function, and the running programs will have to monitor and handle the OnTradeTransaction events to match the request_id value in the MqlTradeResult structure (sent by the trade server) and also keep track the order processing.

 
MoneyCaptain:

Hi Alain,

In fact, the two functions have differences. The OrderSend() function will always return 'true' when the trade server has received the order for processing, and as result, the trader server fills the order ticket (field 'order') in the MqlTradeResult structure of the function. The mql programs have to use this order ticket to keep track of its processing. The OrderSendAsync() function will always return 'true' when the terminal successfully sent the request (through the internet port of the client terminal), at this point, there is no guarantee that the trade server has received the order, the positive result only indicates that the request was sent by the thread of the EA, and as result, the terminal client (not the trade server) will fill the field 'request_id' of the MqlTradeResult structure of the function, and the running programs will have to monitor and handle the OnTradeTransaction events to match the request_id value in the MqlTradeResult structure (sent by the trade server) and also keep track the order processing.

I know but it doesn't make much difference in practice because when OrderSend() returns you still don't have a clue about the trade (position), only about the order.
 
Alain Verleyen #:
I know but it doesn't make much difference in practice because when OrderSend() returns you still don't have a clue about the trade (position), only about the order.

Hi Alain,

I am puzzled by this:

If OrderSend is not really synchronous (it will not block the execution of the code until a response has been received), how is it possible that we can access result.retcode (MqlTradeResult) immediately after OrderSend?

The trade server may take a while to process the request and reply with a retcode, right?

-----------------

Additional rambling:

I see the trade server as, essentially, a backend server, or an API, and the OrderSend is sending an API request and will receive a response. If the code execution does not wait for a response then it cannot have the result of the request in the next line of code. In a language like JavaScript this is solved with constructs called Promises that do not block the flow of execution and can change state later on (when the request result is received) and this triggers execution of other parts of the code when that happens, but I do not see a similar concept being applied in MQL5.


UPDATE:

After some additional research, I am now convinced that SendOrder is 100% synchronous.

The problem is that the history of orders and positions is not updated synchronously on the server (while execution is blocked on the MT5 client waiting for a response to OrderSend ).

This means that if you exectute OrderSend and immediately after that you request information about pending orders or open positions this information will very likely not be fully up-to-date.

That is why some people insist that OrderSend is not synchronous.

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Result Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Result Structure
  • www.mql5.com
Trade Request Result Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: