How to check whether M1 candle is from today?

 

My indicator plots lines in the M1 timeframe, which works fine.

However, I like to color all lines starting from today's M1 candles with a different color.

Is there a standard function or an easy way to check whether a candle I'm looping over is from today?

I'm new to the MQL4 language and trying to get hold of this.

Thanks a lot for your help! 

 
  datetime midnight = TimeCurrent() - (TimeCurrent()%(PERIOD_D1 * 60));
  if(Time[i]>=midnight)
       {
       //Bar i is from today
       }

.

 
GumRai:

.


Thanks a lot, it works just as intended!

Very  interesting to see how to work around things in MQ4L.

 
or
#define HR2400 86400
int   time(datetime when=0){
   return int(when == 0 ? TimeCurrent() : when) % HR2400;                     }
datetime   date(datetime when=0){
   return datetime( (when == 0 ? TimeCurrent() : when) - time(when) );        }
/////////////
datetime midnight=date();
if(Time[i]>=midnight){
 

Thanks a lot, I've been going over this.

Regarding the computed properties, it would be great if you could explain to me how the 

when = 0

part inside the brackets works.

I thought it would take the parameter from its call and name it 'when', and if there is no parameter given, it will be 0.

This assumption obviously is not correct.

 

Also, the compiler will throw an error complaining about a '=' operand missing in the first ternary statement.

I can't see where this one would be missing. I've copy and pasted your code, so it is exactly the same.

 

Thanks again, much appreciated! 


 
ZetaTrader: I thought it would take the parameter from its call and name it 'when', and if there is no parameter given, it will be 0.
This assumption obviously is not correct.
  1. Exactly correct, and when zero the ternary uses TimeCurrent.
  2. The code compiles just fine under strict.
 

Thank you for the reply.

 As I'm trying to understand the MQ4L language:

 Since in your code snippet there is no call to time(datetime) without a parameter, why do assign a standard value inside the brackets? Is this a common practice in MQ4L?

 

Also, I've copied your code in a new project to test and it still won't compile: '=' - operand expected. It's weird, I can't find anything wrong with it. 

 

ZetaTrader:

Since in your code snippet there is no call to time(datetime) without a parameter, why do assign a standard value inside the brackets? 

Also, I've copied your code in a new project to test and it still won't compile: '=' - operand expected. It's weird, I can't find anything wrong with it. 

  1. If you call date(dt) with a datetime dt, shouldn't the call be time(dt) so you get midnight of dt? If you call date() without a datetime, wouldn't you expect to get today's midnight?
  2. Post your code.
 
WHRoeder:
  1. If you call date(dt) with a datetime dt, shouldn't the call be time(dt) so you get midnight of dt? If you call date() without a datetime, wouldn't you expect to get today's midnight?
  2. Post your code.

Thank you for your patience! 

 I managed to get it to work, your code was totally fine.

 I set it up again by copying all the code into a new indicator template and it works just as intended, thank you!

 No idea why it didn't work in the other indicator window. Maybe because it was an older file that I modified. Also, it was a script, not an indicator file, as I originally intended it to be.

 

Thank you for the explanation regarding the variable's input parameter, that makes perfect sense! 

Reason: