MQL5 retrieving commissions and swaps of an open position

in MQL5 how can I get the commissions and swaps of an open position (by ticket)? is this not possible?

so is this 100% correct?

Is it possible to retrieve the commissions and/or swaps of an open position?

I understand the field exists in the deal for this but I remember hearing somewhere in MT5 (unlike MT4) you can’t get commissions on open positions.

but I’m not sure if what I read was 100% correct because it was not documentation but in a forum post, as well it didn’t mention anything about swaps.

Wouldn’t a deal just be when you enter or exit the position, not the current status of a position? so I don’t see how swaps could be present in this situation as they are not charged/credited upon opening a position.

t depends on your broker (and system patches from MQL5).

There is a bit of history regarding this on commission.

A long time back, commission was absent in the positions and deals for both the demo account , and live account.

Commission was only calculated into your live statement as a separate entry. It is the gross amount including exchange fee if any.

I have checked my old indicator, and it can now grab commission from each deal on the demo account, which is great. (Just use double comm = HistoryDealGetDouble(ticket, DEAL_COMMISSION) )

But on live account, commission is still absent.

So in the meantime, you have to configure this number into your trading system.

I have checked with my broker and the help desk says that MQL5 is going to patch their system to enable commission during real time trading on live account.

I asked for it because if they do not enable this feature, the traders may lose more than the deposit… It is bad for the traders as well as for the broker.

Equity calculation will be wrong if commission is not computed into the deals. On some cases, commission is actually dynamic depending on the sign up package with the broker.

So in actual fact, commission is a dynamic number, not something that can be logically computed according to lot size, but depend on the broker’s own rules in server.

So you might have to wait for this feature to be added in, some time this year. (This is a 100% confirmation from my side.)

You have to ask the help desk of your broker regarding this.

P.S. I can’t comment on Swaps, because I have no experience retrieving this on MQL5. Only on MQL4 using OrderSwap(). Need others to comment on this.

thanks for the info, really a great explanation.

About Swaps I was asking because it is, by nature, hard to test - was yesterday at least… luckily today I still had some positions open, and could now see that the POSITION_SWAP works because it is now a non-zero value in the terminal.

If you can see on your terminal, you should be able to extract it. They differentiate position, order and deal on MQL5.

#include <MT4Orders.mqh> 
 for (int i = 0; i < OrdersHistoryTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
      {
         if (OrderSymbol() == Symbol() && (OrderType() == OP_SELL || OrderType() == OP_BUY) && OrderMagicNumber() == MagicNumber && OrderCloseTime()!=0){
            if ( OrderOpenTime() >= date) {
               DailyProfit=DailyProfit+(OrderProfit()+OrderSwap()+OrderCommission());
               TotalDailyOrders++;
               if(OrderType()==OP_BUY)  DailyPips += NormalizeDouble(OrderClosePrice()-OrderOpenPrice(),_Digits);
               if(OrderType()==OP_SELL) DailyPips += NormalizeDouble(OrderOpenPrice()-OrderClosePrice(),_Digits);
            }     
         }   
      }
   }

can this MQL4 simulation library place async trades? (or close trades asynchronously also?)

Yes.

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

bool CloseAllPositionsAsync( const bool AllSymbols = true, const int Slippage = 0 )
{
  bool Res = true;
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS) && (OrderType() <= OP_SELL) &&
        (AllSymbols ? true : (OrderSymbol() == Symbol())))
      Res &= OrderCloseAsync(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage);
      
  return(Res);
}