Download Detecting the start of a new bar or candle for MetaTrader 5

Detecting the start of a new bar or candle

Detecting the start of a new bar or candle

This is a powerful addition to your MetaTrader 5 toolkit designed to optimize market analysis and performance. This Expert Advisor serves as automated trading software. It is utilized to monitor financial markets and execute trades based on predefined algorithmic rules, enabling precise position management without the need for constant manual oversight.

MT5 expert Pack 📂

How to Setup and Use Detecting the start of a new bar or candle

1. Installation: Open the "File" menu, select "Open Data Folder," navigate to MQL/Experts, paste your file, and restart the terminal.

2. Activation: Drag the EA from the Navigator onto a chart, ensure "Allow live trading" is checked in the Common tab, and verify the AutoTrading button is green.

3. Optimization: Right-click your chart, choose "Expert List," click "Properties" to adjust inputs, and save your preferred setup as a set file for future use.

4. Maintenance: Regularly check the "Experts" tab in the terminal window to monitor trade logs and potential execution errors.

Frequently Asked Questions

Q: Why is my EA not opening trades? A: Check the "AutoTrading" button, ensure "Allow live trading" is enabled, and verify your broker allows automated trading on your account type.

Q: Can I run multiple EAs on one chart? A: No, each chart can only host one active EA; however, you can open multiple charts for different currency pairs to run several EAs.

Q: What does the "smiley face" icon mean? A: A smiley face in the top-right corner of the chart indicates the EA is successfully running; a frowny face means it is disabled.

Description & Settings


For an Expert Advisor (EA), when a new tick quote arrives, the MetaTrader terminal calls the default event handling function. However, there is no default event handling function for when a new bar (candle) starts or opens.
To detect this, one needs to monitor the opening time of the currently most recent bar. Once it changes, it signifies the start of a new bar, and one can react to it and handle the event. The following sample code, compatible with both MQL4 and MQL5, shows one such method on how this can be achieved:
In the above code, the keeps track of the bar's opening time, even when returning from the OnTick() function. Unlike a normal local variable, it memorizes its data content and does not release it when leaving the function. This is the key to detecting a change in the opening time of the current bar.
It's also important to note, that when the EA is first placed on a chart, the above code reacts as if the bar has just opened. This condition requires special handling if the situation needs to be handled differently.
Please note, that all my CodeBase publications' source code are now also available in "Public Projects" tab of MetaEditor under the name "FMIC".

You May Also Like

RobotFX does not own any of the code provided on this platform. All tools are freely available on the internet; we simply index and re-offer them for download. We are not responsible for any financial losses that may occur. Trading responsibilities rely solely on the traders downloading and using the displayed Expert Advisors, indicators, and scripts. These tools are provided for educational purposes only and may require modification or optimization to align with a trader's specific strategy or needs.
© ROBOTFX - Best MetaTrader Expert Advisors & Indicators