Will a static variable lose its value when EA restart from disconecting?

 

hi,

Will a static variable lose its value when EA restart from disconecting?

Does any one know ?

 

My ea uses some stactic variables to store their former values.

but i am not sure whether their value will lose or not when EA restart from disconecting.

 

Anyone knows?

thanks~ 

 
"luenbo:

hi,

Will a static variable lose its value when EA restart from disconecting?

Does any one know ?"

 

If the EA executes the "OnInit()" function again, the static variable will lose its former values. 

 
JMRodMartins:

 Since  EA executes the "OnInit()" function again when  changing pair or timeframe , Is it meams that  changing pair or timeframe will make the static variable lose its former value?

And what about disconecting? Will it  executes the "OnInit()" function again?

I am confused.. 

 
luenbo:

 Since  EA executes the "OnInit()" function again when  changing pair or timeframe , Is it meams that  changing pair or timeframe will make the static variable lose its former value?

And what about disconecting? Will it  executes the "OnInit()" function again?

I am confused.. 

All variables lose their values or are re-initialized everytime the EA starts. This includes chaning timeframes and re-compiling.  If you are just disconnected but the terminal is still running, the EA is simply waiting for the next tick, nothing is lose, no processing is happening.

To really preserve values, use GlobalVariables, https://www.mql5.com/en/docs/globals 

Documentation on MQL5: Global Variables of the Terminal
Documentation on MQL5: Global Variables of the Terminal
  • www.mql5.com
Global Variables of the Terminal - Documentation on MQL5
 
fxyekim:

All variables lose their values or are re-initialized everytime the EA starts. This includes chaning timeframes and re-compiling.  If you are just disconnected but the terminal is still running, the EA is simply waiting for the next tick, nothing is lose, no processing is happening.

To really preserve values, use GlobalVariables, https://www.mql5.com/en/docs/globals 

Thanks!
 

The answer is NO.

Actually, I already answer this on number 3 in here https://www.mql5.com/en/forum/9701#comment_395157, but not complete.

In an Expert Advisor (but not in Custom Indicator or Script), a globally declared variable and static local variable will NOT lose its value when changing symbol, time frame, opening EA property (press F7), and NOT lose its value when disconnected. Furthermore, as long as there is incoming tick, the calculation inside OnTick will keep running if we change symbol, timeframe or opening EA property, but not when disconnected. You may want to read that again.

The code below is an EA to demonstrate this. Attach this EA on a chart, and read the print out on Expert tab, then change symbol, time frame, and/or open EA property (press F7), change it's input or not.

When we open EA property, changing input (or not), or changing symbol or time frame :

1. Notice that the calculation is still running and continue running from the last calculation.

2. Notice to the value of static variable in OnInit and OnTick also the value of globally declared variable does NOT change and therefore does NOT reset to zero.

3. The only way to change the value of globally declared variable is to change it inside OnInit or OnDeinit, and for statically declared local variable we need to use some sort of "if" condition. All of them, only if there is changing condition in UninitializeReason, but I'm not showing how to do that in the EA below. 

4. Since calculation is still keep running when we change symbol or time frame, then it may good idea to use ChartSetSymbolPeriod to set symbol and period of a chart and therefore the symbol and time frame can not be change. However this function is asynchronous, therefore we have to check and re-check if the symbol and time frame is the one we want, and since broker can change the symbol name, we have to use some Market Info and String Functions, to check for correct symbol name.

Try the EA, and ask any question after you study its codes (you must have a tick to run the EA), and have fun.

//+------------------------------------------------------------------+
//|                                                   Vars Value.mq5 |
//|                                      Copyright phi.nuts Dec 2012 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright phi.nuts Dec 2012"
#property link      ""
#property version   "1.00"
#property  description "Shonwing if there are value change in an EA"

//--- input parameters
input string   Some_Input="Some Input";

int    Global_Integer = 0;
double Global_Double = 0.0;
bool   Global_Bool = false;
string Global_String = "";

string Is_Input_Change = "", Sym = "";
ENUM_TIMEFRAMES period = 0; 

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Print ("");
   static int Count_On_Init;

   Count_On_Init ++;
   Print("OnInit : This is OnInit number static ",Count_On_Init);
   
   if (Is_Input_Change == "")
     {
     Is_Input_Change = Some_Input;
     Print ("OnInit : First execution OnInit");
     }
     else
     {
     if (Is_Input_Change != Some_Input)
        {
        Is_Input_Change = Some_Input;
        Print ("OnInit : Input has change");
        }
     }
     
   if (Sym == "")
      {
      Sym = _Symbol;
      Print ("OnInit : First running symbol ",Sym);
      }
      else
      {
      if (Sym != _Symbol)
         {
         Print ("OnInit : Symbol has change. Last symbol is ",Sym," now is ",_Symbol);
         Sym = _Symbol;
         }
      }
   
   if (period == 0)
      {
      period = _Period;
      Print ("OnInit : First running period ",period);
      }
      else
      {
      if (period != _Period)
         {
         Print ("OnInit : Period has change. Last period is ",period," now is ",_Period);
         period = _Period;
         }
      }  
     
   Print ("");   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  Print ("");
  Print ("OnDeinit executed. Thank you :)");
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  static int    my_local_int    = 0;
  static double my_local_double = 0.0;
  static string my_local_string = "";
  static bool   my_local_bool   = false;

    my_local_int   ++;
    Global_Integer ++;
    
    my_local_double += 2;
    Global_Double   += 2;
    
    if (my_local_bool == false)
      {
      my_local_string = "local ON";
      my_local_bool   = true;
      }
      else
      {
      my_local_string = "local OFF";
      my_local_bool   = false;
      }
    
    if (Global_Bool == false)
      {
      Global_String = "global YES";
      Global_Bool   = true;
      }
      else
      {
      Global_String = "global NO";
      Global_Bool   = false;
      }
        
    Print("OnTick : Static int : ",my_local_int," | double : ",my_local_double," | bool : ",my_local_bool," | string : ",my_local_string );
    Print("OnTick : Global int : ",Global_Integer," | double : ",Global_Double," | bool : ",Global_Bool," | string : ",Global_String );
    Print("OnTick : Symbol ",_Symbol," TimeFrame ",_Period);   
    
    PlaySound("wait.wav");
  return;
  }
//+------------------------------------------------------------------+
MQL5: Get the amount of deals since the postions was (re)opened
MQL5: Get the amount of deals since the postions was (re)opened
  • www.mql5.com
So for example if I do the following trades on any currency pair:.
 

hi, phi.nuts 

Thank you for your detailed answer!  I am totally clear of the question now,and i will try the code to testify it later~ 

Thanks again!

 
i get implicit enum conversion error on line 64 and 72.
Reason: