Detect New Candle

 

Hi Guys, 

I've read the forums, and this question has come up alot. I am trying to detect a when a new candle forms, and my code is simple but for some reason, Time[0] within a while loop does not update.

Could you guys explain why please?


bool NewBar() {

    datetime date = Time[0]+PeriodSeconds();

    while(Time[0] != date){

  }

    return(true);

}

 
/******************************************************************************/
//                                                                            //
//============================================================================//
// AIS NEW BARS CONTROLLER : FUNCTION 1                                   //<  >
//------------------------------------------------------------------------//<-->
PLAIN  CHECK_NEW_CURRENT ()                                             { //< 1>
                                                                          //< 2>
static datetime TIME_CURRENT_LAST = NULL                                ; //< 3>
       datetime TIME_CURRENT_THIS = iTime (SYMBOL, PERIOD_CURRENT , 0 ) ; //< 4>
                                                                          //< 5>
if   ( TIME_CURRENT_LAST < TIME_CURRENT_THIS  )                           //< 6>
     { TIME_CURRENT_LAST = TIME_CURRENT_THIS                            ; //< 7>
                                                                          //< 8>
                                                                          //< 9>
       FLAG_NEW_CURRENT  = TRUE                                         ; //<10>
     }                                                                    //<11>
else   FLAG_NEW_CURRENT  = NULL                                         ; //<12>
                                                                          //<13>
                                                                          //<14>
                                                                          //<15>
                                                                          //<16>
                                                                        } //<17>
//============================================================================//
//                                                           2020-06-26 20:35 //
/******************************************************************************/
 
goodestboy1997: Time[0] within a while loop does not update.
  1. Of course not. After Sleep and between server calls the market will have changed. You must update your variables before using any Predefined Variables or series arrays — you must call RefreshRates. RefreshRates updates:
    Predefined variables: Ask, Bars, Bid, Close[], High[], Low[], Open[], Point, Time[], Volume[]
              RefreshRates - Timeseries and Indicators Access - MQL4 Reference
              Predefined Variables - MQL4 Reference
  2. Why are you looping? Just return and wait for a new tick and retest.

  3. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.