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!
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.
Do not count on OrderCommission/OrderSwap. Some brokers don’t use those fields.
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.
#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);
}