Alert Only if New bar in Specific period while I'm viewing the chart in other period.

 

Hi all,

I would like to ask if it possible to code this situation:
I have alert indicator for Stochastic Oscillator Cross, The alert it is limit to alert me only If the indicator crossed in the H4 Timeframe. I note that if I'm viewing the chart in the (M5) Timeframe it alert me with every new bar in the (M5) Time frame.

My Question:
Is it possible to setup the alert , to alert only if their is a new bar in (H4) timeframe even if I'm viewing the chart in lower timeframe?

 
static datetime LastH4Bar=0;
datetime ThisH4Bar=iTime(NULL,PERIOD_H4,0);
if(LastH4Bar!=ThisH4Bar)
  {
   // code to run on new H4 bar
   LastH4Bar=ThisH4Bar;
  }
 

Hi honest knave,

Thank you very much.

Reason: