detecting the start of a new bar or candle

detecting the start of a new bar or candle
Download ALL MT4 experts (885)
YouTube Video Thumbnail



Similar MetaTrader Tools

detecting the start of a new bar or candle

Info

The detecting the start of a new bar or candle is a Expert Advisor for MetaTrader 4 that 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.

Usage

This tool is typically used for automated trading on major forex pairs and gold.

Platform

This Expert Advisor works exclusively on MetaTrader 4 (both build 600+ and newer versions).

Setup

Place the downloaded file in MQL4/Experts folder via File ? Open Data Folder in MetaTrader 4.


How to Install 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.

What this tool does

for an expert advisor (ea), when a new tick quote arrives, the metatrader terminal calls the default event handling function.

Typical Use Case

This Expert Advisor excels in automated trading and technical analysis on MetaTrader 4.

Compatible Platform & Setup

This Expert Advisor works on MetaTrader 4. Place the file in the MQL4/Experts folder and restart the terminal.

Description & Settings

Related: starter trend-following expert advisor - another powerful expert for MetaTrader 4 traders.


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.

Also recommended: startermod: a comprehensive trading tool - similar expert with strong performance on MetaTrader 4.

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: Detecting New Bar Events in Expert Advisors - excellent alternative for expert users on MetaTrader 4.

Source Code

яю#property copyright "Copyright \x00A9 2022, Fernando M. I. Carreiro, All rights reserved"
#property link "https://robotfx.org"
#property version   "1.001"
#property strict
void OnTick()
{
static datetime dtBarCurrent  = WRONG_VALUE;
datetime dtBarPrevious = dtBarCurrent;
dtBarCurrent  = iTime( _Symbol, _Period, 0 );
bool     bNewBarEvent  = ( dtBarCurrent != dtBarPrevious );

.......

Leave your opinion, ask a question, share some knowledge

Limitations & Risk Warning

  • This tool is provided for educational and testing purposes only.
  • Past performance does not guarantee future results.
  • Trading involves substantial risk of loss. Use on a demo account first.
  • Results may vary depending on market conditions, broker, and settings.
  • We recommend thorough backtesting and forward testing before using with real funds.
© ROBOTFX Free educational tools by RobotFX. Use entirely at your own risk; we are not liable for any financial losses incurred.