Skip to content
All library documents

Detecting New Bars in MetaTrader Expert Advisors

Article MQL5 code base

Summary

This note explains how an Expert Advisor can detect the opening of a new candle in MetaTrader when a tick arrives. Since the platform does not provide a dedicated new-bar event handler, the method stores the opening time of the current bar and compares it with the value seen on the previous tick. A changed timestamp indicates a new bar, allowing the EA to run bar-start logic.

The example is described as compatible with MQL4 and MQL5. It also distinguishes the first tick after the EA is attached from later bar transitions: initialization can make the current bar appear new even when it is already underway. That first-run case needs separate handling if the strategy must behave differently at startup. The document provides an implementation pattern but no performance evidence; its usefulness is operational, and it does not address missed ticks, data availability, or broader execution behavior.

Key ideas

  • A new bar can be detected by comparing the current bar’s opening time across ticks.
  • The timestamp comparison can be used in both MQL4 and MQL5 Expert Advisors.
  • The first tick after attachment may trigger the same condition as a normal bar opening.
  • Startup behavior should be handled separately when strategies require it.

Tags

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