Download MetaTrader 5

How to detect a new bar - page 2

To add comments, please log in or register
Hennie Reyneke
1419
Hennie Reyneke  
Ian Harris:

Hi oneillj,

I thought I'd replied already, but it seems to have vanished into the internet ether.

The code fragment you provided is pretty much the one I was complaining about in the first place. Why so complex? I just figured there had to be an easier way. I came up with this one, which seems to work. Please correct me if I'm wrong. (I also tried using BarsCalculated, but it was always equal to Bars, so the test always evaluated to false.)

   static int   LastBarCount = 0;
   if (Bars(_Symbol, _Period) > LastBarCount)
      LastBarCount = Bars(_Symbol, _Period);
   else
      return;

;-) Ian

I agree completely that we need something as simple as possible. 

Sadly this code does not seem to work for me in MQL5. 

honest_knave
Moderator
2342
honest_knave  
RoboSpider:
Sadly this does not seem to work in MQL5.

Bars would be a bad way to detect a new bar anyway.

The only reliable way to check for a new bar is using time. Not volume, not prices, not bars.

static datetime LastBar = 0;
datetime ThisBar = (datetime)SeriesInfoInteger(_Symbol,PERIOD_CURRENT,SERIES_LASTBAR_DATE);
if(LastBar != ThisBar)
  {
   printf("New bar: %s",TimeToString(ThisBar));
   LastBar = ThisBar;
  }
Hennie Reyneke
1419
Hennie Reyneke  
honest_knave:

Bars would be a bad way to detect a new bar anyway.

The only reliable way to check for a new bar is using time. Not volume, not prices, not bars.

static datetime LastBar = 0;
datetime ThisBar = (datetime)SeriesInfoInteger(_Symbol,PERIOD_CURRENT,SERIES_LASTBAR_DATE);
if(LastBar != ThisBar)
  {
   printf("New bar: %s",TimeToString(ThisBar));
   LastBar = ThisBar;
  }
You're my hero!
whroeder1
17061
whroeder1  
honest_knave Bars would be a bad way to detect a new bar anyway. The only reliable way to check for a new bar is using time. Not volume, not prices, not bars.
  1. Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 forum.) Always use time. New candle - MQL4 forum
Alain Verleyen
Moderator
35042
Alain Verleyen  
honest_knave:

Bars would be a bad way to detect a new bar anyway.

The only reliable way to check for a new bar is using time. Not volume, not prices, not bars.

static datetime LastBar = 0;
datetime ThisBar = (datetime)SeriesInfoInteger(_Symbol,PERIOD_CURRENT,SERIES_LASTBAR_DATE);
if(LastBar != ThisBar)
  {
   printf("New bar: %s",TimeToString(ThisBar));
   LastBar = ThisBar;
  }

Just for information, I have the following note in my references :

      //-- Sometimes SeriesInfoInteger with SERIES_LASTBAR_DATE return an error,

That's a long time ago and I am not using/testing this function for years, so maybe it's not more the case. But if you want to use it to detect a new bar, it's probably better to check it ;-)

honest_knave
Moderator
2342
honest_knave  
Alain Verleyen:

Just for information, I have the following note in my references :

      //-- Sometimes SeriesInfoInteger with SERIES_LASTBAR_DATE return an error,

That's a long time ago and I am not using/testing this function for years, so maybe it's not more the case. But if you want to use it to detect a new bar, it's probably better to check it ;-)

That's very cryptic Alain! What type of errors?

In which case, for belts'n'braces either check SeriesInfoInteger >0 and/or ResetLastError before call then check _LastError after call.

Or get the time source for somewhere else e.g. time[] if in OnCalculate, or CopyTime etc etc. 

Mladen Rakic
71703
Mladen Rakic  

Why not simply using something like this :

   static datetime prevTime=0;
          datetime lastTime[1];
          if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
          {
               prevTime=lastTime[0];
              
               // ...
          }
It should work in all cases (even when there is an error with CopyTime() it avoids a trap)
Alain Verleyen
Moderator
35042
Alain Verleyen  
honest_knave:

That's very cryptic Alain! What type of errors?

In which case, for belts'n'braces either check SeriesInfoInteger >0 and/or ResetLastError before call then check _LastError after call.

Or get the time source for somewhere else e.g. time[] if in OnCalculate, or CopyTime etc etc. 

I am just saying that when I used it years ago SeriesInfoInteger() was not really reliable. Up to you to see what you do with this information :-D
honest_knave
Moderator
2342
honest_knave  
Alain Verleyen:
I am just saying that when I used it years ago SeriesInfoInteger() was not really reliable. Up to you to see what you do with this information :-D

Not a lot TBH - I don't need to detect a new bar in MT5  But thanks for sharing - I'll bear it in mind for the future.

I was just trying to simplify something that people seemed to be making excessively complicated.

Nevertheless, the principle remains: use time. Caveat: choose your method of obtaining time at your own peril having regard to Alain's incomplete historical notes (how's XP Service Pack 1 looking, by the way?)

Alain Verleyen
Moderator
35042
Alain Verleyen  
honest_knave:

Not a lot TBH - I don't need to detect a new bar in MT5  But thanks for sharing - I'll bear it in mind for the future.

I was just trying to simplify something that people seemed to be making excessively complicated.

Nevertheless, the principle remains: use time. Caveat: choose your method of obtaining time at your own peril having regard to Alain's incomplete historical notes (how's XP Service Pack 1 looking, by the way?)

123
To add comments, please log in or register