HistoryDeal functions not work

 

Hello everyone,

I am new to MQL5 programming. I have converted a code that retrieves data from the last closed trade from MQL4 to MQL5. Even though the code is written correctly and there are no programming errors, it does not work at all, neither in live nor in the tester. I kindly request experienced members here to review the code and inform me of any errors or corrections that need to be made and added.

The Code >>

//=========== "Get Last Closed Trade Information For Only One Order" code =========== 
double LastClosedPosition(string info, int type = -1)
{
   double result = 0;
   int total = HistoryDealsTotal();
   for (int i = total - 1; i >= 0; i--)
   {
      ulong ticket=HistoryDealGetTicket(i);
      bool select = HistoryDealSelect(ticket);

      string symbol=HistoryDealGetString(ticket, DEAL_SYMBOL);
      long magicNumber=HistoryDealGetInteger(ticket, DEAL_MAGIC);
      bool entryType=HistoryDealGetInteger(ticket, DEAL_ENTRY);
      double lots=HistoryDealGetDouble(ticket, DEAL_VOLUME);
      double entryPrice=HistoryDealGetDouble(ticket, DEAL_PRICE);
      double profit=HistoryDealGetDouble(ticket, DEAL_PROFIT);
      double takeProfit=HistoryDealGetDouble(ticket, DEAL_TP);
      double stopLoss=HistoryDealGetDouble(ticket, DEAL_SL);

            if (symbol == Symbol() && magicNumber == MagicNumber)
            {
               if (type == entryType || type == -1)
               {

                  if (info == "Lots")
                     result = lots;
                  else if (info == "EntryPrice")
                     result = entryPrice;
                  else if (info == "Profit")
                     result = profit;
                  else if (info == "Type")
                     result = entryType;
                  else if (info == "TP")
                     result = takeProfit;
                  else if (info == "SL")
                     result = stopLoss;
               }
            }
         }
  
   return (result);
}