{"id":20,"date":"2007-04-21T13:48:11","date_gmt":"2007-04-21T13:48:11","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/04\/21\/using-a-static-variable-key\/"},"modified":"2007-07-08T11:08:25","modified_gmt":"2007-07-08T11:08:25","slug":"using-a-static-variable-key","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/04\/21\/using-a-static-variable-key\/","title":{"rendered":"Keying Static Variables"},"content":{"rendered":"
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.<\/p>\n
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.<\/p>\n
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:<\/p>\n
<\/span>global <\/span>SVKey<\/span>;\r<\/span>SVKey <\/span>= <\/span>Name<\/span>()+<\/span>NumToStr<\/span>(<\/span>GetChartID<\/span>(),<\/span>1.0<\/span>,<\/span>False<\/span>);\r\r<\/span>procedure xStaticVarSet<\/span>( <\/span>SName<\/span>, <\/span>SValue <\/span>)\r{\rglobal <\/span>SVKey<\/span>;\r<\/span>InIndicator <\/span>= <\/span>Status<\/span>(<\/span>"Action"<\/span>) == <\/span>1<\/span>;\rif( <\/span>InIndicator <\/span>) <\/span>StaticVarSet<\/span>(<\/span>Sname<\/span>+<\/span>SVKey<\/span>, <\/span>Svalue<\/span>);\r}\r\rfunction <\/span>xStaticVarGet<\/span>( <\/span>SName <\/span>)\r{\rglobal <\/span>SVKey<\/span>;\rif( <\/span>IsNull<\/span>( Var = <\/span>StaticVarGet<\/span>(<\/span>Sname<\/span>+<\/span>SVKey<\/span>) ) ) Var = <\/span>0<\/span>;;\rreturn Var;\r}\r\r<\/span>procedure xStaticVarSetText<\/span>( <\/span>SName<\/span>, <\/span>SValue <\/span>)\r{\rglobal <\/span>SVKey<\/span>;\r<\/span>InIndicator <\/span>= <\/span>Status<\/span>(<\/span>"Action"<\/span>) == <\/span>1<\/span>;\rif( <\/span>InIndicator <\/span>) <\/span>StaticVarSetText<\/span>(<\/span>Sname<\/span>+<\/span>SVKey<\/span>, <\/span>Svalue<\/span>);\r}\r\rfunction <\/span>xStaticVarGetText<\/span>( <\/span>SName <\/span>)\r{\rglobal <\/span>SVKey<\/span>;\rreturn <\/span>StaticVarGetText<\/span>(<\/span>Sname<\/span>+<\/span>SVKey<\/span>);\r}<\/span><\/pre>\nEdited by Al Venosa<\/p>\n","protected":false},"excerpt":{"rendered":"
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 […]<\/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\/20"}],"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=20"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}