Can the prefix variable in varget be an array?

For example:

A = varget( "Static" + Array1 );

When I do that, my plots change whenever I move the vertical selection bar or zoom in or out.

First of all varname (1st argument of VarSet/VarGet) is supposed to be type string. It is variable's name.

Variable names are supposed to only consist of combination of underscore, letters ("a-z", "A-Z"), digits ("0-9") as suffix (e.g. "var1") or in between (e.g. "_var123_result"). Any other characters within variable name are considered illegal. Also starting variable names by digit is not allowed (e.g "1st_var" -> wrong).

Note: if you still use illegal characters then AB (vrs. 6.30) internally replaces them to underscore.


Now, as for examples... if you do this the standard way:

var1 = Close;
result = Roc(var1, 1);

Then you can do the same via dynamic variables (VarSet/VarGet)

VarSet( "var1", Close );
result = Roc(VarGet("var1"), 1);

So as you can see varname (1st argument ) of VarSet/VarGet is supposed to be a legal variable name as if you would do without using dynamic variables (see 1st example).


Secondly... prefix? I guess you mean varname suffix... in your case 'Array1'.

So do you mean like this:

Array1 = Close;
VarSet( "var"+Array1, Close );
result = Roc(VarGet("var"+Array1), 1);

If yes, then this is wrong usage of dynamic variable names.

Suffix that you add to a prefix string of varname is supposed to be a number (digit) or another string but not array.

You can do this

varname = "var_" + 1;// digit added as suffix to "var_"
VarSet( varname, Close );
result = Roc(VarGet(varname), 1);

or within a loop

for ( i = 1; i <= 10; i++ ) 
	VarSet( "var_"+i, Roc(C, i));

Now....If you add array as suffix to variable name then it will not produce error but it makes no sense as then it will be converted to number (LastValue(array) or SelectedValue(array) -> e.g chart selection, scroll) since there isn't array of strings.

Bottom line: do not add array to dynamic variables' name. Do what manual says (and follow its examples). Nowhere does it suggest to add array as variable name's suffix.

2 Likes

Thank you for your quick response. It looks like I would have to use loops for my application. What I wanted to do was to automatically adjust the period of my trading system based on momentum. What I did was use varset to store the signals for each period in their own array. The name of the dynamic variable would have the period number as the suffix. To call on the dynamic variables I employed an algorithm to cross reference momentum with the period and store this result in an array. This array would be in the suffix of the varget function.

Would loops then be the only way to do this?