New Bar Detection
The standard "new bar" snippet keeps one static datetime at the top of OnTick and compares it against the current bar's open time. It works, but only for exactly one symbol/timeframe pair per call site - an EA that watches a signal on M15 and a filter on H1, or the same logic across several symbols, ends up either duplicating the snippet at every call site or building a class around it. This header keeps a small internal table instead, keyed by symbol and timeframe, so IsNewBar(symbol, timeframe) can be called for as many different pairs as needed from one place and each pair gets its own correctly isolated state. The first call for any given pair deliberately returns false. It records the current bar as the baseline rather than reporting it as "new," because it is not new - it is just the first time this code has looked. Returning true on that first call is what makes so many EAs fire their new-bar logic once, immediately, the instant they are attached to a chart, which is rarely the intended behaviour and is not a genuine new-bar event. IsNewBar also accepts an explicit bar-open time as an optional third argument. Left at its default (0) it reads iTime() itself, which is what real trading code uses. Supplied directly, it drives the same state machine with a caller-chosen value instead - this is what lets the bundled demo prove the baseline-then-true transition deterministically, without needing to sit and wait for an actual bar to close, and it is equally useful for anyone testing their own code that depends on this header, or feeding it bar times from a source other than the standard chart series. Notes and limitations - State lives inside the header's own arrays for the lifetime of the program that includes it. It is not persisted across restarts or between separate programs - two different EAs each including this header get independent tracking tables, not a shared one. - The lookup is a linear scan over tracked pairs. Fine for the handful of symbol/timeframe combinations a typical EA watches; not built for hundreds. - Passing barTime explicitly bypasses iTime() entirely, including its own "no history yet" zero-return check. Only pass 0 for live chart data, or a value you know is meaningful. - Not repainting-related: this header only detects that a new bar has started based on its open time, it does not read or judge indicator values.
Script · MT5 · v1.0 · Free
About New Bar Detection
New Bar Detection is a script for MetaTrader 5. MQL source code included.
Files in the download: NewBar.mq5.mqh, NewBar.txt, NewBarDemo.ex5, NewBarDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Buy/Sell Arrows · MT5 Script · MQL5 Source Code