You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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;
;-) Ianif (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.
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.
datetime ThisBar = (datetime)SeriesInfoInteger(_Symbol,PERIOD_CURRENT,SERIES_LASTBAR_DATE);
if(LastBar != ThisBar)
{
printf("New bar: %s",TimeToString(ThisBar));
LastBar = ThisBar;
}
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.
datetime ThisBar = (datetime)SeriesInfoInteger(_Symbol,PERIOD_CURRENT,SERIES_LASTBAR_DATE);
if(LastBar != ThisBar)
{
printf("New bar: %s",TimeToString(ThisBar));
LastBar = ThisBar;
}
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.
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 ;-)
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 :
datetime lastTime[1];
if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
{
prevTime=lastTime[0];
// ...
}
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
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?)
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?)