Slow performance in a especific function


My backtests take forever to execute when I call this function inside the OnTick:

(currentDateLastCheck gets the current day, so I don't store the whole historic)

void dailyGain(){
   HistorySelect(StringToTime(currentDateLastCheck), TimeCurrent());
   uint     total=HistoryDealsTotal();
   ulong    ticket=0;
   double   profit;
   datetime time;
   string   symbol;
   dailyProfit = 0;
   for(uint i = 0; i< total; i++){
      if((ticket=HistoryDealGetTicket(i)) > 0){
         profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
         symbol = HistoryDealGetString(ticket, DEAL_SYMBOL);
         time = HistoryDealGetInteger(ticket, DEAL_TIME);
         if(symbol==Symbol() && TimeToString(time, TIME_DATE) == TimeToString(TimeCurrent(), TIME_DATE)){
            dailyProfit += profit;
Could anyone give me an insight why it's making my backtest performance terrible?
When I say it takes forever, I'm talking about more than an hour to run 2 months of data.

HistorySelect is notoriously slow function, especially with a large number of trades in history.

To avoid cache being rebuild, do not change the first date as it will invalidate the cache.

Use a smaller time range and divide into more tests