Trying to get a list of all pairs with at least one trade active - Can't stop an Array from being filled

 

Hi there,

I'm trying to get a list of all pairs with a trade active to finally make a table with a "marketwatch" (to show some stuff on a chart) dinamically adding and removing them from the list.

I think creating an array is the right way to go. So, this is my logic:


- loop into active trades
- get OrderSymbol()
- write symbol in an array
- check if this symbol already exists in the array (don't want duplicates)
- and finally end the loop.


On the terminal I see a print line with the right symbols, but they are repeated and the array seems to be in a loop being wiped and filled, wiped, filled (gradually) and then wiped again.

This is the code:

#property strict
#property indicator_chart_window

string arraySymbols[];  //Array containing list of Symbols


//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetTimer(1);

   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
int start()
  {
   return 0;
  }

//+------------------------------------------------------------------+

void OnTimer() 
{
   string str;
   
   for(int i = 0; i < OrdersTotal(); i++)    //Loop through all the Orders
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) )
      {
         if (checkIFsymbolExists(arraySymbols,OrderSymbol()) == False)
         {
            ArrayResize( arraySymbols, ArraySize( arraySymbols ) + 1 );
            arraySymbols[ ArraySize( arraySymbols ) - 1  ] = OrderSymbol(); 
         }
      }
   }
   
   for(int i=0;i<ArraySize(arraySymbols)-1;i++) 
   {
      str = StringConcatenate(str,arraySymbols[i]);
      Print(str);
   }
}

//+------------------------------------------------------------------+
bool checkIFsymbolExists( string &SymbolsArray[], string passedSymbol )
{
   bool   SymbolExists = False;
   for(int i =0; i < ArraySize(SymbolsArray)-1; i++)
   {  
      if( SymbolsArray[i] == passedSymbol )
      {
         SymbolExists = True;
         break;
      }
   }
   return( SymbolExists );
}

This is my output, and it loops very fast



Where am I doing wrong?
Any tips to think different?

Thanks in advance,
Francesco
 

 
Franzkekko:

...

Where am I doing wrong?
Any tips to think different?

Thanks in advance,
Francesco
 

Don't subtract 1 from ArraySize of your both loop condition.
 

Do what Mohammed says.

You may also consider the need for your array to be declared globalscope. The array will retain symbols for trades even after they have been closed.

Is that whet you want?

 
Mohammad Hossein Sadeghi:
Don't subtract 1 from ArraySize of your both loop condition.

Thanks Muhammad. I've tried your suggestion, but I get "Array out of range error" now =(


Keith Watford:

Do what Mohammed says.

You may also consider the need for your array to be declared globalscope. The array will retain symbols for trades even after they have been closed.

Is that whet you want?

Thanks Keith. Keeping symbols in array after trades being closed is not my goas. I want them to be in this array as long as they have orders active on the symbol itself =)

Other hints to help me achieve that?

=)

 
Franzkekko:

Thanks Keith. Keeping symbols in array after trades being closed is not my goas. I want them to be in this array as long as they have orders active on the symbol itself =)

Other hints to help me achieve that?

=)

Not tested

//+------------------------------------------------------------------+
//|                                                 OrderSymbols.mq4 |
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
  EventSetTimer(1);
  return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
//---

//--- return value of prev_calculated for next call
  return(rates_total);
}
//+------------------------------------------------------------------+
void OnTimer()
{
  string arraySymbols[];  //Array containing list of Symbols DECLARED LOCALLY
  int as=0;               //Variable to hold the array size

  for(int i = OrdersTotal()-1; i>=0; i--)    //Loop through all the Orders
   {
    if(OrderSelect(i, SELECT_BY_POS) )
     {
      string sym=OrderSymbol();
      if (checkIFsymbolExists(arraySymbols,sym,as) == False)
       {
        as++;
        ArrayResize(arraySymbols,as);
        arraySymbols[as-1] = sym;
       }
     }
   }

  string str;
  for(int i=0; i<as; i++)
   {
    str+=arraySymbols[i]+" ";
    Print(str);
   }
}
//+------------------------------------------------------------------+
bool checkIFsymbolExists( string &SymbolsArray[], string passedSymbol, int as )
{
  for(int i =0; i < as; i++)
   {
    if( SymbolsArray[i] == passedSymbol )
     {
      return(true);
     }
   }
  return( false );
}
//+------------------------------------------------------------------+
 
Keith Watford:

Not tested

Thanks for the code Watford. 
Adding this "as" simply solved the repeated symbols in the array, but it still loops from emptying to filling the array.

I've found this indicator that has that function inside. I'm Just trying to figure out where it's coded and get the logic :D


Files:
iExposure.mq4  9 kb
 
Franzkekko:

Thanks for the code Watford. 
Adding this "as" simply solved the repeated symbols in the array, but it still loops from emptying to filling the array.

I've found this indicator that has that function inside. I'm Just trying to figure out where it's coded and get the logic :D


My name is Keith, my surname is Watford, please don't call me by my surname, it is not polite.

Using as did not solve the repeated symbols in the array. It saves the repeated calls to ArraySize().

Yes, it fills the array every time, that way you will only have symbols with open trades in the array.

Reason: