StaticVarSet
- sets the value of static variable

Miscellaneous functions
(AmiBroker 4.60)


SYNTAX StaticVarSet( ''varname'', value, persistent = False, compressionMode = cmDefault )
RETURNS NUMBER
FUNCTION Sets the value of static variable. Returns 1 on success 0 on failure.

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 supported from version 5.30 and MATRIX static variables are supported from version 6.10

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().

Persistency

Starting from version 5.80 there is a new parameter persist. If it is set to True then static variable will be stored in PersistVars.bin file when AmiBroker is closing and reloaded automatically on next startup, preserving the values of static variables between application runs). In addition to saving them automatically on exit, persistent static variables can be auto-saved at user-specified intervals using SetOption("StaticVarAutoSave", interval );

Compression

Starting from version 6.10 there is a new parameter compressionMode that decides whenever given variable will be compressed or not.By default only persistent static variables will be compressed (cmDefault). You can turn it off completely compressionMode = cmNever, or turn it on for persitent and non-persistent variables using compressionMode = cmAlways

Compression is done by removing repeated values from the sequence as repeated values are restored when doing StaticVarGet. Compression is NOT compatible with non-aligned mode of StaticVarGet. If compressed array is retrieved by StaticVarGet with align=False, then repeated values found in original array would not be retrieved. Turning compression on slows down StaticVarSet (as it needs to do some extra processing), but does not affect performance of other functions, so StaticVarGet is equally fast with or without compression..

Static variables vs composites

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:

  • 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. This limitation applies to StaticVarSet but does NOT apply to StaticVarAdd (new in 6.10) which is designed to be functional equivalent of AddToComposite
  • 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)
  • 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.
  • 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.
  • 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:
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 StaticVarSetText() function , StaticVarGet() function

References:

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

More information:

See updated/extended version on-line.