Why it will send emails repeat and frequently?

 
#property indicator_chart_window 
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 LightSkyBlue 
extern int BRARperiod = 26;
extern double BRAR_low = 60;
double BR[]; 
double AR[]; 
double CrossUp[];
double CrossDown[];
double HCY; 
double CYL; 
double HO; 
double OL; 
int mark =0 ;
         
int init() 
       { 
       IndicatorBuffers(4); 
       SetIndexBuffer(0, BR); 
       SetIndexBuffer(1, AR); 
       SetIndexStyle(2, DRAW_ARROW,EMPTY,EMPTY,clrRed);
       SetIndexBuffer(2, CrossUp);
       SetIndexArrow(2, 233);
       SetIndexStyle(3, DRAW_ARROW,EMPTY,EMPTY,clrLightSkyBlue);
       SetIndexBuffer(3, CrossDown);
       SetIndexArrow(3, 234);     
       return (0); 
       } 

int deinit() 
         { 
         return (0); 
         } 
         
int start() 
         { 
          int counted_bars = IndicatorCounted();        
        
          if (counted_bars < 0) 
          return (-1); 
         
          if (counted_bars > 0) 
          counted_bars--; 
          int i = Bars - BRARperiod + 1; 
         
          if (counted_bars > BRARperiod - 1) 
          i = Bars - counted_bars - 1; 
         
          while (i >= 0) 
              {
               CrossUp[i]= EMPTY; 
               CrossDown[i]= EMPTY; 
               HCY = 0; 
               CYL = 0; 
               HO = 0; 
               OL = 0; 
              
               for (int k = i + BRARperiod - 1; k >= i; k--) 
               { 
                HCY += MathMax(0, High[k] - (Close[k + 1])); 
                CYL += MathMax(0, Close[k + 1] - Low[k]); 
                HO += High[k] - Open[k]; 
                OL += Open[k] - Low[k]; 
               } 
              
               BR[i] = 100.0 * HCY / CYL;
               AR[i] = 100.0 * HO / OL; 
              
               if((BR[i]<=60) && (AR[i]<=60)&& (BR[i+1]<=60) && (AR[i+1]<=60)&& (BR[i]>BR[i+1] && BR[i+2]>BR[i+1] && AR[i]>AR[i+1] && AR[i+2]>AR[i+1])&&mark!=0){CrossUp[i]=Low[i]*0.9995;mark=0;}
               i--;             
               }
            if((BR[1]<=60) && (AR[1]<=60)&& (BR[2]<=60) && (AR[2]<=60)&& (BR[1]>BR[2] && BR[3]>BR[2] && AR[1]>AR[2] && AR[3]>AR[2])&&mark!=1)
             {  SendMail(Symbol() + "5分钟做多预警--MT4邮件提醒" ,Symbol()+" 平台时间 "+TimeToStr(TimeCurrent(),TIME_SECONDS));
                mark =1;
                printf("发送邮件");
              }   
             else {mark=2;}           

           }
 
 
joshua_ryo: Why it will send emails repeat and frequently?
            if((BR[1]<=60) && (AR[1]<=60)&& (BR[2]<=60) && (AR[2]<=60)&& (BR[1]>BR[2] && BR[3]>BR[2] && AR[1]>AR[2] && AR[3]>AR[2])&&mark!=1)
             {  SendMail(Symbol() + "5分钟做多预警--MT4邮件提醒" ,Symbol()+" 平台时间 "+TimeToStr(TimeCurrent(),TIME_SECONDS)

You are checking that condition every tick, therefor it sends every tick. Act on a change of signal.
          MQL4 (in Strategy Tester) - double testing of entry conditions - MQL4 programming forum #1 (2017)

 
joshua_ryo:

It's OK, we've all been here, myself included! It's a common thing people trip up on as it's not blatantly obvious. 

But, yes, as William said, anything that goes into the: 

void OnTick(){


}

section will check for the condition(s) on every tick (every time a new price comes in on the server), as long as your condition returns true on each incoming tick, it will fire off an email. 

If you want to check the condition on every bar - as opposed to every tick - I use the following;

Hope this helps. 

int OnInit(){

        datetime BarTime = Time[0];


}

        void OnTick(){

        if(...)
        if(...)
        if(...)
        if(BarTime != Time[0]){

                SendMail(...)
        
                        BarTime = Time[0];

        }
                        

        

}
 
TheHonestPrussian #:

It's OK, we've all been here, myself included! It's a common thing people trip up on as it's not blatantly obvious. 

int OnInit(){
        datetime BarTime = Time[0];
}

It is no good declaring a variable in OnInit() and then using the variable in OnTick().

Use a static variable in OnTick()

 
Keith Watford #:

It is no good declaring a variable in OnInit() and then using the variable in OnTick().

Use a static variable in OnTick()

Groovy, Baby! :P