I'm sure I'm doing something stupid but I'm not seeing it.
Anyone know why my SVGR ranks are all null?
Also, I would have expected turnover (StaticVarGet) and check_turnover (derived again in "open" code) to be equal?
Lastly, does my calculation for average total turnover look correct? I can export data to Excel and check my math - especially null handling - but if you see something obvious please let me know.
// One-off loop
if (Status("stocknum") == 0)
{
// Get list of symbols from current watchlist
symbollist = CategoryGetSymbols(categoryWatchlist, GetOption("FilterIncludeWatchlist"));
// cleanup variables created in previous runs (if any)
StaticVarRemove("*");
// derive our desired metric for each symbol in the watchlist
sumturnover=0;
cntturnover=0;
for (i=0; (symbol = StrExtract(symbollist,i)) != ""; i++)
{
// set the OHLC arrays to the current symbol
SetForeign(symbol);
// derive our desired metric(s) for each symbol in the watchlist
ibdrs = 0.4 * ROC(C,63) + 0.2 * ROC(C,126) + 0.2 * ROC(C,189) + 0.2 * ROC(C,252);
turnover = MA(C*V,20);
// say we also want to derive the average turnover for all the stocks
sumturnover+=Nz(turnover); // if the array element is null it is converted to zero
cntturnover+=NOT(IsNull(turnover)); // if the array element is null it equals 1 else 0, NOT reverses the number
RestorePriceArrays();
// save the metric to a static variable
// the static varname must be unique per symbol, i.e. include "symbol" in its name
StaticVarSet("ibdrs" + symbol, ibdrs);
StaticVarSet("turnover" + symbol, turnover);
}
// save the average turnover for all the stocks to a static variable
avgturnover=SafeDivide(sumturnover,cntturnover);
StaticVarSet("avgturnover",avgturnover);
// now that we have a time series array for each symbol,
// use StaticVarGenerateRanks to generate the rank for each symbol
// I believe the way this works is StaticVarGenerateRanks reads all the static vars with that prefix name,
// sorts them, then generates a rank number and saves that to the output prefix + symbol
StaticVarGenerateRanks("rank_ibdrs","ibdrs",0,1224);
StaticVarGenerateRanks("rank_turnover","turnover",0,1224);
}
// Now that we are in open code, the first pass of the backtest engine loops over every symbol in the watchlist
symbol = Name();
ibdrs = StaticVarGet("ibdrs" + symbol);
check_ibdrs = 0.4 * ROC(C,63) + 0.2 * ROC(C,126) + 0.2 * ROC(C,189) + 0.2 * ROC(C,252);
rank_ibdrs = StaticVarGet("rank_ibdrs" + symbol);
turnover = StaticVarGet("turnover" + symbol);
check_turnover = MA(C*V,20);
rank_turnover = StaticVarGet("rank_turnover" + symbol);
avgturnover = StaticVarGet("avgturnover");
// pretend buy signals
Buy = rank_ibdrs < 3
AND rank_turnover < 10
AND turnover > avgturnover
;
// exploration code for debugging
Filter = 1;
//Filter = Buy;
AddColumn(ibdrs,"ibdrs");
AddColumn(check_ibdrs,"check_ibdrs");
AddColumn(rank_ibdrs,"rank_ibdrs");
AddColumn(turnover,"turnover");
AddColumn(check_turnover,"check_turnover");
AddColumn(rank_turnover,"rank_turnover");
AddColumn(avgturnover,"avgturnover"); // should be the same for all symbols for a given date
if (Status("Action") == actionExplore) SetSortColumns(2,5,8);