if( Status("stocknum") == -1 ) // ALL WRONG !!!! This must **NEVER** appear!!!
The minus one stocknum ("impossible value") was introduced to prevent accidental (second) execution of code that was intended to run ONCE (at the beginning of analysis), not to allow them.
Using it the way it is used above is completely INCORRECT.
I removed obviously incorrect code from the above so people do NOT copy these incorrect ways.
The only proper way to use Status("action") in the code that performs ranking is:
if ( Status( "stocknum" ) == 0 ) // THE ONLY CORRECT WAY to run code on first symbol only
{
// StaticVarGenerateRanks code here
// or any other code that is intended to be run on first symbol
}
Zero, not minus one, is the only correct value here.
BTW: If you want to run code in custom backtest context you should be using
I also tried your simpler version with one single watchlist. The issue of your formula is that your ranking based on Max(momo_avg,0) switch you to cash when the best asset of your universe has a negative momentum and your strategy is not fully invested. This might explain the large perfromance difference. I would be interested in how you solve this. Thanks !
Just a heads-up for people who blindly copy that formula. The formula is written with ASSUMPTIONS that if not met, cause this formula to throw errors.
It assumes that you are running it in Analysis window (it should NOT make such assumption)
It assumes that you have Filter set to some watch list (again, it should run WITHOUT such assumption)
It assumes that given watch list is NOT empty (again it should NOT assume that).
If any of the three assumptions are NOT valid it will fail with errors (because it does not handle situation when these conditions are not meet) because it fails to define all variables (namely PosSize and PosScore) UNCONDITIONALLY.
At minimum the code MUST DEFINE ALL VARIABLES UNCONDITIONALLY
Of course Tomasz is right regarding the assumptions, hence the reference to i.e. the watch- and filterlists in the description, but how to account for them in the code? Any guidance would be appreciated.