Code: Select all
bool IsNewBar(int period=0)
{
//+------------------------------------------------------------------+
//| Returns true if it's a new bar |
//+------------------------------------------------------------------+
static int dt[] = {0,0,0,0,0,0,0,0,0};
datetime now;
int index;
string tf;
int curPeriod, nextPeriod;
now = TimeCurrent();
switch( period )
{
case PERIOD_M1: index = 0; tf = TFToStr(period); break;
case PERIOD_M5: index = 1; tf = TFToStr(period); break;
case PERIOD_M15: index = 2; tf = TFToStr(period); break;
case PERIOD_M30: index = 3; tf = TFToStr(period); break;
case PERIOD_H1: index = 4; tf = TFToStr(period); break;
case PERIOD_H4: index = 5; tf = TFToStr(period); break;
case PERIOD_D1: index = 6; tf = TFToStr(period); break;
case PERIOD_W1: index = 7; tf = TFToStr(period); break;
case PERIOD_MN1: index = 8; tf = TFToStr(period); break;
}
// if there is a saved gv then sync it with the index array
if(gvc(tf)) dt[index] = gvg(tf);
//get the current period start time from the array
curPeriod = dt[index];
//calculate the next period start time
nextPeriod = MathMax(iTime(NULL,period,0),dt[index] + (60*period));
//log("IsNewBar: Index= ",index,"dt[index]",curPeriod,nextPeriod,now,"Period=",TFToStr(period),"now >= nextPeriod",BoolToStr(now >= nextPeriod));
if (now >= nextPeriod)
{
dt[index] = nextPeriod;
gvs(tf,nextPeriod);
log("IsNewBar: NewBar has started ",TimeToStr(now,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
return(true);
}
return(false);
}
This causes problems when, for instance, I want the value of an indicator shifted to the bar just closed.
Has anybody got any ideas on how this might be tweaked to be accurate for all pairs, or better still, been there and done that?
