Keying Static Variables

When you trade in RT using automated trading, you must make sure that your Static Variables are unique to your system, i.e. they should not be global. Global variables can be accessed from anywhere, and you must make sure that there are no naming conflicts when you run different programs simultaneously in different panes or windows. You can do this by Keying the Static Variable name with a unique string. While you could type in this unique key each time you use a Static Variable, a simpler way is to define a Global key that changes with the environment that the formula runs in.

An easy way to do this is to replace (wrap) the Amibroker StaticVariable functions with custom functions that have an “x” prefixed to the AmiBroker function name, such as: xStaticVarSet(). The advantage of using a simple name modification is that you can quickly convert the functions from one type to the other using the AFL editor’s Replace function.

The Static Variables in the code below are keyed with the Name() and ChartID and, in most cases, this works fine. However, you can also key Static Variables with only the Name(), the System’s filename, or other unique strings. How you key a Static Variable depends on the required scope of the Static Variable. For example, to make a Static Variable common to all tickers traded you need to remove the Name() part from the key. Below are four functions that you can substitute for the standard ones:

global SVKey;
SVKey Name()+NumToStr(GetChartID(),1.0,False);

procedure xStaticVarSetSNameSValue )
{
global SVKey;
InIndicator Status("Action") == 1;
if( InIndicator StaticVarSet(Sname+SVKeySvalue);
}

function xStaticVarGetSName )
{
global SVKey;
if( IsNull( Var = StaticVarGet(Sname+SVKey) ) ) Var = 0;;
return Var;
}

procedure xStaticVarSetTextSNameSValue )
{
global SVKey;
InIndicator Status("Action") == 1;
if( InIndicator StaticVarSetText(Sname+SVKeySvalue);
}

function xStaticVarGetTextSName )
{
global SVKey;
return StaticVarGetText(Sname+SVKey);
}

Edited by Al Venosa

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.00 out of 5)
Loading...Loading...

2 Responses to “Keying Static Variables”

  1. JD Fagan
    September 1st, 2007 | 6:18 am

    Could you give an example of why this tactic is important? I’m trying to follow but an example would help in understanding how its used. And to know if we always need to do this or just sometimes depending on the variable or effect we are trying to achieve?

    Thanks, appreciate all this great content in one place!

    JD

  2. Herman
    September 1st, 2007 | 12:04 pm

    The need for Static variables was explained in http://www.amibroker.org/userkb/2007/04/21/static-variables-in-rt-systems/ and a simple example is provided in http://www.amibroker.org/userkb/2007/07/22/order-simulation-using-debugview/

    In the order-simulation example _TRACE() statements are substituted for IBc Place/Modify-Order commands. An “InLong” Static Variable is used to maintain position information across multiple passes through the code. If you were to run this code simultaneously for different tickers the “InLong” variable would, since it is global, be set by Buy signals from different tickers. It would no longer be unique to any one ticker. This is a typical case where you would want to Key (append) the ticker’s name to the Static variable name to ensure that it is ticker-specific.

    Thanks for your feedback, I might add a simple example to the post.

    I hope this helps,
    herman