I have tried the following code in MQL5 but getting errors. This code was I guess from MQL4.
Code:
int OnInit()
{
// if you don't want to execute on the first tick
IsBarClosed(-1,false);
return(1);
if(!IsBarClosed(0,true)) // true/false here allows to keep old bar for check again later in the code and reset
return(0);
}
//+------------------------------------------------------------------+
//| check if new bar has formed
//+------------------------------------------------------------------+
bool IsBarClosed(int timeframe,bool reset)
{
static datetime lastbartime;
if(timeframe==-1)
{
if(reset)
lastbartime=0;
else
lastbartime=iTime(NULL,timeframe,0);
return(true);
}
if(iTime(NULL,timeframe,0)==lastbartime) // wait for new bar
return(false);
if(reset)
lastbartime=iTime(NULL,timeframe,0);
return(true);
}
Output:
'iTime' - function not defined testing lines and trdae.mq5 243 25
'iTime' - function not defined testing lines and trdae.mq5 246 8
'iTime' - function not defined testing lines and trdae.mq5 249 21
3 error(s), 0 warning(s) 4 1
Kindly, help me in getting it done correctly with MQL5. I am trying to detect the candle Bar closing time and not opening time. I just want to attempt when the bar closes.
iTime()
function does not exist in MQl5, only in MQL4. Use CopyRates()
or SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE)
.static
is a keyword in MQL4/5 with very specific properties. Many versions it worked in the following way: if you attach ea to the chart, static
is zero and then updates. if you reattach - from zero to actual. if you change timeframe or settings of the ea/ind - static remains same (it doesnt deinitialize, doesnt become zero and then actual value). The earliest 1000+ build of MT4 worked like that (two updates back from now). Maybe someone find this keyword useful in mql4 that allows to keep variables together with their functions and not in the globals; of course keeping in mind the problem above or ignoring it. But there is no reason to use this word in MQL5. if you need a set of functions - create a class and keep all variables related to it. Then you wont have problems with static variables that havent been reinitialized.open time + PeriodSeconds(_Period)-1
). I do not know why you need to reset parameters in your code, try the following:
datetime iTime=(datetime)(SeriesInfoInteger(_Symbol,Period(),SERIES_LASTBAR_DATE)/PeriodSeconds()*PeriodSeconds());
and then replace iTime()
in your code with iTime variable, that may help