Position Counting code

 

My function to count open positions is not working.

EDIT: In case it isn't obvious, I want to count a given type, either buy or sell positions.

This is how I am placing the trades:

void PlaceTrade(int i,int Oper_local, string trigger)
  {
   string par_temp=SymbolList[i];
   if(StringLen(par_temp)!=6) {
      printf("Symbol Length !=6 in PlaceTrade");
      return;
   }
   SetSize(i);
   NormalizeDouble(LotSizeList[i],2);

//--- preparar Order Request
   MqlTradeRequest request= {};
   MqlTradeResult result= {};
   request.action=TRADE_ACTION_DEAL;         // Mudar para TRADE_ACTION_PENDING se for Stop ou limit
   request.magic=magic;                         // ORDER_MAGIC
   request.comment=trigger;
   request.symbol=par_temp;                     // symbol
   request.volume=NormalizeDouble(LotSizeList[i],2);               // volume in 0.1 lots
   request.deviation=5;

   if(Oper_local==SELL)
     {
      if(contaPos(par_temp,POSITION_TYPE_SELL)>= MaxOrders) return;
      request.type=POSITION_TYPE_SELL;
      request.price=SymbolInfoDouble(par_temp,SYMBOL_BID);  // open price
      request.sl=SymbolInfoDouble(par_temp,SYMBOL_ASK)+SLList[i]; 
      request.tp=SymbolInfoDouble(par_temp,SYMBOL_BID)-TPList[i]; 
      request.comment=trigger+" #1";
      OrderSend(request,result);
     }
   else
      if(Oper_local==BUY)
        {
         if(contaPos(par_temp,POSITION_TYPE_BUY)>= MaxOrders) return;
         request.type=POSITION_TYPE_BUY;
         request.price=SymbolInfoDouble(par_temp,SYMBOL_ASK);  // open price
         request.sl=SymbolInfoDouble(par_temp,SYMBOL_BID)-SLList[i];
         request.tp=SymbolInfoDouble(par_temp,SYMBOL_ASK)+TPList[i];
         request.comment=trigger+" #1";
         OrderSend(request,result);
        }
}


This is how  I am counting the positions:

int contaPos(string par_temp,int type) {
   int i,pairIndex,resultado=0;
   ulong ticket;
   if(PositionsTotal()>0) {
      for(i = 0; i < PositionsTotal(); i++) {
         if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
            {
            if(m_position.Symbol()==par_temp && m_position.Magic()==magic && m_position.Type()==type) resultado++;
         }
      } // Fim de For Pos Total
   }// Fim de if PosTotal >0
   return(resultado);
}

Count returns 0, regardless if I use position_type or Order_type

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 Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Nuno Costa:

My function to count open positions is not working.

EDIT: In case it isn't obvious, I want to count a given type, either buy or sell positions.

This is how I am placing the trades:


This is how  I am counting the positions:

Count returns 0, regardless if I use position_type or Order_type

Saw on another post the solution posted by @Vladimir Karputov. Problem is solved! Instead of using.

m_position.Type()

I should use 

m_position.PositionType()

Thanks Vladimir!