Indicators: isNewBar

isNewBar:

The function allows checking whether a new bar has appeared on the specified timeframe.

For the indicators and Expert Advisors it's very often necessary the information whether a new bar has appeared on the specified timeframe. Here is an example, how it can be done.

After the first call of the isNewBar() function, the current bar is not assumed as a new bar. The function saves its time, it's the current bars, the next bar will be new. The code is commented, it's easy to understand, I hope it will be useful.

Author: Prival

Corrected, thank you.

Rosh:
Поправлено, спасибо

изменения не вступают в силу "немедленно"? пока все осталось без изменений...

Changes will take effect when the new version is published. Done. Thank you.

What are peoples thoughts on the best way to use an IsNewBar() type approach, during back-testing?


making decisions based on tick count is one, but using IsNewBar() based on time or Bars() just doesn't back test well at all...

Just what I need! Thank you.

For this function to work with historical bars, you need to add one more input parameter – the time of the current bar on the chart – and use this time as the starting point in the CopyTime() function:

int copied=CopyTime(_Symbol,timeFrame,time,1,new_Time);

In this way, you can process the chart’s historical bars in a loop and check whether the new bar belongs to the current or another (for example, an earlier) timeframe.