How to get comission and swap easy

 

Hello. I made a simple script that prints information about positions and deals. I noticed that PositionGetDouble(POSITION_COMMISSION) does not return comission data, and HistoryDealGetDouble(_ticket,DEAL_SWAP) does not return swap data. Is there an easier way to get these data or am I doing something wrong?

#include <Trade\PositionInfo.mqh>

CPositionInfo     cc_position;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() {
        Print("##### INICIO del SCRIPT ####");
//---
        Print("####### POSITIONs using PositionInfo.mqh ##########");
        for( int i = 0 ; i <= PositionsTotal(); i++ ) 
                if( cc_position.SelectByIndex(i) )      // Searching for new open trades
                        printf("## ORDER %d in %s: PnL= %g ( Commission= %g + Swap= %g )", 
                                cc_position.Ticket(), 
                                cc_position.Symbol(),
                                cc_position.Profit(), 
                                cc_position.Commission(), 
                                cc_position.Swap() );

        Print("####### POSITION using PositionSelect() ##########");
        PositionSelect( _Symbol );
        printf("## POSITION %d in %s: PnL= %g ( Commission= NA + Swap= %g )", 
                PositionGetInteger(POSITION_TICKET),
                PositionGetString (POSITION_SYMBOL),
                PositionGetDouble (POSITION_PROFIT),
                PositionGetDouble (POSITION_SWAP) );
                
        Print("####### DEALs using HistoryDealSelect() ##########");
        ulong _ticket=0;
        HistorySelect( iTime(NULL, PERIOD_D1,4), TimeCurrent());
        for ( int i = 0; i <= HistoryDealsTotal(); i++ )  {
                
                if ( (_ticket=HistoryDealGetTicket(i)) > 0 && HistoryDealGetString(_ticket,DEAL_SYMBOL) == _Symbol )
                        printf("## DEAL %d (order %d) in %s: lots=%g; PnL=%g ( Commission= %g + Swap= %g )",
                                _ticket,
                                HistoryDealGetInteger(_ticket,DEAL_ORDER),
                                HistoryDealGetString(_ticket,DEAL_SYMBOL),
                                HistoryDealGetDouble(_ticket,DEAL_VOLUME),
                                HistoryDealGetDouble(_ticket,DEAL_PROFIT),
                                HistoryDealGetDouble(_ticket,DEAL_COMMISSION),
                                HistoryDealGetDouble(_ticket,DEAL_SWAP) );
        }
//---  
        Print("##### FINAL del SCRIPT ####");
}


Output of the above script where I highlighted the discrepancies


 

I just noticed the following code within PositionInfo.mqh

//+------------------------------------------------------------------+
//| Get the property value "POSITION_COMMISSION"                     |
//+------------------------------------------------------------------+
double CPositionInfo::Commission(void) const
  {
   return(PositionGetDouble(POSITION_COMMISSION));
  }

I cannot find the property POSITION_COMISSION listed in MQL5 documentation

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
 

Forum on trading, automated trading systems and testing trading strategies

Question about pending orders access

fxsaber, 2018.03.27 19:43

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

void OnStart()
{
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS))
      OrderPrint();
}

OrderSwap() and OrderCommission().

 

I have encountered the same problem.