How to get the datetime for last open trade (code mql5)

 

Hi everybody,

I'am are very new in mql5, i try to develop a EA but i need get the datetime for last open trade. I have the next code but i get forever "2020-07-22 01:00:00" without work.


Thank you,

Regards


datetime GetLastTradeOpenTime() {
  uint TotalNumberOfDeals = HistoryDealsTotal();
  ulong TicketNumber = 0;
  long OrderType, DealEntry;
  double OrderProfit = 0;
  string MySymbol = "";
  string PositionDirection = "";
  string MyProfit = "";
  double Volume = 0;
  datetime DateTimeOpenLastOp = "";
  datetime LastTime = "";

  HistorySelect(0, TimeCurrent());

  for (uint i = 0; i < TotalNumberOfDeals; i++) {
    if ((TicketNumber = HistoryDealGetTicket(i)) > 0) {
      OrderProfit = HistoryDealGetDouble(TicketNumber, DEAL_PROFIT);
      OrderType = HistoryDealGetInteger(TicketNumber, DEAL_TYPE);
      MySymbol = HistoryDealGetString(TicketNumber, DEAL_SYMBOL);
      DealEntry = HistoryDealGetInteger(TicketNumber, DEAL_ENTRY);
      Volume = HistoryDealGetDouble(TicketNumber, DEAL_VOLUME);
      DateTimeOpenLastOp = HistoryDealGetInteger(TicketNumber, DEAL_TIME);
      if (MySymbol == _Symbol) {
        if (OrderType == ORDER_TYPE_BUY || OrderType == ORDER_TYPE_SELL) {
          if (DealEntry == 1) {
            if (OrderType == ORDER_TYPE_BUY) {
              PositionDirection = "SELL-TRADE";
            }
            if (OrderType == ORDER_TYPE_SELL) {
              PositionDirection = "BUY-TRADE";
            }
            LastTime = DateTimeOpenLastOp;
          }
        }
      }
    }
  }
  return LastTime;
}
 

You reset and re-initialize your 'DateTimeOpenLastOp' variable each time you loop. You need to compare it with another variable.

Note: not an "order", but a "deal". It is important.

Example:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
datetime GetLastTradeOpenTime()
  {
   datetime time=0;
//---
   uint TotalNumberOfDeals = HistoryDealsTotal();
   ulong TicketNumber = 0 ;
   long DealType, DealEntry;
   double DealProfit = 0 ;
   string MySymbol = "" ;
   string PositionDirection = "" ;
   string MyProfit = "" ;
   double Volume = 0 ;
   datetime DateTimeOpenLastOp = "" ;

   HistorySelect(0, TimeCurrent());

   for(uint i = 0 ; i < TotalNumberOfDeals; i++)
     {
      if((TicketNumber = HistoryDealGetTicket(i)) > 0)
        {
         DealProfit = HistoryDealGetDouble(TicketNumber, DEAL_PROFIT);
         DealType = HistoryDealGetInteger(TicketNumber, DEAL_TYPE);
         MySymbol = HistoryDealGetString(TicketNumber, DEAL_SYMBOL);
         DealEntry = HistoryDealGetInteger(TicketNumber, DEAL_ENTRY);
         Volume = HistoryDealGetDouble(TicketNumber, DEAL_VOLUME);
         DateTimeOpenLastOp = HistoryDealGetInteger(TicketNumber, DEAL_TIME);
         if(MySymbol == _Symbol)
           {
            if(DealType == DEAL_TYPE_BUY || DealType == DEAL_TYPE_SELL)
              {
               if(DealEntry == DEAL_ENTRY_OUT)
                 {
                  if(DealType == DEAL_TYPE_BUY)
                    {
                     PositionDirection = "SELL-TRADE" ;
                    }
                  if(DealType == DEAL_TYPE_SELL)
                    {
                     PositionDirection = "BUY-TRADE" ;
                    }
                  if(DateTimeOpenLastOp>time)
                    {
                     time=DateTimeOpenLastOp;
                    }
                 }
              }
           }
        }
     }
//---
   return(time);
  }
//+------------------------------------------------------------------+