GlobalVariables are of double type. If you want to store a value of a different type, you must be able to typecast it into a double and then typecast it back to its original value when you want to access it.
They're stored on disk and are loaded into memory every time you start up MT4. They have a lifespan of 4 weeks since the last access.
GlobalVariableCheck | Checks the existence of a global variable with the specified name |
GlobalVariableSet | Sets the new value to a global variable |
GlobalVariableGet | Returns the value of a global variable |
That's the 3 main functions.
They're pretty self-explanatory based on their names and descriptions.
First, do a check for the existence of the GlobalVariable in your EA using GlobalVariableCheck (usually done in OnInit). If it doesn't exist, then create it using GlobalVariableSet.
When your EA needs to make a decision, use GlobalVariableGet to check the value. Likewise, if you need to change the value, use GlobalVariableSet.
Note: if you think the user might end up deleting the GlobalVariable, you should run GlobalVariableCheck before calling GlobalVariableGet or handle errors when calling GlobalVariableGet.

- docs.mql4.com
Ah! So should it be written like this as an example?
input bool Allow = true; int OnInit() { if(!GlobalVariableCheck("AllowTrade") { if (Allow) GlobalVariableSet("AllowTrade", 1); if (!Allow) GlobalVariableSet("AllowTrade", 1); } } void OnTick() { if (GlobalVariableGet("AllowTrade") == 1) { OrderSend(Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"Buy Order",0,0,0) } }
From this, each time I initialize the EA on a new chart with the input bool Allow, I change the GlobalVariable "AllowTrade" for all active charts right?
Austin John Villarico Salvador #:
From this, each time I initialize the EA on a new chart with the input bool Allow, I change the GlobalVariable "AllowTrade" for all active charts right?
Correct.
no need for the "Check", if you read the documentation, you would have noticed, " Sets a new value for a global variable. If the variable does not exist, the system creates a new global variable."
I would have it this way...
// Global Variables bool Allow = false, NotAllowed = false, _current_value=false; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { NotAllowed = {0;}; if(NotAllowed<GlobalVariableGet("AllowTrade_"+_Symbol,_current_value)) { OrderSend(Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"Buy Order",0,0,0) NotAllowed = _current_value); } } //+------------------------------------------------------------------+

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Does anyone has a sample of a code using GlobalVariables in the simplest form in MQL4?
I want to use it in my EA but I have not fully understand how to use it yet. I have read the https://docs.mql4.com/globals but I'm still confused, it's better if I get more examples.
Let's say I want to stop trades in all EA of all charts if one chart declares a bool GlobalVariable to be false. How should it be written?
Thank you in advance! I'm still new at this so please help.