Persistent Variables (v3)

Static Variables retain their values as long as AmiBroker is running. If you shut down AmiBroker or experience a computer crash, your Static Variables lose their values. This can create serious problems in Automated Trading. For example, suppose you experience a computer crash while you have many pending orders. After restarting everything, you are unable to modify the orders, and so you are forced to use the TWS to manually clean up the mess and restart.

To prevent this situation, you can use Persistent variables that store their values on your Hard Disk. They will remain there until you delete them. Using Persistent variables, if you experience a crash or shut down your system during the night, the persistent variables will automatically be reloaded when you power up again.

Persistent variables can also be used to save ticker-specific system parameters. For example, you could run an optimization and save the optimized parameters in a Persistent Variable encoded with the Ticker’s name. Below are examples for saving text and numerical values. Arrays are not included here because arrays are better handled in other ways. Included in the code below is a handy function to create Persistent lists and to remove items from these lists. The list functions are useful when you wish to save lists of Symbols and/or OrderIDs dynamically.

Note that these parameters must be stored in a specific folder on your Hard Disk (see first code line below for an example of a typical recommended path). You must assign the desired location to the string variable PersistentPath. Remember that Persistent variables are Global just like Static variables, and their names may have to be encoded with ChartIDs and/or Symbol names to prevent them from being modified by different programs.

PersistentPath = "C:\\Program Files\\AmiBroker\\PersistentVariables\\";

function PersistentVarSetText( VarName, String )
{
global PersistentPath;
fh = fopen( PersistentPath+VarName+".pva","w" );
if( fh )
{
fputs( String, fh );
fclose( fh );
}
return fh;
}

function PersistentVarGetText( VarName )
{
global PersistentPath;
fh = fopen( PersistentPath+VarName+".pva","r" );
if( fh )
{
String = fgets( fh );
fclose( fh );
}
else string = "";
return String;
}

function PersistentVarSet( VarName, Number )
{
global PersistentPath;
String = NumToStr(Number);
fh = fopen( PersistentPath+VarName+".pva","w" );
if( fh )
{
fputs( String, fh );
fclose( fh );
}
return fh;
}


function PersistentVarGet( VarName )
{
global PersistentPath;
fh = fopen( PersistentPath+VarName+".pva","r" );
if( fh )
{
String = fgets( fh );
fclose( fh );
Number = StrToNum(String);
}
else Number = Null;
return Number;
}

function PersistentListAdd( VarName, String )
{
SubStrExists = 0;
if( String != "" )
{
List = PersistentVarGetText( VarName );
for( i=0; ( LoopItem = StrExtract( List, i ) ) != ""; i++ )
{
if( LoopItem == String ) SubStrExists = 1;
}
if( NOT SubStrExists )
{
List = List + String+",";
PersistentVarSetText( VarName, List );
}
}
Return List;
}

function PersistentListRemove( VarName, String )
{
if( String != "" )
{
List = PersistentVarGetText( VarName );
NewList = "";
for( i=0; ( LoopItem = StrExtract( List, i ) ) != ""; i++ )
{
if( LoopItem != String ) NewList = NewList + LoopItem +",";
}
PersistentVarSetText( VarName, NewList );
}
Return NewList;
}

function PersistentVarAddLineVarNameLine )
{
    global PersistentPath;
    fh 0;
    if ( Line != "" )
    {
        fh fopenPersistentPath VarName ".pva""a" );
        if ( fh )
        {
            fputsLine "\n"fh );
            fclosefh );
        }
    }
    return fh;
}

// Here is an example on how to export lines from an Exploration
// Look for the file in at the path defined at the top of this post
Filter Status"LastBarInTest" );
AddColumnC"C"1.2 );
ToFile Name() + "," NumToStrLastValue), 1.2 );
PersistentVarAddLine"MyData"ToFile );

The following function was kindly contributed by suresh (see comment) and can be used to delete Persistent variables:

function PersistentVarRemoveVarName )
{
global PersistentPath;
Fn=PersistentPath VarName ".pva";
fh=fdeleteFn ) ;
return fh;
}

Edited by Al Venosa

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

3 Responses to “Persistent Variables (v3)”

  1. suresh
    October 7th, 2007 | 11:52 pm

    1. what would be the values for if i do a PersistentVarGet and the variabe does not exist. Would it be null or empty. can i use nz in that case.
    2. can u give some example around PersistentListAdd

    thanks a lot Herman. This set of functions is proving to be very useful for me.

  2. suresh
    October 8th, 2007 | 5:54 pm

    added a extra function to delete persistant variables. i added this becauase i needed it. but if you can add it to the library, it might be useful for someone

    function PersistentVarRemove( VarName )
    {
    global PersistentPath;
    fh = fdelete(PersistentPath+VarName+”.pva” ) ;

    return fh;
    }

  3. Herman
    October 8th, 2007 | 8:45 pm

    Thank you suresh, I added your function to the post. I also modified the PersistentGet() to return NULL when it doesn’t exist. This is similar to the way Static Variables work. As it was before it would return a zero which would be indistinguishable from a valid zero.

    The PersistentListAdd() can be used to maintain your own list(s) of the OrderIDs returned when placing orders on the TWS. OrderIDs are normally not saved when you shut down AB/IBc – this can be a problem during unscheduled shutdowns.