Getting open and close time gives wrong value

 

I have had this code throughout my eas, scripts ands indicators but suddenly broke and returns wrong values. Then I surfed the forum, there is enough information on this but all the snippets i took also gave the same wrong answer.

Is this a bug or Mql5 changed?

   HistorySelect(0,TimeCurrent());

   string   name;
   uint     total=HistoryDealsTotal();
   ulong    ticket=0;
   double   price;
   double   profit;
   datetime entryTime, closeTime;
   string   symbol;
   long     type;
   long     entry;
   double dealTp, dealSl;


   for(uint i=0; i<total; i++)
     {

      if((ticket=HistoryDealGetTicket(i))>0)
        {

         price =HistoryDealGetDouble(ticket,DEAL_PRICE);
         entryTime  =(datetime)HistoryDealGetInteger(ticket,DEAL_TIME);
         symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);
         type  =HistoryDealGetInteger(ticket,DEAL_TYPE);
         entry =HistoryDealGetInteger(ticket,DEAL_ENTRY);
         profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
         dealSl = HistoryDealGetDouble(ticket,DEAL_SL);
         dealTp  = HistoryDealGetDouble(ticket,DEAL_TP);
         datetime midignt = (TimeCurrent()/86400)*86400;

         if(draw)
            if(price && entryTime && symbol==Symbol())
              {
               if(entryTime >= midignt && dealSl >0)
                 {
                  if(entry == DEAL_ENTRY_OUT)
                    {
                     closeTime = (datetime)HistoryDealGetInteger(ticket,DEAL_TIME);
                    }
                  name="TradeHistory_Deal_"+string(ticket);
                  Comment(closeTime   +"  " + entryTime);
                  createRectangle(name, entryTime, price, closeTime, dealTp, C'189,244,189,10');
                 }
              }
        }
     }

Output

 
What is written in the logs? You don't control if there is an error and what error (_LastError).