Code: Select all
bool isNewBar() {
static datetime lastBarTime = -1;
bool newBar = false;
if ( lastBarTime < iTime( _Symbol, _Period, 0 ) ) {
lastBarTime = iTime( _Symbol, _Period, 0 ) + 1;
newBar = true;
}
return(newBar);
}The attached CBar class is to be used if you (like myself) are running multi-symbol/multi-timeframe code. As you know ticks for different symbols are arriving at different times. So, it is possible that one symbol has started a new candle (bar) whilst the other is still on the old candle. Is this important? I don't know -- that depends on your code.
The use of the CBar class is dead-simple:
1. Somewhere at the top of you file, you create a CBar object like so
Code: Select all
#include <Bar.mqh>
CBar myBar;
Code: Select all
if ( myBar.isNewBar( symbol, timeFrame ) ) {
// do something
}
I am a big fan of building (and sharing) a set of re-usable software components. This makes the task of creating new EAs much easier and quicker.
Hope this is useful for some of the coders.
Edit: I have made a little change to CBar, to stop it from reporting a new bar when it is first loaded, whilst in the middle of a bar. It should now patiently wait for a real new bar.