Global Variables

Back to topics list To post a new topic, please log in or register
avatar
14
jt27 2016.05.06 16:40
 
If I create a global variable in my EA and launch that EA in multiple windows, is that variable visible to all instances of the EA?  If so, how do I create a variable that is visible only to each individual instance of the EA?
avatar
1202
FMIC 2016.05.06 17:03 #
 
jt27:
If I create a global variable in my EA and launch that EA in multiple windows, is that variable visible to all instances of the EA?  If so, how do I create a variable that is visible only to each individual instance of the EA?

Yes, it is visible to all EA's on all Charts on all Symbols on all time-frames. If you want to have a filter, simply use prefixes for the names of the global variables.

For example, if you want your EA to differentiate between the same EA running on different symbols and time-frames, use that as part of the prefix for the names. Lets say your EA is called "ChannelBreakout" and you could give it a short name of "CBO".

So if your EA was running on a M5 EURUSD Chart, it could have the global variable for the StopLoss be "CBO.EURUSD.5.StopLoss" where the "EURUSD" was derived from _Symbol and the "5" was derived from _Period. The same EA running on a GBPUSD on a H1 Chart would have its global variable as "CBO.GBPUSD.60.StopLoss".

If for example, your EA was not time-frame dependent, then you could build a name using just the symbol "CBO.EURUSD.StopLoss" or "CBO.GBPUSD.StopLoss".

Here is an example from my own EA's:

Global Variables

avatar
14
jt27 2016.05.06 17:35 #
 
FMIC:

Yes, it is visible to all EA's on all Charts on all Symbols on all time-frames. If you want to have a filter, simply use prefixes for the names of the global variables.

For example, if you want your EA to differentiate between the same EA running on different symbols and time-frames, use that as part of the prefix for the names. Lets say your EA is called "ChannelBreakout" and you could give it a short name of "CBO".

So if your EA was running on a M5 EURUSD Chart, it could have the global variable for the StopLoss be "CBO.EURUSD.5.StopLoss" where the "EURUSD" was derived from _Symbol and the "5" was derived from _Period. The same EA running on a GBPUSD on a H1 Chart would have its global variable as "CBO.GBPUSD.60.StopLoss".

If for example, your EA was not time-frame dependent, then you could build a name using just the symbol "CBO.EURUSD.StopLoss" or "CBO.GBPUSD.StopLoss".

Here is an example from my own EA's:


Thank you, that sounds like a perfect solution.  Any chance you could clue me in to how to do that programatically?  Would that be done through some string functions when I am declaring my variables?

 

 

avatar
1202
FMIC 2016.05.06 18:02 #
 
jt27:

Thank you, that sounds like a perfect solution.  Any chance you could clue me in to how to do that programatically?  Would that be done through some string functions when I am declaring my variables?

Standard String Manipulations:

input string EACode = "CBO";
...
string nameGVStopLoss = StringConcatenate( EACode, ".", _Symbol, ".", _Period, ".StopLoss" );
...
GlobalVariableSet( nameGVStopLoss, 20.0 );
...
avatar
14
jt27 2016.05.06 18:25 #
 
FMIC:

Standard String Manipulations:

Thanks again, you are a gentleman and a scholar.
avatar
12985
WHRoeder 2016.05.06 21:06 #
 
jt27: If I create a global variable in my EA and launch that EA in multiple windows, is that variable visible to all instances of the EA?  If so, how do I create a variable that is visible only to each individual instance of the EA?
  1. Terminal variables (Global Variables of the Terminal - MQL4 Reference) are visible to all scripts and the Human (F3.)
  2. Global Variables - Variables - Language Basics - MQL4 Reference declared (common) variables are visible to all procedures
  3. If you're not trying to communicate with other instances, why would you make a terminal variable?






avatar
1202
FMIC 2016.05.06 21:59 #
 
WHRoeder:
3. If you're not trying to communicate with other instances, why would you make a terminal variable?
To have persistent storage of a variable that will be "recoverable" on the next time the EA is used on that particular chart, such as after a failure, especially when "flushed" regularly.
avatar
12985
WHRoeder 2016.05.07 16:06 #
 
FMIC: To have persistent storage of a variable that will be "recoverable"
Except that the OP didn't state that reason (poor man's persistent storage.) 

avatar
1202
FMIC 2016.05.07 16:55 #
 
WHRoeder:
FMIC: To have persistent storage of a variable that will be "recoverable"
Except that the OP didn't state that reason (poor man's persistent storage.) 

There is no reason to "belittle" Global Terminal Variables. Yes, there are other alternatives such External Files, SQL Databases, Windows Registry, etc. but each have their advantages and disadvantages. So, there is no reason to call it a "poor man's persistent storage".

I personally use them and have never had them fail me, even after crashes, hard drive failures, etc. because I also implement other versioning and backup tools for the entire MetaTrader folder structure.


avatar
12985
WHRoeder 2016.05.07 19:24 #
 
If you don't call flush directly after every modification (or group of,) the code is broken. Flush didn't exist before build 600 (2/14)
avatar
1202
FMIC 2016.05.07 20:46 #
 
WHRoeder:
If you don't call flush directly after every modification (or group of,) the code is broken. Flush didn't exist before build 600 (2/14)

If you read my post again you will see that I wrote "especially when flushed regularly".

Besides, we are no long in the "old ages" of the "pre-600's". We are already in the "900's" so it makes no sense to condemn something which has been improved ages ago. That is like saying - don't use mobile phones because the first ones were to big to "fit my pocket".

By the way, writing to external files also suffers from the same problem if you don't flush it regularly either. So there goes your argument!

Back to topics list  

To add comments, please log in or register