OrderSend return ticket of an order which has not completely

To add comments, please log in or register
md42
77
md42  

Hi,

I would like to ask if OrderSend cound return ticket of an order which has not completely. 

Here is an example of code : 

ulong ticket = Buy();

if(ticket > 0){
   Modify(ticket);
}


void Modify(ulong ticket)
{
   if(PositionSelectByTicket(ticket_parameter)){
      .....
   }else Print("Ticket = "+ticket+" the order was not selected");
}

ulong Buy(){......}

i observe that sometimes on real account(server) while the ticket is returned the PositionSelectByTicket couldn't be able to select that order.

Is that situation is possible or something i am missing?

Thanks in advance!


asd

Samuel Akinbowale
2754
Samuel Akinbowale  
use for loop and scan all tickets
md42
77
md42  
Samuel Akinbowale:
use for loop and scan all tickets

If the ticket cannot be selected then i think the for loop doesn't solve the problem. Because even if you find the ticket, the PositionSelectByTicket will still return false as the original version.

The ticket exist because the order was completely successfully but the PositionSelectByTicket fail to recognise that ticket.

I would like to know if someone experience that situation. 

Thank you!

whroeder1
17943
whroeder1  
md42: If the ticket cannot be selected then i think the for loop doesn't solve the problem.
EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
Alain Verleyen
36041
Alain Verleyen  
whroeder1:
EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
To avoid confusion, I removed your OrderSelect link which is irrelevant here as it's mql5 topic.
Alain Verleyen
36041
Alain Verleyen  
md42:

Hi,

I would like to ask if OrderSend cound return ticket of an order which has not completely. 

Here is an example of code : 

i observe that sometimes on real account(server) while the ticket is returned the PositionSelectByTicket couldn't be able to select that order.

Is that situation is possible or something i am missing?

Thanks in advance!


asd

1. All is possible as we have no idea what is returned by your Buy() function (are you sure it's a valid ticket ?).

2. The problem is most likely the terminal positions information are not updated yet.

3. Why do you want to modify a position you just open ?

md42
77
md42  
Alain Verleyen:

1. All is possible as we have no idea what is returned by your Buy() function (are you sure it's a valid ticket ?).

2. The problem is most likely the terminal positions information are not updated yet.

3. Why do you want to modify a position you just open ?

Thank you for the reply!

1) I am sure that is valid ticket because when the PositionSelectByTicket function fails firstly it prints that failed and secondly prints which ticket tried to select. So the ticket number which is printed ,is the same with the ticket number of the open order which opened at the same time with the message. 

2) I will read it!

3)Some types of accounts(ECN BROKERS) doesn't allow to set the stoploss and takeprofit together with the OrderSend. So in these situations we need to set these properties after the order is completely. Correct me if i am wrong.

Marco vd Heijden
Moderator
8157
Marco vd Heijden  

Yes it is correct on ECN you first open the position without TP and SL and then select the position to assign these values.

In your case:

 To make sure that you receive valid position data, it is recommended to call PositionSelect() before you access the data.

https://www.mql5.com/en/docs/trading/positionselectbyticket

Did you print GetLastError() ? what code did it give ?

Documentation on MQL5: Trade Functions / PositionSelectByTicket
Documentation on MQL5: Trade Functions / PositionSelectByTicket
  • www.mql5.com
Selects an open position to work with based on the ticket number specified in the position. If successful, returns true. Returns false if the function failed. Call GetLastError() for error details. The PositionSelectByTicket() function copies position data to the program environment. Further calls of PositionGetDouble...
Alain Verleyen
36041
Alain Verleyen  

md42:

...

3)Some types of accounts(ECN BROKERS) doesn't allow to set the stoploss and takeprofit together with the OrderSend. So in these situations we need to set these properties after the order is completely. Correct me if i am wrong.

That's not exact. You can set your sl/tp at the same time even with an ECN broker.

Alain Verleyen
36041
Alain Verleyen  
Marco vd Heijden:

Yes it is correct on ECN you first open the position without TP and SL and then select the position to assign these values.

False.

In your case:

 To make sure that you receive valid position data, it is recommended to call PositionSelect() before you access the data.

False. You don't need PostionSelect() if you are using PositionSelectByTicket().
fxsaber
10256
fxsaber  
md42:

i observe that sometimes on real account(server) while the ticket is returned the PositionSelectByTicket couldn't be able to select that order.

Is that situation is possible or something i am missing?

When sending a market order (MqlTradeRequest.action=TRADE_ACTION_DEAL), the successful result of the OrderSend() function does not mean that the order has been executed (appropriate trades have been performed). In this case, 'true' means only that the order has been successfully placed in the trading system for further execution. The trade server can fill in the deal or order field values in the returned result structure, if it is aware of these data when forming a response to an OrderSend() call. Generally, event(s) of executing trades corresponding to an order may happen after sending a response to the OrderSend() call. Therefore, for any type of a trade request, when receiving the OrderSend() execution result, we should first check the retcode trade server response code and theretcode_external external system response code (if necessary) available in the obtained result structure.

Try this demo-server to play this situation.
Documentation on MQL5: Trade Functions / OrderSend
Documentation on MQL5: Trade Functions / OrderSend
  • www.mql5.com
[in,out]  Pointer to a structure of MqlTradeResult type describing the result of trade operation in case of a successful completion (if true is returned). parameter are filled out correctly. If there are no errors, the server accepts the order for further processing. If the order is successfully accepted by the trade server, the OrderSend...
12
To add comments, please log in or register