Understanding GlobalVariables()

 

Hi All,

I am not an advanced MQL4 programmer and I am struggling to understand the proper use of the GlobalVariables() function. Very simply I have an EA running on four currency pairs and there are 3 key variables (Profit, Stoploss, Ticket) that I need to monitor for the EA to run properly. I have set these at a 'global' level within the EA but of course if the PC is turned off or crashes the values are lost. I am looking for advice on how to properly use the GlobalVariables() function to retain these values and retrieve them again when connection is regained etc;

Is it as simple as this:

.
.
GlobalVariableSet("uniquename",Profit);
GlobalVariableSet("uniquename",Stoploss);
GlobalVariableSet("uniquename",Ticket);
.
.

and then using the GlobalVariableGet() function in the init() block to retrieve the data values when the EA is re-started?

Also do I only declare these functions once at the top of the programme? (in the same place as variables normally declared on a global scope?)

thanks for your help.

 
These "variables" aren't declared like a normal variable, think of them more like files . . . you might want to consider using a file to hold your information instead, a file can be backed up offline, GlobalVariables can't . . .
 
RaptorUK:
These "variables" aren't declared like a normal variable, think of them more like files . . . you might want to consider using a file to hold your information instead, a file can be backed up offline, GlobalVariables can't . . .


Thanks good suggestion, I think I might go for that option. Just for completeness, is that the right/wrong way to use the GlobalVariable() function above?

thanks

 
sd59:

Just for completeness, is that the right/wrong way to use the GlobalVariable() function above?

Read the Documentation, then ask questions if it isn't clear. Basically, Set when you need to set one and Get when you need to read one.
 
Remember that on a OS crash/terminal crash/power loss, the global variables will not be written/updated to disk.
 
sd59:

Is it as simple as this:

and then using the GlobalVariableGet() function in the init() block to retrieve the data values when the EA is re-started?

You need to use unique names for each global variable

GlobalVariableSet("uniquename1",Profit);
GlobalVariableSet("uniquename2",Stoploss);
GlobalVariableSet("uniquename3",Ticket);
 

The contents of GlobalVariables are available to all charts so if you want to share common values then that's what to use. If you're worried about losing the values because the application or PC shuts down then save them to a CSV file ready for use when the application is restarted.

 
geoffh254:

The contents of GlobalVariables are available to all charts so if you want to share common values then that's what to use. If you're worried about losing the values because the application or PC shuts down then save them to a CSV file ready for use when the application is restarted.


thanks for everybody's answer. Nice also to know that on a pc crash variables stored using GlobalVariableSet() are lost - they don't tell you this in the documentation. BTW I read the documentation and it's cr*p that's why I'm asking the questions.
Reason: