Problem with 1st order ticket number

 

I am getting an error "market order #1 cannot be deleted" "OrderDelete error 4108" "invalid ticket" when I want to delete an old pending order. That is in spite of the fact that my error code reports the correct order ticket number for the pending order - OrderTicket:1. All the subsequent pending order are successfully deleted. Why not the first one??

Here is the code

    for(i = 0; i <= OrdersTotal()-1; i++)   
        
               {
                  if (OrderSelect(i,SELECT_BY_POS)
                     && OrderSymbol() == Symbol() 
                     && OrderMagicNumber() == MagicNumber 
                     && ((OrderType()== OP_SELLSTOP)
                     || (OrderType()== OP_BUYSTOP)))
              
                  while(IsTradeContextBusy()) Sleep(10);        
                            bool Deleted = OrderDelete(OrderTicket(),Red);
Is there perhaps a problem with my counter?
 
Is it the case that when you delete the order at position 0 the order that was at position 1 becomes the order at position 0 . . . accepted wisdom is to always count down not up . . .
 
RaptorUK:
Is it the case that when you delete the order at position 0 the order that was at position 1 becomes the order at position 0 . . . accepted wisdom is to always count down not up . . .

I still do not understand. The program picks up the correct OrderTicket number(#1) - why cannot it be deleted? As far as I see it does not matter in what position the order is in the list - as long as the ticket number is correct - which it is in this case. Why then "invalid ticket"?

There is a good reason why I count up instead of down - I want to identify the last transaction on the list, iow the one with the highest order number must be read last.

Do I need to change my code to solve the problem?

 

Interesting it says . . . "market order #1 cannot be deleted" not "pending order #1 cannot be deleted" maybe while you were in the loop it activated ?

As part of your error reporting print the order type, it should give you a clue . . .

 
RaptorUK:

Interesting it says . . . "market order #1 cannot be deleted" not "pending order #1 cannot be deleted" maybe while you were in the loop it activated ?

As part of your error reporting print the order type, it should give you a clue . . .


You are right! I discovered that the pending order#1 was in fact activated! But the plot is thickening, because why does the code want to delete a market order when the code clearly stipulates only stop orders!!
 
The only thing that makes sense to me at the moment is that it was activated after being selected but before being deleted. If that is the case then it may be hard to catch it, you could add an additional check immediately before you do the OrderDelete to confirm it's still a pending order.
 
ernest02:

I still do not understand. The program picks up the correct OrderTicket number(#1) - why cannot it be deleted?

There is a good reason why I count up instead of down - I want to identify the last transaction on the list, iow the one with the highest order number must be read last.

  1. Because you are NOT correctly picking. You code:
        for(i = 0; i <= OrdersTotal()-1; i++)   
            
                   {
                      if (OrderSelect(i,SELECT_BY_POS)
                         && OrderSymbol() == Symbol() 
                         && OrderMagicNumber() == MagicNumber 
                         && ((OrderType()== OP_SELLSTOP)
                         || (OrderType()== OP_BUYSTOP)))
                  
                      while(IsTradeContextBusy()) Sleep(10);        
                                bool Deleted = OrderDelete(OrderTicket(),Red);
    Properly formated:
    for(i = 0; i <= OrdersTotal()-1; i++){
        if( OrderSelect(i,SELECT_BY_POS)
        &&  OrderSymbol() == Symbol() 
        &&  OrderMagicNumber() == MagicNumber 
        &&  (  OrderType()== OP_SELLSTOP
            || OrderType()== OP_BUYSTOP
            )
        )   while(IsTradeContextBusy()) Sleep(10);        
        bool Deleted = OrderDelete(OrderTicket(),Red);
    }
    Your IF only applies to the while statment, you are deleteing ALL orders
  2. Your counting up means you are missing every other order. you delete position 0, position 1 becomes zero, then you select order 1. You've missed one. Might be yours might not. It is UNNECESSARY. ALWAYS count DOWN. If you want to remember the highest one do it
    int count = 0;
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
            ...
        ){
           count++;
           if (count == 1) // remember this one

 

WHRoeder:

Your IF only applies to the while statement, you are deleting ALL orders

Well spotted.
 
WHRoeder:
  1. Because you are NOT correctly picking. You code: Properly formated: Your IF only applies to the while statment, you are deleteing ALL orders
  2. Your counting up means you are missing every other order. you delete position 0, position 1 becomes zero, then you select order 1. You've missed one. Might be yours might not. It is UNNECESSARY. If you want to remember the highest one do it

The subtleties of MQ4 programming! WHRoeder wish i had your experience and insight!

I will change my counter to count down to be on the safe side.

About the algorithm that deletes ALL orders because of wrong formatting I show below a little more of the code to show why I did not put a close bracket after the

while statement. The question is - if formatted this way does it not achieve the same result as the one you gave?

BUT WHY WILL IT DELETE ALL ORDERS IF THE CODE SPECIFIES ONLY STOPORDERS?

           {  
       
             for(i = 0; i <= OrdersTotal()-1; i++)   
        
               {
                  if (OrderSelect(i,SELECT_BY_POS)
                     && OrderSymbol() == Symbol() 
                     && OrderMagicNumber() == MagicNumber 
                     && ((OrderType()== OP_SELLSTOP)
                     || (OrderType()== OP_BUYSTOP)))
              
                  while(IsTradeContextBusy()) Sleep(10);        
                  bool Deleted = OrderDelete(OrderTicket(),Red);
                            
                                                                            
            // Error handling
                  if(Deleted == false)
                     {
                        ErrorCode = GetLastError();
                        ErrDesc = ErrorDescription(ErrorCode);

                        ErrAlert = StringConcatenate("Delete Old Pending Order - Error ",ErrorCode,": ",ErrDesc);
                        Alert(ErrAlert);

                        ErrLog = StringConcatenate("OrderTicket: ",OrderTicket());
                        Print(ErrLog);
                      }
                  }
               }
           }
 

Your formatting is not incorrect it just doesn't make your mistake very obvious. . . that is my excuse for missing it ;-)

The point that WHRoeder is making is this (I think) . . .

if (true && true && ( true || false ) )    //  if the result in the brackets is true . . .

while (true) sleep(10);    // . . . this line gets executed if true, not if false

bool Deleted = OrderDelete(ticket);    //  this line gets executed regardless of the  if  above


//  if you want to execute multiple lines as a result of an if, while, for . . . you need to use {}  braces


if (true && true && ( true || false ) )    //  if the result in the brackets is true . . .

   {
   while (true) sleep(10);    // . . . this line gets executed if true, not if false

   bool Deleted = OrderDelete(ticket);    //  this line also gets executed depending on the  if above
   }

//   Your code . . . . . . . . 
{  
             for(i = 0; i <= OrdersTotal()-1; i++)   
        
               {
                  if ( OrderSelect(i,SELECT_BY_POS)
                     && OrderSymbol() == Symbol() 
                     && OrderMagicNumber() == MagicNumber 
                     && ( (OrderType()== OP_SELLSTOP)
                     || (OrderType()== OP_BUYSTOP) ) )
                  {                                                  // add {} braces
                  while(IsTradeContextBusy()) Sleep(10);        
                  bool Deleted = OrderDelete(OrderTicket(),Red);
                  }                                                   // add {} braces         
                                                                            
            // Error handling
                  if(Deleted == false)
                     {
                        ErrorCode = GetLastError();
                        ErrDesc = ErrorDescription(ErrorCode);

                        ErrAlert = StringConcatenate("Delete Old Pending Order - Error ",ErrorCode,": ",ErrDesc);
                        Alert(ErrAlert);

                        ErrLog = StringConcatenate("OrderTicket: ",OrderTicket());
                        Print(ErrLog);
                      }
                  }
               }
           }
 
RaptorUK:

Your formatting is not incorrect it just doesn't make your mistake very obvious. . . that is my excuse for missing it ;-)

The point that WHRoeder is making is this (I think) . . .


Guys thanks for all the help so far. I have made the changes to my counter to count down instead of up and i have put in the braces. NEVERTHELESS I STILL get the error with the market order not being deleted.

This from my journal:

08:35:40 2011.05.17 08:37 MartingaleEA EURUSD,M1: delete #5 sell stop 3.31 EURUSD at 1.41605 sl: 1.41785 tp: 1.41414 ok
08:35:40 2011.05.17 08:37 MartingaleEA EURUSD,M1: delete #4 sell stop 3.22 EURUSD at 1.41415 sl: 1.41605 tp: 1.41264 ok
08:35:40 2011.05.17 08:37 MartingaleEA EURUSD,M1: market order #1 cannot be deleted
08:35:40 2011.05.17 08:37 MartingaleEA EURUSD,M1: OrderDelete error 4108
08:35:40 2011.05.17 08:37 stdlib EURUSD,M1: loaded successfully
08:35:40 2011.05.17 08:37 MartingaleEA EURUSD,M1: Alert: Delete Old Pending Order - Error 4108: invalid ticket
08:35:40 2011.05.17 08:37 MartingaleEA EURUSD,M1: OrderTicket: 1

This is messing up my program because certain actions later on depends on the non-existince of any type of order.

I still do not understand why the system wants to delete a non-existing market order when the code clearly specifies only stop orders.

Please help me solve this problem!

Reason: