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?


   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++)


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

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


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