Experts: Detecting the start of a new bar or candle


Detecting the start of a new bar or candle:

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.

Author: Fernando Carreiro

[Private Message]: hi i came across in codebase the code you wrote for "Detecting the start of a new bar or candle" i am trying to make my EA look at the signal indicator after every 60 minutes would this piece of code help me or is there another project in codebase that could help explain to me better what i am trying to do.

Yes, if you use the H1 time-frame or place the EA on a H1 chart then at the start of each H1 bar you can retrieve the indicator data.


Forum on trading, automated trading systems and testing trading strategies

I want place an order at the beginning of a new candle in Renko chart.

Fernando Carreiro, 2023.02.13 11:29

// Default tick event handler
   void OnTick() {
      // Check for new renko brick
         static datetime dtBrickTimeCurrent  = WRONG_VALUE;
         static double   dbBrickOpenCurrent  = WRONG_VALUE;
                datetime dtBrickTimePrevious = dtBrickTimeCurrent;
                double   dbBrickOpenPrevious = dbBrickOpenCurrent;
                         dtBrickTimeCurrent  = iTime( _Symbol, _Period, 0 );
                         dbBrickOpenCurrent  = iOpen( _Symbol, _Period, 0 );
                bool     bNewBrickEvent      = ( dtBrickTimeCurrent != dtBrickTimePrevious ) ||
                                               ( dbBrickOpenCurrent != dbBrickOpenPrevious );
      // React to a new brick event and handle it.
         if( bNewBrickEvent ) {
            // Detect if this is the first tick received and handle it.
               /* For example, when it is first attached to a chart and
                  the brick is somewhere in the middle of its progress and
                  it's not actually the start of a new brick. */
               if( dtBrickTimePrevious == WRONG_VALUE ||
                   dbBrickOpenPrevious == WRONG_VALUE    ) {
                  // Do something on first tick or middle of brick ...
               } else {
                  // Do something when a normal brick starts ...
            // Do something irrespective of the above condition ...
         } else {
            // Do something else ...
      // Do other things ...