Lifetime of Global Variables???

 

Hi, 

I cannot understand what's going on in MQL5 with regard to global variable values. According to the MQL5 blurb.........

A variable declared outside all functions is located into the global scope. Access to such variables can be done from anywhere in the program.These variables are located in the global pool of memory, so their lifetime coincides with the lifetime of the program.

 So when I run this simple test program below the 'Done' value is constantly reset to zero every time the OnInit() function is called. Eg every time the chart timeframe is changed the indicator 'forgets' the values for it's global variables ie 'Done' is reset to zero - this is hardly the lifetime of the program! 

 I am experienced in MT4 where the global variables always maintain the last value which were assigned to them. It seems in MT5 every time the OnInit() function is called ALL global variable values are reset which seems pretty ridiculous to me. 

 This seems to be a fundamental flaw in MQL5. 

Great if someone could enlighten me. 

 

#property copyright "Copyright 2011, MetaQuotes Software Corp."

#property link      "http://www.mql5.com"

#property version   "1.00"

#property indicator_chart_window


int Done;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

  Print ("Done=",Done);

  Done=1;

  return(0);

  }


//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+ 

Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

I don't want to disappoint you, but the behavior of the variables declared in the global scale is exactly the same in MT4. The program lifetime is from the even OnInit() (init() in MT4) till the even OnDeinit() (deinit() in MT4). When you switch timeframes, the program is deinitialized and then initialized again.

If you want variables to persist through the lifetime of your MetaTrader terminal instance, use GlobalVariableSet/GlobalVariableGet functions.

Documentation on MQL5: Global Variables of the Terminal / GlobalVariableSet
  • www.mql5.com
Global Variables of the Terminal / GlobalVariableSet - Documentation on MQL5
 
enivid:

I don't want to disappoint you, but the behavior of the variables declared in the global scale is exactly the same in MT4. The program lifetime is from the even OnInit() (init() in MT4) till the even OnDeinit() (deinit() in MT4). When you switch timeframes, the program is deinitialized and then initialized again.

If you want variables to persist through the lifetime of your MetaTrader terminal instance, use GlobalVariableSet/GlobalVariableGet functions.

Hi, many thanks for your reply. 

Am I correct in saying in MT4 when the chart timeframe was changed the deinit() fcn was not called but in MT5 the OnDeinit() fcn is always called as a matter of course?

It seems to be the case. 

Could you explain how the OnInit() fcn in MT5 effects global variables held within a library?

 I use an MT4 library which accesses a DLL which talks to a mqsql web database on the web. A function in the DLL returns an integer (when a connection is established on the server) . I declared this 'Connection flag' as a global variable in the library file. Once the connection was established I simply put an 'If (Connection_Flag==1) return;' line in so the MT4 calling program didn't keep logging onto the server every time the chart timeframe was changed. I didn't use the global variable table - I just used the global variable declared in the library and MT4 didn't reset it every time the chart timeframe was changed.

 It seems MT5 resets the connection flag every time the chart timeframe is changed.

 Happy to go with Global Variables held in the GVAR table but just wanted to understand why the two platforms seem to treat variables held in libraries slightly differently.

 Thanks very much for your assistance.

  

 
fxalgotrader:

Hi, many thanks for your reply. 

Am I correct in saying in MT4 when the chart timeframe was changed the deinit() fcn was not called but in MT5 the OnDeinit() fcn is always called as a matter of course?

It seems to be the case.

MT4 also calls deinit() on timeframe change. The whole script is unloaded from memory and is loaded again.

fxalgotrader:

Could you explain how the OnInit() fcn in MT5 effects global variables held within a library?

 I use an MT4 library which accesses a DLL which talks to a mqsql web database on the web. A function in the DLL returns an integer (when a connection is established on the server) . I declared this 'Connection flag' as a global variable in the library file. Once the connection was established I simply put an 'If (Connection_Flag==1) return;' line in so the MT4 calling program didn't keep logging onto the server every time the chart timeframe was changed. I didn't use the global variable table - I just used the global variable declared in the library and MT4 didn't reset it every time the chart timeframe was changed.

 It seems MT5 resets the connection flag every time the chart timeframe is changed.

 Happy to go with Global Variables held in the GVAR table but just wanted to understand why the two platforms seem to treat variables held in libraries slightly differently.

Sorry, but I know little about how MT4/5 treats variables declared inside DLL. But as far as I remember, you can only import functions from DLL and those function may only use the variables declared inside them or passed to them as arguments.
 
enivid:

MT4 also calls deinit() on timeframe change. The whole script is unloaded from memory and is loaded again.

Sorry, but I know little about how MT4/5 treats variables declared inside DLL. But as far as I remember, you can only import functions from DLL and those function may only use the variables declared inside them or passed to them as arguments.

Enivid,

 

Thanks for your reply. There's definitiely something different in the way MT5 accesses DLLs. In MT4 you could set global variables in the DLL and they would stay resident while the EA/indicator was running. It appears in MT5 every time the chart timeframe is refreshed the global variables held in the DLL are also refreshed/re-initialized too. This means programs which used global vars held in a DLL will have to be re-architected to use MT5 based global variables rather than DLL based global vars (declared on a global scope in C++). 

 

Effectively it means using a load of MT5 based GVARs. Is there a limit on the number of GVARs which can be used in MT5?

 

Cheers

Documentation on MQL5: Standard Constants, Enumerations and Structures / Chart Constants / Chart Timeframes
  • www.mql5.com
Standard Constants, Enumerations and Structures / Chart Constants / Chart Timeframes - Documentation on MQL5
 
There's nothing about such limit in the MQL help files.
Reason: