Could it be an error in the mql5 platform?

 

Hello everyone, I'm having trouble retrieving the magic number and the comment when the order is closed. It places the sell or buy limit order containing the magic number and the comment. If the order is pending, I can check it, and if the order was executed and is in progress, I can retrieve it. However, if the order is closed positively or negatively, I can't. I've tried several methods without success. I tried more than one approach to find between Deal and Order but without success. When closing and opening the platform, it returned the magic number to me, but the comment has not returned to me in any way so far. Could it be an error in the mql5 platform?

void CancelOrders() {

    datetime currentTime = TimeCurrent();
    HistorySelect(currentTime - 3 /* * 86400 */, currentTime); //History of the last 3 dayss

    for (int i = HistoryDealsTotal() - 1; i >= 0; i--) {
        ulong deal_ticket = HistoryDealGetTicket(i);
        if (HistoryDealSelect(deal_ticket)) {
            // Initialize the variable to store the profit
            double lucroFechado = HistoryDealGetDouble(deal_ticket, DEAL_PROFIT);
            
            // Checks if the order was closed with a profit
            if (lucroFechado > 0) {
                //string parFechado = HistoryDealGetString(deal_ticket, DEAL_SYMBOL);
               // ulong order_ticket = HistoryDealGetInteger(deal_ticket, DEAL_ORDER); // Obtém a ordem original
               // ulong identificador =0;
               // string coMentario;
               // string coMentarioDeal = HistoryDealGetString(deal_ticket, DEAL_COMMENT);
                /*
               string parFechado = HistoryDealGetString(deal_ticket, DEAL_SYMBOL);
                
                // Obtém o ticket da ordem original (para, por exemplo, recuperar o comentário da ordem)
                ulong order_ticket = HistoryDealGetInteger(deal_ticket, DEAL_ORDER);
                
                // Aqui, em vez de usar HistoryOrderSelect para obter o magic,
                // pegamos o magic diretamente da DEAL
                ulong identificador = HistoryDealGetInteger(deal_ticket, DEAL_MAGIC);
                
                // Se desejar, tente também obter o comentário da ordem (opcional)
                string coMentario = "";
                if(HistoryOrderSelect(order_ticket)) {
                    coMentario = HistoryOrderGetString(order_ticket, ORDER_COMMENT);
                } else {
                    Print("⚠No comment identified from original order.");
                }*/
                
                string parFechado = HistoryDealGetString(deal_ticket, DEAL_SYMBOL);
                ulong order_ticket = HistoryDealGetInteger(deal_ticket, DEAL_ORDER); // Obtém a ordem original
                ulong identificador =0;
                string coMentario;
                string coMentarioDeal = HistoryDealGetString(deal_ticket, DEAL_COMMENT);
                
                // Find the correct Magic Number from the original order
                if (HistoryOrderSelect(order_ticket)) {
                    identificador = HistoryOrderGetInteger(order_ticket, ORDER_MAGIC);
                    coMentario = HistoryOrderGetString(order_ticket, ORDER_COMMENT);
                    //coMentario = PositionGetString(order_ticket, POSITION_COMMENT);
                    //PositionGetString(POSITION_COMMENT, coMentario);
                   // HistoryOrderGetString(deal_ticket, ORDER_COMMENT, coMentario);
                   // HistoryDealGetString(order_ticket, DEAL_COMMENT, coMentario);
                   //coMentario = HistoryOrderGetString(order_ticket,ORDER_COMMENT);
                } else Print("⚠ Não identificado MAGIC NUMBER na ordem original.");

                string ind = IntegerToString(identificador);
                
                // Displays information about the closed order
                Print("Operation closed with profit - Parity: ", parFechado,
                      " - Ticket: ", deal_ticket,
                      " - Order Commentary: ", coMentario,
                      " - Deal's comment: ", coMentarioDeal,
                      " - Profit: ", lucroFechado,
                      " - Identifier: ", ind);
                      
               Alert("Operation closed with profit - Parity: ", parFechado,
                      " - Ticket: ", deal_ticket,
                      " - Order Commentary: ", coMentario,
                      " - Deal's comment: ", coMentarioDeal,
                      " - Profit: ", lucroFechado,
                      " - Identifier: ", ind);

                // Closes all pending orders with the same pair and identifier
                ClosePendingOrders(parFechado, identificador);
            }
        }
    }
}

 
  • Hello. Definitely this is not a problem with MQL5. 
  • The comment is within the DEAL_IN. You have to iterate the historical deals and check DEAL_INs for comment. 
  • Generally comments may be modified by brokers in unpredicted manner. So it is best not to rely on them.