MT4. Is there a way to know we are in the current candle?

 

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,

 
toksis: 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?

Code Base

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.
 
toksis: I always do is if Open price is the same 

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)

 
Thank you guys.

Man, I am having difficulty of this time variable. I need to learn it deeply.
 
toksis #:
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;
Datetime Type - Integer Types - Data Types - Language Basics - MQL4 Reference
Datetime Type - Integer Types - Data Types - Language Basics - MQL4 Reference
  • docs.mql4.com
Datetime Type - Integer Types - Data Types - Language Basics - MQL4 Reference
 
Alexander Martinez #:


Read up on the datetime type; time is like this in many programming languages.

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:

Also, since datetime is based on number of seconds:

thank you. This will give me a start up idea.
 

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?

 
toksis #: 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

  1. Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
  2. Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08)
  3. date/time (2017)
  4. Find bar of the same time one day ago - MQL4 programming forum (2017)
Reason: