Script help - template not applied

 

Hi, I've written this short script to open 5 charts on the 15 min timeframe and apply a template to those charts, then open another 5 charts on the 5 min timeframe so that I can apply a different template to those. The script runs but the last part doesn't work. What am I doing wrong? Thanks in advance.

void OnStart()

  {

//---


   ChartOpen("AUDCAD",15);

   ChartOpen("AUDCHF",15);

   ChartOpen("AUDJPY",15);

   ChartOpen("AUDNZD",15);

   ChartOpen("AUDUSD",15);


   long firstchart=ChartFirst();

   while(firstchart>0)

     {

      ChartApplyTemplate(firstchart,"template1.tpl");

      firstchart=ChartNext(firstchart);

     }


   ChartOpen("AUDCAD",5);

   ChartOpen("AUDCHF",5);

   ChartOpen("AUDJPY",5);

   ChartOpen("AUDNZD",5);

   ChartOpen("AUDUSD",5);


   //code seems to work fine to here but template2 doesn't get applied to the 5 min charts??

   

   long chartfirst=ChartFirst();

   while(chartfirst>0)

     {

      if(Period()== 5)

        {

         ChartApplyTemplate(chartfirst,"template2.tpl");

         chartfirst=ChartNext(chartfirst);

        }

     }

    }


 
fxsquirrel:

Hi, I've written this short script to open 5 charts on the 15 min timeframe and apply a template to those charts, then open another 5 charts on the 5 min timeframe so that I can apply a different template to those. The script runs but the last part doesn't work. What am I doing wrong? Thanks in advance.

Your condition: if(Period()== 5) is wrong. Period() returns the current chart period. Instead you have to use:

if(ChartPeriod(chartfirst)==PERIOD_M5)
 
Petr Nosek:

Your condition: if(Period()== 5) is wrong. Period() returns the current chart period. Instead you have to use:

Hey, thanks very much for that, I had been looking in MQL4 reference for that but completely overlooked it. Your answer has helped me to the next set of issues I have. Hopefully get those sorted. As part of the "while" loop I think i need to use "continue" also but the following seems to work for me. Also I found the insert code button for this reply. Thanks again.

Another quick question, is there a bit of code to arrange the windows? Something that mimics the Tile Windows shortcut "ALT + R"?

void OnStart()

  {

   ChartOpen("AUDCAD",5);
   ChartOpen("AUDCHF",15);
   ChartOpen("AUDJPY",30);
   ChartOpen("AUDNZD",60);
   ChartOpen("AUDUSD",5);
   ChartOpen("AUDCAD",15);
   ChartOpen("AUDCHF",30);
   ChartOpen("AUDJPY",60);
   ChartOpen("AUDNZD",5);
   ChartOpen("AUDUSD",5);

   long scriptchart=ChartID(); //the chart the script is added to
   long firstchart=ChartFirst();
   while(firstchart>0)
     {
      if(firstchart==scriptchart)
        {
         firstchart=ChartNext(firstchart);
         continue;
        }
      if(ChartPeriod(firstchart)==5)
        {
         ChartApplyTemplate(firstchart,"S_colour1.tpl");
         firstchart=ChartNext(firstchart);
         continue;
        }
      if(ChartPeriod(firstchart)==15)
        {
         ChartApplyTemplate(firstchart,"S_colour2.tpl");
         firstchart=ChartNext(firstchart);
         continue;
        }
      if(ChartPeriod(firstchart)==30)
        {
         ChartApplyTemplate(firstchart,"S_colour3.tpl");
         firstchart=ChartNext(firstchart);
         continue;
        }
      ChartApplyTemplate(firstchart,"S_colour4.tpl");
      firstchart=ChartNext(firstchart);

    }
}
Reason: