Close all error 4051 when backtest

 

Hi all,

I write some codes on my EA to close all my order after 12:00 EST. But when i backtest my EA, i get message error below.

EURUSD,M5: invalid ticket for OrderClose function

EURUSD,M5: invalid ticket for OrderClose function

Please help me to fix this error .

Thank you,

Bizu

void closeall() // Close all orders from 12:00 GMT
{
   int TimetoCloseOrder = 12;
   int Total = OrdersTotal();
   int cnt = 0;
   
   if (Hour()>= TimetoCloseOrder)
   {   
      if (Total>0)
      {
         for (cnt = 0 ; cnt <= Total ; cnt++)
         {
            OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()==OP_BUY)
               OrderClose(OrderTicket(),OrderLots(),Bid,5);
            if(OrderType()==OP_SELL) 
               OrderClose(OrderTicket(),OrderLots(),Ask,5);
         
         }
      }
   } 
}
 
  1. If the orderSelect fails so does the rest of the code - What are Function return values ? How do I use them ? - MQL4 forum
  2. Loops and Closing or Deleting Orders - MQL4 forum
  3. You don't need to check the order type, just use OrderClosePrice() instead of Bid/Ask.



 

Thanks WHRoeder,

It worked with OrderClosePrice().

Reason: