Deal profits of the last 3 closed orders

 

I've a code that determines the deal profit of the last closed deal below.

How do I modify it to return the profit of the  deal before the last  one e.g deal 2 in history and the one before e.g deal 3?

My code for the profit of the latest code:

// Profit of the latest closed deal:
double deal_profitY()
  {

   ulong ticket;
   datetime closeTime=0;
   bool found=false;
    double deal_profit=0;
    //ulong order_magic;
     string   symbol;
   if(HistorySelect(0,TimeCurrent()))
     {
      for(int i=HistoryDealsTotal()-1; i>=0; i--)
        {
         ticket=HistoryDealGetTicket(i);
         symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);

         if((HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT)&&symbol==Symbol())
           {
            found=true;
            deal_profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
            break;
           }
        }
     }
   return ( deal_profit);
  }
 
Peter Kaiza:

I've a code that determines the deal profit of the last closed deal below.

How do I modify it to return the profit of the  deal before the last  one e.g deal 2 in history and the one before e.g deal 3?

My code for the profit of the latest code:

Here's one way (call "deal_profitY(1)" for last profit, "deal_profitY(2)" for 2nd last, and so on...):

double deal_profitY(int num)
{
   :
   :
   
         if((HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT)&&symbol==Symbol())
           {
            if (--num==0)
            {
               deal_profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
               break;
            }
           }
   :
}
 
Seng Joo Thio:

Here's one way (call "deal_profitY(1)" for last profit, "deal_profitY(2)" for 2nd last, and so on...):

Will this part of the code stay the same  throughout the three calls?
for(int i=HistoryDealsTotal()-1; i>=0; i--)
        {
 
Peter Kaiza:
Will this part of the code stay the same  throughout the three calls?
Yes
 

Code doesn't work , i.e doesnt retrieve profit of the last 4th trade in the list, remember this trade profit has to change because trade history is constantly update. 

double deal_profitT4(int num)
  {

   ulong ticket;
   datetime closeTime=0;
   bool found=false;
    double deal_profit=0;
    //ulong order_magic;
     string   symbol;
   if(HistorySelect(0,TimeCurrent()))
     {
      for(int i=HistoryDealsTotal()-1; i>=0; i--)
        {
         ticket=HistoryDealGetTicket(i);
         symbol=HistoryDealGetString(ticket,DEAL_SYMBOL);

         if((HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT)&&symbol==Symbol())
           {
             if (num==4)
            {
               deal_profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
               break;
            }
           }
        }
     }
        return (deal_profit);
 }
//==============================================
 
Peter Kaiza:

Code doesn't work , i.e doesnt retrieve profit of the last 4th trade in the list, remember this trade profit has to change because trade history is constantly update. 

My Suggestion Your Changes
if (--num==0)
if (num==4)
 
Seng Joo Thio:
My Suggestion Your Changes

Faulty code only returns the third deal on the list.

 
Peter Kaiza:

Faulty code only returns the third deal on the list.

any other suggestions?

 
Peter Kaiza:

Code doesn't work , i.e doesnt retrieve profit of the last 4th trade in the list, remember this trade profit has to change because trade history is constantly update. 

your code working good but please I put in the code with no mistake is it now working 
Reason: