static Keyword

The static keyword allows to declare

Syntax:

static identifier;

static identifier1, identifier2, identifier3, ...;

The static keyword declares identifier as static variable

Declared static variables can and should be used as 'regular' variables, no need to call functions (Static*).

IMPORTANT: The static keyword is only available when you use

#pragma enable_static_decl(prefix)

at the top of your formula. The prefix is added automatically to actual static variable name to avoid name clashes/conflicts. Prefix should be constant string literal (as in Example 1).

You can also have declared static variable with unique prefix that includes current chartID, interval or symbol. To do so use {chartid}, {interval} or {symbol} runtime tokens in the prefix string. They will get replaced in runtime by corresponding values (see Example 2).

Example 1.

// the pragma below is required to enable support for static declarations
// it also defines the prefix which is added to variables declared with static keyword
// it is one prefix per formula.
#pragma enable_static_decl(prefix)

// it is really good idea to consequently use naming convention that
// distinguishes declared static variables from the others
// we highly recommend using UNDERSCORE '_' before all declared static variables
// so they can be easily spotted in the code
// The other choice could be s_ prefix

static _myvar; // this var is also accessible by "prefix_myvar"

printf( "This variable is really static %g", _myvar++ );

// keep in mind that for speed static variable is read only once at the declaration
// and saved only once - when formula execution is completed
// This way declared static variables offer same speed as regular variables (no speed penalty)

Example 2.

// Example 2 - if you want per-chartID declared static variables
#pragma enable_static_decl "myprefx{chartid}"
static x; // this static will really have name of myprefixIDx where ID is a current chartID

More information on static keyword is included on the forum: https://forum.amibroker.com/t/amibroker-6-27-1-beta-released/2925/