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

 
William Roeder #:

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)

In this case @William Roeder, your usual logic does not apply because of dealing with Renko bricks, where all three have to be considered, Open Time, Open Price and Tick Volume.

Provided the Renko generator is correctly producing the bricks, then zero volume denotes virtual filler bricks, and time alone is unreliable because you can have multiple bricks within the same second, so you have to consider both the time and the open price to detect a new brick and also the volume if you want to skip over the virtual filler bricks.

 
Fernando Carreiro #:

In this case @William Roeder, your usual logic does not apply because of dealing with Renko bricks, where all three have to be considered, Open Time, Open Price and Tick Volume.

Provided the Renko generator is correctly producing the bricks, then zero volume denotes virtual filler bricks, and time alone is unreliable because you can have multiple bricks within the same second, so you have to consider both the time and the open price to detect a new brick and also the volume if you want to skip over the virtual filler bricks.

Hi, Fernando

Can you give me some example code to detect at the begging of a new candle for Renko with using both the brick's open price and its time?

How to code it ?

 
Hong Ling Mu #: Hi, Fernando. Can you give me some example code to detect at the begging of a new candle for Renko with using both the brick's open price and its time? How to code it ?
// 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 ...
   };
 
Fernando Carreiro #:

Thank you so much for taking time.

It is first time to study this code.

I will try to understand one by one.

 
Hong Ling Mu #: Thank you so much for taking time. It is first time to study this code. I will try to understand one by one.
You are welcome!
Reason: