Detecting the start of a new bar or candle
Fernando Carreiro, 2022.04.24 00:46
Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
MT4: New candle - MQL4 programming forum #3 (2014)
MT5: Accessing variables - MQL4 programming forum #3 (2022)
I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011)
Man, I am having difficulty of this time variable. I need to learn it deeply.
Thank you guys.
Man, I am having difficulty of this time variable. I need to learn it deeply.
Read up on the datetime type; time is like this in many programming languages.
The datetime type is intended for storing the date and time as the number of seconds elapsed since January 01, 1970.
Because values of datetime are stored as a number, you can perform arithmetic on a datetime value in order to look forward or backward in time:
const datetime currentBarTime = Time[0]; const datetime nextBarTime = currentBarTime + PeriodSeconds(); const datetime prevBarTime = currentBarTime - PeriodSeconds();
Also, since datetime is based on number of seconds:
const int minute = 60; const int hour = 60 * minute; const int day = 24 * hour;
- docs.mql4.com
When you do this:
const int minute = 60; const int hour = 60 * minute; const int day = 24 * hour;
Is that in the 1970's time or it will be the current time?
Neither. Those are just the duration (in seconds) of that unit.
See also
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Is there a way to know that we are in the current candle or bar? Well I always do is if Open price is the same , we are in the current candle.
But is there another way?
Thanks,