Preserve variable state

 

Hi,


I am trying to write a Martingale type EA.


The problem I have is that I can't find the correct way to create a variable that gets a value at the beginning of the script (the order size) and then gets altered based on orders closing in a win or in a loss.


What I though it would be logical is to declare the variable in the init() section of the program, but if I do that, the script won't compile because it says that order_size is not initialized.

If I initialize and set the initial value for the variable in the start() section, what happens is that it is reset all the time and it places all the orders of the same size.


Any help would be appreciated! Thanks!


Regards,

Marius

 


Hi,

Use 'static' instead of 'extern' for global variables, they will be available in every function of your program just like the 'extern' ones.

exple: static double BalanceS = 0;

 

Hi Jacques,


Thank you!

I'll give this a try also, but in the meantime I figured out another solution. I initialize the variable outside of init() function, and also outside of start() function, and it works fine.


Thanks again!

Regards,

Marius

 
mariuspa:

Hi,


I am trying to write a Martingale type EA.


The problem I have is that I can't find the correct way to create a variable that gets a value at the beginning of the script (the order size) and then gets altered based on orders closing in a win or in a loss.


What I though it would be logical is to declare the variable in the init() section of the program, but if I do that, the script won't compile because it says that order_size is not initialized.

If I initialize and set the initial value for the variable in the start() section, what happens is that it is reset all the time and it places all the orders of the same size.


Any help would be appreciated! Thanks!


Regards,

Marius

GlobalVariableGet, GlobalVariableSet

with these functions, variable values are stored in a persistent way. The values are always available even when you exit from the terminal

Reason: