What's the best way to check if the tick is the first tick of the new bar?

 

I found in the sample code, it uses

if(Volume[0]>1) return;

as the criterion to check if the tick is the first tick of the new bar. I just worry if it will miss some conditions. What's the best way to check the condition? Thanks a lot.

 

I know every time new bar appears, Bars will change and IndicatorCounted() changes just one tick after Bars changes, so could I just use a static int to restore the Bars and compares the new Bars to that int? What I worry is that maybe there is a maximum number for Bars and Bars will not change after reaching that number, is that right?

 

I always use:

bool IsNewBar()

{

static datetime lastbar = 0;

datetime curbar = Time[0];

if(lastbar!=curbar)

{

lastbar=curbar;

return (true);

}

else

{

return(false);

}

}

It triggers on the first tick of the new bar.

Hope that helps!

-CS

 
cubesteak:
I always use:
bool IsNewBar()

{

static datetime lastbar = 0;

datetime curbar = Time[0];

if(lastbar!=curbar)

{

lastbar=curbar;

return (true);

}

else

{

return(false);

}

}

It triggers on the first tick of the new bar.

Hope that helps!

-CS

@cubesteak: Always nice to see coders use functions

Once coded/blackBoxed && tested! can be shoved into .mqh file and forgotten about!

My imho observation:

What about first call?

Will give true.

Chance of calling on first tick problematical, yes?

I mention as very noticeable in stratTester and can sometimes, when trade conditions indicate "trade" + is on first entry to start() and therefore first call to IsNewBar() which returns true even though not new bar so EA trades and potentially makes nice loss...

Suggest (slight) mod:

//mod is to check for firstTime called state. Many ways to structure code. eg,

if(lastbar<=0) {lastbar = curbar; return(false);} //placed at function top.

//Consideration: overhead of testing for firstTime called state is less than minimal...

//But if seen as issue:

//another method is to leave code untouched and place priming callinside init(); ie,

IsNewBar(); //this will load lastbar with current Time[0]

//and thereafter (until) actual new bar state occurs, 2nd+ calls will return false