How do I modify indicator to be alerted only once per occurrence?

Back to topics list To post a new topic, please log in or register
avatar
3
KClark 2015.02.24 21:13
 

I modified an indicator to add email alerts. However, when the alert is triggered, it sends me repeated messages. How do I modify the code so that the alert is sent to my phone once?

Here is the code:


#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 LightGray
#property indicator_color3 LightGray
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_DOT
#property indicator_style3 STYLE_DOT
//---- input parameters
extern int RSIPeriod=14;
extern int ApplyTo=0;
extern bool EmailAlert = TRUE;
extern string Email_Subject = "RSI Alert!";
extern string Up_Strength_Msg = "The RSI is above 70.";
extern string Down_Strength_Msg = "The RSI is below 30.";
extern int OverBought=70;
extern int OverSold=30;
//---- buffers
double RSIBuffer[];
double RSIOBBuffer[];
double RSIOSBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,RSIBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,RSIOBBuffer);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,RSIOSBuffer);
//---- name for DataWindow and indicator subwindow label
   short_name="RSI-Alert("+RSIPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   SetIndexLabel(1,"OverBought");
   SetIndexLabel(2,"OverSold");
//----
   SetIndexDrawBegin(0,RSIPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
//----
   if(Bars<=RSIPeriod) return(0);
//----
   i=Bars-RSIPeriod-1;
   if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
   while(i>=0)
   {
      RSIBuffer[i]=iRSI(NULL,0,RSIPeriod,ApplyTo,i);
      RSIOBBuffer[i]=OverBought;
      RSIOSBuffer[i]=OverSold;
      i--;
   }
   
   if(EmailAlert)
   {
      if(RSIBuffer[1]<OverBought && RSIBuffer[0]>=OverBought)
        SendMail(Email_Subject+" "+Symbol(), Up_Strength_Msg);
      else if(RSIBuffer[1]>OverSold && RSIBuffer[0]<=OverSold)
         SendMail(Email_Subject+" "+Symbol(), + Down_Strength_Msg);
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
avatar
Moderator
3427
GumRai 2015.02.24 22:17 #
 

You could consider something like this

 

avatar
Moderator
3427
GumRai 2015.02.24 22:18 #
 
      static bool OB_Alerted=false;
      static bool OS_Alerted=false;
      if(RSIBuffer[1]<OverBought && RSIBuffer[0]>=OverBought && OB_Alerted==false)
        {
        SendMail(Email_Subject+" "+Symbol(), Up_Strength_Msg);
        OB_Alerted=true;
        OS_Alerted=false;
        }
      else 
      if(RSIBuffer[1]>OverSold && RSIBuffer[0]<=OverSold && OS_Alerted==false)
        {
        SendMail(Email_Subject+" "+Symbol(), + Down_Strength_Msg);
        OS_Alerted=true;
        OB_Alerted=false;
        }

You could also store the Bar time of the alert and only alert once per bar

avatar
3
KClark 2015.02.24 23:37 #
 
GumRai:

You could also store the Bar time of the alert and only alert once per bar

GumRai:

You could consider something like this

 

Thanks GumRai. It works like a charm! This was my first time to modify an indicator. Not a programmer, just using tips found in the forum. Great community.
avatar
13009
WHRoeder 2015.02.25 03:17 #
 
You are alerting on condition
      if(RSIBuffer[1]<OverBought && RSIBuffer[0]>=OverBought)
        SendMail(Email_Subject+" "+Symbol(), Up_Strength_Msg);
Alert on the first change of condition
static bool isBuy=false;
bool wasBuy = isBuy; isBuy = RSIBuffer[1]<OverBought && RSIBuffer[0]>=OverBought;
if(!wasBuy && isBuy)
        SendMail(Email_Subject+" "+Symbol(), Up_Strength_Msg);

Back to topics list  

To add comments, please log in or register