Small problem with Alert

 

I know alerts are a popular question and I have read how to do an alert and faithfully followed an example that seemed a good fit for what I seek to do with an indicator I am working on.

I have lines that "cross" and assign a value 1 to a "Long" cross and -1 for a "Short" cross.  My alert correctly alerts Long or Short but once alerted, subsequent crosses are not Pop-Up alerting.  I know this because I also print to comments and see in the Experts tab. 


extern bool    Alert_Cross = true;
int PrevAlertTime=0;

   if (Alert_Cross == true)
      {
        if((Cross == 1) && (CurTime() - PrevAlertTime > Period()*60)) 
      {
      Alert("LONG Cross " + Symbol() + " at: " + TimeToStr(TimeCurrent(), TIME_MINUTES) + "h " + Period() + " mins chart");
      PrevAlertTime=CurTime();
      }
      else if ((Cross == -1) && (CurTime() - PrevAlertTime > Period()*60)) 
      {
      Alert("SHORT RPG Cross " + Symbol() + " at: " + TimeToStr(TimeCurrent(), TIME_MINUTES) + "h " + Period() + " mins chart");
      PrevAlertTime=CurTime();
      }
}

 I wonder if someone can see why there are no subsequent pop-up alerts? 

All ideas greatfully received,

 
NewtonLeo:

I know alerts are a popular question and I have read how to do an alert and faithfully followed an example that seemed a good fit for what I seek to do with an indicator I am working on.

I have lines that "cross" and assign a value 1 to a "Long" cross and -1 for a "Short" cross.  My alert correctly alerts Long or Short but once alerted, subsequent crosses are not Pop-Up alerting.  I know this because I also print to comments and see in the Experts tab. 


extern bool    Alert_Cross = true;
int PrevAlertTime=0; 


PrevAlertTime  should not be an  int  it should be a datetime

Why do you use CurTime() ?  what does it do ?  why not simply use TimeCurrent()

You said that you get comments and output to the log ?  I don't see any code that will do that in what you have shown. 

 

Thanks for your help RaptorUK.  I have made the changes you suggested and will check it out.

I got the code snippet from here : https://www.mql5.com/en/code/7331 and that is where it had CurTime() and PrevAlertTime as an int. 

I'll check now to make sure it is behaving properly.  Very grateful for your help.  I am learning - it's been a while since I've been stumped like this. 

 
NewtonLeo:

Thanks for your help RaptorUK.  I have made the changes you suggested and will check it out.

I got the code snippet from here : https://www.mql5.com/en/code/7331 and that is where it had CurTime() and PrevAlertTime as an int. 

I'll check now to make sure it is behaving properly.  Very grateful for your help.  I am learning - it's been a while since I've been stumped like this. 

Ah I see,  CurTime() is an obsolete function.  Don't assume that all the code in the codebse is good code,  it is not checked before being added. ;-)
 
It looks like you have steered me in the right direction :) It was unfortunate I randomly came across some code with an obsolete function in it.  I was baffled how it worked right from the beginning.  I was concerned about the "... PrevAlertTime > Period()*60" as I have no idea what/how that worked and wondered if the 60 was the problem.   It'll take a day I guess to see if it is now working entirely correctly but early indications are looking very good.  I'm very greatful, once again RaptorUK.
 
NewtonLeo:
It looks like you have steered me in the right direction :) It was unfortunate I randomly came across some code with an obsolete function in it.  I was baffled how it worked right from the beginning.  I was concerned about the "... PrevAlertTime > Period()*60" as I have no idea what/how that worked and wondered if the 60 was the problem.  
Timeframes are in minutes,  so Period() returns 15 for M15, 60 for H1, etc.  so to convert the timeframe into seconds it is multiplied by 60.  datetimes are the number of seconds since midnight 1st Jan 1970.  So that code is saying if the last alert was less than a bars time ago don't alert again.
 

Datetimes are Unsigned integers. The subtraction gives seconds since.

Convert them to Signed integers, the subtraction gives a negative and your If fails.

Normally to average to doubles (or ints) you use (D1+D2)/2. This fails on datetimes (integer overflow.) You need to use D1+(D2-D1)/2

 

Thanks WHRoeder, I'll searched for information about Signed/Unsigned integers but found nothing to help me understand.  With the help RaptorUK offered, I have revised the block to this: 

//
   if (Alert_Cross == true)
     {
      if((Cross_Status == 1) && (TimeCurrent() - PrevAlertTime > Period()*60)) 
        {
         Alert("LONG Cross " + Symbol() + " at: " + TimeToStr(TimeCurrent(), TIME_MINUTES) + "h " + Period() + " mins chart");
         PrevAlertTime=TimeCurrent();
        }
      else if((Cross_Status == -1) && (TimeCurrent() - PrevAlertTime > Period()*60)) 
       {
        Alert("SHORT Cross " + Symbol() + " at: " + TimeToStr(TimeCurrent(), TIME_MINUTES) + "h " + Period() + " mins chart");
        PrevAlertTime=TimeCurrent();       
       }
   }
//

If I understand you correctly,  I should change

  if((........) && (TimeCurrent() - PrevAlertTime > Period()*60))

 to

  if((.........) &&  (TimeCurrent() + (PrevAlertTime -  TimeCurrent())/2) > 0)))   ??

 

RaptorUK - this explaination ("Timeframes are in minutes,  so Period() returns 15 for M15, 60 for H1, etc.  so to convert the timeframe into seconds it is multiplied by 60.  datetimes are the number of seconds since midnight 1st Jan 1970.  So that code is saying if the last alert was less than a bars time ago don't alert again.") is succinct.  I totally understand now. I appreciate you taking the time to explain.

 

Reason: