How to access the second or third deal in MT5 history ?

 

Hello friends,

The following function is designed to search the history of the deals then it breaks when it finds the first deal out from below.

Is there a way in MQL5 to find the second or the third deal from below, and extract information from it?

//+------------------------------------------------------------------+
//| GET LAST ORDER HISTORY INFO                                      |
//+------------------------------------------------------------------+
void GetFirstHistory(long &type, double &lot, double &profits)
  {
if(HistorySelect(0, TimeCurrent()) == true)
 {
for(int i = HistoryDealsTotal() - 1; i >= 0; i-- )
  {
ulong ticket = HistoryDealGetTicket(i);

if(ticket > 0)
if(HistoryDealGetInteger(ticket, DEAL_MAGIC) == _Magicnumber)
if(HistoryDealGetInteger(ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
 { 
type    = HistoryDealGetInteger(ticket, DEAL_TYPE);
lot     = HistoryDealGetDouble(ticket,  DEAL_VOLUME);
profits = HistoryDealGetDouble(ticket,  DEAL_PROFIT);
break;  
 } } } }

A screenshot to show what I want to do

select 2nd deal out in history

 
Muhammad Elbermawi:

Hello friends,

The following function is designed to search the history of the deals then it breaks when it finds the first deal out from below.

Is there a way in MQL5 to find the second or the third deal from below, and extract information from it?

A screenshot to show what I want to do


Why don't you store the one found and start searching from there?
 
_MAHA_ #:
Why don't you store the one found and start searching from there?
How to do it? ... Show your code.
 

FYI.

void GetFirstHistory(ulong magic,string symbol,long &type, double &lot, double &profits,int counter = 1)
{
   int cnt = 0;
   if(HistorySelect(0, TimeCurrent()) == true)
   {
      for(int i = HistoryDealsTotal() - 1; i >= 0; i-- )
      {
         ulong ticket = HistoryDealGetTicket(i);
         if(ticket > 0)
         {
            if(HistoryDealGetInteger(ticket, DEAL_MAGIC) == magic && HistoryDealGetString(ticket,DEAL_SYMBOL)==symbol && HistoryDealGetInteger(ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
            {
               type    = HistoryDealGetInteger(ticket, DEAL_TYPE);
               lot     = HistoryDealGetDouble(ticket,  DEAL_VOLUME);
               profits = HistoryDealGetDouble(ticket,  DEAL_PROFIT);
               if(++cnt>=counter)break;
            }
         }
      }
   }
}
 
Ziheng Zhuang #:

FYI.

So, you added a counter that breaks when it reaches the desired record in history.
That is a clever solution.

Thanks, Z.Z 🙏
Reason: