Not getting The right value with MAGIC NUMBER

 

Hello friends.

I have this code I wrote. I tried getting all the closed profits by magic number over a period of time. But it's not getting the right value.

It works perfectly for mt4, but mt5 is not so clear, because if the concept of IN and OUT. The IN part has the magic number but the OUT part doesn't, so its a problem when getting information about the closed trades.

Please help. 

Below are my lines of code.

Thanks


double totalClosedProfits()
{  
   HistorySelect(StringToTime(startDateTime), TimeCurrent());
   double total_profit=0.0;
   uint   total=HistoryDealsTotal();
   ulong  ticket=0;
//--- for all deals
   for(uint i=0; i<total; i++)
     {
      //--- try to get deals ticket
      if((ticket=HistoryDealsTotal())>0)
        {
         double deal_commission   =  HistoryDealGetDouble(ticket,DEAL_COMMISSION);
         double deal_swap         =  HistoryDealGetDouble(ticket,DEAL_SWAP);
         long   deal_magic        =  HistoryDealGetInteger(ticket,DEAL_MAGIC);
         double deal_profit       =  HistoryDealGetDouble(ticket,DEAL_PROFIT);
         
         if (HistoryDealGetDouble(ticket, DEAL_PRICE) == 0) continue;
         if(deal_magic==2000001)
            {
               total_profit+=deal_commission+deal_swap+deal_profit;
            }
                     
        }
     }

return(total_profit);
}
 
      if((ticket=HistoryDealsTotal())>0)
  1. HistoryDealsTotal does not return a ticket.
  2. That is potentially confusing. Is that an assignment or a comparison, or a typo? Prefer clear code.
    ticket=HistoryDealsTotal(); if(ticket>0)

 
As a side note that may help you, read the following thread as well ... Retrieve profit by position, best practice?
Reason: