Problem with 1st order ticket number - page 2

 

A market order cannot be Deleted . . . that is why you get the error, something in your code is still trying to delete a market order instead of a pending order IMO

From: https://docs.mql4.com/trading/OrderDelete "Deletes previously opened pending order."

Show your modified code please . . . I've had coffee already ;-)

 
RaptorUK:

A market order cannot be Deleted . . . that is why you get the error, something in your code is still trying to delete a market order instead of a pending order IMO

From: https://docs.mql4.com/trading/OrderDelete "Deletes previously opened pending order."

Show your modified code please . . . I've had coffee already ;-)

There is one possible cause for this situation and that is the fact that three or more transactions are activated on the same bar. There could be a problem with the sequence of transactions, e.g. stop loss/profit take, activating a pending into a market order, opening a new pending order, deleting pending orders (if it is a profit transaction). How does one make sure that the sequence of transactions at the opening of the bar is in the right order? OK! I Know with the correct sequence in the code! Or is there another way? For example, can hold back certain transactions until the opening of the next bar?

Here is the modified code:

           {  
       
             for(i = OrdersTotal()-1; i >= 0; 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);
                      }
                  }
               }
           }
 

I would add these print statements to see what is happening . . . .

{  
       
             for(i = OrdersTotal()-1; i >= 0; i--)   
        
               {
                  if (OrderSelect(i,SELECT_BY_POS)
                     && OrderSymbol() == Symbol() 
                     && OrderMagicNumber() == MagicNumber 
                     && ((OrderType()== OP_SELLSTOP)
                     || (OrderType()== OP_BUYSTOP)))
              
                  {
                   Print("Ticket = ",OrderTicket(), " Type= ",OrderType() );  //  <-- here
                   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);
                        Print("Time now= ", TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS), "OrderOpen= ", TimeToStr(OrderOpenTime(),TIME_DATE|TIME_SECONDS) );   //  <-- here
                      }
               }
}
 
If the Pending Order ticket no 1 is activating during the loop you will see it from the OrderOpen time as it should be very close to the "Time now"
 
RaptorUK:
If the Pending Order ticket no 1 is activating during the loop you will see it from the OrderOpen time as it should be very close to the "Time now"

The problem that I discovered is that at the same bar a pending order has to be changed into a market Buy order (Price reaches Buy Price) and an existing Buy market order is reaching its Profit Target.

What happens is that the pending order is triggered first to change to a Buy order although its price is 0.00002 higher than the profit target and therefore should in reality reach its target later than the Buy order reaching its profit target. The only reason I can deduct that this weird wrong sequence is followed is that the ticket number of the pending order is #1 and that of the Buy order is #3.

If the Buy profit target is activated first (as it should be because it has the lower price) then there would have been no problem, but now the buy pending order is activated first to become a Buy market order, and that is causing my problem.

How does one solve this problem??

 
ernest02:


How does one solve this problem??


Maybe this code needs to be run for each tick and not each bar ?
 
RaptorUK:
Maybe this code needs to be run for each tick and not each bar ?
Well (as far as I know) the code IS run with every tick - as is the MetaTrader tester. I can say this because there are more than a hundred iterations for every bar.
 
ernest02:
Well (as far as I know) the code IS run with every tick - as is the MetaTrader tester. I can say this because there are more than a hundred iterations for every bar.

Ah OK, you said "The problem that I discovered is that at the same bar a pending order has to be changed into a market Buy order (Price reaches Buy Price) and an existing Buy market order is reaching its Profit Target." so I thought you might be just running this once per bar.
 
RaptorUK:
Ah OK, you said "The problem that I discovered is that at the same bar a pending order has to be changed into a market Buy order (Price reaches Buy Price) and an existing Buy market order is reaching its Profit Target." so I thought you might be just running this once per bar.

I agree my words could have been misleading.

I have spent several days with this problem and still do not have a solution. I am not sure if it is only the MT4 tester that works this way or will it also happen when I run it on live data. The problem is I cannot back test my EA properly and get some reliable results. This is so annoying!

 

Buy orders hitting their TP (or SL) and Pending orders activating is handled on the Brokers Server so are out of your control . . . there can always be a condition when using Pending orders that you might want to Delete it just as it's being activated . . . there is always a way to avoid the conflict between your code and what the Brokers server is doing, it really depends on how involved you want to get with your code and how creative you can be . . .

For example, you could chose to not Delete a Pending order if it is within a set price range from its entry price, check this before you try to delete it

Reason: