Delete Orders and pending orders

 

I am trying to create a code that will delete my orders as well as pending orders.

Unfortunately I cannot get any results but only error like "UNKNOWN RETCODE 0" and "ERROR 4014". The code is :

void ClosePendingOrders() {

   int total_orders = OrdersTotal();  // Ottieni il numero totale di ordini attivi
   if (total_orders > 0) {
      for (int i = total_orders - 1; i >= 0; i--) {
         ulong ticket = OrderGetTicket(i);  // Ottieni il ticket dell'ordine corrente
         if (ticket>0) {         // Seleziona l'ordine tramite ticket
         if(OrderGetString(ORDER_SYMBOL)==_Symbol)
           {
            int order_type = OrderGetInteger(ORDER_TYPE);  // Ottieni il tipo di ordine
            int order_state = OrderGetInteger(ORDER_STATE); // Ottieni lo stato dell'ordine

            // Debug: informazioni sull'ordine
            Print("Ordine ", ticket, " - Tipo: ", order_type, " - Stato: ", order_state);

            // Verifica se l'ordine è pendente e non eseguito
               if (order_type == ORDER_TYPE_BUY_LIMIT || 
                   order_type == ORDER_TYPE_SELL_LIMIT || 
                   order_type == ORDER_TYPE_BUY_STOP || 
                   order_type == ORDER_TYPE_SELL_STOP) 
               {
                   // Tentativo di cancellazione dell'ordine pendente

                   if (trade.OrderDelete(ticket)) {
                      Print("Ordine pendente chiuso: ", ticket);
                   } else {
                      int error_code = GetLastError();
                      Print("Ticket: ", ticket, 
                            " Code error: ", error_code);

                      // Dettagli sugli errori più comuni
                      if (error_code == 4756) {
                         Print("Errore 4756: L'ordine non può essere cancellato perché il prezzo corrente non soddisfa le condizioni.");
                      } else if (error_code == 4109) {
                         Print("Errore 4109: L'account non ha i permessi per cancellare ordini.");
                      } else if (error_code == 4014) {
                         Print("Errore 4014: Errore di trading, verifica le condizioni di mercato o la connettività.");
                      } else {
                         Print("Errore sconosciuto: ", error_code);
                      }
                   }
               } else {
                   Print("Ordine ", ticket, " non è un ordine pendente (tipo: ", order_type, ").");
               }
         } else {
            Print("Errore nella selezione dell'ordine: ", GetLastError());
         }
         }
      }
      Print("Tutti gli ordini pendenti sono stati processati.");
   } else {
      Print("Non ci sono ordini pendenti aperti.");
   }
}

Tnaks in advance

Francesco

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893