MQL4 on New Bar: Why Time-Based Detection Is the Only Reliable Approach
Camovia Tray Team · 2026-09-16
Every MQL4 developer eventually faces the same question: how do I detect the exact moment a new bar opens? At first glance, it seems straightforward—the chart draws a new candle, and your EA needs to act. But look closer, and the problem reveals its complexity.
Consider what actually happens when a new bar forms. The terminal receives a tick, processes it, and if that tick arrives after the previous bar's close time, the new bar begins. But here's the catch: you can't know precisely when a candle closes. You only know when a new tick arrives that starts the next bar—and that tick could arrive almost at the end of the bar's duration .
So the real question becomes: what's the most reliable way to detect this event in your Expert Advisor?
The Pitfalls of Popular Approaches
Many developers naturally reach for the Bars function. It seems logical—when the bar count changes, a new bar must have appeared. But this method has hidden flaws. A chart refresh or reconnection can change the number of bars without a new bar actually forming. The bar count can sometimes decrease instead of increase, or remain the same even when the chart updates .
Volume-based detection suffers from similar unreliability—ticks can be missed, leaving your EA unaware of a new bar's arrival. Price-based comparison using the equality operator (==) is equally problematic due to duplicate prices .
The Standard Solution: Time-Based Detection
The community has converged on a solution that works consistently across both MQL4 and MQL5: monitoring the opening time of the most recent bar. When that timestamp changes, you have a new bar .
Here's the standard implementation pattern that the MQL5 community recommends:
void OnTick()
{
static datetime dtBarCurrent = WRONG_VALUE;
datetime dtBarPrevious = dtBarCurrent;
dtBarCurrent = iTime(_Symbol, _Period, 0);
bool bNewBarEvent = (dtBarCurrent != dtBarPrevious);
if (bNewBarEvent)
{
// Your new bar logic here
// Important: handle the initial attachment case
if (dtBarPrevious == WRONG_VALUE)
{
// First tick when EA is attached—this isn't a real new bar
// Handle this case separately
}
else
{
// Genuine new bar event
// Execute your strategy logic
}
}
} This approach works because the iTime() function returns the opening time of the current bar. When the bar closes and a new one opens, the timestamp changes, and your EA catches it on the next tick .
The Development Experience
What's interesting is how this challenge reflects the broader experience of building automated trading systems. You start with a straightforward idea—"detect a new bar and run my strategy"—then discover that the platform doesn't provide a dedicated OnNewBar() event handler. You research, test different methods, find the pitfalls, and eventually settle on the time-based solution .
This is the kind of practical nuance that separates robust EAs from fragile ones. And it's also the kind of detail that makes MetaTrader development feel like a craft rather than just coding.
Where Camovia Tray Fits In
For traders who build and test EAs, there's a workflow reality that often goes unmentioned: you're frequently switching between chart windows, checking terminal states, monitoring positions, and debugging your EA's behavior.
When you're in the middle of refining your new bar detection logic and need to quickly check your open positions or see if your EA has executed as expected, you face a choice. Do you keep the terminal maximized and clutter your screen, or do you minimize it and lose visibility?
Camovia Tray addresses this by letting you keep MT4 running in the background while still maintaining situational awareness. From the system tray, you can check live quotes, view open positions with their current P&L, and even close positions with a two-step confirmation [citation:PRODUCT_KNOWLEDGE]. Your EA continues running, your charts stay hidden from Alt+Tab and the taskbar if you choose, but you retain quick access to the information you need [citation:PRODUCT_KNOWLEDGE].
For developers testing new bar strategies, this means less context-switching: your MT4 can sit quietly in the background, processing ticks and executing logic, while you work elsewhere—and you can peek at the terminal state without pulling the entire application to the foreground.
Beyond the Basics
As your EA development progresses, you might encounter edge cases that the simple time-based method doesn't fully handle. For instance, when an EA is first attached to a chart, the code reacts as if a new bar has just opened, even if the bar is already halfway through its lifespan . This requires special handling, as shown in the code example above.
For more complex scenarios—such as detecting bars on higher timeframes while running on a lower timeframe—you can maintain an array of timestamps and check each one on every tick . And for Renko and Range bars, you may need additional checks since the timestamp alone isn't sufficient to identify a new bar formation .
But the principle remains consistent: time-based detection using iTime() is the most reliable foundation. Other methods—Bars, volume, price—introduce edge cases that can break your EA's logic when you least expect it.
In the end, reliable new bar detection isn't just about getting the code right. It's about building confidence that your EA sees the market exactly when it needs to. And anything that helps you focus on that goal—whether it's cleaner code or a better development environment—is worth your attention.
MT4/MT5 Tray Assistant
Silent tracking, one-click close - check quotes and manage positions right from the tray.
Download Camovia TrayFrequently Asked Questions
Are my quotes and positions uploaded anywhere?
No. Quote and position data is read 100% from your local MT5/MT4 terminal and never leaves your computer. See the privacy policy for details.
Which systems and terminals are supported?
Windows 10 / 11, with MetaTrader 5 or MetaTrader 4 (installed and logged in; MT4 needs the bridge EA attached once).
Is the MT4 bridge EA safe? What do I need to enable?
Yes. The bridge EA (CamoviaBridge) is bundled with the app - it only reads quotes/positions locally and executes close commands; no DLLs, no data uploads. Closing positions requires turning on AutoTrading in the MT4 toolbar.
How much does it cost? Is there a free trial?
Subscription pricing starts at $2.49/month (also $6.99/3 months, $13.49/6 months, $23.99/year), all plans with full features. New users get a 2-day free trial on first activation (once per device and per email), then decide whether to subscribe.
Popular Symbols Trading Hours
Open/close times, weekend hours & live market status:
Related Articles
How to Hide MT5 Window on Windows 10 – A Smarter Way to Keep Your Trading Private
Hide MT5 window on Windows 10 completely with Camovia Tray – remove from taskbar & Alt+Tab, check quotes from system tray, and manage positions privately.
MT5 Market Watch Not Working? Here's How to Fix It and a Better Way to Keep an Eye on Your Positions
Fixing MT5 Market Watch not working? Learn quick troubleshooting steps, plus discover how Camovia Tray lets you check live quotes and manage positions right from your system tray—without keeping the full terminal open.
How to Hide MT4 from Alt+Tab on Windows 10 (And Why You Might Want To)
Learn how to hide MT4 from Alt+Tab on Windows 10 for better privacy. Camovia Tray lets you monitor positions and quotes from your system tray while keeping MT4 hidden.
MetaTrader 4 on Windows 11: Download, Install, and the One Thing Every Trader Needs
Download MT4 on Windows 11 and enhance your trading workflow. Camovia Tray turns MT4 into a tray tool for quick quotes and one-click position management.
Market Data Tools vs. Tick Charts: Why the Best Tool Isn’t Always a Chart
Turn your MT5/MT4 into a tray tool with Camovia Tray. Check live quotes and manage positions without opening your terminal—perfect for tick chart traders who need fast, frictionless market data access.