Alert Once for Cross

 
I've got an EA that evaluates the previous 2 bars for a cross over of an indicator line. The problem I'm having is that the EA is alerting multiple times because it rechecks the previous bars repeatedly while the current bar is active. How do I prevent this from happening, so that it alerts only once?
 

MM

start()
{

  if (Volume[0]==1)  // On first tick of current bar only
    {
      // Do stuff here to evaluate


    }





return(0);
}

Good Luck

-BB-

 
BarrowBoy:

if (Volume[0]==1)

[...]

Being pedantic, it depends whether mixtermind wants to process signals only at the start of a bar, or only once per bar. The former is a specific subset of the latter. The check on Volume[0] will mean that the EA only places trades if it is running when a bar's first tick comes in (and is vulnerable to missing trades if MT4 misses ticks because it's busy, or the internet connection is slightly flaky). If the EA is e.g. running on an H1 chart, and it is started at e.g. 12:00:01 and there has already been a tick, then it won't place any trades before 13:00:00. This may or may not be desirable, depending on exactly what mixtermind is after.


If the requirement is for an EA which more broadly only examines signals once per bar, then the usual method recommended on this forum is something like the following:


// Store the start time of the most recent bar which has been examined, in a static variable which persists
// across calls to start() - but ***not*** across reloads of the EA...
static int MostRecentBarStart = 0;

// See if this bar is new
if (Time[0] > MostRecentBarStart) 
{
	// Store the time of the current bar, preventing further action during this bar
	MostRecentBarStart = Time[0];

	...process potential signals here
}
 

> Being pedantic, it depends whether mixtermind wants to process signals only at the start of a bar

Indeed :)

Though the implication was 'once at start' as he is processing prior bar values :)

I take the good points re the vulnerability of 'first tick' but, as they say 'is simples' :)

-BB-

 

Thanks guys!!


Appreciate the help.


Ken

Reason: