How to use Global Variables? (F3)

To add comments, please log in or register
freak
9
freak  
How to do that?
ukt
473
ukt  
Hello

I'd suggest firstly familiarise self with Global variables in MQL4 quick reference in the Navigator (control-D) then at bottom click Dictionary tab.
Excuse me if already know all about Navigator...

Basically, MT let's you set one or more variables each with different "name" and give the variable a value.
The value is numeric type double and the variables are held in MT system memory - separate from all the .ex4's which means any .ex4 can reference a Global variable - as long as it 'knows' the "name" of the variable.

Simple way is to use just two functions:
Call GlobalVariableSet( string name, double value) to tell MT to reserve memory for the variable and assign it a value.
Call GlobalVariableGet( string name) to ask MT for it's numeric value.

Can get more involved if want to by using other Global variable functions too.

What's cool about it is that now you can reference this variable in other indicators/experts/scripts by using appropriate Global variable function calls. EG, GlobalVariableGet()

If you look at a post I made yesterday - I am using Globals because MT only allows an indicator to have 8 index buffers to draw lines on chart. (course, have issue with the setup and hope some knowledgeable coder will put me right!)

Anyway, so I run the same indicator twice on the chart.
The first time it runs, it asks MT if a Global variable with a certain "name" has been previously created and if not it tells MT to create the Global variable with a certain "name" and give it a value.

Now what happens, is when the same indicator is run for the second time, the same code of course runs but this time when checking to see if the Global variable is in the system it sees that it already exists.
This means that the code can 'see itself' as the second copy running on the chart and plot other lines or do other actions requiring more than 8 index buffers.

//-----

The principle can be applied to all sorts of things once you get the hang of it.

Say you only want an indicator to do something AFTER you have done some action in another indicator...
Firstly you decide on the "name" of the Global variable so that both .ex4's can be referencing same thing!
Now assuming both are running on a chart, the second one 'sees' that the "name" Global variable is either not there or does not have the right value - so it just Sleep()'s or return()'s UNTIL the Global variable is either created or say, set to the right value by the first indicator. At which time the second one can do it's actions...

Anyway, that's my take on Global Variables!

Always, in the end... make up your own mind of course, cause nothing to say I got it right!

Cheers

PS
Have taken liberty of uploading .gif file which show 'why' I need two indicators to plot 12 lines.
Presently am using two separate indicator files - aim is just the one indicator file using Global variables.
The original Vegas Currency Daily - Tunnel Method uses one indicator file but.. . you gotta mess with the inputs each time you run it.
I've modified so that the code does all the hard work so not need to set up any inputs 'cept if wanna change line styles.
Anyways, all a good laugh!
Files:
To add comments, please log in or register