Adding a time delay between alerts

 

Hi

I'm trying to add a time delay between alerts, to prevent fresh alerts appearing every tick when conditions are satisfied.

I'm guessing this can be achieved by using an array storing the time of previous alerts, and having a specified time delay.

However, my theory is a little ahead of my practice at the moment so any pointers about how I would actually achieve this would be very much appreciated!

 

For a single Alert() u can do something like this:

bool AlertDelay() {
   static datetime last_time;
   if (TimeCurrent()-last_time<30) {    // delay for 30 seconds
      return(false);
   } else {
      last_time=TimeCurrent();
      return(true);
   }
}

And then simply check AlertDelay()'s return before using Alert() like so:

if (AlertDelay()) Alert("Whatever...");


For more than one alert u need to extend this example so as each alert has it's own memory of the last time it was triggered...

 
That's great, thanks a lot :)
 
I am trying to add this
gordon:

For a single Alert() u can do something like this:

And then simply check AlertDelay()'s return before using Alert() like so:


For more than one alert u need to extend this example so as each alert has it's own memory of the last time it was triggered...

I am trying to add this code to stop alert and push notification from going off continually to this existing customer indicator for CCI as follows:

 When I tried to add it to this at the end I got an error saying 'alertdelay' can be defined in global space or something like that.  Can someone explain how to add this code into this customer indictor thanks

//+------------------------------------------------------------------+
//|                                                          CCI.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net//"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 LightSeaGreen
//---- input parameters
extern int CCIPeriod=14;
extern int CCIHigh=100;
extern int CCILow=-100;
int PlayedSoundH = False;

int PlayedSoundL = False;
//---- buffers
double CCIBuffer[];
double RelBuffer[];
double DevBuffer[];
double MovBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 3 additional buffers are used for counting.
   IndicatorBuffers(4);
   SetIndexBuffer(1, RelBuffer);
   SetIndexBuffer(2, DevBuffer);
   SetIndexBuffer(3, MovBuffer);
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,CCIBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="CCI("+CCIPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,CCIPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Commodity Channel Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double price,sum,mul;
   if(Bars<=CCIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=CCIPeriod;i++) CCIBuffer[Bars-i]=0.0;
      for(i=1;i<=CCIPeriod;i++) DevBuffer[Bars-i]=0.0;
      for(i=1;i<=CCIPeriod;i++) MovBuffer[Bars-i]=0.0;
     }
//---- last counted bar will be recounted
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
//---- moving average
   for(i=0; i<limit; i++)
      MovBuffer[i]=iMA(NULL,0,CCIPeriod,0,MODE_SMA,PRICE_TYPICAL,i);
//---- standard deviations
   i=Bars-CCIPeriod+1;
   if(counted_bars>CCIPeriod-1) i=Bars-counted_bars-1;
   mul=0.015/CCIPeriod;
   while(i>=0)
     {
      sum=0.0;
      k=i+CCIPeriod-1;
      while(k>=i)
       {
         price=(High[k]+Low[k]+Close[k])/3;
         sum+=MathAbs(price-MovBuffer[i]);
         k--;
       }
      DevBuffer[i]=sum*mul;
      i--;
     }
   i=Bars-CCIPeriod+1;
   if(counted_bars>CCIPeriod-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      price=(High[i]+Low[i]+Close[i])/3;
      RelBuffer[i]=price-MovBuffer[i];
      i--;
     }
//---- cci counting
   i=Bars-CCIPeriod+1;
   if(counted_bars>CCIPeriod-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      if(DevBuffer[i]==0.0) CCIBuffer[i]=0.0;
      else CCIBuffer[i]=RelBuffer[i]/DevBuffer[i];
      i--;
     }
   if(CCIBuffer[0]<CCIHigh){PlayedSoundH = False;}
  
   if(CCIBuffer[0]>CCILow){PlayedSoundL = False;} 
  
   if(CCIBuffer[0]>=CCIHigh && PlayedSoundH == False){bool AlertDelay() {
   static datetime last_time;
   if (TimeCurrent()-last_time<30) {    // delay for 30 seconds
      return(false);
   } else {
      last_time=TimeCurrent();
      return(true);
   }
}
    PlaySound("alert.wav");
    SendNotification("Sell " + _Symbol);
    PlayedSoundH = True;}
   
   if(CCIBuffer[0]<=CCILow && PlayedSoundL == False){
    PlaySound("alert.wav");
    SendNotification("Buy " + _Symbol);
    PlayedSoundL = True;}
//----
   return(0);
  }
//+------------------------------------------------------------------+

 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Don't dredge up five year old threads unless you have a good reason.
  3.  if(CCIBuffer[0]>=CCIHigh && PlayedSoundH == False){bool AlertDelay() {
    Don't post code that doesn't even compile
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Don't dredge up five year old threads unless you have a good reason.
  3. Don't post code that doesn't even compile

Thanks for the your input.  Will attach it correctly which now compiles without error. 

Yes thread is old but the code is useful to this application of applying a delay to an alert.

//+------------------------------------------------------------------+
//|                                                          CCI.mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 LightSeaGreen
//---- input parameters
extern int CCIPeriod=14;
extern int CCIHigh=100;
extern int CCILow=-100;
int PlayedSoundH = False;

int PlayedSoundL = False;
//---- buffers
double CCIBuffer[];
double RelBuffer[];
double DevBuffer[];
double MovBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 3 additional buffers are used for counting.
   IndicatorBuffers(4);
   SetIndexBuffer(1, RelBuffer);
   SetIndexBuffer(2, DevBuffer);
   SetIndexBuffer(3, MovBuffer);
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,CCIBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="CCI("+CCIPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,CCIPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Commodity Channel Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double price,sum,mul;
   if(Bars<=CCIPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=CCIPeriod;i++) CCIBuffer[Bars-i]=0.0;
      for(i=1;i<=CCIPeriod;i++) DevBuffer[Bars-i]=0.0;
      for(i=1;i<=CCIPeriod;i++) MovBuffer[Bars-i]=0.0;
     }
//---- last counted bar will be recounted
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
//---- moving average
   for(i=0; i<limit; i++)
      MovBuffer[i]=iMA(NULL,0,CCIPeriod,0,MODE_SMA,PRICE_TYPICAL,i);
//---- standard deviations
   i=Bars-CCIPeriod+1;
   if(counted_bars>CCIPeriod-1) i=Bars-counted_bars-1;
   mul=0.015/CCIPeriod;
   while(i>=0)
     {
      sum=0.0;
      k=i+CCIPeriod-1;
      while(k>=i)
       {
         price=(High[k]+Low[k]+Close[k])/3;
         sum+=MathAbs(price-MovBuffer[i]);
         k--;
       }
      DevBuffer[i]=sum*mul;
      i--;
     }
   i=Bars-CCIPeriod+1;
   if(counted_bars>CCIPeriod-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      price=(High[i]+Low[i]+Close[i])/3;
      RelBuffer[i]=price-MovBuffer[i];
      i--;
     }
//---- cci counting
   i=Bars-CCIPeriod+1;
   if(counted_bars>CCIPeriod-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      if(DevBuffer[i]==0.0) CCIBuffer[i]=0.0;
      else CCIBuffer[i]=RelBuffer[i]/DevBuffer[i];
      i--;
     }
   if(CCIBuffer[0]<CCIHigh){PlayedSoundH = False;}
   
   if(CCIBuffer[0]>CCILow){PlayedSoundL = False;}  
   
   if(CCIBuffer[0]>=CCIHigh && PlayedSoundH == False){
    PlaySound("alert.wav");
    SendNotification("Sell " + _Symbol);
    PlayedSoundH = True;}
    
   if(CCIBuffer[0]<=CCILow && PlayedSoundL == False){
    PlaySound("alert.wav");
    SendNotification("Buy " + _Symbol);
    PlayedSoundL = True;}
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
perhaps best to start a new topic with this
Reason: