As traders, we often need to know when a new bar appears on our chosen timeframe. This is where the isNewBar function comes in handy. Let’s dive into how this works!
When you first call the isNewBar() function, the current bar isn't considered a new one yet. The function tracks the time of the current bar, so it knows that the next bar will be new. The code is commented for clarity, making it straightforward to follow.
Be mindful when using this method, which is commonly utilized in MQL4 - the bars counting using the Bars function (you can find more on this here).
In MQL5, this will only function if your chart's max bars setting is set to Unlimited:

When new bars appear, the Bars function will return an increasing number of bars. However, if the max bars in your chart is capped (like 10,000 bars), this approach won't work! It's an easy check to perform.
This is why it's crucial to keep track of the times of the last bars and compare them, as the time remains unchanged until a bar is completed. This method is incorporated into the indicator for better accuracy.
Related Posts
- Unlocking MetaCOT 2: Your Ultimate CFTC Indicator Toolkit for MT4
- Unlocking the Power of Master Tools for MetaTrader 4
- Visualize Current Trends Across All Time Frames with This MetaTrader 4 Indicator
- Mastering the MACD Candle Indicator for MetaTrader 4
- Hourly Buffers for Data Collection in MetaTrader 5: A Simple Guide