Question About Global(scope) Variables of indicators

 

In EA when I make a Variable in the Global Scope it is Initialize only once at the beginning of the program. And it doesn't get reinitialize/its value change to the original value when Time Frames change. But when I try the same concept on an Indicator, I found that the variables in the global scope gets reinitialize.

//-- For EA

datetime prevTime = Time[0];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   Print("EA : prevTime = ",prevTime);
   return(INIT_SUCCEEDED);
  }

//-- For Indicator(same thing)

datetime prevTime = Time[0];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   Print("Indicator : prevTime = ",prevTime);
   return(INIT_SUCCEEDED);
  }

Is there a way to get the indicator works in the same way as the EA ?

Thanks in advance.

 
pipspider:

In EA when I make a Variable in the Global Scope it is Initialize only once at the beginning of the program. And it doesn't get reinitialize/its value change to the original value when Time Frames change. But when I try the same concept on an Indicator, I found that the variables in the global scope gets reinitialize.

Is there a way to get the indicator works in the same way as the EA ?

Thanks in advance.

Yep

//-- For EA

datetime prevTime;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   prevTime = Time[0];
   Print("EA : prevTime = ",prevTime);
   return(INIT_SUCCEEDED);
  }

//-- For Indicator(same thing)

datetime prevTime = Time[0];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   Print("Indicator : prevTime = ",prevTime);
   return(INIT_SUCCEEDED);
  }
 
Fernando Morales:

Yep

That will not prevent the indicator from re-initialising the variable when the timeframe is changed.

The OP could possibly use Terminal Global Variables, but would need to check if the variable exists before giving it a value and then delete it manually when the variable needs to be reset.

 
Keith Watford:

That will not prevent the indicator from re-initialising the variable when the timeframe is changed.

The OP could possibly use Terminal Global Variables, but would need to check if the variable exists before giving it a value and then delete it manually when the variable needs to be reset.

Hello Keith, Thanks for the reply.
Yes GlobalVariables could be of a help.

Is there anything mentioned in the documentation about this global(scope) variable re-initialization during a time frame change in indicators, because this doesn't happen on the EA ?, If there is I must have missed it.

Again Thanks both of you for your answers.

 
pipspider: Is there a way to get the indicator works in the same way as the EA ?
Nope.
Fernando Morales: Yep
Nope, indicators get reloaded.Indicators use to do the same thing, now they are always reloaded.
pipspider: Is there anything mentioned in the documentation about this global(scope) variable re-initialization during a time frame change in indicators
Silent, undocumented change.
 
whroeder1:
Nope.Nope, indicators get reloaded.Indicators use to do the same thing, now they are always reloaded.Silent, undocumented change.

Hello @whroeder1 Thanks for your answers.
Then it seems I have to use GlobalVariables(of the terminal) in these cases.
Anyway it is pretty weird that this happens only on indicator.

Thanks all for your time, much appreciated.. :)

 

Hello guys, its kind of mentioned in the documentation indirectly (I missed this part. LOL :D),

"In case the symbol or timeframe of a chart, to which the Expert Advisor is attached, changes, Expert Advisors are not loaded or unloaded. In this case client terminal subsequently calls OnDeinit() handlers on the old symbol/timeframe and OnInit() on the new symbol/timeframe (if they are such), values of global variables and static variables are not reset. All events, which have been received for the Expert Advisor before the initialization is completed (OnInit() function) are skipped."

This has mentioned under the Expert Advisor unloading, as a special feature relevant to EAs only, so this kind of indirectly says that it does not happen to indicators.

https://www.mql5.com/en/docs/runtime/running

Documentation on MQL5: MQL5 programs / Program Running
Documentation on MQL5: MQL5 programs / Program Running
  • www.mql5.com
Each script and each Expert Advisor runs in its own separate thread. All indicators calculated on one symbol, even if they are attached to different charts, work in the same thread. Thus, all indicators on one symbol share the resources of one thread. All other actions associated with a symbol, like processing of ticks and history...