How to change global variable value from inside function

 

How do I change the value of a global variable from inside a function. I tried simply 

Globalvar1 = 1;

void changing.globalvar1()
{
Globalvar1 = (Globalvar1*2);
}

But this does not work. How do I achieve this?

Thank you

 
35117153:

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);




 
rrocchi:

OP is asking about common variables (globally declared variables). He was not asking about terminal variables (Global Variables of the Terminal).

35117153: 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?

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);
 
rrocchi:


'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);
What is the use of " Symbol() + "?
 
William Roeder:

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).


double Globalvar1 = 1;

void change.globalvar1()
{
::Globalvar1 = (::Globalvar1*2);
}

change.globalvar1();
I tried this but the value of globalvar1 remains 1 and is not multiplied by 2.
 
35117153:

'GlobalVariableSet' - unexpected token, probably type is missing?

I tried 

What is the use of " Symbol() + "?

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:

globalvariableget


Set:

globalvariableset



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. 

Documentation on MQL5: Global Variables of the Terminal / GlobalVariableGet
Documentation on MQL5: Global Variables of the Terminal / GlobalVariableGet
  • www.mql5.com
GlobalVariableGet - Global Variables of the Terminal - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
35117153: 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?
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
}
 
Fernando Carreiro:

This does not work, the value of Globalavr1 is unchanged after function is called.

 
35117153: 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.

 
Fernando Carreiro:

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);
  
  }
  }
 
35117153: By does not work I meant that it does not change the value of the global variable. Here is my code.
  1. You only need the "::" if you have local variables of the same name.
  2. The global variable "VL" does not exist.
  3. You are updating the global variable "VOL", but you are using the variable "buyorderVL" when placing the trade.
  4. Updating "VOL" will not automatically update " buyorderVL" just because you initially set it to "VOL".
  5. Your function is called "VOLdoubling", but you are calling "LOTdoubling" in the OnTick() event handler.
  6. There is no need to normalise a given price quote. Ask, Bid and other price quote data are already normalised.
  7. In fact, you should avoid using NormalizeDouble() when calculating prices and instead properly adjust price calculations based on tick size instead.
  8. You should also be adjusting your volume lots, based on the contract specifications of volume minimum, maximum and step size.
Obviously, that is not your proper code. The code you posted is incomplete and cannot be compiled and tested. If you are serious about getting help, then post your proper 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); // "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);
       }
    }
    PS! And please learn to format/style your code properly or use the "Styler" in MetaEditor.