MQL5 - History Deals not updating

To add comments, please log in or register
Fabrizio Failla
30
Fabrizio Failla  
Hi, I'm having problems with the code to retrieve closed trades from the history in MQL5. I'm using this code but when I close a trade, it does not show up in the history deals.
I need a function to retrieve the latest trades closed. I'm calling this function from OnTrade(). I use to program in MQL4 and I'm new to MQL5. Thanks for the help!
void NewClosedTradeTicket(int &tickets[])
{
   ArrayResize(tickets,0);
   datetime times[];
  
  if(HistorySelect(0,TimeLocal()))
  {
   for(int i=HistoryDealsTotal()-1; i>=0; i--)
   {
      if(dealInfo.SelectByIndex(i))
       {
           // Read deal out and get deal time
           if(dealInfo.Entry()==DEAL_ENTRY_OUT)
           {   
               datetime close_time = dealInfo.Time();    
               ulong tic = dealInfo.Ticket();  
               
               if(close_time > lastCloseTradeTime)
               {
                 
                  
                   ArrayResize(tickets,ArraySize(tickets)+1);
                   tickets[ArraySize(tickets)-1]  =tic;
                   
                   ArrayResize(times,ArraySize(times)+1);
                   times[ArraySize(times)-1] = close_time;
         
               }    
           }
       }


   }
  

  if(ArraySize(times)>0)
  {
   int maxTime = ArrayMaximum(times,0,WHOLE_ARRAY);
  
   if(maxTime>-1)
   lastCloseTradeTime = times[maxTime];
  }
 }

}
Vladimir Karputov
Moderator
161151
Vladimir Karputov  

You need to use the time of the trading server. And it’s better to take it with a margin:

      if(HistorySelect(0,TimeCurrent()+60*60*24;))


In general, the last deal needs to be caught in OnTradeTransaction.

Vladimir Karputov
Moderator
161151
Vladimir Karputov  
How to start with MQL5
How to start with MQL5
  • 2019.08.18
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Fabrizio Failla
30
Fabrizio Failla  
Thank you so much Vladimir!
To add comments, please log in or register