Total Commissions of all open orders.

To add comments, please log in or register
Chee Chua
734
Chee Chua  

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!

Alois Mwirichia
168
Alois Mwirichia  
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
William Roeder
18299
William Roeder  
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
Chee Chua
734
Chee Chua  
Sorry forget to mention - MT5
lippmaje
605
lippmaje  
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.
fxsaber
12704
fxsaber  
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);
}
To add comments, please log in or register