Global variables issue mql5

 
Hey guys, I coded something and had to use global variables because when I switched timeframes the program re initialized and all as it does and I needed some variables to not lose their values when reinitialized. I am not talking about global variables in termes of scope but global variables like using GlobalVariableSet() function. It helped me overcome that problem of storing values of variables when Indicator is reinitialized but what I am experiencing is that when I have multiple charts with the indicator on it, when I change a value, it changes value of that variable on all the opened charts which is logical. But I want to know if there’s a way to stop that? Thanks in advance 
 
Bryan Djoufack Nguessong: Hey guys, I coded something and had to use global variables because when I switched timeframes the program re initialized and all as it does and I needed some variables to not lose their values when reinitialized. I am not talking about global variables in termes of scope but global variables like using GlobalVariableSet() function. It helped me overcome that problem of storing values of variables when Indicator is reinitialized but what I am experiencing is that when I have multiple charts with the indicator on it, when I change a value, it changes value of that variable on all the opened charts which is logical. But I want to know if there’s a way to stop that? Thanks in advance 

Just as an EA uses a Magic Number to identify its own trades, use a "Magic Prefix" for the global terminal variables to differentiate one instance from another.

This "magic prefix" can either be set as a fixed parameter input, or it can be constructed to include the chart symbol and time-frame, and any other unique identifier, that will help minimise the multiple occurrences when using the same indicator on multiple charts. You can also use a combination of both a fixed input tag and the symbol, time-frame, etc.

 
Bryan Djoufack Nguessong:
Hey guys, I coded something and had to use global variables because when I switched timeframes the program re initialized and all as it does and I needed some variables to not lose their values when reinitialized. I am not talking about global variables in termes of scope but global variables like using GlobalVariableSet() function. It helped me overcome that problem of storing values of variables when Indicator is reinitialized but what I am experiencing is that when I have multiple charts with the indicator on it, when I change a value, it changes value of that variable on all the opened charts which is logical. But I want to know if there’s a way to stop that? Thanks in advance 

Add the chart id to the variable names.

 
Samuel Manoel De Souza #: Add the chart id to the variable names.

That is acceptable but not always ideal. If you close a chart and later reopen a new chart on the same symbol and time-frame, you may want to carry over the previous data, and it will fail if the chart id is different.

It really depends on whether the OP wants to only keep data live while for the duration of the chart life or if he wants the data to persist into a future reopening of the symbol / time-frame / etc. conditions.

 
Fernando Carreiro #:

That is acceptable but not always ideal. If you close a chart and later reopen a new chart on the same symbol and time-frame, you may want to carry over the previous data, and it will fail if the chart id is different.

It really depends on whether the OP wants to only keep data live while for the duration of the chart life or if he wants the data to persist into a future reopening of the symbol / time-frame / etc. conditions.

Thanks for this explanation. Because It’s an indicator and I think I don’t need it to store previous data if I close and reopen chart. But what I would like to understand is I think chartid is 0 for the current chart or maybe I am wrong. So if I add chart id to variables names when ever I will add the indicator to a new opened chart, chartid will still be 0 and issue will still occur?
 
Samuel Manoel De Souza #:

Add the chart id to the variable names.

Thanks for this idea. But what I would like to understand is I think chartid is 0 for the current chart or maybe I am wrong. So if I add chart id to variables names when ever I will add the indicator to a new opened chart, chartid will still be 0 and issue will still occur?
 
Bryan Djoufack Nguessong #But what I would like to understand is I think chartid is 0 for the current chart or maybe I am wrong. So if I add chart id to variables names when ever I will add the indicator to a new opened chart, chartid will still be 0 and issue will still occur?

No, there is a misunderstanding on your part. Using a Chart ID of "0", tells the functions to use the "current chart", but it does not signify that the chart's actual id is 0. The chart's actual ID is usually a large number (e.g. 132683146766314648).

 
Bryan Djoufack Nguessong #: Thanks for this explanation. Because It’s an indicator and I think I don’t need it to store previous data if I close and reopen chart.

However, I still suggest using a more "human" readable prefix for global terminal variables, as it will be easier for you to follow up on the variables, in the Global Variables Window (F3), and also to modify values or delete older variables.

If you just use the ID, you will have difficulty identifying which chart is which just from the ID reference alone.

 
Fernando Carreiro #:

However, I still suggest using a more "human" readable prefix for global terminal variables, as it will be easier for you to follow up on the variables, in the Global Variables Window (F3), and also to modify values or delete older variables.

If you just use the ID, you will have difficulty identifying which chart is which just from the ID reference alone.

Understood. Thanks a lot will try and get back with a feedback
 
Fernando Carreiro #:

No, there is a misunderstanding on your part. Using a Chart ID of "0", tells the functions to use the "current chart", but it does not signify that the chart's actual id is 0. The chart's actual ID is usually a large number (e.g. 132683146766314648).

I think adding of the symbol and chartid because if I do symbol and timeframe
Let’s say I have GlxxxxxUSDJPYH4 when I am on UJ H4 TF
When I switch timeframe to h1 the init function will then set the global variable name to GlxxxxUSDJPYH1 and I think the previous variable won’t be checked anymore in the program and loss of value as GlxxxxxUSDJPYH4 was the variable before. Now if I have 2 charts of the same symbol opened. ChartID Will be different for those 2? Is it unique ?
 
Bryan Djoufack Nguessong #: I think adding of the symbol and chartid because if I do symbol and timeframe. Let’s say I have GlxxxxxUSDJPYH4 when I am on UJ H4 TF. When I switch timeframe to h1 the init function will then set the global variable name to GlxxxxUSDJPYH1 and I think the previous variable won’t be checked anymore in the program and loss of value as GlxxxxxUSDJPYH4 was the variable before. Now if I have 2 charts of the same symbol opened. ChartID Will be different for those 2? Is it unique ?

Yes, that makes sense, but it really depends on how your Indicator reacts to chart changes.

If the indicator needs to reset data if you change time-frame, then it is best to keep track of the time-frame in the prefix. It it keeps that data, then it will be best NOT to use the time-frame in the prefix.

The same applies to the Symbol. If the Indicator needs to reset for a symbol change, then keep the symbol name in the prefix, but if it keeps the data regardless of the symbol, then don't use the symbol name in the prefix.

Reason: