Discussion of article "MQL5 Cookbook: Analyzing Position Properties in the MetaTrader 5 Strategy Tester"
Good evening, please help me, in the function CheckNewBar static variable new_bar is zeroed in the first line, and then the logic of the function is based on whether it is equal to zero or not. Could you please tell me where I don't understand?!?!!?!
bool CheckNewBar() { //--- Variable for the current bar opening time static datetime new_bar=NULL; //--- Array for obtaining the current bar opening time static datetime time_last_bar[1]={0}; //--- Get the opening time of the current bar // If there was an error while receiving, we will report it if(CopyTime(_Symbol,Period(),0,1,time_last_bar)==-1) { Print(__FUNCTION__,": Error copying bar opening time: "+IntegerToString(GetLastError())+""); } //--- If this is the first function call if(new_bar==NULL) { // Set the time new_bar=time_last_bar[0]; Print(__FUNCTION__,": Initialisation ["+_Symbol+"][TF: "+TimeframeToString(Period())+"][" +TimeToString(time_last_bar[0],TIME_DATE|TIME_MINUTES|TIME_SECONDS)+"]"); return(false); // Return false and exit } //--- If the time is different if(new_bar!=time_last_bar[0]) { new_bar=time_last_bar[0]; // Set the time and exit return(true); // Remember the time and return true } //--- We got to this point - so the bar is not new, let's return false return(false); }
Good evening, please help me, in the function CheckNewBar static variable new_bar is zeroed in the first line, and then the logic of the function is based on whether it is equal to zero or not. Could you please tell me where I don't understand?!?!!?!
The help says:
Local variables declared with the keyword static retain their values for the entire time of the function's existence. At each next call of the function such local variables contain the values they had at the previous call.
That is, at a new call of the CheckNewBar() function, the new_bar variable will retain the value received at the previous call of the function, but in the first line it will be assigned a new value NULL...... and then it is not clear to me why all this and how it works. Please dispel my confusion, most likely I am somewhere stupid, but WHERE ?????
Good evening, please help me, in the function CheckNewBar static variable new_bar is zeroed in the first line, and then the logic of the function is based on whether it is equal to zero or not. Could you please tell me where I don't understand?!?!!?!
If I understood the Help correctly, in the linestatic datetimenew_bar=NULL; " If the initial values are not specified, the variables of the static memory class take zero initial values". So it shouldn't have been initialised with null at all and then the logic would be flawless. Or not ????
If I understood the Help correctly, the linestatic datetimenew_bar=NULL; " If initial values are not specified, static memory class variables take zero initial values." So it shouldn't have been initialised with null at all and then the logic would be flawless. Or not ???
Variables MUST always be initialised. It is an unwritten law. Those who don't fulfil it sooner or later catch very hard-to-find errors :)
How a static variable works:
//+------------------------------------------------------------------+ //|Test EA.mq5 | //|Copyright © 2018, Vladimir Karputov | //| http://wmua.ru/slesar/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2018, Vladimir Karputov" #property link "http://wmua.ru/slesar/" #property version "1.000" //+------------------------------------------------------------------+ //| Expert initialisation function| //+------------------------------------------------------------------+ int OnInit() { //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialisation function| //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function| //+------------------------------------------------------------------+ void OnTick() { //--- we work only at the time of the birth of new bar static datetime PrevBars=0; Print("PrevBars ",PrevBars); datetime time_0=iTime(Symbol(),Period(),0); if(time_0==PrevBars) return; PrevBars=time_0; Print("New bar. PrevBars ",PrevBars); } //+------------------------------------------------------------------+
Print to the Experts tab. The very first input to OnTick() is the initialisation of a static variable, a new bar.
2018.01.08 00:00:00 PrevBars 1970.01.01 00:00:00 2018.01.08 00:00:00 New bar. PrevBars 2018.01.08 00:00:00 2018.01.08 00:00:30 PrevBars 2018.01.08 00:00:00 2018.01.08 00:00:59 PrevBars 2018.01.08 00:00:00 2018.01.08 00:01:00 PrevBars 2018.01.08 00:00:00 2018.01.08 00:01:00 New bar. PrevBars 2018.01.08 00:01:00 2018.01.08 00:01:30 PrevBars 2018.01.08 00:01:00 2018.01.08 00:02:00 PrevBars 2018.01.08 00:01:00
Variables MUST always be initialised. This is an unwritten law. Those who do not fulfil it sooner or later catch very hard-to-find errors :)
How a static variable works:
Printout to the Experts tab. The very first input to OnTick() is the initialisation of a static variable, a new bar.
OK, got it, thanks for the concise and very lucid answer.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
New article MQL5 Cookbook: Analyzing Position Properties in the MetaTrader 5 Strategy Tester is published:
We will present a modified version of the Expert Advisor from the previous article "MQL5 Cookbook: Position Properties on the Custom Info Panel". Some of the issues we will address include getting data from bars, checking for new bar events on the current symbol, including a trade class of the Standard Library to a file, creating a function to search for trading signals and a function for executing trading operations, as well as determining trade events in the OnTrade() function.
Author: Anatoli Kazharski