Good afternoon. Tell me how to correctly describe a new bar on any time frame ?
the compiler swears at the instruction.
‘isNewBar’ – function can be declared only in the global scope New_Bars.mq5 37 6
OK. I Declare in globalka and there is no result.
//+------------------------------------------------------------------+
//| Returns true if there is a new bar for the symbol/period |pair
//+------------------------------------------------------------------+
bool isNewBar()
{
/ / - - - in a static variable, we will remember the opening time of the last bar
static datetime last_time=0;
/ / - - - current time
datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
/ / - - - if this is the first function call
if(last_time==0)
{
//--- set the time and go out
last_time=lastbar_time;
return(false);
}
/ / - - - if the time is different
if(last_time!=lastbar_time)
{
//- - - remember the time and return true
last_time=lastbar_time;
return(true);
}
/ / - - - we have reached this point-so the bar is not new, we will return false
return(false);
}
greshnik1
With such questions, it is better to refer to the help, learn how to use it.
barabashkakvn
Show the full code. If you have a question, please develop the habit of showing the full code and attaching the code to the message in the form of an mq5 file.
setborg
‘InpTimeFrame’ – cannot convert enum New_BARS.mq5 80 35
Swears at the enumeration
peterkonow
No. And what exactly works differently on MT5? No time series Time[]? Then you can use iTime(Symbol(), TimeFrame,0);
barabashkakvn
New bar. If a new bar is detected on the current symbol and on the specified timeframe “InpTimeFrame” – it skips us further, if the bar is not new-it throws us out of OnTick().
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- we work only at the time of the birth of new bar
static datetime PrevBars=0;
datetime time_0=iTime(Symbol(),InpTimeFrame,0);
if(time_0==PrevBars)
return;
PrevBars=time_0;
...
}
The variable “InpTimeFrame” is of type ENUM_TIMEFRAMES.
setborg
Do you have an example but only for MQL5 ???