How to count pairs in basket MQL5

 

Hi i need to count how many pairs has opened positions on my account. It can happend that one pair has more than 1 trade open and in that case must be counted one time only.

Thank you

 
Create an array in which you will store symbols name that are traded.
Free the array.
Cycle all your opened trades and (if not found already into the array) add the trade's symbol into the array.
The array size is the amount of traded symbols.
 

Thank you Fabio

have you got an example of code on which I can work?

 

Unfortunately not, but if you have some basic coding skills, will be very easy. It will be something like this. (Code not compiled and not tested)

string TradedSymbols[];

void CountSymbols() {
   ArrayFree(TradedSymbols);
   for( int i=PositionsTotal()-1; i>=0; i-- ) {
      if( !m_position.SelectByIndex(i) ) continue;
      if( ArrayFind(TradedSymbols,m_position.Symbol()) == -1 ) {
         ArrayResize(TradedSymbols,ArraySize(TradedSymbols)+1);
         TradedSymbols[ArraySize(TradedSymbols)]-1] = m_position.Symbol();
      }
   }
}


int ArrayFind(string &array, string item) {
   for( int i=0; i<ArraySize(array); i++ ) if( item == array[i] ) return i;
   return -1;
}

Then, in your OnTick call the "CountSymbols". After that call, the array size = the amount of traded symbols. Of course if you already have a cycle through all positions, will be more effective to add the part into your existing cycle instead of creating a new one.

 

Thanks Fabio you were very kind!

Have a great Sunday!

 
Salvatore Caligiuri #:

Thanks Fabio you were very kind!

Have a great Sunday!

You too :)

Reason: