I am unable to get closing time for any position and ontrade not working - page 3

 
   // Select the trade history from the start date to the current time
   HistorySelect(startDate, TimeCurrent());

   // Get the total number of deals in the history
   int total = HistoryDealsTotal();
   
   ulong position_id;

   // Iterate through the history of deals using an incremental loop
   for (int i = 0; i < total; i++) // Incrementing from 0 to HistoryDealsTotal()-1
   {
      ulong deal_ticket = HistoryDealGetTicket(i);
      ulong deal_position_id = HistoryDealGetInteger(deal_ticket, DEAL_POSITION_ID);

      // Check if the deal is an entry (buy/sell)
      int entry_type = HistoryDealGetInteger(deal_ticket, DEAL_ENTRY);

      if (entry_type == DEAL_ENTRY_IN) // If it's an entry deal
      {
         position_id = deal_position_id;

         datetime deal_time = HistoryDealGetInteger(deal_ticket, DEAL_TIME);

         // Now, search for a matching exit deal by iterating forward from the current position
         for (int j = i + 1; j < total; j++) // Incrementing from i+1 onwards
         {
            ulong other_deal_ticket = HistoryDealGetTicket(j);
            ulong other_deal_position_id = HistoryDealGetInteger(other_deal_ticket, DEAL_POSITION_ID);
            int exit_type = HistoryDealGetInteger(other_deal_ticket, DEAL_ENTRY);

            datetime other_deal_time = HistoryDealGetInteger(other_deal_ticket, DEAL_TIME);

            // Check if the position IDs match and it's an exit deal
            if (other_deal_position_id == position_id && exit_type == DEAL_ENTRY_OUT)
            {
               // Matching entry and exit deals found
               Print("Matching Entry Deal at: ", TimeToString(deal_time), 
                     " and Exit Deal at: ", TimeToString(other_deal_time), 
                     " for Position ID: ", position_id);
               
               break; // Stop once the exit deal is found
            }
         }
      }
   }