HOW TO EXPLICITLY CONTROL BAR OPENING TIME IN EA

 
I have an EA that I am working on, however, the start() function is being executed at every tick. I want the start() function to be executed at the close of each bar (say if attached to a one hour chart) and not at every tick.

My code snippet to control bar opening time is pasted below but it is not working as expected as new ticks still get processed within bars.

static int pTime=0;
if((pTime==Time[0]) && (CurTime() - pTime) > 3600) {
newBarOpened = false;
} else {
newBarOpened = true;
}

Please is anybody in this forum able to help me with a snippet to control bar opening time?

ta

supa
 
1901:
My code snippet to control bar opening time is pasted below but it is not working as expected as new ticks still get processed within bars.

The method you are using is the standard one, and gets discussed on this forum at least once a fortnight. The principle is sound, although your implementation of it is slightly unusual. I think your only problem is that you need an or condition (||), not an and condition (&&). It looks like you're trying to handle the situation where pTime is zero on the first tick. However, it's not possible to be certain because you've not shown where you're setting pTime.

A slight variant, but using the same fundamental approach, would go like this:

static int LatestBarStart = 0;
bool NewBarOpened = false;

// On the EA's very first tick, LatestBarStart will be zero and needs to be initialised
if (LatestBarStart == 0) {
   LatestBarStart = Time[0];
   NewBarOpened = false; // Change if you don't want to wait for the first new bar after the EA starts 

// Otherwise, LatestBarStart contains the last Time[0] we saw. If it's not the same
// as the current value, we have a new bar
} else if (LatestBarStart != Time[0]) {
   NewBarOpened = true;
   // Record the time of this new bar in the static variable
   LatestBarStart = Time[0];

// In the same bar as the last pass through start()
} else {
   NewBarOpened = false;
}
 
this snippet works!

thanks

supa!
 
bool New_Bar=false;
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//------------------------------------------------------------------+
Fun_New_Bar();
if (New_Bar == false)
return;
//------------------------------------------------------------------+
{

}
}
//+------------------------------------------------------------------+
void Fun_New_Bar()
{
static datetime New_Time = 0;
New_Bar = false;
if (New_Time!= Time[0])
{
New_Time = Time[0];
New_Bar = true;
}
}
//-------------------------------------------------------------------+