How do I change the value of a global variable from inside a function. I tried simply
But this does not work. How do I achieve this?
Thank you
int value1 = 100; double value2 = 1.5; // to set those 2 variables as global, by giving them an easy name and pass the value: // GlobalVariableSet(Symbol() + "VariableOne", (int)value1); GlobalVariableSet(Symbol() + "VariableTwo", (double)value2); // To get those global variables values, and pass them to another two new internal variables: int myVar; double anotherVar; myVar = (int)GlobalVariableGet(Symbol() + "VariableOne"); anotherVar = (double)GlobalVariableGet(Symbol() + "VariableTwo"); // then you can do the multiplication as your example myVar = myVar * 2; // and update the global var again GlobalVariableSet(Symbol() + "VariableOne", (int)myVar);
OP is asking about common variables (globally declared variables). He was not asking about terminal variables (Global Variables of the Terminal).
But this does not work. How do I achieve this?
It doesn't work because you named the function identically. Use the scope operator (Other Operations - Operations and Expressions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5).
::Globalvar1 = (::Globalvar1*2);
'GlobalVariableSet' - unexpected token, probably type is missing?
I tried
double val1 = 100 double val2 = 200 GlobalVariableSet(Symbol() + "VariableOne", (double)value1); GlobalVariableSet(Symbol() + "VariableTwo", (double)value2); Also tried double val1 = 100 double val2 = 200 GlobalVariableSet("VariableOne", (double)value1); GlobalVariableSet("VariableTwo", (double)value2);
OP is asking about common variables (globally declared variables). He was not asking about terminal variables (Global Variables of the Terminal).
It doesn't work because you named the function identically. Use the scope operator (Other Operations - Operations and Expressions - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5).
I tried this but the value of globalvar1 remains 1 and is not multiplied by 2.double Globalvar1 = 1; void change.globalvar1() { ::Globalvar1 = (::Globalvar1*2); } change.globalvar1();
'GlobalVariableSet' - unexpected token, probably type is missing?
I tried
The purpose of Symbol() + "name" is to not let a globar var from a symbol interfere with another symbol (consider you are using the same program/script/advisor on several symbols)
This way, the final global var name will be:
GPUAUDVariableOne = 100
EURUSDVariableOne = 123
And since they are Global for the Terminal, they will not interfere with each other, if your function runs on different symbols.
But if you want to share the variable between any symbol, on any open window inside the terminal, just remove the Symbol()+ from the command, and the final variable will be:
VariableOne = 100
By doing this way, every instance of your function, on any symbol, will share [and can change] the same global var simultaneously.. depending on the purpose, this can be the case on some situation.
Technical info:
Get:
Set:
PS: As @willian pointed, my answer is not exactly what you have asked about, since I replied about Terminal Global Vars. And you asked about Global Declared Vars ( my fault of attention while replying to you, sorry)
Anyway, it may help you [and others] sometime, if you need to use this kind of Global vars.
- www.mql5.com
double Globalvar1 = 1.0; // You need to declare the global variable's datatype. void changing_globalvar1() // Don't use "." in function names (or variables). Its used for class/structure members/methods. { PrintFormat( "Globalvar1 = %f (before)", Globalvar1 ); // Look at the Experts Log for result Globalvar1 = ( Globalvar1 * 2.0 ); PrintFormat( "Globalvar1 = %f (after)", Globalvar1 ); // Look at the Experts Log for result }
This does not work, the value of Globalavr1 is unchanged after function is called.
Show your full code then, because you must be doing something else that is causing the issue. showing us only part of it, will not help solve the problem if it is elsewhere.
And also show the log output. Don't just say "it does not work". That has no meaning if not explained or substantiated.
Show your full code then, because you must be doing something else that is causing the issue. showing us only part of it, will not help solve the problem if it is elsewhere.
And also show the log output. Don't just say "it does not work". That has no meaning if not explained or substantiated.
By does not work I meant that it does not change the value of the global variable.
Here is my code.
double VOL = 0.10; double buyorderVL = VOL; double buyorderTP = 1; double buyorderSL = 1; void VOLdoubling() { double balance = AccountInfoDouble(ACCOUNT_BALANCE); double x=1; while (balance > 11000) { x++; ::VOL = (::VL*x); balance = (balance - 11000); } } void OnTick() { double ASK=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits); if(IND[1]==1) { Print ("entering long"); LOTdoubling(); trade.Buy(buyorderVL,_Symbol,ASK,ASK - buyorderSL,ASK + buyorderTP); } }
- You only need the "::" if you have local variables of the same name.
- The global variable "VL" does not exist.
- You are updating the global variable "VOL", but you are using the variable "buyorderVL" when placing the trade.
- Updating "VOL" will not automatically update " buyorderVL" just because you initially set it to "VOL".
- Your function is called "VOLdoubling", but you are calling "LOTdoubling" in the OnTick() event handler.
- There is no need to normalise a given price quote. Ask, Bid and other price quote data are already normalised.
- In fact, you should avoid using NormalizeDouble() when calculating prices and instead properly adjust price calculations based on tick size instead.
- You should also be adjusting your volume lots, based on the contract specifications of volume minimum, maximum and step size.
PS! And please learn to format/style your code properly or use the "Styler" in MetaEditor.double VOL = 0.10; double buyorderVL = VOL; double buyorderTP = 1; double buyorderSL = 1; void VOLdoubling() { double balance = AccountInfoDouble(ACCOUNT_BALANCE); double x=1; while (balance > 11000) { x++; ::VOL = (::VL*x); // "VL" does not exist balance = (balance - 11000); } } void OnTick() { double ASK=NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits); if(IND[1]==1) { Print ("entering long"); LOTdoubling(); trade.Buy(buyorderVL, _Symbol, ASK, ASK - buyorderSL, ASK + buyorderTP); } }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
How do I change the value of a global variable from inside a function. I tried simply
But this does not work. How do I achieve this?
Thank you