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 */ }
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]; } }
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 |
That is true only if you do not use strict.
What is unclear to you?
- Variables have always their values (even if you use strict)
- "Default" value for datetime is usually 0 (even if you use strict)
MQL4
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 */ }
What is unclear to you?
- Variables have always their values (even if you use strict)
- "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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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 ?