- gets the value of static variable

Miscellaneous functions
(AmiBroker 4.60)

SYNTAX StaticVarGet( ''varname', align = True' )
FUNCTION Gets the value of static variable.

Static variable - the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to Null unless another value is specified. Static variables allow to share values between various formulas. ARRAY static variables are now supported (version 5.30 and above).

Please note that static array variable will consume 8 * (number_of_bars) bytes of memory and it won't be released until program is closed or variable is removed using StaticVarRemove().

Static arrays can be even 100 faster than AddToComposite/Foreign, however these two are not strictly equivalent.

There are following limitations / differences of static arrays as compared to Foreign/AddToComposite:
a) static array variables store only as many bars as there are currently in use by given chart (so they do not affect QuickAFL in any way). This is different that AddToComposite that forces usage and store of all bars.
b) static array variables work best if you read them using the same interval as they were written to. I.e when you create static array variables using 5-minute chart, for best results read them in some other 5-minute chart. Reading in different intervals is possible, but subject to limitations of timestamping (see below)
c) when you read static variable in a different interval that it was originally stored, static variables perform padding/synchronization and time compression/decompression automatically in a similar way as foreign, however Foreign compresses data always from base-time interval, while static variables operate on previously stored interval, hence result may differ. For example, if previously stored data was in daily interval, and you read such static variable in intraday chart, you will see essentially flat lines for each day, representing static data from daily interval.
d) static array variables do not work well for non-time based intervals (tick/n-volume/n-tick) because timestamps in those intervals may not be unique (i.e. several bars may have same time stamp), so time synchronization is not reliable.
e) static array variables are little slower than normal AFL variables, so for best performance, use read-once, write-once paradigm, using temporary normal variable for any processing during formula execution, like this: The new align parameter (default = true) decides whenever AmiBroker performs timestamp synchronization/alignment or not.

The default value is True and it means that values stored in static variables are retrieved and aligned to currently selected symbol data/timestamp on each bar basis so data for corresponding date/time stamps match. This is recommended setting and this is the way it worked in previous versions.

When align is switched to False - it means that AmiBroker does not perform any checks nor any alignment and will fill the array with consecutive values stored in static array regardless of their timestamps. If there are less bars in the static array than in the current arrays, the last value of static array will be propagated till BarCount - 1.

It is advised NOT to use align=False, unless you know exactly what you are doing and you are aware that date/time stamps have no meaning in particular variable or in case when date/time stamps are are aligned using your own method.

Note that speed difference between align 'on' and 'off' is usually negligible because alignment algorithm is very fast and has similar complexity as plain memory copy.

EXAMPLE // start of the formula:
temp =
StaticVarGet("mystaticarray" );

// now perform all necessary calculations using temp variable

temp =
Nz(temp) + C/2;

// at the end of the formula store to static
StaticVarSet("mystaticarray", temp );
SEE ALSO StaticVarSet() function , StaticVarSetText() function , StaticVarGetText() function


The StaticVarGet function is used in the following formulas in AFL on-line library:

More information:

See updated/extended version on-line.