Delete pending orders on deInit

 

Hi, I am having troubles deleting pending orders when I release the EA from chart. OnDeInit() I use the following code

void OnDeinit(const int reason)
{
//---
// delete pending orders

   for(int i=0; i<OrdersTotal(); i++)
      if(OrderGetTicket(i))
         if(OrderGetString(ORDER_SYMBOL)==_Symbol)
            if(OrderGetInteger(ORDER_MAGIC)==inpMagicEA)
               trade.OrderDelete(OrderGetInteger(ORDER_TICKET));

}

Does anyone have know why this wouldnt work, and it would be much appreciated if someone could share the correct way to delete pending orders when expert is removed. 

 
altmql: the correct way to delete pending orders when expert is removed. 
  1. Just to be precise, the documentation says: The OnDeinit() function run is restricted to 2.5 seconds. You don't have time to close/delete them.

  2. Do you really want to remove them on just a chart timeframe change?

    There is no need to create pending orders in code.

    1. The pending has the slight advantage, A) you are closer to the top of the queue (filled quicker), B) there's no round trip network delay (filled quicker.)

      Don't worry about it unless you're scalping M1 or trading news.

    2. Humans can't watch the screen 24/7, so they use pending orders; EAs can, so no need for pending orders, have it wait until the market reaches the trigger price and just open an order.

 
altmql:

the correct way to delete pending orders when expert is removed. 

void OnDeinit( const int reason )
{
//---
// delete pending orders

  trade.SetAsyncMode(true);

  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderGetTicket(i) && (OrderGetInteger(ORDER_MAGIC) == inpMagicEA) && (OrderGetString(ORDER_SYMBOL) == _Symbol))
      trade.OrderDelete(OrderGetInteger(ORDER_TICKET));
}
 
fxsaber #:

Thank you for that!

 
fxsaber #:

Tried your code, but it doesnt delete the pending orders when the EA is removed. Anyone got a solution? 

 
altmql #:

Tried your code, but it doesnt delete the pending orders when the EA is removed. Anyone got a solution? 

Most likely you did something wrong.

 
altmql #: Tried your code, but it doesnt delete the pending orders when the EA is removed. Anyone got a solution? 

Asked and answered #1.1