Skip to content
All library documents

Detecting New Bars in MetaTrader by Tracking Bar Opening Time

Article MQL5 code base

Summary

This tutorial explains how an Expert Advisor can detect the start of a new candle in MetaTrader, where tick events are available but there is no separate default event for a new bar. Its method stores the opening time of the current bar and compares it with the value observed on the previous tick. A changed timestamp signals that the terminal has begun a new bar, allowing the program to run bar-specific logic.

The example is described as compatible with MQL4 and MQL5 and uses persistent function-level state to retain the previous timestamp between tick calls. The article also calls out an initialization caveat: when an EA is first attached, the first observed bar may already be in progress, yet the timestamp change check will still trigger. Users may need to handle that first event separately from ordinary bar openings. The tutorial explains the detection pattern but gives no performance measurements or broader trading strategy.

Key ideas

  • MetaTrader invokes an Expert Advisor’s tick handler when a new quote arrives, not specifically when a new candle opens.
  • Comparing the current bar’s opening time with its previously stored value detects a newly started bar.
  • Persistent function-level state retains the previous opening time across handler calls.
  • The first tick after attaching an Expert Advisor can trigger the detection logic mid-bar and may need separate handling.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.