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;
I agree completely that we need something as simple as possible.
Sadly this code does not seem to work for me 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.
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.
Why not simply using something like this :
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?)