SendMail() from EA and Chartchange

 

In my EA I have a simple trigger to send an email

something like this:

bool email_sent;

[....]

void OnTimer()
 {
  if([no positions]) email_sent=0;

  if([positions open] && [some indicator > some value] && email_sent==0)
   {
    SendMail(header,body);
    email_sent=1;
   }
 }

It works nicely as the email is only sent once, after a certain value is reached. It allows me to pick up the email, go to my terminal and make some decisions on open positions.

However, when i change the chart's timeframe (e.g. from 5M to 1D) the email is sent again. (if indicator condition is still valid)

Of course that makes sense as a Chart change triggers an OnDeInit() and an OnInit().

While i've succesfully been able to circumvent that problem using:

OnDeinit()
 {
  if(UninitializeReason() == REASON_CHARTCHANGE)
    {
     EventSetTimer(15);
     return(INIT_SUCCEEDED);
    }
   [....]       
  }

and 

OnInit()
 {
  if(UninitializeReason() == REASON_CHARTCHANGE)
    {
     EventSetTimer(15);
     return(INIT_SUCCEEDED);
    }
   [....]       
  }

it doesn't change the fact the input variable

bool    email_sent;

gets initialized again and is set to "0" , despite bypassing OnDeinit() and OnInit() and voila.. a crispy new email pops up every time I change the chart.


Now.. i guess there is a solution using Global Variables, but as i'm not a big fan of those - I find them unintuitive and cumbersome to work with - i was wondering if the is another solution solely within the code of the EA , keeping the control of all variables within the EA.

Anyone any ideas?

 

On a chart change, the indicator is removed and reloaded. You must remember your boolean outside of code. (files, or global variables)

EAs are not removed and reloaded. You shouldn't have had the problem.

 

I'm not following.

The indicator is not the problem.

The resetting to '0' of 'email_sent' is. It happens every time i change the timeframe of the chart on which the EA is running. Are you saying that should NOT happen?

 
Route206 #:

I'm not following.

The indicator is not the problem.

The resetting to '0' of 'email_sent' is. It happens every time i change the timeframe of the chart on which the EA is running. Are you saying that should NOT happen?

If your email_sent is reset to 0, that means you have some code resetting it, as a timeframe change doesn't reset a global variable.

From the pseudo-code you posted [no positions] must become true again.

If you need coding help, post all the relevant code that compiles.

 
Alain Verleyen #:

<snip> as a timeframe change doesn't reset a global variable.<snip>

But that is the point. As you can see (from the pseudo code) this is a normal variable and is declared in the EA on top. I'm trying to avoid global variables, as per initial post

If my assumption that these variables will be reset by a chartchange is FALSE then please confirm.

Alain Verleyen #:

<snip> you have some code resetting it <snip>

Yes, again that is as per (pseudo) code in my initial post: It is reset when positions are "0", but positions are checked BEFORE this test, so should not be a factor.

 

MY BAD!

forgot (to mention) that i had declared the variable as an external variable!

extern bool email_sent;

vs

bool email_sent; 
or
input bool email_sent;

makes *all* the difference here!!

Reason: