AFL Variable Scope Across Chart Panes

The provides some info on variable scope within functions.

Are there any articles or AFL documentation that discuss and provide examples for an AFL formula variable’s scope?

For example, are variables “private” to an AFL formula in situations like:
a. Two chart windows (same formula used on each of the two chart windows (in a chart pane))
b. One chart window with one chart pane. The chart pane has two formulas that use the same variable name.
c. One chart window with 2 chart panes. Each chart pane has a different formula, and but use several variables that have the same name.

I am asking about regular AFL variable names.

In other words, is there a global or local scoping that can be provided within a formula to guarantee that the variables in a formula remain private, or can optionally be made global and shareable across chart panes, chart windows, or a session?

1 Like

Meant the above as first link in conjunction with the second…

The simple answer to all those combinations is that since the AFL code executes line by line, the output/ value of any ‘regular’ (assuming user defined, dynamic) variable would get overwritten by another instance of the ‘same variable’ defined differently in code execution progression (this also encompasses usage of the ‘#include’ instruction) per pane/ formula. Now you can figure out the outcomes in your proposed permutations :smile:

Also, refer to @Tomasz’s remark about good coding practice here

And @fxshrat’s instructive example here

@zbin thank you. Can you or someone explain the scope of user defined variables between two chart panes?
For example,

Chart Pane one’s says closeMA = EMA(C,0);
Chart Pane two’s says If (closeMA > EMA(C,26)) {…;}
Will CloseMA be seen in Chart pane two (of the same chart window)?

@zbin thank you.

After some testing, I found that variables with the exact same name, used concurrently in two different chart panes (within the same window), will keep their own respective values. In other words, the chart panes maintain private variables. The variable’s are tied to the chartID. Even with the use of the Global statement, the variables remain private to the chartID (chart pane).

For additional info, visit: