Why HistoryDealSelect() function is not working as expected

 

This is a very simple code snippet. I am trying to iterate over all the closed orders and print the deal numbers for now. Why the function returns 4755 error code after the 1st iteration? If the comment out the inner `if` condition, the deal numbers are printed correctly otherwise only in the 1st iteration of the loop.


int OnInit()
{
   if (!HistorySelect(0, TimeCurrent()))
    {
        Print("❌ Failed to select trade history.");
        return(INIT_SUCCEEDED);;
    }

    int totalDeals = HistoryDealsTotal();
    
    if (totalDeals == 0)
    {
        Print("❌ No closed deals today.");
        return(INIT_SUCCEEDED);;
    }

    for (int i = 0; i < totalDeals; i++)
    {
        ulong deal_ticket = HistoryDealGetTicket(i);
        Print("Deal:", deal_ticket, " INDEX:", i);
        
        if (!HistoryDealSelect(deal_ticket))  // <-- THIS LINES NOT WORKING?
        {
         Print(_LastError);
         continue;
        }

    }
    
   return(INIT_SUCCEEDED);
}
  
 
Nabeel Bashir:

This is a very simple code snippet. I am trying to iterate over all the closed orders and print the deal numbers for now. Why the function returns 4755 error code after the 1st iteration? If the comment out the inner `if` condition, the deal numbers are printed correctly otherwise only in the 1st iteration of the loop.


Because you don't need it, the deal is ALREADY selected with HistoryDealGetTicket(i). Please read the documentation carefully.