Experts: GlobalVariable

 

GlobalVariable:

This advisor demonstrates an example of global variables usage. The task of the advisor: store the calculation result in a global variable and get the global variable value on new initialization.

Author: Karputov Vladimir

 
Automated-Trading:

GlobalVariable:

Author: Karputov Vladimir

got a global variable, saved the global variable, so what? I'm sorry, dear, but I don't understand anything. Why do you need these variables at all?
 
spbkit:
get a global variable, save the global variable, so what? I'm sorry, dear, but I don't understand anything. Why do you even need these variables?

I apologise. This example was created in response to a request of one of the forum members: we needed an example of working with global variables of the terminal. Global variables of the terminal save their values after reloading the terminal and this example shows how to read and write a global variable.

 

Thanks, useful code! But it didn't work for MT4... Can you please advise me how to better adapt this sample code for MT4?

 
Aleksandr Glebov:

Thanks, useful code! But it didn't work for MT4... Would you mind giving advice on how to better adapt this sample code for MT4?

It is enough to change the file extension from *.mq5 to *.mq4.
 
Not a clear example... Never got a chance to apply it.... And how to save two variables (two price levels) is not clear...?
 
Aleksandr Glebov:
Not a clear example... Never got a chance to apply it.... And how to save two variables (two price levels) is not clear...?

Show me what didn't work, please.

Aleksandr Glebov:
Not clear example... I never managed to apply it.... And how to save two variables (two price levels) is not clear...?

And in what place of the programme do you want to save values: in OnInit(), in OnTick() or in OnDeinit?

 
Karputov Vladimir:

Show me what didn't work, please.

And in what place of the programme do you want to save values: in OnInit(), in OnTick() or in OnDeinit?

I save values in OnTick() , and tried to get them with OnInit(), but it didn't work. It worked only in this way: I saved in OnTick() - GlobalVariableSet("OER", OER ); and here, in OnTick(), the very first condition - if orders are more than 0, then OER = GlobalVariableGet("OER"). The OER variable, now, on reloading, is not reset to zero. But as soon as orders are opened during normal operation, the required variable takes the conditions of the previous global variable.... Is it necessary to try to get the values from OnInit()...?
 
Aleksandr Glebov:
I save values in OnTick(), and tried to get them with OnInit(), but it didn't work. It worked only in this way: I saved in OnTick() - GlobalVariableSet("OER", OER ); and here, in OnTick(), the very first condition - if orders are more than 0, then OER = GlobalVariableGet("OER"). Now the OER variable is not reset on reloading. Do you think this is the right way to write the code?
What is the purpose of saving prices in global variables? What do you want to implement? What is the algorithm of action?
 
Karputov Vladimir:
What is the purpose of saving prices in global variables? What do you want to implement? What is the algorithm of action?
When reloading two variables are reset and because of this when loading the owl does not work correctly.... I need to learn how to save these variables for such a case (if there are orders when loading, use data from glob.).
 
Aleksandr Glebov:
When reloading, two variables are reset to zero and because of this the EA does not work correctly when loading. I need to learn how to save these variables for such a case (if there are orders when loading, use data from glob.).

In OnDeinit() write your condition - whether or not you need to overwrite your global variables.

In general, I ask again: "What is the algorithm of action?". At the moment it's a heap-mala. Understand that unless you understand what you need, you will not be able to implement it. Draw a flowchart - it will help.