Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1327

 
SanAlex:

all the same zeros and does not want to show commission

double Komsa(string Pair) {
   double  commicion=0;
   int tikett,entry;
   ulong tik,ord;
//---------------------------------------------------------------   
   for(int i=PositionsTotal()-1;i>=0;i--) {
   if(PositionGetSymbol(i)==Pair){  tik=PositionGetTicket(i); 
//---------------------------------------------------------------         
   HistorySelect(0,TimeCurrent());
   for(uint r=HistoryDealsTotal()-1;r>0;r--)   {
   if((tikett=HistoryDealGetTicket(r))>0)   {
   ord=HistoryDealGetInteger(tikett,DEAL_ORDER);
   entry =HistoryDealGetInteger(tikett,DEAL_ENTRY);
   if(entry==DEAL_ENTRY_IN) {
   if(tik==ord)  { commicion+=HistoryDealGetDouble(tikett,DEAL_COMMISSION)*2;    }  }  } }  } }
//--------------------------------------------------------------  
  Comment( " commisia "+commicion);
   return(commicion);  }

I do not know if it is right or wrong but I checked it and it shows correctly

 
Aleksandr Egorov:

Anyway, that's how it counts on the selected pair

Thank you! - I'll try it now, I need to figure out this function after all

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Yes! It's working. Thank you!

Fx10 4

 
SanAlex:

Thank you! - I'm going to try it out now, I've got to figure out this feature after all.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Yes! It shows. Thank you!


It turns out that if the commission will be counted only for one side, then the function will not work correctly because it is multiplied by 2

DEAL_ENTRY_IN
We need to perfect it somehow
 
Aleksandr Egorov:

it turns out that if the commission is only counted for one side, then the function will not work correctly because it is multiplied by 2

I need to fix it to perfection

it seems to show everything correctly

I've made some adjustments - I don't know if this is right or not, but the warnings are gone

//+------------------------------------------------------------------+
//|                                                   Commission.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Komsa(_Symbol);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Komsa(string Pair)
  {
   double  commicion=0;
   ulong tikett,entry;
   ulong tik,ord;
//---------------------------------------------------------------
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      if(PositionGetSymbol(i)==Pair)
        {
         tik=PositionGetTicket(i);
         //---------------------------------------------------------------
         HistorySelect(0,TimeCurrent());
         for(uint r=HistoryDealsTotal()-1; r>0; r--)
           {
            if((tikett=HistoryDealGetTicket(r))>0)
              {
               ord=HistoryDealGetInteger(tikett,DEAL_ORDER);
               entry =HistoryDealGetInteger(tikett,DEAL_ENTRY);
               if(entry==DEAL_ENTRY_IN)
                 {
                  if(tik==ord)
                    {
                     commicion+=HistoryDealGetDouble(tikett,DEAL_COMMISSION);
                    }
                 }
              }
           }
        }
     }
//--------------------------------------------------------------
   Comment(" commisia "+DoubleToString(commicion,2));
   return(commicion);
  }
//+------------------------------------------------------------------+

Fx10 5

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

I opened an opposite position and the commission has increased - seems correct

BTCUSDH1

 
SanAlex:

It seems to show everything accurately.

I tweaked it a bit - I don't know if it's right or not, but the warnings are gone

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

I opened an opposite position and the commission has increased - it seems correct.


there is a need to check whether it counts for entry and exit or only for entry and withdraw part of the position if you open with 1 lot and then close with 0.5 lot

I've got a lot of work to do.
 

This script shows the profit for the period, total lot and commission for each instrument.
No thanks.

Files:
 
Dmitiry Ananiev:

This script shows the profit for the period, total lot and commission for each instrument.
Do not thank me.

The script is ok but the thing is that you need a comsa on open positions and this is in the history

 
Aleksandr Egorov:

The script is fine, but the thing is that you need a comsa on open poses, and this is in the story.

It has already been discussed before. You can see the commission (declared by the broker) in the broker's trading conditions. And the real commission for the execution of an order (order) you can only find out after the execution of the order, you cannot find out before the execution.

ZS. It is like the spread on the next tick. In the current tick we do not know what will be the next tick, neither the price, nor the spread, nor the swaps, nor the commission. And the broker has the right to change them.

 
Aleksandr Egorov:

I need to check what counts for entry and exit or only for entry and deduct part of the position if I opened with 1 lot and then closed with 0.5 lot.

I have to mess around yet.

I' m working out something funny - I don't understand it myself yet. I check it separately when opening a position and separately when closing it.

wise

//+------------------------------------------------------------------+
//|                                                   Commission.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
CTrade trade;
#property script_show_inputs
//---
input bool Inp_open_close = false; // Open --> false || Close --> true
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(void)
  {
//---
   if(!Inp_open_close)
     {
      if(!trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.1,SYMBOL_ASK,0.0,0.0))
         Print(trade.ResultRetcode());
      PlaySound("ok.wav");
      CSV_OPEN();
     }
   if(Inp_open_close)
     {
      if(TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) && Bars(_Symbol,_Period)>100)
         trade.PositionClose(_Symbol,3);
      PlaySound("ok.wav");
      CSV_CLOS();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Komsa(void)
  {
   double  commicion=0;
   ulong tikett,entry;
   ulong tik,ord;
//---------------------------------------------------------------
   for(int i=PositionsTotal()-1; i>=0; i--)
     {
      if(PositionGetSymbol(i)==_Symbol)
        {
         tik=PositionGetTicket(i);
         //---------------------------------------------------------------
         HistorySelect(0,TimeCurrent());
         for(uint r=HistoryDealsTotal()-1; r>0; r--)
           {
            if((tikett=HistoryDealGetTicket(r))>0)
              {
               ord=HistoryDealGetInteger(tikett,DEAL_ORDER);
               entry =HistoryDealGetInteger(tikett,DEAL_ENTRY);
               if(entry==DEAL_ENTRY_IN)
                 {
                  if(tik==ord)
                    {
                     commicion+=HistoryDealGetDouble(tikett,DEAL_COMMISSION);
                    }
                 }
              }
           }
        }
     }
//--------------------------------------------------------------
   Comment(" commisia "+DoubleToString(commicion,2));
   return(commicion);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CSV_OPEN(void)
  {
//---
   ResetLastError();
   int filehandle=FileOpen("CommissionOpen.csv",FILE_READ|FILE_WRITE|FILE_CSV);
   if(filehandle==-1)
     {
      int res=GetLastError();
      Print("File open error :  ",
            res);
      return;
     }
   FileSeek(filehandle, 0, SEEK_END);
   FileWrite(filehandle,Komsa());
   FileClose(filehandle);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CSV_CLOS(void)
  {
//---
   ResetLastError();
   int filehandle=FileOpen("CommissionClose.csv",FILE_READ|FILE_WRITE|FILE_CSV);
   if(filehandle==-1)
     {
      int res=GetLastError();
      Print("File open error :  ",
            res);
      return;
     }
   FileSeek(filehandle, 0, SEEK_END);
   FileWrite(filehandle,Komsa());
   FileClose(filehandle);
  }
//+------------------------------------------------------------------+
 
Valeriy Yastremskiy:

It has already been discussed before. You can see the commission (declared by the broker) in the broker's trading conditions. And the real commission for order (order) execution you can only find out after the execution of the order, you cannot find out before the execution.

ZS. It is like the spread on the next tick. In the current tick we do not know what will be the next tick, neither the price, nor the spread, nor the swaps, nor the commission. And the broker has the right to change them.

So he needs to know on his open positions.

Reason: