History Orders

 

I am trying to convert a 'simple' EA from MT4 to MT5 to list History Orders - in particular I want PROFIT, Time OPEN & CLOSE etc

I have tried and had partial success shown in the messagebox (using string variable MB)

Display of MESSAGEBOX

I use the following code

void On_My_HistoryOrder()
{  
   /*datetime df          = TimeCurrent()-(2*24*60*60);   // equates to 2*24 hours
   datetime dt          = TimeCurrent();

   HistorySelect(df,dt);
   */
   MB = MB+"\nhistory_orders = "+IntegerToString(HistoryOrdersTotal())+" @ "+TimeToString(TimeCurrent());
   for(int i=0; i < HistoryOrdersTotal(); i++ )
   {  ulong    LocalTicket    = HistoryOrderGetTicket             (i);
      string   LocalSymbol    = HistoryOrderGetString             (LocalTicket,ORDER_SYMBOL);
      string   LocalComment   = HistoryOrderGetString             (LocalTicket,ORDER_COMMENT);
      ulong    LocalMagic     = HistoryOrderGetInteger            (LocalTicket,ORDER_MAGIC);
      double   LocalPriceO    = HistoryOrderGetDouble             (LocalTicket,ORDER_PRICE_OPEN);
      double   LocalPriceC    = HistoryOrderGetDouble             (LocalTicket,ORDER_PRICE_CURRENT);
      datetime LocalTimeStart = (datetime)HistoryOrderGetInteger  (LocalTicket,ORDER_TIME_SETUP);
      datetime LocalTimeEnd   = (datetime)HistoryOrderGetInteger  (LocalTicket,ORDER_TIME_DONE);
      if ( Symbol() == LocalSymbol && LocalTicket == 247264318 ) 
      {  MB = MB + "\nTicket         = "+IntegerToString(LocalTicket);
         MB = MB + "\nComment        = "+LocalComment;
         MB = MB + "\nSymbol         = "+LocalSymbol;
         MB = MB + "\nMagic          = "+IntegerToString(LocalMagic);
         MB = MB + "\nOpen price     = "+DoubleToString(LocalPriceO,5);
         MB = MB + "\nCurrent price  = "+DoubleToString(LocalPriceC,5);
         MB = MB + "\nTimeStart      = "+TimeToString(LocalTimeStart);
         MB = MB + "\nTimeEnd        = "+TimeToString(LocalTimeEnd);
      } 
   }     
   
   return;
}

the deatails (downloaded report) are summarised as follows:-

Open Thu 15-Sep-22 11:30       correct
Ticket 247264318                     correct
AUDUSD                                   correct
sell                                           No attempt to retrieve
Lots 0.01                                 No attempt to retrieve
Open 0.67414                          NOT as shown in MessageBox
Close Thu 15-Sep-22 11:52      NOT as shown in MessageBox
Close Price 0.67488                 NOT as shown in MessageBox
Profit 0.37                              No attempt to retrieve

The OPEN price is 0.0000

The CURRENT PRICE is what should be the OPEN price and I am unable to find a CLOSE time or PRICE

If I look at HISTORYDEALS then they use a different ID --- Not the Ticket number shown here

Like to know / understand what instructiosn to use?

Any help appreciated

 

I use the following before entering the routine

datetime df          = TimeCurrent()-(2*24*60*60);   // equates to 2*24 hours
   datetime dt          = TimeCurrent();
   MB = MB + "Start @ "+TimeToString(df)+" to "+TimeToString(dt);
   HistorySelect(df,dt);
 

I have found the following post https://www.mql5.com/en/code/17622

which appears to solve my problems.

Still confused why open price is not correct

CHistoryPositionInfo Class - library for MetaTrader 5

HistoryPositionInfo
HistoryPositionInfo
  • www.mql5.com
Returns position profit in points based on the trading history.
 

Yes excellent - just found the library

 
Peter Williams #:I use the following before entering the routine
datetime df          = TimeCurrent()-(2*24*60*60);   // equates to 2*24 hours

You probably want two trading days, not two calendar days.
          Find bar of the same time one day ago - MQL4 programming forum (2017)

Reason: