Trying to count number of positions containing usd in symbol name

 

I have an EA running on multiple USD pairs, but I want to limit the number of USD positions allowed open at a time. I am trying to count them by using StringFind to see if "USD" exists in the symbol name, if it does it adds +1 to the count which it compares to my externally set variable of USD. For some reason this is returning some odd values for the usd counter like 661 and 32759 when there is only one position open.


bool isymbolcount()
  {
   int usd,eur,gbp,chf,jpy,cad,nzd,aud = 0;
   string isymbol;
   bool icount=false;
   string thissymbol = Symbol();
   string lastisymbol;
   icount=false;

   for(int iiii=0; iiii<=PositionsTotal(); iiii++)
     {
      if(!m_position.SelectByIndex(iiii))
         continue;
      isymbol=PositionGetSymbol(iiii);
      if((!Global_Filters && PositionGetInteger(POSITION_MAGIC)==MagicNumber) || (Global_Filters))
         if(isymbol!=lastisymbol)
           {

            if(((int)StringFind(isymbol,"USD",0)!=-1) && ((int)StringFind(thissymbol,"USD",0)!=-1))
              {
               usd++;
               if(usd>=USD)
                 {cntt++;ObjectDeleteMQL4("isymbol"); CreateObjectGray("isymbol","Too many USD trades open already " +usd,15,20+inc+Spacer*cntt); return(true);}
              }

            lastisymbol=isymbol;
           }
     }
return(false);
}
 
Go through your loop with the debugger and check the relevant variables and their changes..