Get Open or Close Bar event

 

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!

 
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.
  • 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
 
Fernando Carreiro #:

Thank you so much! It worked perfectly!

Excelent your post.