Opening parameter window in analysis resets the arrays

I am computing certain arrays and storing/persisting them using staticvarset. These compuations are very time consuming and ideally to be done once for a given database and stored so that subsequent runs can use them without having to compute again. Code looks something like below

doCompute = ParamToggle( "Compute arrays", "No|Yes", 0 );

overnightHoldParam = ParamToggle( "Hold Positions overnight", "No|Yes", 0 );     maxLossPerDayLimitAsPercentageOfDayStartCapitalParam = Param( "Maximum Loss Per Day Limit %", 3, 1, 50, 0.5, 0 ); 

function computeTimeConsumingArrays( doCompute )
{
..do some calculation that takes lot of time

staticVarSet("array1", array1, true);

}

function findPattern()
{
arr1=staticvarget("array1"); //use the arrays stored in persistVars
}

So When backtest for the very first time over a database then I will toggle the value of doCompute to Yes. And in subsequent runs it will be set to No so that stored values are used. so lets assume I run this afl for the very first time on a given database. I select the formula in Analysis window. Then i open the parameter window. Toggle the param to Yes. Run. This run takes 1 hour because of the computation and the trade list is populated at the end of the run. Now I wish to change some of the other parameters like overnightHoldParam and maxLossPerDayLimitAsPercentageOfDayStartCapitalParam so that I can see the results with different values (e.g, overnightHoldParam may control if the position is carried over or squared off at the end of the day and hence may impact the profit/loss). But first doCompute has to be set to No. So I click the parameter button to bring up the parameter window. Here lies the problem. when the parameter window opens the entire afl is executed and since the doCompute is set to yes (from the previous run) all the array values stored in persistVars.bin are lost. Is this by design? or am I doing something wrong?
I have read fxshrats post about using params only in chart and in analysis window But that is not a practical solution. He suggest using hardcoded values instead. But that would mean changing the code every time which is undesirable.

Yes, it is by design, because Parameter window has to know what parameters you have defined in your code. Therefore it must actually EXECUTE your AFL formula to find out what parameters are.

You may however prevent execution of time consuming part when parameter window execution is happening simply by checking Status("actionex")

if( Status("actionex") == actionExAAParameters )
{
  // this means code is executed for Parameters window
}

It is documented in the users guide: AFL Function Reference - STATUS

1 Like

Thankyou, I have implemented this code. I will post back the result after testing. Thankyou so much for helping me promptly.

1 Like

I am happy to help when someone simply asks a question, instead of immediately assuming bugs :slight_smile:
Your question was perfect example how everyone should prepare their questions - well described problem, with exact steps done before problem occurs, together with code snippet. No guessing games needed.

1 Like

I have tested it. It works perfectly. Thanks again.

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