MT4 coding (stop it sending alerts every tick?)

I have been playing around with an indicator I found and have set it up to be able to email me when it triggers. the problem is it sends the mail every tick.

what I havent been able to do is get the code that allows it to send an alert and then not send another for 15 mins (until the next bar as its on a 15 min chart)

At the moment I have a bunch of code that checks for the signal and sets indicator trigger to 1 for up trigger and 2 for down then the code that sends the mail, which checks for the trigger and then checks that its triggering for the current minute so that it doesnt trigger for every historical trigger on every chart!

if((indicatortrigger>0) && (TimeYear(Time[i])>=Year())&&(TimeMonth(Time[i])>=Month())&&(TimeDay(Time[i])>=Day())&&(TimeHour(Time[i])>=Hour())&&(TimeMinute(Time[i])>=Minute()))

i tried adding something like sentmin=itime(NULL,0,i) inside the triggers ‘if’ and then turning the indicator off if sentmin<Time[i] but it didnt seem to work.

Try something like this:

  if ( Trigger != Time[0] &&  Sound.Alert ) 
  {
    if( HP_BuferUp[0] &gt;  0 ) { Trigger = Time[0] ;  PlaySound(Sound.File); }
    if( HP_BuferDn[0] &gt;  0 ) { Trigger = Time[0] ;  PlaySound(Sound.File); }        
  } 

or

  if ( Trigger != Bars &&  Sound.Alert ) 
  {
    if( HP_BuferUp[0] &gt;  0 ) { Trigger = Bars ;  PlaySound(Sound.File); }
    if( HP_BuferDn[0] &gt;  0 ) { Trigger = Bars ;  PlaySound(Sound.File); }        
  }

thanks. hmmm similar to what I tried before but I think the != may be what caused the problem.

ill have to wait until next time it triggers to find out :stuck_out_tongue:

gah still triggers every pip for the first minute (due to the line above that makes sure its the current minute so it doesnt trigger for the entire chart hustory when mt4 is opened)

makes no sense

in the code that checks for up or down trigger

if((acchange==1) && (sentmin!=Time[0]) && (Volume[i+1]>=100) && (TrendON==1) && (StochCHANGE==1) && (Close[i+1]>=Open[i+1]) ) Trigger=1;

then in if statement that sends the alert
if trigger>0
sentmin=Time[0]
send email etc etc
end if
trigger = 0

makes no sense, it sets sentmin whenever the trigger is activated, then checks to make sure sentmin isnt the same when it checks for the trigger. Time[0] on a 15 min chart only changes after 15 mins when the next bar opens right? so for 15 mins after a trigger, sentmin should equal time[0] and not trigger again! but it somehow ignores it.

gaaaah nothing works!! Ive attached the indicator im playing around with. its basically the cowabunga indicator and im playing around with it testing various things.

at the moment it checks the previous bar and the one before that for a valid signal, but then its resending the signal on every tick of the current bar even with the code i put in the IF statement where it sends the emails at line 173 to try and stop it sending any for 15 mins but it continues to send on every tick for the first minute of the current bar (after the 1st minute the other part of the IF statement stops any further triggers).

can anyone see whats wrong with it? im not bothered about the rest of the code thats a bit of a mess with trying various things, i just need to let it send 1 alert email and then not do that again for 15 mins until the next bar opens so i dont get 300 emails a day!


//+------------------------------------------------------------------+
//|                                          Cowabunga- Any Pair.mq4 |
//|                                    by Pipalot, 18th October 2007 |
//|                                                                  |
//| The original Cowabunga indicator was posted by ybop01 and this   |
//| version in as adaption created by Pipalot on 18th October 2007.  |
//| Main changes made since the original version:                    |                            
//|   1. iMACD replaced with iOsMA (MT4's MACD histogram function)   |
//|   2. Turned on the feature to send email alerts                  |
//|   3. Alerts will also now play a sound (email.wav)               |                                   |
//|   3. changes to enable it to work on any pair (15 minute chart)  |                 |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red

extern int IndicatorDisplacement = 15;
extern bool Show_Trend = true;
extern bool Only_Show_Valid_Triggers= true;
extern bool Send_Mail = true;
//---- buffers
double UPTrend[];
double DNTrend[];
double UPTrigger[];
double DNTrigger[];

int MailSent = -1   ; // ADDED BY TRO

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- additional buffers are used for counting
   IndicatorBuffers(4);
//---- indicators

   SetIndexLabel(0,"UP TREND");
   SetIndexLabel(1,"DOWN TREND");
   SetIndexLabel(2,"UP ARROW");
   SetIndexLabel(3,"DOWN ARROW");
   
   if((Period()==PERIOD_M15))
      {
      SetIndexStyle(0,DRAW_LINE,STYLE_DOT,1,Blue);
      SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1,Red);
      }
   else
      {
      SetIndexStyle(0,DRAW_LINE,0,3,Blue);
      SetIndexStyle(1,DRAW_LINE,0,3,Red);
      }
   SetIndexStyle(2,DRAW_ARROW,EMPTY,3,Blue);
   SetIndexStyle(3,DRAW_ARROW,EMPTY,3,Red);
   SetIndexArrow(2,SYMBOL_ARROWUP);
   SetIndexArrow(3,SYMBOL_ARROWDOWN);
   SetIndexBuffer(0,UPTrend);
   SetIndexBuffer(1,DNTrend);
   SetIndexBuffer(2,UPTrigger);
   SetIndexBuffer(3,DNTrigger);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
   int maCross,StochchANGE,maCross1h,StochchANGE1h,MACDchange,i,counted_bars=IndicatorCounted();
   double ma5now,ma10now,ma5prev,ma10prev,ma5now1h,ma10now1h,ma5prev1h,ma10prev1h;
   double RSI,Stoch1,Stoch2,Signal1,Signal2,MACDchange1,MACDchange2,RSI1h,Stoch1H1,Stoch1h2,Signal1H1,Signal1h2;
   int Trigger,i1h;
   static int TrendON;
   bool Period15m,MACDEnable;
   string text,CRLF;   
   datetime lastAlertTime;   
   int AlertSleepMins=14;   
      
   CRLF=CharToStr(13) + CharToStr(10);
   
   Period15m=(Period()==PERIOD_M15);
   if(Period15m) MACDEnable=true;          

   if(Bars<=100) return(0);
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   i=Bars-counted_bars;

   while(i>=0)
      {
                        //---- TREND1h
      i1h=iBarShift(NULL,PERIOD_M30,iTime(NULL,0,i),true);
      ma5now1h=iMA(NULL,PERIOD_M30,5,0,MODE_EMA,PRICE_CLOSE,i1h);
      ma10now1h=iMA(NULL,PERIOD_M30,10,0,MODE_EMA,PRICE_CLOSE,i1h);  
      ma5prev1h=iMA(NULL,PERIOD_M30,5,0,MODE_EMA,PRICE_CLOSE,i1h+1);
      ma10prev1h=iMA(NULL,PERIOD_M30,10,0,MODE_EMA,PRICE_CLOSE,i1h+1);
      if(ma5now1h > ma5prev1h && ma10now1h > ma10prev1h && ma5prev1h < ma10prev1h && ma5now1h > ma10now1h) maCross1h=1;
      else if(ma5now1h < ma5prev1h && ma10now1h < ma10prev1h && ma5prev1h > ma10prev1h && ma5now1h < ma10now1h) maCross1h=2;
      else maCross1h=0;
      RSI1h= iRSI(NULL,PERIOD_M30,9,PRICE_CLOSE,i1h);
      Stoch1H1= iStochastic(NULL,PERIOD_M30,10,3,3,MODE_SMA,0,MODE_MAIN,i1h);
      Stoch1h2= iStochastic(NULL,PERIOD_M30,10,3,3,MODE_SMA,0,MODE_MAIN,i1h+1);
      Signal1H1= iStochastic(NULL,PERIOD_M30,10,3,3,MODE_SMA,0,MODE_SIGNAL,i1h);
      Signal1h2= iStochastic(NULL,PERIOD_M30,10,3,3,MODE_SMA,0,MODE_SIGNAL,i1h+1);
      if(Stoch1H1>Stoch1h2 && Signal1H1>Signal1h2) StochchANGE1h=1;
      else if(Stoch1H1<Stoch1h2 && Signal1H1<Signal1h2) StochchANGE1h=2;
      else StochchANGE1h=0;

      if((maCross1h==1) ) TrendON=1;
      if((maCross1h==2) ) TrendON=-1;
      
      if(ma5prev1h<ma5now1h) TrendON=1;
      if(ma5prev1h>ma5now1h) TrendON=-1;

      if(Show_Trend)
         {
         if(TrendON==1) UPTrend[i]= High[i]+IndicatorDisplacement*Point;
         if(TrendON==-1) DNTrend[i]= Low[i]-IndicatorDisplacement*Point;         
         }
      
      if(Period15m)
         {
               //---- TRIGGERS
         ma5now=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,i+1);
         ma10now=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,i+1);  
         ma5prev=iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,i+2);
         ma10prev=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,i+2);
         if(ma5now > ma5prev && ma10now > ma10prev && ma5prev < ma10prev && ma5now > ma10now) maCross=1;
         else if(ma5now < ma5prev && ma10now < ma10prev && ma5prev > ma10prev && ma5now < ma10now) maCross=2;
         else maCross=0;
         RSI= iRSI(NULL,0,9,PRICE_CLOSE,i+1);
         Stoch1= iStochastic(NULL,0,10,3,3,MODE_SMA,0,MODE_MAIN,i+1);
         Stoch2= iStochastic(NULL,0,10,3,3,MODE_SMA,0,MODE_MAIN,i+2);
         Signal1= iStochastic(NULL,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,i+1);
         Signal2= iStochastic(NULL,0,10,3,3,MODE_SMA,0,MODE_SIGNAL,i+2);
         if(Stoch1>Stoch2 && Signal1>Signal2) StochchANGE=1;
         else if(Stoch1<Stoch2 && Signal1<Signal2) StochchANGE=2;
         else StochchANGE=0;
         
         //MACDchange1=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i);
         MACDchange1=iOsMA(NULL,0,12,26,9,PRICE_CLOSE,i+1);
         //MACDchange2=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,i+1);
         MACDchange2=iOsMA(NULL,0,12,26,9,PRICE_CLOSE,i+2);
         
         if(MACDchange2<MACDchange1) MACDchange=1;
         else if(MACDchange2>MACDchange1) MACDchange=2;
         else MACDchange=0;
//if (Symbol()=="AUDUSD") Print("Yes");
         if((maCross==1) && (RSI>50) && (StochchANGE==1) && (MACDchange==1) && (Volume[i+1]>100) && (Close[i+1]>Open[i+1]) && ((TrendON==1)||(Only_Show_Valid_Triggers==false))) Trigger=1;
         if((maCross==2) && (RSI<50) && (StochchANGE==2) && (MACDchange==2) && (Volume[i+1]>100) && (Close[i+1]<Open[i+1]) && ((TrendON==-1)||(Only_Show_Valid_Triggers==false))) Trigger=2;
         
         if(Trigger==1) UPTrigger[i+1]= High[i+1]+7*Point;
         if(Trigger==2) DNTrigger[i+1]= Low[i+1]-7*Point;
         
         if((Trigger>0) && (lastAlertTime<=TimeCurrent()-AlertSleepMins*60) && (TimeYear(Time[i])>=Year())&&(TimeMonth(Time[i])>=Month())&&(TimeDay(Time[i])>=Day())&&(TimeHour(Time[i])>=Hour())&&(TimeMinute(Time[i])>=Minute())&&((Time[i])>=Minute()))
            {
            lastAlertTime=TimeCurrent();
            if(Trigger==1) text="BUY ";
            else text="SELL ";
            text=text + "trigger occurred on " +TimeDay(Time[i])+"/"+TimeMonth(Time[i])+"/"+TimeYear(Time[i])+" at "+TimeHour(Time[i])+":"+TimeMinute(Time[i]) + "  (server time)" + CRLF;
            text=text + CRLF + CRLF;
            
            text=text + "Trend is ";
            if(TrendON==1) text=text + "UP " + CRLF + CRLF;
            else text=text + "DOWN " + CRLF + CRLF;
            text = text +"MA5 = " + ma5now + CRLF;
            text = text +"MA10 = " + ma10now + CRLF;
            text = text +"MA5 Previous Bar = " + ma5prev + CRLF;
            text = text +"MA10 Previous Bar = " + ma10prev + CRLF;
            text = text +"RSI = " + RSI + CRLF;
            text = text +"STOchASTIC MAIN = " + Stoch1 + CRLF;
            text = text +"STOchASTIC SIGNAL = " + Signal1 + CRLF;
            text = text +"STOchASTIC MAIN Previous Bar = " + Stoch2 + CRLF;
            text = text +"STOchASTIC SIGNAL Previous Bar = " + Signal2 + CRLF;            
            text = text +"MACD = " + MACDchange1 + CRLF;
            text = text +"MACD Previous Bar = " + MACDchange2 + CRLF;

//-------------------------- BEGIN TRO MODIFICATION --------------------------
// ONLY SEND MAIL ONCE PER BAR  
          
            if( MailSent != Bars )
            {
               SendMail("COWABUNGA Trigger " + Symbol(),text);
               MailSent = Bars ;
            }
//-------------------------- END TRO MODIFICATION ----------------------------            

            PlaySound("email.wav");
            Print(text);
            }          
         }
      Trigger=0;
      i--;
      }
   return(0);
   }
//+-----------------------------------------------------------------+

Ta, i shall try that.

seems to be based on number of bars increasing by 1 every time a new bar starts, but wouldnt this mean an endless number of bars? I would have thought when a new bar appears the oldest one is removed so you dont end up with 5 billion bars crashing the whole program :stuck_out_tongue:

im not sure the indicator is working very well at the moment but at least i wont get spammed to death on top of losing money :smiley: