Globals Replacement

 

Global Variable Replacement

If you're anything like me, you'd like to be able to read/write between multiple symbols and indicators that are working together. MT4 has a global variable function, but it has a few limitations. (It can't save strings, your variables expire after a period of time, etc.)

I've created a replacement that is extremely easy to use. I don't know if this has been done before, but I tend to come up with my own solutions as opposed to searching for hours online...

Instructions

Place "globals.mqh" in your "MetaTrader 4\experts\include" directory. To use, simply add "#include " at the top of your EA/Script.

Functions:

globalWrite (name, value)

- Writes a global variable. Can be string, numbers, combination, etc. Name is what you'd like the variable to be called, and value is whatever data you want to store.

globalReadString(name)

Gets the data and returns it as a string.

globalReadDouble(name)

Gets the data and returns it as a double (works fine for ints as well)

globalExists(name)

Returns true if that global var exists and false if not.

globalDelete(name)

Deletes the global variable.

A note about use:

I set up a string named "prefix" that is unique to that individual EA. When using the global vars, I'll do something like globalWrite(prefix+"lots",1.0) so that the variables do not get mixed up between different EAs. Just a thought.

Questions and comments welcome.

Files:
globals.mqh  2 kb
Reason: