Detecting New Bar Events in Expert Advisors
Info
The Detecting New Bar Events in Expert Advisors is a Expert Advisor for MetaTrader 4 that detecting new bar events is a common requirement for many traders. IntroductionThis article provides a solution to detect new bar events in the current timeframe and other timeframes.
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 New Bar Events in Expert Advisors
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
Detecting new bar events is a common requirement for many traders.
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: retrieve high impact events from forex factory. com and display them in your indicator or expert advisor - another powerful expert for MetaTrader 4 traders.
Detecting new bar events is a common requirement for many traders.Also recommended: detecting the start of a new bar or candle - similar expert with strong performance on MetaTrader 4.
Introduction
This article provides a solution to detect new bar events in the current timeframe and other timeframes. Detecting new bar events in the current timeframe is relatively simple, but detecting events in other timeframes can be more complex due to certain restrictions.
For example, MT4 does not support the OnBar event, but it is possible to put the upward times into an array and check the array times every tick. If the right time is reached, the new bar event can be executed. This means that if you run a backtest on the M5 timeframe, you can catch the M6, M7, and D1 events.
The reason why it is only possible to detect upward trends is related to how MetaTrader generates tick data. However, this information is not relevant to the solution provided in this article.
Explanation of the Code
The code provided in this article uses the following approach: in the init function, the time array is filled with the starter times. Then, in the start function, it checks if enough time has elapsed and executes the new bar event if necessary.
- The code is written in a straightforward manner.
- The solution is easy to implement and does not require extensive programming knowledge.
That is all you need to do to detect new bar events in your Expert Advisor.
Update 1.1
: The code has been updated to provide clearer explanations and improved readability.
You may also like: 3 Level ZZ Semafor Expert Advisor - excellent alternative for expert users on MetaTrader 4.
Source Code
#property copyright "robotfx"
#property link "https://robotfx.org"
#define max 9
int curindex;
datetime times[max];
int start;
int init () {
curindex = utils.periodtoperiodindex(period());
times[curindex] = time[0];
for(int i=curindex+1; i<max; i++)
.......
⚠ 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.