new bar formed? - page 3

 
Benjamin Dixon:
Sorry for the necro but a slightly better solution for MT4 was not yet posted... iTime()

Can be used to count new bars in OnTick() and or OnTimer() of multi symbol and or multi time-frame EA. For example:
(**as is, on the first call it will report a large number but will be accurate after that)

 

short CheckNewBar(short oslc,short otlc,string cursym,ENUM_TIMEFRAMES curtf)
  {
   datetime cbartime=iTime(cursym,curtf,0);
   if(cbartime!=nbartime[oslc,otlc])
     {
      datetime bardiftime=cbartime-nbartime[oslc,otlc];
      ulong bardif=bardiftime/(curtf*60);
      nbartime[oslc,otlc]=cbartime;
      if(curtf==PERIOD_D1 && DayOfWeek()==0 && bardif>=1) bardif--; //Do not count Sunday bars on a daily chart
      //Print(bardif," new bars found.");
      return (short)bardif;
     }
   return 0;

I don't like that at all.

1) Over complicated for the specification

2) Bar count returned includes imaginary bars that are not on the chart. e.g. Saturday bars or out of hours bars on lower timeframes when market is closed

3) curtf*60 is not a good way to get the seconds in a period, use PeriodSeconds()

 
bool newBar(int period) {
//
static datetime previousTime;
datetime currentTime;
bool returnValue;
//
currentTime = iTime(NULL,period,0);
if (returnValue = (previousTime < currentTime))
   previousTime = currentTime;
return returnValue;
}


 
Brian Dee - Random Trader:

W

> How we know when a new bar is formed

When the current bar gives its first tick

You can detect this with

...

...

Good Luck

-BB-

Thank you.

Reason: