{"id":40,"date":"2007-04-24T17:32:08","date_gmt":"2007-04-24T17:32:08","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/04\/24\/persistent-variables\/"},"modified":"2008-02-17T08:26:57","modified_gmt":"2008-02-17T08:26:57","slug":"persistent-variables","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/04\/24\/persistent-variables\/","title":{"rendered":"Persistent Variables (v3)"},"content":{"rendered":"

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.<\/p>\n

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.<\/p>\n

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.<\/p>\n

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<\/em>. 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.<\/p>\n

PersistentPath = \"C:\\\\Program Files\\\\AmiBroker\\\\PersistentVariables\\\\\";<\/code><\/p>\n

function PersistentVarSetText( VarName, String )
\n{
\nglobal PersistentPath;
\nfh = fopen( PersistentPath+VarName+\".pva\",\"w\" );
\nif( fh )
\n{
\nfputs( String, fh );
\nfclose( fh );
\n}
\nreturn fh;
\n}<\/code><\/p>\n

function PersistentVarGetText( VarName )
\n{
\nglobal PersistentPath;
\nfh = fopen( PersistentPath+VarName+\".pva\",\"r\" );
\nif( fh )
\n{
\nString = fgets( fh );
\nfclose( fh );
\n}
\nelse string = \"\";
\nreturn String;
\n}<\/code><\/p>\n

function PersistentVarSet( VarName, Number )
\n{
\nglobal PersistentPath;
\nString = NumToStr(Number);
\nfh = fopen( PersistentPath+VarName+\".pva\",\"w\" );
\nif( fh )
\n{
\nfputs( String, fh );
\nfclose( fh );
\n}
\nreturn fh;
\n}<\/code><\/p>\n


\nfunction PersistentVarGet( VarName )
\n{
\nglobal PersistentPath;
\nfh = fopen( PersistentPath+VarName+\".pva\",\"r\" );
\nif( fh )
\n{
\nString = fgets( fh );
\nfclose( fh );
\nNumber = StrToNum(String);
\n}
\nelse Number = Null;
\nreturn Number;
\n}<\/p>\n

<\/code><\/p>\n

function PersistentListAdd( VarName, String )
\n{
\nSubStrExists = 0;
\nif( String != \"\" )
\n{
\nList = PersistentVarGetText( VarName );
\nfor( i=0; ( LoopItem = StrExtract( List, i ) ) != \"\"; i++ )
\n{
\nif( LoopItem == String ) SubStrExists = 1;
\n}
\nif( NOT SubStrExists )
\n{
\nList = List + String+\",\";
\nPersistentVarSetText( VarName, List );
\n}
\n}
\nReturn List;
\n}<\/code><\/p>\n

function PersistentListRemove( VarName, String )
\n{
\nif( String != \"\" )
\n{
\nList = PersistentVarGetText( VarName );
\nNewList = \"\";
\nfor( i=0; ( LoopItem = StrExtract( List, i ) ) != \"\"; i++ )
\n{
\nif( LoopItem != String ) NewList = NewList + LoopItem +\",\";
\n}
\nPersistentVarSetText( VarName, NewList );
\n}
\nReturn NewList;
\n}<\/code><\/p>\n

<\/span>function <\/span>PersistentVarAddLine<\/span>( <\/span>VarName<\/span>, <\/span>Line <\/span>)\r{\r    global <\/span>PersistentPath<\/span>;\r    <\/span>fh <\/span>= <\/span>0<\/span>;\r    if ( <\/span>Line <\/span>!= <\/span>"" <\/span>)\r    {\r        <\/span>fh <\/span>= <\/span>fopen<\/span>( <\/span>PersistentPath <\/span>+ <\/span>VarName <\/span>+ <\/span>".pva"<\/span>, <\/span>"a" <\/span>);\r        if ( <\/span>fh <\/span>)\r        {\r            <\/span>fputs<\/span>( <\/span>Line <\/span>+ <\/span>"\\n"<\/span>, <\/span>fh <\/span>);\r            <\/span>fclose<\/span>( <\/span>fh <\/span>);\r        }\r    }\r    return <\/span>fh<\/span>;\r}\r\r<\/span>\/\/ Here is an example on how to export lines from an Exploration\r\/\/ Look for the file in at the path defined at the top of this post\r<\/span>Filter <\/span>= <\/span>Status<\/span>( <\/span>"LastBarInTest" <\/span>);\r<\/span>AddColumn<\/span>( <\/span>C<\/span>, <\/span>"C"<\/span>, <\/span>1.2 <\/span>);\r<\/span>ToFile <\/span>= <\/span>Name<\/span>() + <\/span>"," <\/span>+ <\/span>NumToStr<\/span>( <\/span>LastValue<\/span>( <\/span>C <\/span>), <\/span>1.2 <\/span>);\r<\/span>PersistentVarAddLine<\/span>( <\/span>"MyData"<\/span>, <\/span>ToFile <\/span>);<\/span><\/pre>\n

The following function was kindly contributed by suresh (see comment) and can be used to delete Persistent variables:<\/p>\n

<\/span>function <\/span>PersistentVarRemove<\/span>( <\/span>VarName <\/span>)\r{\rglobal <\/span>PersistentPath<\/span>;\r<\/span>Fn<\/span>=<\/span>PersistentPath <\/span>+ <\/span>VarName <\/span>+ <\/span>".pva"<\/span>;\r<\/span>fh<\/span>=<\/span>fdelete<\/span>( <\/span>Fn <\/span>) ;\rreturn <\/span>fh<\/span>;\r}<\/span><\/pre>\n

Edited by Al Venosa<\/p>\n","protected":false},"excerpt":{"rendered":"

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 […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}