my sellstop keeps opening over and over MQ5

 

my sellstop keeps opening over and over even if i put measures in place to control it

void OpenPending()
{

int me=0;
if(nump==0){
  int _tp=PositionsTotal();
   for(int i=_tp-1; i>=0; i--)
     {
      string _p_symbol=PositionGetSymbol(i);
      if(_p_symbol!=_Symbol) continue;
      if(Magic!=PositionGetInteger(POSITION_MAGIC)) continue;
      _s_point = SymbolInfoDouble(_p_symbol, SYMBOL_POINT);
      _s_levSt = SymbolInfoInteger(_p_symbol, SYMBOL_TRADE_STOPS_LEVEL);
      _s_dig   = (int)SymbolInfoInteger(_p_symbol,SYMBOL_DIGITS);
      _p_sl    = PositionGetDouble(POSITION_SL);
      _p_tp    = PositionGetDouble(POSITION_TP);
      _p_op    = PositionGetDouble(POSITION_PRICE_OPEN);
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){tbuy=true;}else{tbuy=false;}
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){tsell=true;}else{tsell=false;}
      if(_p_sl==0) _p_sl=_p_op;
      lot=PositionGetDouble(POSITION_VOLUME)*Multiplier;
      //---
      }
if(nump<1){ //All pending orders are counted and this ensures  that when 1 pending order is opened, another will not be placed, but it still opens multiple
      
      if(tbuy==true)
         {
         double newsprice=_p_op-((distance/2)*tpoint);
         double ssl=newsprice+(distance*tpoint);
         double stp=newsprice-((distance/2)*tpoint);
         if(nump!=1){
            me++;
            Comment(me);
            if(!trade.SellStop(lot,newsprice,Symbol(),ssl,stp,0,0,"Pending"))     {
             //--- failure message
            Print("BuyStop() method failed. Return code=",trade.ResultRetcode(),
            ". Code description: ",trade.ResultRetcodeDescription());
            }
         }
         }
         
      if(tsell==true)
         {
         double newbprice=_p_op+((distance/2)*tpoint);
         double bsl=newbprice-(distance*tpoint);
         double btp=newbprice+((distance/2)*tpoint); 
         if(nump!=1){
            trade.BuyStop(lot,newbprice,Symbol(),bsl,btp,0,0,"Pending");
            nump=1;
            
            }
         }
         }
        }
   }
 

Pendings it is orders, not positions. 

 OrdersTotal() PositionsTotal()

 
Arkadii Zagorulko:

Pendings it is orders, not positions. 

 OrdersTotal() PositionsTotal()

thanks a million, that was exactly the problem, it was counting incorrectly in many functions
Reason: