How get position profit? - page 2

 
Morteza Soltani :

I have 40 open positions now.

10 positions has same magic number.

I want calculate sum profit of these 10 positions; if was more than 70$, then i want close all 10 positions.

It was simple in MT4

But in MT5, it's complicating.

I tried this:

Total_Profit = POSITION_PROFIT + POSITION_COMMISSION + POSITION_SWAP + POSITION_FEE

I can't get  POSITION_COMMISSION   and  POSITION_FEE

I know I should use " DEAL_COMMISSION " and " DEAL_FEE ", but i don't know how.

I can't find any relationship between DEAL and own POSITION

note: these positions are open (no in history)

I'm thankful of your answers.

In a cycle, go around all positions and summarize profit, swap and commission:

//+------------------------------------------------------------------+
//| Profit all positions                                             |
//+------------------------------------------------------------------+
double ProfitAllPositions(void)
  {
   double profit=0.0;

   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
            profit+=m_position.Commission()+m_position.Swap()+m_position.Profit();
//---
   return(profit);
  }
 
Vladimir Karputov:

In a cycle, go around all positions and summarize profit, swap and commission:

Thank you very much man, for your quick answer.

 
Morteza Soltani:

I have 40 open positions now.

10 positions has same magic number.

I want calculate sum profit of these 10 positions; if was more than 70$, then i want close all 10 positions.

It was simple in MT4

But in MT5, it's complicating.

I tried this:

Total_Profit = POSITION_PROFIT+ POSITION_COMMISSION + POSITION_SWAP+ POSITION_FEE

I can't get POSITION_COMMISSION and POSITION_FEE

I know I should use "DEAL_COMMISSION" and "DEAL_FEE", but i don't know how.

I can't find any relationship between DEAL and own POSITION

note: these positions are open (no in history)

I'm thankful of your answers.


I found answer of my question in MT5.

Each closed position, has 2 deal and 1 opposite position.

commission is calculated in deals. then you need to have deals of each position by POSITION_IDENTIFIER

POSITION_IDENTIFIER

Position identifier is a unique number assigned to each re-opened position. It does not change throughout its life cycle and corresponds to the ticket of an order used to open a position.

 

Position identifier is specified in each order (ORDER_POSITION_ID) and deal (DEAL_POSITION_ID) used to open, modify, or close it. Use this property to search for orders and deals related to the position.

 

When reversing a position in netting mode (using a single in/out trade), POSITION_IDENTIFIER does not change. However, POSITION_TICKET is replaced with the ticket of the order that led to the reversal. Position reversal is not provided in hedging mode.

long


double POSITIONS_TOTAL_PROFIT()
  {
   double Total_Profit=0;
   double deal_commission=0;
   double deal_fee=0;
   double deal_profit=0;
   int total=PositionsTotal();
   for(int i=0; i<total; i++)
     {
      ulong  position_ticket=PositionGetTicket(i);                                      // ticket of the position
      ulong  magic=PositionGetInteger(POSITION_MAGIC);                                  // MagicNumber of the position
      string comment=PositionGetString(POSITION_COMMENT);                               // position comment
      long   position_ID=PositionGetInteger(POSITION_IDENTIFIER);                         // Identifier of the position

      if(magic==EXPERT_MAGIC || comment==IntegerToString(EXPERT_MAGIC))
        {
         HistorySelect(0,TimeCurrent());
         int deals=HistoryOrdersTotal();
         for(int j=deals-1; j>=0; j--)
           {
            ulong deal_ticket=HistoryDealGetTicket(j);
            if(HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID) == position_ID)
              {
               deal_profit=HistoryDealGetDouble(deal_ticket, DEAL_PROFIT);
               deal_commission=HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION)*2;
               deal_fee=HistoryDealGetDouble(deal_ticket, DEAL_FEE);
               break;
              }
           }
         Total_Profit += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP) + deal_profit + deal_commission + deal_fee;
        }
     }
   return(Total_Profit);
  }

Each closed position, has 2 deal and 1 opposite position.

when position is open yet, it has just 1 deal with half commission. for calculate total commision of a open position, you need to multiply 2

deal_commission=HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION)*2;

Good Luck

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...
 

This code is better

//+------------------------------------------------------------------+
//| TOTAL_PROFIT_OF_OPEN_POSITIONS                                   |
//+------------------------------------------------------------------+
double TOTAL_PROFIT_OF_OPEN_POSITIONS()
  {
   double Total_Profit=0;
   double deal_commission=0;
   for(int i=0; i<PositionsTotal(); i++)
     {
      ulong  position_ticket=PositionGetTicket(i);                      // ticket of the position
      ulong  magic=PositionGetInteger(POSITION_MAGIC);                  // MagicNumber of the position
      string comment=PositionGetString(POSITION_COMMENT);               // position comment
      long   position_ID=PositionGetInteger(POSITION_IDENTIFIER);       // Identifier of the position
      if(magic==EXPERT_MAGIC || comment==IntegerToString(EXPERT_MAGIC))
        {
         HistorySelect(POSITION_TIME,TimeCurrent());
         for(int j=0; j<HistoryDealsTotal(); j++)
           {
            ulong deal_ticket=HistoryDealGetTicket(j);
            if(HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID) == position_ID)
              {
               deal_commission=HistoryDealGetDouble(deal_ticket, DEAL_COMMISSION)*2;
               break;
              }
           }
         Total_Profit+=PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP)+deal_commission;
        }
     }
   return(Total_Profit);
  }