Default value of a datetime variable.

 

Hi..

I'm trying to write an EA that checks for a condition at the start of each bar. I was able to find solution to it in this webpage.

http://mql4tradingautomation.com/execute-action-once-per-bar-mql4/

After running the code on a sample EA, I found that the code actually works. 

In the beginning, the author declared a datetime variable - LastActiontime and ...

In the OnTick() function, he checked if this variable is not equal to the current time. What I could not understand is this...

Initially he declared the LastActiontime variable and did not assign any value to it. So, at the beginning, it has no data... If that is the case then how is the author trying to see if the value in this variable (LastActionTime) is not equal to current time ?

It is confusing to me... Could anyone explain what is going on ? Does variables have any sort of default value ?

How to Execute an Action only Once per Bar with MQL4
How to Execute an Action only Once per Bar with MQL4
  • 2017.01.04
  • Luca Spinello
  • mql4tradingautomation.com
When you read about the tick and MQL4 you learned that the tick is a trigger for a portion of code. You also learned that generally there are more ticks happening in a candlestick, whether this is a short or long timeframe. In many cases you may want to execute some code only once in a bar or candlestick. In this article you will see how you...
 

MQL4

void OnTick()
{
//---
     static datetime LastActiontime=0;
     if(LastActiontime == Time[0])
            return;
     LastActiontime=Time[0];
     
     /* work */
}
MQL4/5
void OnTick()
{
//---
     static datetime LastActiontime=0;
     datetime BarTime[];
     if(CopyTime(_Symbol, 0, 0, 1, BarTime) != 1)
          return;
     if(LastActiontime == BarTime[0])
          return;
     LastActiontime=BarTime[0];

     /* work */
}
 
Sai Karthik:
 

Does variables have any sort of default value ?

Variables have always their values. "Default" value for datetime is usually 0 (that means midnight of the January 01, 1970). But in general in this case it is better to use local static variable (instead of global variable) and set it to zero.

void OnTick()
  {
   static datetime LastActiontime=0;
   //Comparing LastActionTime with the current starting time for the candle
   if(LastActiontime!=Time[0] && YOUR_CONDITION){
      //Code to execute once in the bar
      Print("This code is executed only once in the bar started ",Time[0]);
      
      LastActiontime=Time[0];
   }
  }
 
Petr Nosek: Variables have always their values. "Default" value for datetime is usually 0 (that means midnight of the January 01, 1970).

That is true only if you do not use strict.

Old MQL4 compiler New MQL4 compiler New MQL4 with #property strict
Implicit initialization of all the variables (both global and local ones) by zero Ditto Only global variables are initialized. In local variables, only strings are initialized implicitly
          Updated MQL4 - MQL4 Reference
 
whroeder1:

That is true only if you do not use strict.

What is unclear to you?

  1. Variables have always their values (even if you use strict)
  2. "Default" value for datetime is usually 0 (even if you use strict)
 
Konstantin Nikitin:

MQL4

MQL4/5

Alternatively, 

   static datetime last_action=0;
   datetime bar_time = (datetime)SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
   if(last_action!=bar_time)
   {
      last_action=bar_time;
      /* work */
   }
Petr Nosek:

What is unclear to you?

  1. Variables have always their values (even if you use strict)
  2. "Default" value for datetime is usually 0 (even if you use strict)

Agree with you that you should use local variables instead of globals. Using a global variable when a local could have been used is terrible form and devs who overuse globals are just flat out lazy and don't care about portability! I'm not sure exactly what you guys are arguing about, however, since you mentioned initializing your local static variable. 

Reason: