Commission

 
How to get commision forom opened positions? I can get volume, OP, swap... but commision?
 
David Diez: How to get commision forom opened positions? I can get volume, OP, swap... but commision?

How about doing a search on MQL5 website? ... https://www.mql5.com/en/docs/standardlibrary/tradeclasses/cpositioninfo/cpositioninfocommission

 

If you don't like using OOP and the Trading Class, then use "POSITION_COMMISSION" with PositionGetDouble().

 
Fernando Carreiro:

If you don't like using OOP and the Trading Class, then use "POSITION_COMMISSION" with PositionGetDouble().

You can't get POSITION_COMMISSION through PositionGetDouble().

 
David Diez: You can't get POSITION_COMMISSION through PositionGetDouble().
Sorry, my fault! Use "DEAL_COMMISSION" with "HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION)";
 
Fernando Carreiro:

How about doing a search on MQL5 website? ... https://www.mql5.com/en/docs/standardlibrary/tradeclasses/cpositioninfo/cpositioninfocommission

This does not work

//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                                Copyright 2021, Simón Del Vecchio |
//|                    https://www.mql5.com/es/users/simondelvecchio |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Simón Del Vecchio"
#property link      "https://www.mql5.com/es/users/simondelvecchio"
#property version   "1.00"

#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
CTrade Trade;
CPositionInfo Position;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   Trade.Buy(0.01, NULL, 0, 0, 0, "Test_1");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//The comment works fine but the commission doesn't
   Position.SelectByIndex(0);
   Comment("Comment: ", Position.Comment(),
           "  ",
           "Commission: ", Position.Commission());
  }
//+------------------------------------------------------------------+
 
Antonio Simon Del Vecchio:

This does not work

it is  DEAL_COMMISSION

 
Antonio Simon Del Vecchio:

This does not work

This code works correctly if there is commission in the last opened order.

 
How to get comission and swap easy
How to get comission and swap easy
  • 2018.09.05
  • www.mql5.com
Hello. I made a simple script that prints information about positions and deals...
 

Function that returns the commission of the position

//+------------------------------------------------------------------+
//| Receives the position ticket and returns the commission          |
//+------------------------------------------------------------------+
double Comision(ulong TicketPos)
  {
   HistorySelect(iTime(NULL, PERIOD_D1, 7), TimeCurrent());
   for(int i = 0; i < HistoryDealsTotal(); i++)
     {
      ulong TicketDeal = HistoryDealGetTicket(i);
      if(TicketPos == HistoryDealGetInteger(TicketDeal, DEAL_POSITION_ID))
        {
         return(HistoryDealGetDouble(TicketDeal, DEAL_COMMISSION));
        }
     }
   return(0);
  }
 
Fernando Carreiro:
Sorry, my fault! Use "DEAL_COMMISSION" with "HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION)";
Antonio Simon Del Vecchio:

This does not work

Nguyen Phuong Hoang:

it is  DEAL_COMMISSION

Mehmet Bastem:

This code works correctly if there is commission in the last opened order.

Tried through this lines and I'm still getting wrong values:

      for(int j=0;j<PositionsTotal();j++){
         ulong jTicket=PositionGetTicket(j);
         if(PositionGetString(POSITION_SYMBOL)==SName){
            double PV=PositionGetDouble(POSITION_VOLUME);
            double Swap=PositionGetDouble(POSITION_SWAP);
            double Com=HistoryDealGetDouble(jTicket,DEAL_COMMISSION);
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
               if(FirstOrderTP==true||LongPos>1){
                  double Breakeven=NormalizeDouble(LongBEF/PV++,SDigits);
                  Swap++;if(Swap>0){Print("Positions: ",LongPos,", SwapLong: ",Swap);}
                  Com++;if(Com>0){Print("Long positions: ",LongPos,", Commission: ",Com);}
                  }
               if(Martingale>0){
                  }
               }
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){
               if(FirstOrderTP==true||ShortPos>1){
                  double Breakeven=NormalizeDouble(ShortBEF/PV++,SDigits);
                  Swap++;if(Swap>0){Print("Positions: ",ShortPos,", SwapLong: ",Swap);}
                  Com++;if(Com>0){Print("Short positions: ",ShortPos,", Commission: ",Com);}
                  }
               if(Martingale>0){
                  }
               }
            //if(StopLoss){CloseFIFO;}
            }
         }

2021.08.18 09:49:27.787 Core 1 2016.12.19 12:02:00   Positions: 2, SwapLong: 1.0

2021.08.18 09:49:27.787 Core 1 2016.12.19 12:02:00   Long positions: 2, Commission: 1.0

2021.08.18 09:49:27.787 Core 1 2016.12.19 12:02:20   Positions: 3, SwapLong: 1.0

2021.08.18 09:49:27.787 Core 1 2016.12.19 12:02:20   Long positions: 3, Commission: 1.0

Reason: