alert keeps repeating indifinitely

 

I have an ATR indi that sends an alert once the line crosses certainl level

The problem is, the alert keeps repeating continously!

Is there any way to play the alert ONLY ONCE?


Thanks in advance!!

//+------------------------------------------------------------------+
//|                                                ATR_alert.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int AtrPeriod=14;
extern bool Above=true;
extern double Level=0.0014;
//---- buffers
double AtrBuffer[];
double TempBuffer[];
bool email_sent = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 1 additional buffer used for counting.
   IndicatorBuffers(2);
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AtrBuffer);
   SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="ATR("+AtrPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,AtrPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Average True Range                                               |
//+------------------------------------------------------------------+
int start()
  {
   int i,counted_bars=IndicatorCounted();
   if(Bars<=AtrPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=AtrPeriod;i++) AtrBuffer[Bars-i]=0.0;
//----
   i=Bars-counted_bars-1;
   while(i>=0)
     {
      double high=High[i];
      double low =Low[i];
      if(i==Bars-1) TempBuffer[i]=high-low;
      else
        {
         double prevclose=Close[i+1];
         TempBuffer[i]=MathMax(high,prevclose)-MathMin(low,prevclose);
        }
      i--;
     }
//----
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   for(i=0; i<limit; i++)
      AtrBuffer[i]=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);
//----
   if( Above==true && (AtrBuffer[0]>Level) && email_sent==false ){
      SendMail("From ATR Send Email", "ATR for " + Symbol() + " is above " + DoubleToStr(Level,Digits));
      Alert("alert.wav");
   }
   if( Above==true && (AtrBuffer[0]<Level) && email_sent==true){
      Alert("alert.wav");
   }
   if( Above==false && (AtrBuffer[0]<Level) && email_sent==false ){
      SendMail("From ATR Send Email", "ATR for " + Symbol() + " is below " + DoubleToStr(Level,Digits));
      Alert("alert.wav");
   }
   if( Above==false && (AtrBuffer[0]>Level) && email_sent==true){
      Alert("alert.wav");
   }
   Comment("Desired Level: " + DoubleToStr(Level,Digits) + "\nCurrent Level: " + DoubleToStr(AtrBuffer[0],Digits) +
           "\nAbove: " + Above + "\nEmailSent: " + email_sent);
           
         
         
         
        
         
         
           
return(0);
  }
//+------------------------------------------------------------------+
 
bali2002:
Is there any way to play the alert ONLY ONCE?
  1. Only send the alert for bar zero
  2. Only send the alert when it crosses, not when it is above.
  3. if (above=true) == if (true==true) redundant, if(above)
  4. above is never set/cleared
  5. email sent is never set/cleared
  6. You have two alerts above and below, why do you have 4 conditions?
static datetime nextAlert;
if (AtrBuffer[0]>Level && TimeCurrent() > nextAlert){
   nextAlert=Time[0] + Period() * 60;
   Alert(...)
}
Reason: