How to get current profit value of a symbol in MT5

 

Hi,

Can somebody share a code to get profit value of a current symbol from multiple open position. (Example: I have 5 buy position on EURUSD and I want to know total loss of profit of all 5 position)

Thanking you in advance!

 
//+------------------------------------------------------------------+
//|                                                                  |
double TotalProfit(int magic)
  {
   double pft=0;
   for(int i=PositionsTotal()-1;i>=0;i--)
     {
      ulong ticket=PositionGetTicket(i);
      if(ticket>0)
        {
         if(PositionGetInteger(POSITION_MAGIC)==magic && PositionGetString(POSITION_SYMBOL)==Symbol())
           {
            pft+=PositionGetDouble(POSITION_PROFIT);
           }
        }
     }
   return(pft);
  }
//+------------------------------------------------------------------+
 
Ziheng Zhuang:

Thank you so much!

 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".
 
Ziheng Zhuang #:

Hi i tried but my function return different value with total profit in Trade Window. Any one have same problem?

//+------------------------------------------------------------------+
//| Get All Open Positions Profit                                    |
//+------------------------------------------------------------------+
double OpenPositionsProfit()
  {
   double allProfit = 0;
   if(PositionsTotal() > 0)
      for(int i = 0; i < PositionsTotal(); i++)
        {
         ulong ticket = PositionGetTicket(i);
         if(PositionSelectByTicket(ticket))
            if(PositionGetInteger(POSITION_MAGIC) == magicNumber)
               if(PositionGetString(POSITION_SYMBOL) == _Symbol)
                  allProfit += PositionGetDouble(POSITION_PROFIT);
        }
   return allProfit;
  }