Alert being set off, can't find reason why

 

Hi all. I'm trying to program an alert that goes off once volatility is greater than 3 times ATR. I have almost everything taken care of, but I have a stubborn bug I can't identify. I have included a time stamp as a global variable to try and make sure the alert can only go off once every candle even though I am running it on 8 charts at once. However, for some reason, if an alert goes off, and I switch down time frames from the 5Min to the 1Min, the spike goes off with every new tick update (as opposed to once a candle limit). I can't seem to find the reason why it does this. Mind you, it only does this on the 1 minute, all the other time frames the alert just goes off once as you change to a new time frame. I will highlight the code I think is the problem. Is there any chance you can take a look at the code and let me know where the problem lies? I am still a very new coder to mql4 and would greatly appreciate the help.

i think this variable is the problem:

if(Spike == true && Stamp != Time[0]) //if theres a spike and no alert has been issued in past candle then
{
Alert ("Spike on: ", Symbol());
Spike = false; //reset counter
GlobalVariableSet(TimeStamp,Time[0]);
Stamp = GlobalVariableGet(TimeStamp);
}
Files:
 
Make Stamp a globally declared variable and also read the Global Variable within init(), each time you change timeframes init() is executed.
 
RaptorUK:
Make Stamp a globally declared variable and also read the Global Variable within init(), each time you change timeframes init() is executed.

When you say make Stamp a global variable, do you mean as a global variable within the program, as in declare it above int init()? Or do you mean use the GlobalVariableSet to declare it? Also thanks for letting me know init() is executed every time a timeframe change happens. I didn't realize that.
 
deadromeo:

When you say make Stamp a global variable, do you mean as a global variable within the program, as in declare it above int init()? Or do you mean use the GlobalVariableSet to declare it? Also thanks for letting me know init() is executed every time a timeframe change happens. I didn't realize that.
I didn't say make it a Global Variable . . . I said make it globally declared . . so yes, declare it outside of all functions so it has a global scope.
 
  1. Global Variables are for communicating between EAs. Globally declared and static variables retain their values between calls.
  2. Deinit and init (except 0,1) are called for these reasons Uninitialize reason codes - MQL4 Documentation
  3. If (spike == true) You would never say if ( (2+2==4) == true ) would you? If (bool) or if (!bool) is sufficient.
datetime lastSpikeAlert;
int start(){
   :
   bool     isSpike = ...;
   datetime time0   = Time[0];
   if(isSpike && lastSpikeAlert != time0){ lastSpikeAlert = time0;
      Alert ("Spike on: ", Symbol());
   }
 
Thanks WHRoeder, for that clarification. I will try and apply the essence of your code to correct the problems I'm having. For the record, this has been an eye opening thread. Thanks for the help gents. I'll let you know how it turns out.
Reason: