Sending e-mail only when the alert is different from the previous one

 

Hello, the code below has been designed with the help of several contributors to send an e-mail when conditions are met and only at the begining of a new bar. There are two possible e-mails. One for a bull alert and one for a bear alert. How do we make the code send e-mails at the begeining of a new bar but only when the alert has changed from bull to bear or from bear to bull. Thanks for your help.

 

texcan, u have been asked several times to use the SRC button. U r much more likely to get help if u do so...


 
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua 
#property indicator_color2 Orange

extern int MA_Type = MODE_SMA;
extern int Period_MA_1 = 48;

double ma48; 

double Bull[];
double Bear[];

int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 225);
SetIndexBuffer(0, Bull);

SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 226);
SetIndexBuffer(1, Bear);

return(0);
}

int start()
{

int counted_bars = IndicatorCounted();
int i;
int limit;
if(counted_bars < 0) 
return(-1);
if(counted_bars > 0) 
counted_bars--;
limit = Bars - counted_bars;
for(i=0; i<=limit; i++)
{

ma48 = iMA(Symbol(),0,Period_MA_1,0,MA_Type,Close[i+1],i);

if ( Close[i+1] > ma48 )
{ Bull[i] = Low[i] - 0.0005; } 

if( Close[i+1] < ma48 )
{ Bear[i] = High[i] + 0.0005; }


static datetime Close_Time;

if ( Close_Time != Time[0])
{
   if ( Close[i+1] > ma48 )
   {
      SendMail("EUR up", "test" );
       Close_Time = Time[0]; 
   }

   if( Close[i+1] < ma48 )
   {
      SendMail("EUR down", "test");
       Close_Time = Time[0];    }
}


}
return(0);
}
 

If I've understood your code correctly, I would change the bottom section of your code to something like

   static datetime Close_Time;
   static bool mail_flag_up=true;
   static bool mail_flag_dn=true;

   if (Close_Time != Time[0])
      {
      if (Close[i+1] > ma48 && mail_flag_up )
         {
         SendMail("EUR up", "test" );
         Close_Time = Time[0]; 
         mail_flag_up=false;
         mail_flag_dn=true;
         }

      if (Close[i+1] < ma48 && mail_flag_dn)
         {
         SendMail("EUR down", "test");
         Close_Time = Time[0];
         mail_flag_dn=false;
         mail_flag_up=true;
         }
      }

It assumes that the indicator is either up or down as it relies on the up case to switch the down flag on and vice versa.

I haven't tested it, but hope that helps

V

 
Viffer:

If I've understood your code correctly, I would change the bottom section of your code to something like

It assumes that the indicator is either up or down as it relies on the up case to switch the down flag on and vice versa.

I haven't tested it, but hope that helps

V






Hi Viffer,
Your suggestion has helped me improve the code but it is still not 100%. If you have a moment, I would appreciate it if you took a second look.

I am testing on a 1 min chart. It seems to work fine until it crosses the MA for the first time. At that point you get a several incorrect e-mails. Any suggestions? I have included the code that I am working with for your convenience.

Thanks

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua 
#property indicator_color2 Orange

extern int MA_Type = MODE_SMA;
extern int Period_MA_1 = 48;

double ma48; 

double Bull[];
double Bear[];

int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 225);
SetIndexBuffer(0, Bull);

SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 226);
SetIndexBuffer(1, Bear);

return(0);
}

int start()
{

int counted_bars = IndicatorCounted();
int i;
int limit;
if(counted_bars < 0) 
return(-1);
if(counted_bars > 0) 
counted_bars--;
limit = Bars - counted_bars;
for(i=0; i<=limit; i++)
{

ma48 = iMA(Symbol(),0,Period_MA_1,0,MA_Type,Close[i+1],i);

if (Close[i+1] > ma48)
   {Bull[i] = Low[i] - 0.0005;} 

if(Close[i+1] < ma48 )
   {Bear[i] = High[i] + 0.0005;}


static datetime Close_Time;
static bool mail_flag_up=true;
static bool mail_flag_dn=true;

   if (Close_Time != Time[0])
      {
      if (  Close[i+1] > ma48 && mail_flag_up)
         {
         SendMail("EUR up", "test" );
         Close_Time = Time[0]; 
         mail_flag_up=false;
         mail_flag_dn=true;
         }

      if (  Close[i+1] < ma48 && mail_flag_dn)
         {
         SendMail("EUR down", "test");
         Close_Time = Time[0];
         mail_flag_dn=false;
         mail_flag_up=true;
         }
      }

}
return(0);
}
 
texcan:


Hi Viffer,
Your suggestion has helped me improve the code but it is still not 100%. If you have a moment, I would appreciate it if you took a second look.

I am testing on a 1 min chart. It seems to work fine until it crosses the MA for the first time. At that point you get a several incorrect e-mails. Any suggestions? I have included the code that I am working with for your convenience.

Thanks

I'm guessing your MA is "flickering" around the close as the bar forms.... try shifting it to the last completed bar as you do with close...

ma48 = iMA(Symbol(),0,Period_MA_1,0,MA_Type,Close[i+1],i+1);

hth

V

EDIT: Also, I'm not sure that Close[i+1] is correct in iMA(). Shouldn't that be PRICE_CLOSE...

 

Hi Viffer I still need some help with my code. I made the changes and it is still not 100%. Can you please look at it one last time?
I am looking to have it send an e-mail alerts when a condition is met.
The condition is the 12 bar simple MA. If the price is above it I want an e-mail that says so. Like wise if price is below.
I only want an e-mail the first time it breaches the MA. In other words, the e-mails should alternate between above and below alerts.
To avoid the effects of the fluctuation of the MA I am basing everything on the last complete bar. If the bar is still active I am ignoring it.
The code that I have to date still has a bug that I can't figure out. I am testing it on a 1min chart and it works well until the price breaches the MA.
Would appreciate some more guidance.
For convenience, I am attaching the code.
Thanks for your help

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua 
#property indicator_color2 Orange

extern int MA_Type = MODE_SMA;
extern int Period_MA_1 = 12;

double ma12; 

double Bull[];
double Bear[];

int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 225);
SetIndexBuffer(0, Bull);

SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 226);
SetIndexBuffer(1, Bear);

return(0);
}

int start()
{

int counted_bars = IndicatorCounted();
int i;
int limit;
if(counted_bars < 0) 
return(-1);
if(counted_bars > 0) 
counted_bars--;
limit = Bars - counted_bars;
for(i=0; i<=limit; i++)
{



//**** Places arrows on the the Chart
//------------------------------------------
ma12 = iMA(NULL,0,12,0,MA_Type,Close,i+1);



if (Close[i+1] > ma12)
   {Bull[i] = Low[i] - 0.0005;} 

if(Close[i+1] < ma12)
   {Bear[i] = High[i] + 0.0005;}



//*** Sends the e-mail
//----------------------------

static datetime Close_Time;
static bool mail_flag_up=true;
static bool mail_flag_dn=true;

   if (Close_Time != Time[0])
      {
      if (  Close[i+1] > ma12 && mail_flag_up)
         {
         SendMail("EUR up", "test" );
         Close_Time = Time[0]; 
         mail_flag_up=false;
         mail_flag_dn=true;
         }

      if (  Close[i+1] < ma12 && mail_flag_dn)
         {
         SendMail("EUR down", "test");
         Close_Time = Time[0];
         mail_flag_dn=false;
         mail_flag_up=true;
         }
      }

}
return(0);
}
 

Change

if (  Close[i+1] > ma12 && mail_flag_up)

to

if (  Close[1] > ma12 && mail_flag_up)

and obviously the same with down criteria.

V

 
Viffer:

Change

to

and obviously the same with down criteria.

V


Hi Viffer.
Just want to say thanks for your help.
I implemented the last suggestion and it seems to work well.
Thanks again.

 

You're welcome. It's nice to get appreciation!

V

 
static datetime Close_Time;
static bool mail_flag_up=true;
static bool mail_flag_dn=true;

   if (Close_Time != Time[0])
You don't want to send emails for past crossings, especially if you change the chart timeframe, pair, or just refresh a chart.
if (Close_Time != Time[0] && i==0)
Reason: