0
votes

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.

1

1 Answers

1
votes
  1. iTime() function does not exist in MQl5, only in MQL4. Use CopyRates() or SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE).
  2. 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.
  3. Bar close time is an interesting concept, I am not sure what you mean. if you have a bar (e.g., using MqlRates), you can get its open time. close time is not reported. you may count it yourself of course (open time + PeriodSeconds(_Period)-1).
  4. IsNewBar() function/ class has an important function - it checks each tick and says whether a new bar has started or not. It is possible to tell that a new bar starts because TimeCurrent() or Time of the last tick >= Time of the last known bar + 60*Period(). At the same time, it is not possible to tell whether a bar is closed or not (ok, it is possible to tell that a bar is closed if a new bar is created, and that's the only way) because many ticks may arrive after that (even at the last second of the bar).

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