Detect new candle for each timeframe

angomes  

Hi I want to have a function that I can call from on Tick() that returns a bool if there is a new candle or not for a specific timeframe. I've come up with tis but it's not working properly.

bool isNewCandleFormed(int timeframe)

{

   static datetime last_candle_timeM5 = 0;

   static datetime last_candle_timeM15 = 0;

   static datetime last_candle_timeM30 = 0;

   static datetime last_candle_timeH1 = 0;

   static datetime last_candle_timeH4 = 0;



   datetime current_time = Time[0];



   switch (timeframe)

   {

   case PERIOD_M5:

      if (last_candle_timeM5 == 0) {

         last_candle_timeM5 = iTime(NULL, timeframe, 0);

         return false;

      }

      if (current_time > last_candle_timeM5) {

         last_candle_timeM5 = iTime(NULL, timeframe, 0);

         Print("there is a new candle on:" + DoubleToStr(timeframe));

         return true;

      }

      return false;

   

   case PERIOD_M15:

      if (last_candle_timeM15 == 0) {

         last_candle_timeM15 = iTime(NULL, timeframe, 0);

         return false;

      }

      if (current_time > last_candle_timeM15) {

         last_candle_timeM15 = iTime(NULL, timeframe, 0);

         Print("there is a new candle on:" + DoubleToStr(timeframe));

         return true;

      }

      return false;

   

   case PERIOD_M30:

      if (last_candle_timeM30 == 0) {

         last_candle_timeM30 = iTime(NULL, timeframe, 0);

         return false;

      }

      if (current_time > last_candle_timeM30) {

         last_candle_timeM30 = iTime(NULL, timeframe, 0);

         Print("there is a new candle on:" + DoubleToStr(timeframe));

         return true;

      }

      return false;

   

   case PERIOD_H1:

      if (last_candle_timeH1 == 0) {

         last_candle_timeH1 = iTime(NULL, timeframe, 0);

         return false;

      }

      if (current_time > last_candle_timeH1) {

         last_candle_timeH1 = iTime(NULL, timeframe, 0);

         Print("there is a new candle on:" + DoubleToStr(timeframe));

         return true;

      }

      return false;

   

   case PERIOD_H4:

      if (last_candle_timeH4 == 0) {

         last_candle_timeH4 = iTime(NULL, timeframe, 0);

         return false;

      }

      if (current_time > last_candle_timeH4) {

         last_candle_timeH4 = iTime(NULL, timeframe, 0);

         Print("there is a new candle on:" + DoubleToStr(timeframe));

         return true;

      }

      return false;



    default:

      Print("no timeframe match");

      break;  



     }

     

     return false;

   }
Si Jun Tan  

int bars_count=0;

if(iBars(_Symbol, PERIOD_CURRENT)) > bars_count){

bars_count = iBars(_Symbol, PERIOD_CURRENT);

//.......

}else{



}

Alain Verleyen  
Si Jun Tan #:

int bars_count=0;

if(iBars(_Symbol, PERIOD_CURRENT)) > bars_count){

bars_count = iBars(_Symbol, PERIOD_CURRENT);

//.......

}else{



}

William Roeder  
Si Jun Tan #: if(iBars(_Symbol, PERIOD_CURRENT)) > bars_count){
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Forum rules and recommendations - General - MQL5 programming forum (2023)
              Messages Editor

  2. You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed, and that tick could arrive almost at the end of a bar's duration.

    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.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

Reason: