MQL5 selecting the latest closed deal in history

I have a code that scans all closed deals ( hedging ) in history but I want it to select the latest close deal. This code do

//--- variables for returning values from latest deal  properties +++++++++++++++++++
 

   ulong deal_ticket;            // deal ticket 
   ulong order_ticket;           // ticket of  the deal was executed on 
   datetime transaction_time;    // time of a deal execution 
   long position_ID;             // position ID 
  
  
   string Dsymbol;                // symbol of the deal 
//--- set the start and end date to request the history of deals 
   datetime from_date=0;         // from the very beginning 
   datetime to_date=TimeCurrent();// till the current moment 
//--- request the history of deals in the specified period 
   HistorySelect(from_date,to_date); 
//--- total number in the list of deals 
   int deals=HistoryDealsTotal(); 
//--- now process each trade 
   for(int i=0;i<deals;i++) 
    { 
     if((deal_ticket=HistoryDealGetTicket(i))>0) 
   
     { 
      transaction_time=(datetime)HistoryDealGetInteger(deal_ticket,DEAL_TIME); 
      order_ticket=              HistoryDealGetInteger(deal_ticket,DEAL_ORDER); 
      Dsymbol=                    HistoryDealGetString(deal_ticket,DEAL_SYMBOL); 
      position_ID=               HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID); 
       
      
     } 
  
} 

es not seem to work properly any suggested tweaks?

#include <MT4Orders.mqh> 

void LastTimeMQL4( datetime &OpenTime, datetime &CloseTime )
{
  for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)  
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderType() <= OP_SELL))
    {
      OpenTime = OrderOpenTime();
      CloseTime = OrderCloseTime();
      
      break;
    }
}

void LastTimeMQL5( datetime &OpenTime, datetime &CloseTime )
{
  if (HistorySelect(0, INT_MAX))
  {
    for (int i = HistoryDealsTotal() - 1; i >= 0; i--)
    {
      const ulong Ticket = HistoryDealGetTicket(i);
  
      if (HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
      {
        CloseTime = (datetime)HistoryDealGetInteger(Ticket, DEAL_TIME);

        if (HistorySelectByPosition(HistoryDealGetInteger(Ticket, DEAL_POSITION_ID)))
          OpenTime = (datetime)HistoryDealGetInteger(HistoryDealGetTicket(0), DEAL_TIME);
          
        break;
      }
    }
  }
}

Thanks,

So how do I access the deal profit figure outside the function?

Sorry not familiar with the void type function. Just below, on the on tick section of my EA I want to retrieve order profit and close time but I’m getting error ‘undeclared identifier’ even if i declare it, the programme doesn’t compute the profit.

I just want to do this( if lastdeal loss) …then buy… etc

icortenb
Please help

You need to change the function from fxsaber to return the ticket like this:

bool LastClosedDeal(ulong &ticket, datetime &closeTime)
{
  bool found=false;
  if (HistorySelect(0,TimeCurrent()))
  {
    for (int i=HistoryDealsTotal()-1; i>=0; i--)
    {
      ticket=HistoryDealGetTicket(i);
  
      if (HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT)
      {
        found=true;
        closeTime=(datetime)HistoryDealGetInteger(Ticket,DEAL_TIME);
        break;
      }
    }
  }
  return found;
}

and then invoke it to get the ticket of it:

ulong ticket;
datetime closeTime;

if(LastClosedDeal(ticket, closeTime)) {
   // check if it's been a loss
   ...
}

To check for a loss you need to take the ticket and find out if it had profit with DEAL_PROFIT.