OrderSelect() fails

 

Hello,

The following code always fails on OrderSelect() and the return error code is 0  

if (OrderDelete(TicketNum, CLR_NONE) == TRUE)
{
    ...
    if (OrderSelect(TicketNum, SELECT_BY_TICKET) != TRUE)
    {
        Alert("Server failed to select order ticket=", TicketNum, " Err=", GetLastError());
    }
    ...
}

 I need to select the order again after OrderDelete() because there's some code between OrderDelete() and OrderSelect() (where the first "..." is )  which may change the selected order.

I tried adding MODE_HISTORY to OrderSelect(), although it's not necessary, but the result is the same. Any help would be appreciated.

 
catsailor:

Hello,

The following code always fails on OrderSelect() and the return error code is 0  

<CODE DELETED>

I need to select the order again after OrderDelete() because there's some code between OrderDelete() and OrderSelect() (where the "..." is )  which may change the selected order.

I tried adding MODE_HISTORY to OrderSelect(), although it's not necessary, but the result is the same. Any help would be appreciated.

Please read some other posts before posting . . .

Please edit your post . . .    please use the   SRC   button to post code: How to use the   SRC   button.  

 

Why do you think you should be able to select a pending order that you just deleted ?   if the OrderSelect() returns true it has worked, please read the documentation . . .  so then there is no error so the error code is 0.

 

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Perhaps it takes time before the history is updated.
  3. Select the order before deleting it. If you change the selected order (where the "..." is ) do the orderSelect there.
 
RaptorUK: Why do you think you should be able to select a pending order that you just deleted ? 
The last line of my MySelect() has:
   return(OrderType() <= OP_SELL);  // Avoid cr/bal forum.mql4.com/32363#325360
                                    // https://forum.mql4.com/30708
                                    // Never select canceled orders.
So I've assumed (based on other forum code) that canceled orders appear in the history list.
 
WHRoeder:
Because canceled orders appear in the history list?

OK,  good point,  for some reason I thought deleted Pending Orders only appeared in the Strategy Tester History . . . . .  wonder why I thought that    apologies to the OP  
 

Apologies for posting mistakes, this is my first post and  thanks for your prompt replies. 

To RaptorUK, there's nothing in the documentation that says you can't select a pending order that was just deleted. BTW, OrderSelect() return !true but GetLastError() returns 0.

To WHRoeder, one thing i failed to mention in my original post is that by adding a small delay after OrderDelete(), the following OrderSelect() succeeds. I have no explanation to this except it's a bug in MQL4, as I would assume that OrderDelete() should not return unless after the order is actually deleted and all orders lists are updated. If I'm mistaken, please let me know.

 

I just noticed the // Never selected canceled orderscomment in WHRoeder reply. Can you elaborate if this is a part of MQL4 documentation or rules? If this is true, how can I get information about past deleted orders?

 
catsailor:

I just noticed the // Never selected canceled orders. comment in WHRoeder reply. Can you elaborate if this is a part of MQL4 documentation or rules? If this is true, how can I get information about past deleted orders?

You may use OrderSelect() to select any order regardless of whether it is a market order, pending order, closed order, or canceled order.  See the OrderSelect section in Closing and Deleting Orders.  Can't speak directly for WHRoeder, but I believe he has chosen in his code to select only open/closed market orders.  See Trade Constants/Enumerations.  That doesn't mean that you can only select open/closed market orders...you may select any order using OrderSelect().  Again, see the OrderSelect section in Closing and Deleting Orders.

WHRoeder:

Select the order before deleting it. If you change the selected order (where the "..." is ) do the orderSelect there.

Since ticket numbers are unique, I don't believe there is a need to select an order using OrderSelect() before deleting it if you have the ticket number.
 
catsailor: I just noticed the // Never selected canceled orders. comment in WHRoeder reply. Can you elaborate if this is a part of MQL4 documentation or rules?
Neither. It's part of the code's filter:
OrderType() <= OP_SELL <---- matches OP_BUY or OP_SELL only.
 
Thirteen: Since ticket numbers are unique, I don't believe there is a need to select an order using OrderSelect() before deleting it if you have the ticket number.
Not occurring to the documentation. But the OP was having a problem with OrderSelect() after deleting.
 

I just verified that orders list are not fully updated before OrderDelete() returns. I inserted some code between OrderDelete() and OrderSelect()

if (OrderDelete(TicketNum, CLR_NONE) == TRUE)
{
    ClosedOrderCount = OrdersHistoryTotal();
    WaitLoops = 0;
    while(ClosedOrderCount == OrdersHistoryTotal())
    {
       WaitLoops++;
    }
    Print("Loops=", WaitLoops);
    if (OrderSelect(TicketNum, SELECT_BY_TICKET) != TRUE)
    {
        Alert("Server failed to select order ticket=", TicketNum, " Err=", GetLastError());
    }
}

 The Loops= value ranged from 0 to about 400000. I did only 5 tests.

Any ideas?

Reason: