How to delete pending order? - page 2

 
//+------------------------------------------------------------------+
//|  DELETE PENDING ORDERS                                         |
//+------------------------------------------------------------------+
void deletePendingOrder(int ordertype)
  {
   int res;
//--- variables for returning values from order properties
   ulong    ticket;
   double   open_price;
   double   initial_volume;
   datetime time_setup;
   string   symbol;
   int   type;
   int     order_magic;
   long     positionID;
   uint     total=OrdersTotal();
   string   comment;
//--- go through orders in a loop
//Print("total orders "+total);
   int count=0;
   ulong ticktsToDelete[];
   ArrayResize(ticktsToDelete,total);
   for(uint i=0; i<total; i++)
     {
      //--- return order ticket by its position in the list
      ticket=OrderGetTicket(i);
      if(OrderSelect(ticket))
        {

         //--- return order properties
         open_price    =OrderGetDouble(ORDER_PRICE_OPEN);
         time_setup    =(datetime)OrderGetInteger(ORDER_TIME_SETUP);
         symbol        =OrderGetString(ORDER_SYMBOL);
         order_magic   =(int)OrderGetInteger(ORDER_MAGIC);
         comment       = OrderGetString(ORDER_COMMENT);
         positionID    =OrderGetInteger(ORDER_POSITION_ID);
         initial_volume=OrderGetDouble(ORDER_VOLUME_INITIAL);
         type          =(int)OrderGetInteger(ORDER_TYPE);
         //Alert("symbol "+symbol+" order_magic"+order_magic+" comment "+comment);
         Print(ticket+ " (symbol==selectedSymbol)="+ " type="+type +" ordertype= "+ordertype);
         if(symbol==selectedSymbol && order_magic==MAGIC_NUMBER && type==ordertype)
           {
            ticktsToDelete[count++]=ticket;
           }
        }
     }
   for(uint i=0; i<count; i++)
     {
      if(!m_trade.OrderDelete(ticktsToDelete[i]))
         Alert("Could not delete pending order for ");
     }

  }
After a  lots of research and brainstorming this is my solution. But before the solution I would like to tell the problem of the using this code  given by phi, which is the below code
#include <Trade\Trade.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   int ord_total=OrdersTotal();
   if(ord_total > 0)
     {
      for(int i=ord_total-1;i>=0;i--)
        {
         ulong ticket=OrderGetTicket(i);
         if(OrderSelect(ticket) && OrderGetString(ORDER_SYMBOL)==Symbol())
           {
            CTrade *trade=new CTrade();
            trade.OrderDelete(ticket);
            delete trade;
           }
        }
     }
//---
   return;
  }
It will delete running loop trade which causes problem when you have lots of pending orders. it may not delete all pending orders . so better to store the tickts of pending orders and delete them in a separate loop
 

Alternative code.

Forum on trading, automated trading systems and testing trading strategies

Delete pending orders on deInit

fxsaber, 2023.06.06 17:41

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));
}
 

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.