Using StaticVarSet and StaticVarGet To Transfer Array

Continuing the discussion from How To - Use Optimize Value In Custom Metrics:

@Tomasz Would you kindly clarify (or provide a link to documentation) why it is necessary to use StaticVarSet and StaticVarGet to transfer an array from the first phase to the second phase of the backtest (status(“action”) == action Portfolio)?

The VarSet() online documentation states:

Dynamic variables are always global. Starting from version 6.10 the function accept matrix variables in addition to numbers and arrays.

While I realize now it is absolutely necessary to use Static Variables when transferring an array to the backtest phase 2, I want to better understand when I should use the regular dynamic variables (VarSet()) instead of static variables.

Incidentally, I believe that using VarSet() is more efficient because they do not persist (in memory) once my backtest or AFL formula is finished executing.

If I do use Static Variables in my custom backtest, should i clean them up someplace in the backtest formula? If yes, where is the correct location or backtest phase Status(“actionEX”) to place StaticVarRemove() function … in other words, is there a housekeeping phase that runs after all other custom backtest processing has finished?

1 Like

VarGet/VarSet is only used to access variables within same AFL execution. They are exactly the same as “normal” variables in AFL. They disappear once AFL formula execution completes. Static variables are used to pass values between executions. They stay in memory until removed via StaticVarRemove or AmiBroker is closed. In first phase Each symbol in executed seprately. First phase occurs N times where N is number of symbols under test. Second phase runs AFTER that and it is only run ONCE on portfolio equity ticker ~~~EQUITY.


@Tomasz Thank you for the explanation.
So since the phase 2 executes one time per backtest, it occurs to me that it is a good housekeeping step to place StaticVarRemove function after the bo.ListTrades() (assuming I have no other code that follows).


Yes that is good idea.