Total Commissions of all open orders.

 

How do I get the total commissions of multiple open positions?

How do I go through all the open positions and adding them up?

Thanks!

 
This can be achieved using a for loop and the OrdersTotal function. In the loop increment a variable with OrderCommission function. Exit when done.
OrderCommission - Trade Functions - MQL4 Reference
OrderCommission - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderCommission - Trade Functions - MQL4 Reference
 
Do not count on OrderCommission/OrderSwap. Some brokers don't use those fields.
          please help: Mt4 commission - Trading 4Accounts - MQL4 4and 4MetaTrader 44 - MQL4 4programming 4forum
 
Sorry forget to mention - MT5
 
double SumUpCommissions(string symbol=NULL)
  {
   double sum=0;
   if(symbol==NULL) symbol=_Symbol;

   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      if(PositionGetSymbol(i)==symbol)
         sum+=PositionGetDouble(POSITION_COMMISSION);
     }
   return sum;
  }
Here's a simple function that could be used in a script.
 
Chee Chua:
Sorry forget to mention - MT5
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

double TotalCommission()
{
  double Sum = 0;
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS))
      Sum += OrderCommission();
      
  return(Sum);
}