Download MetaTrader 5
To add comments, please log in or register
One billion tasks have already been executed with MQL5 Cloud Network. Test trading robots even faster!
Ian Venner
2583
Ian Venner 2014.09.06 18:17  
maindoor:

  Is time really that reliable ?

  The granularity of Time[0] is in seconds, so if

  within the first second you get two ticks, it would

  count as two newbars. :/



  The Time[] array holds the open time of each bar. By the time the second tick comes the time var is already updated to the new Time[0]

 

static datetime time = Time[0];

if(Time[0] > time)
{
 time = Time[0]; //newbar, update time
} 
whroeder1
13982
whroeder1 2014.09.06 19:24  
maindoor:   The granularity of Time[0] is in seconds, so if within the first second you get two ticks, it would count as two newbars. :/
   static datetime Time0; if (Time0 == Time[0]) return; Time0 = Time[0];
How would that happen. The code doesn't say to look at TimeCurrrent(), it says to look at Time[0]. The first tick received, (you can miss ticks,) the static doesn't match, new bar detected. Then for the rest of the bar (1 minute, 1 month) they do, same bar.
whroeder1
13982
whroeder1 2014.09.06 19:27  
SDC: The Time[] array holds the open time of each bar. By the time the second tick comes the time var is already updated to the new Time[0]
if(Time[0] > time)
Use not equal, not greater. There's a post a few years ago where the server clock was off on days, not time. When it reset, EA stopped working for days
maindoor
120
maindoor 2014.09.07 21:13  
SDC:

  The Time[] array holds the open time of each bar. By the time the second tick comes the time var is already updated to the new Time[0]

 


yes, you're right. My bad.
Daniel Lagoshniak
213
Daniel Lagoshniak 2015.09.23 10:02  

again with new bar detection. i received the following message in Experts log (after MT4 upgraded)

"2015.09.23 12:45:58.276 sazon_4waves_03 XAUUSD,M5: array out of range in 'sazon_4waves_03.mq4' (89,12)"

here is the code of that area 

https://gyazo.com/0ba6dd69466d430a1cd99339d56b9f8a(picture just to show line 89)

bool NewBar()
{
   if (Time[0] != newBar){// line 89
      newBar = Time[0];
      return true;
   }
   return false;
}

 

so the error when accessing Time[0]... is it my problem only, some more reliable methods of detecting new bars were found, or this is problem of a new terminal?

 

edited, thank you, GumRai. 

Keith Watford
Moderator
8870
Keith Watford 2015.09.23 10:08  

Why don't you just paste the relevant code using the SRC button?

Then we don't have to go look at another webpage to see what you are talking about. 

whroeder1
13982
whroeder1 2015.09.23 11:56  
Daniel Lagoshniak
213
Daniel Lagoshniak 2015.09.23 12:17  
no, i call it from OnTick(). thank you, WHRoeder.
Keith Watford
Moderator
8870
Keith Watford 2015.09.23 22:53  

danik,

If you are only calling this function from OnTick, then it is a concern. 

I would think that you could only get this error if there is absolutely no history loaded for the chart symbol.

How often do you get this error? Is it just at opening the terminal?

bool NewBar()
{
   if(ArraySize(Time)==0)
      return(false);
   if (Time[0] != newBar){// line 89
      newBar = Time[0];
      return true;
   }
   return false;
}

 may be a work around, but it shouldn't really be necessary

Benjamin Dixon
22001
Benjamin Dixon 2017.01.17 19:20  
Sorry for the necro but a slightly better solution for MT4 was not yet posted... iTime()

Can be used to count new bars in OnTick() and or OnTimer() of multi symbol and or multi time-frame EA. For example:
(**as is, on the first call it will report a large number but will be accurate after that)

 

short CheckNewBar(short oslc,short otlc,string cursym,ENUM_TIMEFRAMES curtf)
  {
   datetime cbartime=iTime(cursym,curtf,0);
   if(cbartime!=nbartime[oslc,otlc])
     {
      datetime bardiftime=cbartime-nbartime[oslc,otlc];
      ulong bardif=bardiftime/(curtf*60);
      nbartime[oslc,otlc]=cbartime;
      if(curtf==PERIOD_D1 && DayOfWeek()==0 && bardif>=1) bardif--; //Do not count Sunday bars on a daily chart
      //Print(bardif," new bars found.");
      return (short)bardif;
     }
   return 0;
/ /123
To add comments, please log in or register