checking charts

 

I'm racking my brain about this and can't quite figure it out.

How can I go about checking to see if a chart is open or not?  And if it's not open, open it?  

Thanks. 

 

Loop through the open charts and check the symbol / timeframe as required.

If the chart isn't found, open the chart using ChartOpen() . You can also use ChartApplyTemplate() for your newly opened chart, if required.

An example which will check for the GBPJPY M15 chart, and open one if necessary:

   string sym   = "GBPJPY";
   int    tf    = PERIOD_M15;
   bool   found = false;
   long   ID    = ChartFirst();
   while(ID>=0) {
      if(ChartSymbol(ID)==sym && ChartPeriod(ID)==tf) { 
         found=true;
         break; }
      ID=ChartNext(ID);}
   if(!found) {
      ChartOpen(sym,tf);}
 

You can loop through the open charts with ChartFirst(), ChartNext(), ChartID(), ChartSymbol(), ..

It's all in the reference

Reason: