Why this not working on real account?

 

I'm trying to get last deal profit like this;

 
HistorySelect(0,TimeCurrent());
   double pft=0;
   int i=HistoryOrdersTotal();

   ulong ticket=HistoryDealGetTicket(i);
   pft = HistoryDealGetDouble(ticket,DEAL_PROFIT);

   printf(i,pft);
   return(pft); 

This works on backtesting but not working in real account. On real account there is 7 deal in history but when i print it, it i return 18 and pft return nothing. How to solve this?

Reason: