Keeping track of profit per pair traded

 

Hey guys,

Is there any way to keep track of the profit that the same EA has made per pair while trading?

Thanks 

 

Yep, use Magic Numbers.

You can then either code your own means to track the trade, or simply connect the account to one of the sites offering trade explorers that can break down by magic number and symbol.

 

Loop through closed orders and sum profit for those with the magic number and symbol.

If you need to check open orders, loop through the open orders and add those with the magic number and symbol.

 
GumRai:

Loop through closed orders and sum profit for those with the magic number and symbol.

If you need to check open orders, loop through the open orders and add those with the magic number and symbol.

GumRai:

 

I am using the code below to try and loop through closed orders and then sum profits by symbol with no success (I did change OrdersTotal() to OrdersHistoryTotal() and MODE_TRADES to MODE_HISTORY). Can you help? I am trying to return the total profit for ALL trades of one symbol in the Account History tab (prefer not to use Magic #s). Thanks in advance for your assistance.

   {
   int i, iOrders = OrdersTotal() - 1;        //defining running parameter for below
   double sum_profits = 0;
  
   for(i = iOrders; i >= 0; i--)
   {
   OrderSelect( i, SELECT_BY_POS, MODE_TRADES );     
      if( Symbol() == OrderSymbol() &&
          OrderMagicNumber() == Magic )
      {
          sum_profits += OrderProfit();
      }        
   }
   
   return( sum_profits ); 

   } 

 
7455:

GumRai:

 

I am using the code below to try and loop through closed orders and then sum profits by symbol with no success (I did change OrdersTotal() to OrdersHistoryTotal() and MODE_TRADES to MODE_HISTORY). Can you help? I am trying to return the total profit for ALL trades of one symbol in the Account History tab (prefer not to use Magic #s). Thanks in advance for your assistance.

   } 

I don't see anything wrong with the code that you have posted.

It should give you net profits for open orders that are of the chart symbol and the EA's magic number.

If you prefer to check without the magic number, then remove the check.

Show your code for checking closed trades.

 
GumRai:

I don't see anything wrong with the code that you have posted.

It should give you net profits for open orders that are of the chart symbol and the EA's magic number.

If you prefer to check without the magic number, then remove the check.

Show your code for checking closed trades.

Here my code. The sum profit seemingly changes when an order for the symbol has not closed (I run the EA on several charts/symbols)

{

  int i, iOrders = OrdersHistoryTotal() - 1; 

   double sum_profits = 0;

 

   for(i = iOrders; i >= 0; i--)

   {

   OrderSelect( i, SELECT_BY_POS, MODE_HISTORY);    

      if( Symbol() == OrderSymbol())

)

      {

          sum_profits += OrderProfit();

      }       

   }

  

   return( sum_profits ); 
 
      if( Symbol() == OrderSymbol())

   )


Does the code compile?

You seem to have a stray close bracket

 
GumRai:


Does the code compile?

You seem to have a stray close bracket

No, it did not compile. However, I changed to a custom function (see below), but it still does not seem to calculate correctly. Is the code correct to sum the total of ALL profits for a particular symbol of ALL closed orders of that symbol found in the Account History tab?

 double Money()

 {

   double sum_profits = 0;

   int i, iOrders = OrdersHistoryTotal() - 1;        //defining running parameter for below

   

   for(i = iOrders; i >= 0; i--)

  {

   if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)==true)

   {    

      if( Symbol() == OrderSymbol())  sum_profits += OrderProfit();

   }

  } 

   return( sum_profits );

}     

 

The only thing that I can see is that you don't include commisions and swaps,

 
      sum_profits += OrderProfit()+OrderCommission()+OrderSwap();
 
GumRai:

The only thing that I can see is that you don't include commisions and swaps,

Great!! I'll give it a whirl and let  you know. Thanks!
Reason: