Global Variables Persistence

 

I just notice that global variables declared in a EA keep their value when you stop the EA, and are restored even if you close terminal and reboot your machine.
So these datas are certainly saved on the hard disk but where and what is the format ?

And... while I'm here,... ;) are these datas saved at each tick or when I close or stop the EA ?

thanks and have an excellent day/night according your country

 

Common (globally declared) variables are not restored, they are reloaded on EA start. Not on deinit/init cycles.

Terminal (Global) Variables are not the EA's, they are visible to any script, indicator or EA. If you want to be certain, write a file.

They are saved when set, (exactly when isn't documented so I guess at end of tick, but it's no longer at terminal close which was unsafe.)

Why do you care where they are stored. Just open Tools -> Global Variables or F3

 

Thanks WHRoeder,

I am speaking about Common : (globally declared) variables

But yes they are restored... If you are using by exemple, a double variable which is equal at one moment at 128.24 and you stop the EA, close MT4, reboot your VPS,... when you will restart your EA, the value of this variable will always be equal to 128.24. So it shows that MT4 stores (or saves) theses data somewhere else when EA stop.

But where, how, and when... ?

 
Trader55:

Thanks WHRoeder,

I am speaking about Common : (globally declared) variables

But yes they are restored... If you are using by exemple, a double variable which is equal at one moment at 128.24 and you stop the EA, close MT4, reboot your VPS,... when you will restart your EA, the value of this variable will always be equal to 128.24. So it shows that MT4 stores (or saves) theses data somewhere else when EA stop.

But where, how, and when... ?


Look in "data folder\MQL4\Files\" for new created of modified files after you stop your EA. But better search in source code where it accesses any file in write mode.
 

I understand that variables that are declared without assigning them with an initial value are now no longer automatically set to zero.

Is it possible that when you restart that the EA manages to use the same bytes of memory as it did last time and so the value assigned to the variable is the same as before?

If so, I wouldn't rely on it. Best to give the variable a zero value when declaring it.

 
Trader55:

Thanks WHRoeder,

I am speaking about Common : (globally declared) variables

But yes they are restored... If you are using by exemple, a double variable which is equal at one moment at 128.24 and you stop the EA, close MT4, reboot your VPS,... when you will restart your EA, the value of this variable will always be equal to 128.24. So it shows that MT4 stores (or saves) theses data somewhere else when EA stop.

But where, how, and when... ?

The globals variables (I am not talking about Globals Variables of the Terminal) are NOT preserved when you remove an EA from a chart, you close MT4 or reboot your computer. If after close/reboot...your variables still have the same values it's because your EA save/restore them in one way or an other.
 
Trader55:

I just notice that global variables declared in a EA keep their value when you stop the EA, and are restored even if you close terminal and reboot your machine.
So these datas are certainly saved on the hard disk but where and what is the format ?

And... while I'm here,... ;) are these datas saved at each tick or when I close or stop the EA ?

thanks and have an excellent day/night according your country


This pages may be of some help to you:

https://book.mql4.com/functions/globals

https://book.mql4.com/variables/globals

https://docs.mql4.com/globals

 
Trader55:

I just notice that global variables declared in a EA keep their value when you stop the EA, and are restored even if you close terminal and reboot your machine.
So these datas are certainly saved on the hard disk but where and what is the format ?

And... while I'm here,... ;) are these datas saved at each tick or when I close or stop the EA ?

thanks and have an excellent day/night according your country


persistent global variables are typically stored in \MetaTrader 4\profiles\gvariables.dat

I don't know the specific format for the file, but I believe that is what you are looking for.

 

@CodeMonkey : I think you confuse Common (globally declared) variables and Terminal (Global) Variables .

For my case my question is realted to Common (globally declared) variables

@angevoyageur : Same, my question is relative to Common (globally declared) variables, NOT terminal variables

@GumRai : Not possible that MT4 manage that in memory because it retrieve value event if you stop your PC.

@Evillive : Could you be more precise with the path of the folder :data folder\MQL4\Files\" ??

 
Trader55:

@Evillive : Could you be more precise with the path of the folder :data folder\MQL4\Files\" ??

Click on File > Open data folder in your terminal and look at it's path, it's dependent of OS and trader's account, and even of starting terminal mode (portable or not) so nobody knows your path.
Reason: