Event handling for bar completion

 

Hi,

Is there a way to execute some code immediately upon completion of a bar? I'm currently using OnTick but this means that the price would have already changed since the completion of the last bar. I want to trigger an event as soon as the closing price is recorded for the last bar, i.e. before creation of the new bar.

Thanks.

 
dakr2019:

Hi,

Is there a way to execute some code immediately upon completion of a bar? I'm currently using OnTick but this means that the price would have already changed since the completion of the last bar. I want to trigger an event as soon as the closing price is recorded for the last bar, i.e. before creation of the new bar.

Thanks.

I doubt there is any, because all programmatic actions are triggled by a tick, and nobody will know when a tick is the last one... until a new tick within the timeframe of the new bar comes in.

 
dakr2019: Is there a way to execute some code immediately upon completion of a bar? I'm currently using OnTick but this means that the price would have already changed since the completion of the last bar.

There is no completion of a bar. A bar is complete when a new bar starts. You can never know which tick will be the last. There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific.) requires knowledge of when your broker stops and starts (not necessary the same as the market.)

 

As William said , plus each asset (other than forex) has its own trading times . 
But , in case you aim to code a binary options ea , you know the time beyond which no more ticks will be registered for the current bar ,so if you use Timer events to detect that this specific time has arrived you may manually operate your code in such a way . 

Example , an M5 bar started at 13:55:00 , even if there are no ticks at 14:00:00 or 14:00:++ your code may fire when the server time arrives at 14:00:00 +,or 13:59:59 (in case of BO)

 
Lorentzos Roussos:

As William said , plus each asset (other than forex) has its own trading times . 
But , in case you aim to code a binary options ea , you know the time beyond which no more ticks will be registered for the current bar ,so if you use Timer events to detect that this specific time has arrived you may manually operate your code in such a way . 

Example , an M5 bar started at 13:55:00 , even if there are no ticks at 14:00:00 or 14:00:++ your code may fire when the server time arrives at 14:00:00 +,or 13:59:59 (in case of BO)

Thanks. This is what I had in mind. Do the bars always start/finish at second 0?
 
dakr2019:
Thanks. This is what I had in mind. Do the bars always start/finish at second 0?

No , there are assets where you will see weird bar times ,BUT , you know the expected duration . 
So if a stock has H1 bars which starts at 15:44:33 you know that this bar will logically not receive ticks past 16:44:33.

Do the same for lower timeframes 

 
You can copy MqlRates and check how old is the open value, this you can count to the next bar
 

What I do:


For EAs:

Create a datetime variable to store the current bar's datetime value and initialize it in OnInit(). Then, in OnTick(), check that variable against the current bar's datetime to see if they're the same. If they're not, a new bar has been created, so then execute the code and set the variable to the value of the current bar.


For Indicators:

Check if

rates_total-prev_calculated == 1

in the OnCalculate() body. If true, a new bar has been created.

Reason: