I cant get the count of sell or buy positions?

Omran Alturk  

Hi,

it seems i cant get buy or sell positions but when i call 

CountBuyPositions()

it will return all positions and when calling 

CountSellPositions()

it doesn't return anything. 

this is my code:

//+------------------------------------------------------------------+
int CountBuyPositions()
{
   int count=0;
   uint total=PositionsTotal();
   for(int i=0; i < PositionsTotal() ; i++)
     {
      string position_symbol=PositionGetSymbol(i);
      if(position_symbol == Symbol()
      && MagicNumber == PositionGetInteger(POSITION_MAGIC))
      {
         if(OrderGetInteger(ORDER_TYPE) == POSITION_TYPE_BUY)
         {
            count++;
         }
      }
  } 
  return (count);
}
//+------------------------------------------------------------------+
int CountSellPositions()
{
   int count=0;
   uint total=PositionsTotal();
   for(int i=0; i < PositionsTotal() ; i++)
     {
      string position_symbol=PositionGetSymbol(i);
      if(position_symbol == Symbol()
      && MagicNumber == PositionGetInteger(POSITION_MAGIC))
      {
         if(OrderGetInteger(ORDER_TYPE) == POSITION_TYPE_SELL)
         {
            count++;
         }
      }
  } 
  return (count);
}
Vladimir Karputov  

Mistake #1: You don't selected the position. Mistake #2: You are confusing the concept of "order" and "position".

An example of how to count positions. This example counts the number of positions for the current symbol ('Symbol()') and only those positions for which the 'Magic number' is equal to the given one.

   int total=0;
   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
     {
      ulong ticket=PositionGetTicket(index);
      if(ticket>0);
        {
         if(PositionGetString(POSITION_SYMBOL)==Symbol() && PositionGetInteger(POSITION_MAGIC)==InpMagic)
           {
            total++
           }
        }
     }

Pay attention: enumeration of positions in the loop goes 'to zero'.

We make sure to do 'PositionGetTicket'

The function returns the ticket of a position with the specified index in the list of open positions and automatically selects the position to work with using functions PositionGetDouble, PositionGetInteger, PositionGetString.
Documentation on MQL5: Trade Functions / PositionGetTicket
Documentation on MQL5: Trade Functions / PositionGetTicket
  • www.mql5.com
PositionGetTicket - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Omran Alturk  
Vladimir Karputov #:

Mistake #1: You don't selected the position. Mistake #2: You are confusing the concept of "order" and "position".

An example of how to count positions. This example counts the number of positions for the current symbol ('Symbol()') and only those positions for which the 'Magic number' is equal to the given one.

Pay attention: enumeration of positions in the loop goes 'to zero'.

We make sure to do 'PositionGetTicket'

The function returns the ticket of a position with the specified index in the list of open positions and automatically selects the position to work with using functions PositionGetDouble, PositionGetInteger, PositionGetString.

Thank you Vladimir for your response, that was very helpful.