Number of positions

 

What is wrong in this code that I am unable to get number of positions.

Anyone who knows, please help.

int NumberOfPositions()
  {
  static int N;
  ulong ticket;
  for(int a=0;a<PositionsTotal();a++)
    {
    ticket=OrderGetTicket(a);
    if(PositionGetString(POSITION_SYMBOL)==Symbol())
    if((PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)||(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL))
    N++;
    }
return(N);
}
Documentation on MQL5: Trade Functions / PositionsTotal
  • www.mql5.com
Trade Functions / PositionsTotal - Documentation on MQL5
 

I use this code to get the number of deals in a position.  I believe you can make minor modifications to serve your purpose.

 

//  I use this to return the number of deals in a position.

//+------------------------------------------------------------------+
//|   return Total Deals in current open position                    |
//+------------------------------------------------------------------+
int PositionDeals()
{
   uint pos_total=0;
   int total=0;
   ulong pos_id=0;
   
   pos_total=PositionsTotal();
   if(pos_total > 0) {
   for(uint i=0;i<pos_total;i++)
      {
         if(PositionSelect(Symbol())) 
            {
               pos_id=(ENUM_POSITION_PROPERTY_INTEGER)PositionGetInteger(POSITION_IDENTIFIER);
               HistorySelectByPosition(pos_id);
               total=HistoryDealsTotal();
   
               return(total); 
            }
      }
   }   
   return(0);
}
Documentation on MQL5: Trade Functions / HistoryDealsTotal
  • www.mql5.com
Trade Functions / HistoryDealsTotal - Documentation on MQL5
 
Kengen:

I use this code to get the number of deals in a position.  I believe you can make minor modifications to serve your purpose.

 

 

I meant a bit different but anyway thanks for your help and time.

I was solved the problem before. 

 
Learner21:

What is wrong in this code that I am unable to get number of positions.

Anyone who knows, please help.

 

You need to PositionSelect
first.
 

This is small example which might helps you to do what you want to do.

int PosTotal=PositionsTotal();
   for(int i=0;i<PosTotal;i++)
     {
      string SymName=PositionGetSymbol(uint(i));
      printf("Symbol %s: volume %f, profit %f",
             SymName,
             PositionGetDouble(POSITION_VOLUME),
             PositionGetDouble(POSITION_PROFIT));
     }

 Please read also help topic about function PositionGetSymbol. 

 
alexvd:

This is small example which might helps you to do what you want to do.

 Please read also help topic about function PositionGetSymbol. 

This tells severything. Thank you.