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

 
Tks for the code...
for me, was a totally horrible thing try to find a way to "catch" this event, when I just started coding en MQL...
 
SedeVacante Gracias #: Tks for the code... for me, was a totally horrible thing try to find a way to "catch" this event, when I just started coding en MQL...

You are welcome!

 

I have no clue why it has 1.5 Star! Works perfectly as the description! 5 Stars!

Cheers

 
Zarik #: I have no clue why it has 1.5 Star! Works perfectly as the description! 5 Stars! Cheers

Thank you!

 
[Private Message]: hi i came across in codebase the code you wrote for "Detecting the start of a new bar or candle - expert for MetaTrader 4" 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 ...
   };
 
what is the function of wrong_value.
 
Zombie Night #: what is the function of wrong_value.
                 // Do something on first tick or middle of brick ...
 
int totalBars;

void OnTick()
{
    int bars = iBars(_Symbol,0);
    if (totalBars != bars)
    {
        totalBars = bars; // update total bars
        // rest of the new-bar logic
    }
}

I've always used this code snippet to find new bars; does it function the same as your code?

 
@Guillermo Pineda #: I've always used this code snippet to find new bars; does it function the same as your code?

Yes, the bar-count method also works in most cases, but I personally don't like it much.

Sometimes the bar count decreases, instead of increasing, or even stays the same. I have not found any documented reasoning behind the bar-count behaviour, so I personally don't use it, but many users do use it.

If uncertain, you can combine multiple methods to make your EA more robust. For example, with Renko and Range Bars, one also needs to do extra checks, as the time-stamp alone is not enough to identify a new bar.