Static Variable missing after running Backtest

When I run a simple backtest, not a custom backtest all my static variables are removed. What could be causing this? Something as simple as this is erasing my static variables, what am I missing? Thanks.

Buy=Sell=Short=Cover=0;

ma200= MA(C,200);
Buy=Cross(C,ma200);
Sell=Cross(ma200,C);

AFL Function Reference - STATICVARSET

Please note that static array variable will consume 8 * (number_of_bars) bytes of memory and it won't be released until program is closed or variable is removed using StaticVarRemove().

In the code that you have provided, you are NOT using any static variables.
And no, that code does not erase any static variables.

Unfortunately your question isn't clear enough and does not provide all necessary details. Please follow this advice: How to ask a good question

Well, by running this code my static variables disapper, but only when runing this code in backtest or amy code for that matter.

I run one afl with code

staticvarset("Test",C);

Then if i run this afl

Buy=Sell=Short=Cover=0;
Test1= staticvarget("Test");
ma200= MA(C,200);
Buy=Cross(C,ma200);
Sell=Cross(ma200,C);

[/quote]

If i run explore on second afl test1 has value once I run a backtest on afl 2 test1 is NULL.

Animation`

First Explore AFL

staticvarset("TEST",C);

Second Explore AFL and Backtest, then Explore again the StaticVariable is Null

Buy=Sell=Short=Cover=0;
Test1=StaticVarGet("TEST");
ma200= MA(C,200);
Buy=Cross(C,ma200);
Sell=Cross(ma200,C);

Filter=1;
AddColumn(Test1,"TEST",1.2);

Individual Backtest does not remove the Static Variable, but Portfolio Backtest causes result in video above where Static Variable becomes Null.

What is removing static variable when portfolio backtest is run? There must be some setting i have wrong, but i can not find it.

So can i set a static variable in one afl then run a seperate afl and access the static variable created in first afl? If so, this static variable created should stay available until i exit amibroker or remove it with StaticVarRemove?

No, Backtest DOES NOT REMOVE any static variables.

Look elsewhere for your problem. Anything that happens on your side must be happening because you are doing it (and not realizing it)

Chances are you have some random code elsewhere (in some other formula, say in chart) that removes static variables.
Static variables are global (shared) and if you call StaticVarRemove ANYWHERE in ANY formula running in the background.

For example where if you have LEFTOVER of your code

staticvarset("Test",C);

in your PORTFOLIO EQUITY chart, it might REASSIGN values to static variable without you realizing what happens!
Or you might have such leftover code in your custom backtest code.

You might be also using "test" variable ELSEWHERE and assigning Null values to it, or assigning values in OTHER DATES, effectively removing the dates you are looking at in the analysis Window.

So:

  1. CHECK all your formulas (CHARTS!) for any use of Static* functions - chances are you are overwriting in the background (use Find In Files tool to scan ALL FILES for Static word)
  2. Make sure you are using some UNIQUE name not "test" to avoid conflict with existing code that you wrote.
2 Likes

StaticVarRemove was included in one of the report charts. Thank you! Since I was not running custombacktest I forgot or did not account for the fact that Report Charts were still running.

Thanks again as always you have the answers!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.