The difference is the lifespan of the value. Formalizing your sample, we
have the following code:
...
b = 5;
...
b = 6;
When
running over a watchlist, the script is restarted for each symbol. Using your
example, once the script begins over again for the next symbol, the value of b
just before the line b = 5 will be the same as it was for the first symbol.
Specifically, b will be undefined, not 6 as was the case at the end of the
previous symbol's execution.
In other words, variables do not retain
their value between executions of the script.
The behavior of VarSet is
identical to simply saying b = 5. The only difference is that you can
dynamically construct the name of the variable as opposed to hard coding it.
This is useful when setting variables in a loop (e.g. b1 = ..., b2 = ..., b3 =
...).
Static variables are primarily used for two reasons:
1. To
preserve values from execution to execution.
For example, changing your
original code to use statics (StaticVarSet) would mean that for all subsequent
symbols, the value of b would be 6 just before the line StaticVarSet(b,
5) instead of undefined. For the first symbol, b would still initially have
been undefined until set to 5.
For a more practical example; automation
traders will re-run the same scan/exploration over and over throughout the day
as new bars come in. Using static variables they are able to preserve state
information between executions (e.g. whether or not a position has been
taken), even if running on just a single symbol rather than a
watchlist.
2. To access information across panes.
StaticVarGet can
be used from other panes to access a static value set from a different pane.
Rather than having everything displayed in a single pane in a single script,
it is sometimes more practical to do the calculations once in a "master" pane,
and have one or more additional "detail" panes/scripts that chart a variety of
related information.
Additional usage might include access within the
latter pass of the backtester to values set during the initial signal
generating pass.
Mike
--- In amibroker@xxxxxxxxxps.com,
"Markus Witzler" <funnybiz@xx.> wrote:
>
>
Hello,
>
> could someone enlighten me as to when I "should" use
static variables (Varset)?
>
> I understand that they serve as
"temporary storage space".
>
> But couldn´t I just use any
letter, such as b=5 and later assign b = 6 etc. and retrieve/change its value
whenever needed
>
> Where is the difference to a static
variable?
>
> Sorry - again - for my ignorance!
>
>
Thanks
>
> Markus
>