Get list of unique pairs with open positions

 

I am developing a function to create a list of unique pairs with open positions. Say USDJPY has a long and short position and GBPJPY has a long positions, the result should be "USDJPY" and "GBPJPY". For this I create an empty array to store the names. First I loop through all open positions, then for the symbol, i loop trhough the list of names, and if the symbol is not new, keep going, if new, add it to the list. The code is below.. However, for some reason I am getting all pairs names of all pairs (duplicated) with opened positions... what I am doing wrong?


void PairNamesOpenedPos(string& ArrayPairs[])
{
   string  CurrentSymbol;
   int     nPositions=PositionsTotal();    //n Total open positons

   ArrayFree(ArrayPairs);           // clear up array of prior data.
   ArrayResize(ArrayPairs,1);
      
    //get list of pair names with opened position
    for(int i=0; i<nPositions; i++)
     {
      CurrentSymbol    = PositionGetSymbol(i) ;
         //check if pair is already listed
         int TempPairCount=ArraySize(ArrayPairs); //temporary pair counter
         for(int ii = 0; ii < TempPairCount; ii++)
            {
             if(CurrentSymbol == ArrayPairs[ii]) {break;}else {ArrayResize(ArrayPairs,TempPairCount+1); ArrayPairs[TempPairCount-1]=CurrentSymbol;}
            }
     
     }

 

I doubt this to be the most elegant way to do this,... but here is a solution that works:

void PairNamesOpenedPos(string& PairNams[])
{
   int     total=0;
   int     nPositions=PositionsTotal();    //n Total open positons
   ulong   position_ticket;
   string  CurrentSymbol;
   string  msymbols="";
      
   for(int i=0; i<nPositions; i++)
     {
      position_ticket  = PositionGetTicket(i);  
      CurrentSymbol    = PositionGetString(POSITION_SYMBOL) ;
      
         if( 0>StringFind(msymbols,CurrentSymbol) )
           {
            msymbols += CurrentSymbol+";";
            ArrayResize(PairNams,total+1);
            PairNams[total]=CurrentSymbol;
            total ++;
           }
     }

} 
 

Be careful with the first pair, StringFind would return 0 and you check for >0.

So either add the ";" before each pair:

msymbols += ";"+CurrentSymbol;

or check for:

 if( 0>=StringFind(msymbols,CurrentSymbol) )