Download MetaTrader 5

Best way to count the number of opened charts

To add comments, please log in or register
Rodrigo Malacarne
Moderator
10093
Rodrigo Malacarne  

I have searched the docs, but couldn't find any specific function to count the number of opened charts on MT5 terminal.

To solve this problem I came up with this solution:

string ChartIdList[]; // To be called as a global variable
for(int i=1;i<100;i++) // To be called on OnInit(), for instance
     {
      int CountCharts;
      ArrayResize(ChartIdList,101);
      ChartIdList[0]=ChartFirst();
      ChartIdList[i]=ChartNext(ChartIdList[i-1]);
      if(ChartIdList[i]==-1)
        {
         CountCharts=i;
         Print("Number of opened charts = "+CountCharts);
         return CountCharts;
        }
     }

However, I don't know if this is the best approach, once I'm not sure if such a feature already exists as a built-in function... 

Does anybody know if this feature actually exists?

Documentation on MQL5: Chart Operations / ChartOpen
Documentation on MQL5: Chart Operations / ChartOpen
  • www.mql5.com
Chart Operations / ChartOpen - Documentation on MQL5
Alain Verleyen
Moderator
31973
Alain Verleyen  
Malacarne:

I have searched the docs, but couldn't find any specific function to count the number of opened charts on MT5 terminal.

To solve this problem I came up with this solution:

However, I don't know if this is the best approach, once I'm not sure if such a feature already exists as a built-in function... 

Does anybody know if this feature actually exists?

ChartFirst() and ChartNext() return a long, why are you using a string array to save these values ?

   int chartCount=0;
   long prevChart=ChartFirst();

   while(prevChart!=-1)
   {
      prevChart=ChartNext(prevChart);
      chartCount++;
   }
   Print("Number of charts = ", chartCount);

I don't think there is a built-in function for that.

Rodrigo Malacarne
Moderator
10093
Rodrigo Malacarne  
angevoyageur:

ChartFirst() and ChartNext() return a long, why are you using a string array to save these values ?

I don't think there is a built-in function for that.

Hi angevoyageur,

Thanks for the reply. I'm actually using the long values as an input inside an Expert Advisor. Anyway, I wanted to find a better way to count the opened charts, and your code works perfectly! Thanks
Documentation on MQL5: Chart Operations / ChartOpen
Documentation on MQL5: Chart Operations / ChartOpen
  • www.mql5.com
Chart Operations / ChartOpen - Documentation on MQL5
Alain Verleyen
Moderator
31973
Alain Verleyen  
Malacarne:
Hi angevoyageur,

Thanks for the reply. I'm actually using the long values as an input inside an Expert Advisor. Anyway, I wanted to find a better way to count the opened charts, and your code works perfectly! Thanks
You are welcome.
To add comments, please log in or register