How delete remaining pending order while open new order in mql4

 

I need help bros. I have two pending orders in market. when one order is execute,the remaining pending order need to close. how can delete this pending order.

` OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES); //selecting tha last order
   {                                                    //& put the details into variables:
   int OT = OrderType();          //......................1) the order type
   string Symb = OrderSymbol();   //......................2) the order symbol
   datetime OOT = OrderOpenTime();//......................3) the order open time
   //-----
   for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)  // & now starting to compare to all orders
      {
      OrderSelect (cnt, SELECT_BY_POS,MODE_TRADES);
      
      if (OrderSymbol() == Symb && (OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT|| OrderType() == OP_BUYSTOP||OrderType() == OP_SELLSTOP)&& OrderOpenTime() < OOT) 
         {
         OrderDelete(OrderTicket());    //deleting if it's the same order type & the same symbol & if it's placed (in time) before the last
         }
      }
   }

This code not work well, when one pending order execute as new order the remaining pending order not close immediately. plz help

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  1. 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.


  2. ` OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES); //selecting tha last order

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

  3. Do an OrderSelect loop and find if you have an open order. If you do, do another OrderSelect loop to find the pending order.

  4. From my GUI/Trade Assistant EA: Indicators: Money Manager Graphic Tool - Risk Management - Articles, Library comments - MQL5 programming forum - Page 8 #80 (2022.02).

          uint  nAbove   = 0, nBelow = 0;
             for(int iPos = OrdersTotal(); iPos > 0;) if(
                select_order(--iPos, SELECT_BY_POS, MODE_TRADES)
             && OrderType() > OP_SELL      // Pending
             )  if(OrderOpenPrice() > Ask) ++nAbove;
                else                       ++nBelow;
    
          static bool  isOCO   = false;
          bool        wasOCO   = isOCO;
                       isOCO   =  nAbove * nBelow != 0;
          Comment("");
          if(isOCO)                                          // Two pending
             Comment("OCO active");
          else  if(wasOCO)                                   // One opened or tried.
             for(int iPos = OrdersTotal(); iPos > 0;) if(
                select_order(--iPos, SELECT_BY_POS, MODE_TRADES)
             && OrderType() > OP_SELL                        // Pending
             ){
                int      ticket   = OrderTicket();
                if(!OrderDelete(ticket))
                   Alert(StringFormat("OrderDelete(%i):%i", ticket, _LastError) );
             }

Reason: