nandocabrera87: I need to execute a function when current bar is closed or current bar is open. How can I get that events?
Forum on trading, automated trading systems and testing trading strategies
what is the best code for identifying a new bar ??
Fernando Carreiro, 2021.06.03 14:45
This code will work on both MQL4 and MQL5:
// Check for New Bar static datetime dtBarCurrent = WRONG_VALUE; datetime dtBarPrevious = dtBarCurrent; dtBarCurrent = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE ); bool boolNewBarFlag = ( dtBarCurrent != dtBarPrevious ); if( boolNewBarFlag ) { Print( "... do something ..." ); }
I just create a blog post about this, with more detailed information, to serve as a reference for users having similar queries.
![Detecting the start of a new bar or candle, in an expert advisor. Detecting the start of a new bar or candle, in an expert advisor.](https://c.mql5.com/36/54/detecting-the-start-of-a-new-bar.jpg)
Detecting the start of a new bar or candle, in an expert advisor.
- www.mql5.com
For an Expert Advisor (EA) , when a new tick quote arrives, the MetaTrader terminal calls the default OnTick() event handling function. However, there is no default event handling function for when a
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Dear all, hope you are fine,
I need to execute a function when current bar is closed or current bar is open.
How can I get that events?
Thank you so much!