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

 
fxsaber #:
  for (int i = OrdersTotal() - 1; i >= 0; i--)     if (OrderGetTicket(i) && (OrderGetInteger(ORDER_MAGIC) == inpMagicEA) && (OrderGetString(ORDER_SYMBOL) == _Symbol))       trade.OrderDelete(OrderGetInteger(ORDER_TICKET));
This solution doesn't work on MT5 build 4410 - 21 Jun 2024
 
Hello, I have the same problem, I have tried several approaches but without success, does anyone have a little tip?

Thanks
 
Juan David #:
This solution doesn't work on MT5 build 4410 - 21 Jun 2024
bool CTrade::OrderDelete(const ulong ticket)
  {
//--- check stopped
   if(IsStopped(__FUNCTION__))
      return(false);
//--- clean
   ClearStructures();
//--- setting request
   m_request.action    =TRADE_ACTION_REMOVE;
   m_request.magic     =m_magic;
   m_request.order     =ticket;
//--- action and return the result
   return(OrderSend(m_request,m_result));
  }

The highlighted lines do not allow deleting orders in OnDeinit.

 
micky51480 #:
Hello, I have the same problem, I have tried several approaches but without success, does anyone have a little tip?
input long inpMagicEA = 0;

#include <Trade\Trade.mqh>

class CTrade2 : public CTrade
{
public:
  bool OrderDelete(const ulong ticket)
    {
  //--- check stopped
  //   if(IsStopped(__FUNCTION__))
  //      return(false);
  //--- clean
     ClearStructures();
  //--- setting request
     m_request.action    =TRADE_ACTION_REMOVE;
     m_request.magic     =m_magic;
     m_request.order     =ticket;
  //--- action and return the result
     return(OrderSend(m_request,m_result));
    }  
};

CTrade2 trade;

void OnDeinit( const int reason )
{
  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));
}