A static variable stores its value till next run of EA, doesn't it ?

 

When the EA quited, It stores its value till next run (session) of the EA, or reseted by initial value at the beginning of next session ?

Thanks for help.

 

Best way to test it yourself by this EA:

extern int dummyParameter = 0;

int start() {
   static int count = 0;
   Print("count = " + count);
   count = count + 1;
   return(0);
}
Try to change chart's timeframe, symbol, change parameter of EA on the chart, try EA removing and replacing, terminal restaring, and watch on Experts tab how the static tick counter behaves.
 

Thank you, erzo

erzo:

Best way to test it yourself by this EA:

Try to change timeframe, symbol, parameter, try EA removing and replacing, terminal restaring, and watch on Experts tab how the static tick counter behaves.

By the way, what the hell dummyParameter is ? :-)

 
Finally, it doesn't save its value through sessions !
 
sonthanhthuytu:
Finally, it doesn't save its value through sessions !
No, if you need that write to file or use GlobalVariables
 
sonthanhthuytu:

By the way, what the hell dummyParameter is ? :-)

I tested by this EA what happens with static-s when the user changes a parameter of an EA on the chart, and I needed some parameter I can change during the test. It is only for that.
 
sonthanhthuytu:

When the EA quited, It stores its value till next run (session) of the EA, or reseted by initial value at the beginning of next session ?

Thanks for help.

sonthanhthuytu 2012.05.23 12:32
Finally, it doesn't save its value through sessions !

erzo:

Best way to test it yourself by this EA:

Try to change timeframe, symbol, parameter, try EA removing and replacing, terminal restaring, and watch on Experts tab how the static tick counter behaves.

If you want to retain value of variables - when changing extern input or changing TF or changing symbol - then you can save them in deinit.

Attach this EA to any chart, make sure EA is enabled.

When first attached, there will be no alert. Then press F7 to open it's property window, and close it, the alert will pop out, with values saved in deinit, do it over and over, change TF or even symbol.

Terminal restarting will reset the whole thing.

Have fun :)

//+ CODE REMOVED TO SAVE SOME SPACE, USE ATTACHMENT, + ALSO READ EXPLANATION BELOW BY ERZO

{EDIT : READ NEXT POST BELOW.

I HAVE TO CORRECT MYSELF : APPARENTLY, THE VALUES OF GLOBAL VARS WERE SAVED WHETHER WE OPEN EA PROP WINDOW, CHANGE THE TF, OR CHANGE THE SYMBOL :(

MANY THANKS TO ERZO :) )

Files:
 
onewithzachy:

If you want to retain value of variables - when changing extern input or changing TF or changing symbol - then you can save them in deinit.

Attach this EA to any chart, make sure EA is enabled.

When first attached, there will be no alert. Then press F7 to open it's property window, and close it, the alert will pop out, with values saved in deinit, do it over and over, change TF or even symbol.

Terminal restarting will reset the whole thing.

Have fun :)

I think the real problem with static-s that they DO keep their values when changing extern input or changing TF or changing symbol. Just try the original posting.

Can you write an EA that clears static-s at deinit() or init()?

Perhaps the simplest way not using static-s at all.

 
erzo:

I think the real problem with static-s that they DO keep their values when changing extern input or changing TF or changing symbol. Just try the original posting.

Can you write an EA that clears static-s at deinit() or init()?

Perhaps the simplest way not using static-s at all.

I understood what sonthanhthuytu wanted. Heck, even vars with global scope loss it's values when deinit gets executed for any reason. [EDIT : READ NEXT POST PLEASE]

Personally, I rarely use static. Static vars is just another global vars declared in funny ways.

Nope I can't write that. [EDIT : READ NEXT POST, PLEASE]

 
onewithzachy:

Heck, even vars with global scope loss it's values when deinit gets executed for any reason.

This made me curious, so I tested:

extern int DummyParameter = 0;

int count = 0;

int start() {
   Print("count = " + count);
   count = count + 1;
   return(0);
}

int deinit() {
   Print("deinit!!");
}

Deinit was called, and the var with global scope did not loss it's value.

But you can easily clear it at init().

What sonthanhthuytu wanted, can be achieved by GlobalVariables as Raptor said. (Or by writing to file/database/other custom storage.)

 
erzo:

This made me curious, so I tested:

Deinit was called, and the var with global scope did not loss it's value.

But you can easily clear it at init().

What sonthanhthuytu wanted, can be achieved by GlobalVariables as Raptor said. (Or by writing to file/database/other custom storage.)

You right, and static too.

Alright, Exactly what sonthanhthuytu wanted anyway ?

Reason: