Detecting New Bars in MetaTrader Expert Advisors
Summary
The document explains how a MetaTrader Expert Advisor can detect the start of a new candle despite the platform providing tick events rather than a dedicated new-bar event. The method stores the opening time of the current bar in a persistent static variable and checks on each tick whether that time has changed. A change indicates that a new bar has appeared, allowing the EA to run bar-specific logic once instead of on every incoming tick.
The example is described as compatible with MQL4 and MQL5. It also highlights initialization behavior: the first tick after attaching the EA can look like a new bar because no previous opening time has yet been stored. The code therefore distinguishes initial activation from an ordinary bar transition. This is a programming technique for event handling, not a trading strategy; the document provides no market analysis, execution guidance, or performance evidence.
Key ideas
- MetaTrader EAs receive tick events but have no dedicated event for the start of each bar.
- Comparing the current bar's opening time with a stored prior value detects a new bar.
- A static variable retains the prior opening time across calls to the tick handler.
- The initial tick after attaching an EA needs separate handling because no prior bar time is available.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.