Persistent Static Variables


I am using the following code to save some data to persistent static variables but the variables are not ‘persisting’. When I restart Amibroker my static variables are gone. Looking in the Amibroker folder I can see that the PersistVars.bin file is not being written to when I exit Amibroker. Can anyone see a reason why the static variable wouldn’t be saving to PersistVars.bin?


odbcOpenDatabase(“ODBC;DSN=SQL_ODBC_64bit;UID=amibroker;PWD=xxxx;Trusted_Connection=No;APP=AmiBroker for Windows;WSID=xxxxx;DATABASE=StockScreens”);

if(Status(“StockNum”) == 0){

	symlist = CategoryGetSymbols( categoryWatchlist, 65 );

	StaticVarRemove( "GlobalRanks*" );
	StaticVarRemove( "countryrankGlobalRanks*" );
	countryList = ",";

for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ ){
	foreignrank = odbcGetArraySQL("Select Rank, Date FROM RivkinQualityGlobal Inner Join StockInfo.dbo.StockInfo ON StockScreens.dbo.RivkinQualityGlobal.BBID = StockInfo.dbo.StockInfo.BBID WHERE StockInfo.Symbol ='"+sym+"' ORDER BY Date ASC");
	foreignrank = 201 - foreignrank;
	country = GetFnData("Country");
	if( ! StrFind( countryList, "," + country + "," ) ) countryList += country + ",";
	StaticVarSet( "GlobalRanks"+ country + sym , foreignrank, True);

for( i = 1; ( country = StrExtract( countryList, i ) ) != ""; i++ ){
    StaticVarGenerateRanks( "countryrank", "GlobalRanks" + country, 0, 1224 );

for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ ){
	country = GetFnData("Country");
	StaticVarSet("subRank",StaticVarGet( "countryrank" + "GlobalRanks" + country + sym ), True);


Buy = 0;
Sell = 0;
Short = 0;
Cover = 0;

I think you may be deleting your static variables when your program initally loads.
Try commenting out the above statements.

Hi SwingTradeMonkey,

Sorry for the slow response, I got distracted with another project. I have these lines to make sure I clear any old data stored in these variables but the loops that store the data occur after this so I thought this shouldn't be a problem? The purpose of the code pasted above is simply to store the static variables which are then accessed by other codes.
I have discovered that the static data persists for around half of the stocks that the loop iterates through.
Just to be clear, here are the two scenarios that illustrate my problem:

Scenario 1:

  1. Run the above code.
  2. Run my system backtest
    All works fine

Scenario 2:

  1. Run the above code.
  2. Close then reopen Amibroker
  3. Run my system backtest
    Only approximately half of the trades appear in the results

Is there some kind of storage limit to how much data can be saved in a persistent variable?


There is no storage limit other than physical limitations of your computer and file system limits (like 2GB max file size for certain older file systems).