Commission of a running position

 

Hello guys,

I made this script but it only shows 0.0 as a result when I drag the script into a chart with a running position. Can somebody tell me what's wrong with the code?

void OnStart() {
   for (int i=0;i<PositionsTotal();i++) {
      string symbol=PositionGetSymbol(i);
      if (symbol==_Symbol) {
         ulong ticket=PositionGetTicket(i);
         Print(HistoryDealGetDouble(ticket,DEAL_COMMISSION));
      }  
   }
}
 

Before calling HistoryDealGetDouble you need to have invoked some kind of HistorySelect. PositionGetTicket will select the open position and not the deals. Search CodeBase for samples.

double GridManager::DealComission(ulong ticket)
  {
   if(HistorySelectByPosition(ticket)==true)
   {
      for(int i=HistoryDealsTotal()-1;i>=0;i--)
        {
         ulong deal_ticket=HistoryDealGetTicket(i);
         if(HistoryDealGetInteger(deal_ticket, DEAL_ENTRY) == DEAL_ENTRY_IN)
            return HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION);
        }
   }
   return 0;
  }

https://www.mql5.com/en/code/49186

Basic GridManager Library
Basic GridManager Library
  • www.mql5.com
This is a basic library to create and manage grids.
 
Yashar Seyyedin #:

Before calling HistoryDealGetDouble you need to have invoked some kind of HistorySelect. PositionGetTicket will select the open position and not the deals. Search CodeBase for samples.

https://www.mql5.com/en/code/49186

Works perfect. Thank you!