How to get the last deal's and pre-last deal's profit and deal type for MQL5?

 
Can someone help me with MQL5 coding, I have tried thousand ways to get the profit and deal type from the history for the last 3 deals (last, before the last and the before before-the-last) but always failed. Would be grateful for any help with code.
 
Hakob Grigoryan :
Can someone help me with MQL5 coding, I have tried thousand ways to get the profit and deal type from the history for the last 3 deals (last, before the last and the before before-the-last) but always failed. Would be grateful for any help with code.

We do not see your code. Show you the MQL5 code.

 
Vladimir Karputov #:

We do not see your code. Show you the MQL5 code.

Dear Vladimir thanks for the response,

The code below is for the deal type of the before the before-last deal (if last is our N-th then this deal is (N-2)th)

int DealType3(int nPosic=3)
  {
   int type=0;
//GET INTO HISTORY
   HistorySelect(0,TimeCurrent());
   int      i, nTrans= 0;
   int count=1;
   ulong    ticket= 0;
   ENUM_DEAL_ENTRY tipoTrans= DEAL_ENTRY_IN;
   string   simbPosic= "";
   datetime TransTime= PositionSelect(_Symbol)? (datetime)PositionGetInteger(POSITION_TIME): TimeCurrent();
   if(HistorySelect(0, TransTime-1))
   {
      nTrans= HistoryDealsTotal();
      for(i= nTrans-1; i>=0; i--)
      {
         ticket= HistoryDealGetTicket(i);
         simbPosic= HistoryDealGetString(ticket, DEAL_SYMBOL);
         tipoTrans= (ENUM_DEAL_ENTRY)HistoryDealGetInteger(ticket, DEAL_ENTRY);
         if(simbPosic==_Symbol && tipoTrans==DEAL_ENTRY_IN)
         {
         if(count!=nPosic && simbPosic==_Symbol && ticket>0)
         
           { 
            Print("+ one step count ");
            count++;
           }
         else if(count==nPosic && simbPosic==_Symbol && ticket>0)
           {
           if(HistoryDealGetInteger(ticket,DEAL_TYPE)==DEAL_TYPE_BUY)
            {
             type=2;
             Print("Deal3= ",type);
             break;
            }
           else if(HistoryDealGetInteger(ticket,DEAL_TYPE)==DEAL_TYPE_SELL)
            {
             type=1;
             Print("Deal3= ",type);
             break;
            } 
           } 

         }
      }
   }
   return(type);
   Print("Deal3= ",type);
} 
 

Why request ALL trading history? You need to make ONE request for the entire history and remember the date of the transaction N. Then you will make a request from the saved date to the current date. I didn't look further at the code.

 
Vladimir Karputov #:

Why request ALL trading history? You need to make ONE request for the entire history and remember the date of the transaction N. Then you will make a request from the saved date to the current date. I didn't look further at the code.

Could you please clarify a bit with some example? I was trying to get the deal type by position ID. Basically I am trying to make an EA which performs trading and starting from the 4th deal I want to track the deal type and profits of the last 3 deals. In other words starting from the 4th position and so on I need Deal Type and Profit of last 3 deals to make decision on future transactions

 

Code:

//+------------------------------------------------------------------+
//|                                        Last N DEAL_ENTRY_OUT.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//|                      https://www.mql5.com/en/users/barabashkakvn |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property link      "https://www.mql5.com/en/users/barabashkakvn"
#property version   "1.000"
//--- input parameters
input uchar    InpN        = 3;  // N
input datetime InpFromDate = 0;  // From Date (D'1970.01.01 00:00' -> OFF parameter)
input string   InpSymbol   = ""; // Symbol ("" -> all symbols)
input long     InpMagic    = -1; // Magic number (<0 -> all magics)
//---
datetime m_from_date       = 0;  // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- request trade history
   HistorySelect(InpFromDate,TimeTradeServer()+60*60*24*3);
   uint total_deals=HistoryDealsTotal();
   ulong ticket_history_deal=0;
   int counter=0;
   string text="";
//--- for all deals
   for(uint i=total_deals-1; i>=0; i--)
     {
      //--- try to get deals ticket_history_deal
      if((ticket_history_deal=HistoryDealGetTicket(i))>0)
        {
         long     deal_ticket       =HistoryDealGetInteger(ticket_history_deal,DEAL_TICKET);
         long     deal_time         =HistoryDealGetInteger(ticket_history_deal,DEAL_TIME);
         long     deal_type         =HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE);
         long     deal_entry        =HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY);
         long     deal_magic        =HistoryDealGetInteger(ticket_history_deal,DEAL_MAGIC);
         double   deal_commission   =HistoryDealGetDouble(ticket_history_deal,DEAL_COMMISSION);
         double   deal_swap         =HistoryDealGetDouble(ticket_history_deal,DEAL_SWAP);
         double   deal_profit       =HistoryDealGetDouble(ticket_history_deal,DEAL_PROFIT);
         string   deal_symbol       =HistoryDealGetString(ticket_history_deal,DEAL_SYMBOL);
         //---
         if((InpSymbol==deal_symbol || InpSymbol=="") && (InpMagic==deal_magic || InpMagic<0))
           {
            if(deal_entry==DEAL_ENTRY_OUT)
              {
               counter++;
               string time=TimeToString((datetime)deal_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS);
               text=text+"\n"+time+" | "+DoubleToString(deal_commission+deal_swap+deal_profit,2);
               if(counter==InpN)
                 {
                  m_from_date=(datetime)deal_time;
                  break;
                 }
              }
           }
        }
     }
   Comment(text);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- request trade history
   HistorySelect(m_from_date,TimeTradeServer()+60*60*24*3);
   uint total_deals=HistoryDealsTotal();
   ulong ticket_history_deal=0;
   int counter=0;
   string text="";
//--- for all deals
   for(uint i=total_deals-1; i>=0; i--)
     {
      //--- try to get deals ticket_history_deal
      if((ticket_history_deal=HistoryDealGetTicket(i))>0)
        {
         long     deal_ticket       =HistoryDealGetInteger(ticket_history_deal,DEAL_TICKET);
         long     deal_time         =HistoryDealGetInteger(ticket_history_deal,DEAL_TIME);
         long     deal_type         =HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE);
         long     deal_entry        =HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY);
         long     deal_magic        =HistoryDealGetInteger(ticket_history_deal,DEAL_MAGIC);
         double   deal_commission   =HistoryDealGetDouble(ticket_history_deal,DEAL_COMMISSION);
         double   deal_swap         =HistoryDealGetDouble(ticket_history_deal,DEAL_SWAP);
         double   deal_profit       =HistoryDealGetDouble(ticket_history_deal,DEAL_PROFIT);
         string   deal_symbol       =HistoryDealGetString(ticket_history_deal,DEAL_SYMBOL);
         //---
         if((InpSymbol==deal_symbol || InpSymbol=="") && (InpMagic==deal_magic || InpMagic<0))
           {
            if(deal_entry==DEAL_ENTRY_OUT)
              {
               counter++;
               string time=TimeToString((datetime)deal_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS);
               text=text+"\n"+time+" | "+DoubleToString(deal_commission+deal_swap+deal_profit,2);
               if(counter==InpN)
                 {
                  m_from_date=(datetime)deal_time;
                  break;
                 }
              }
           }
        }
     }
   Comment(text);
  }
//+------------------------------------------------------------------+

Result:


Files:
 
Vladimir Karputov #:

Code:

Result:


Thank you very much for your help, will take a look to the code
 
Vladimir Karputov #:

Code:

Result:


Checked and modified for me, works perfectly, thank you very much again!!! Have a good day
Reason: