How to check a new bar?

 

I see a coding as below to check the formation of a new bar but I am not aware why when the expression if(New_Time!=Time[0]) return ture, a new bar is deemed to be formed. New_Time is defined as 0, but Time[0] returns to date, eg. 2009.11.01 14:34. They should not be equal to always.

Thanks for your tutorial...

void Fun_New_Bar()                              
  {                                             
   static datetime New_Time=0;                  
   New_Bar=false;                               
   if(New_Time!=Time[0])                        
     {
      New_Time=Time[0];                         
      New_Bar=true;                                
     }
  }
 

Hi morken, 

See static examples in reference. It's only defined/initialized once, after that it will hold new values. 

 

Sample:

//////////////////////////////////////////////////////////////////<          >
bool       bNewBar ()                                           //<          >
{                                                               //<          >
//                                                              //<          >
static int iTime_0 = 0                                        ; //<          >
//                                                              //<          >
if       ( iTime_0 < iTime ( 0 , 0 , 0 ) )                      //<          >
         { iTime_0 = iTime ( 0 , 0 , 0 ) ; return ( TRUE  ) ; } //<          >
else     {                                 return ( FALSE ) ; } //<          >
//                                                              //<          >
}                                                               //<          >
//////////////////////////////////////////////////////////////////<          >
     

Explanations:

https://www.mql5.com/en/forum/124521/page5#287213, Answer 2

https://www.mql5.com/en/forum/124521/page8#294769, part 3

And please look at my first post in -> https://www.mql5.com/en/forum/130486 <- about why in critical parts of program "iTime()" is more preferable than "Time[]".

 

Ais I read your post in that other topic where you said use timeseries access functions such as iBars and iTime to get actual data, but you didnt explain why that is so.

I would take your word for it as it is obvious from your other posts you are an excellent programmer and you know what you are talking about, but would you mind explaining why time series access functions give instantly accurate data but predifined variables require refresh rates ? I have never really understood the reason for this, it would seem appropriate that as each iteration of an indicator or an EA is run at the beginning of a new tick, the data of that tick should be the current data and therefore the predifined variables such as Ask, Bid, Bars etc should be accurate for the current cycle of the indicator/EA. I know now that is not always the case, but i would like to understand the reason for that. Unless of course it is simply that a new tick could have arrived before the indicator/EA has finished the current iteration of start() ...

 

Yes, when MQL program starts new run on new tick, exactly in the beginning of "start()" function, the predefined variables have actual values, because the terminal prepares them just before to execute "start()".

If something change, Ask, Bid, Bars etc, during the still executing "start()", the only way to get actual data in these predefined variables is to call "RefreshRates()".

Moreover, if we do not use "RefreshRates()" in the critical parts of program, there is a risk of using invalid data with the predefined variables.

Somewhere in Russian part of this forum someone of MetaQuotes sometime told something like this: "Timeseries functions always return actual data".

.

MQL4 Documentation:

https://docs.mql4.com/series -> "A group of functions intended for access to price data of any available symbol/period."

https://docs.mql4.com/predefined/variables -> "predefined variables ... These data are updated at every launch of an attached expert or a custom indicator automatically or using the RefreshRates() function call. "

 

Thanks for your reply Ais that is very useful information to be aware of, you have cleared something up for me that I was never really sure about.

Reason: