Calculating total Profit/Loss of an account?

 

Hi all.


I am trying to calculate total Profit/Loss of the account but everything I have tried so far has only resulted in failure

its a demo account with deposit : 100K
Profit/Loss : (negative)-31009.63
No active trade

Here is what I have attempted till now :

AccountInfoDouble(ACCOUNT_PROFIT); 

//it returned 0.0  


Then I tried calculating Profit/Loss of all historic trades like this : 

   double tempproloss = 0;
   for(int i=0; i<OrdersHistoryTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         if(OrderType()<=5)
           {
            tempproloss =  tempproloss+OrderProfit();

           }
        }
     }
//This returned -6188.09 which is still not correct 


Then I tried calculating Profit/Loss Of all orders including deposits etc , like this :

double tempproloss = 0;
for(int i=0;i<OrdersHistoryTotal();i++)
  {
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
     {
           tempproloss =  tempproloss+OrderProfit();
     }
  } 

//It returned 29406.49 , no idea how it ended up with this number.


Then I got the Deposit value in a variable and then I tried Calculating Profit/Loss of all closed trades (Not including the deposit etc) and then Subtracted it from Deposit amount , like this :

 
double Deposits = 0;
for(int i=0;i<OrdersHistoryTotal();i++)
  {
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
     {
      if(OrderType() == 6)
        {
        if(OrderProfit()>0)
          {
           Deposits =  Deposits+OrderProfit();
          }
        
        }
     }
  }   
double tempproloss = 0;
for(int i=0;i<OrdersHistoryTotal();i++)
  {
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
     {
     if(OrderType()<6)
       {
           tempproloss =  tempproloss+OrderProfit();
       }
     }
  }  
 Print( Deposits -tempproloss );

// it Returned 106188.09 which wrong again 


Been struggling with this for quite some time, but couldn't figure out a way to make it work , can someone please point out What am I missing ?

Thank you.
 

 

Make sure that in the Account History tab that all history is showing (click on it, then right click anywhere inside of it -> All History)

 

Don't forget to include the Commission and Swap.
double sum  =  OrderProfit() + OrderCommisssion() + OrderSwap();
tempproloss =  tempproloss + sum;

I've never used the bottom code before and it's a weekend so I can't test it, but this could be your open P&L, and that's why it's 0.0:

AccountInfoDouble(ACCOUNT_PROFIT); 
 
Alexander Martinez #:

Make sure that in the Account History tab that all history is showing (click on it, then right click anywhere inside of it -> All History)

 

Don't forget to include the Commission and Swap.

I've never used the bottom code before and it's a weekend so I can't test it, but this could be your open P&L, and that's why it's 0.0:

Hi @Alexander Martinez Thank you for your reply and yes All History is showing in the history tab 


Don't forget to include the Commission and Swap. 

This saved the day, I wasnt adding the commissions and swaps , after adding this Now I am getting the exact P&L perfectly , Thank you 

Alexander Martinez
Alexander Martinez
  • 2019.04.28
  • www.mql5.com
Trader's profile
 
Rhodi_ #:

Hi @Alexander Martinez Thank you for your reply and yes All History is showing in the history tab 


Don't forget to include the Commission and Swap. 

This saved the day, I wasnt adding the commissions and swaps , after adding this Now I am getting the exact P&L perfectly , Thank you 

Glad it could help. Take care, Rhodi. 👍
 
Alexander Martinez #:
Glad it could help. Take care, Rhodi. 👍

Thanks Amigo