Ranking not sequential when switching to different watchlist

Hi all,
I have encountered an unusual problem with the ranking output column from an Exploration. Everything is okay with the ranking for the initial Exploration:
Screen Shot 2020-09-15 at 3.02.42 PM
The ranked column is correctly sequentially numbered 1-20. However, switching to a different watchlist alters the output in this column such that the numbers are NOT sequential:
Screen Shot 2020-09-15 at 3.03.54 PM
The values are properly in descending order but the numbers in the rank column contain skips so that the lowest rank is number 31 instead of 16.
The issues resolves itself with an Amibroker restart but only for the initially selected watchlist; switching to a different one causes the rank column to go a bit wacko.
I have included the full code below and would appreciate it if someone could help me to understand what is causing this anomaly.

_SECTION_BEGIN("btSlopeRsTest");

//===== Detect Watchlist ==============
wlnumber=GetOption("FilterIncludeWatchlist");
watchlist=GetCategorySymbols(categoryWatchlist,wlnumber);
NumberOfSymbols=StrCount(watchlist,",") + 1;
NoS=NumberOfSymbols;
//===== Parameter Inputs ==================================
sloFactor=ParamList("SlopeFactor","0.20|0.40|0.60|0.80|1.00",2);
sloFactor=StrToNum(sloFactor);
//colorMode=ParamList("ColorMode","Dark|Light",0);
///////////////////////////////////////////////////////////
//                  Symbol Ranking                       //
///////////////////////////////////////////////////////////
if (Status("stocknum")==0)
{
    StaticVarRemove("sum*");	StaticVarRemove("ret*");        
    StaticVarRemove("si*");		StaticVarRemove("ri*");      
   	StaticVarRemove("Rank*");
    for (i=0; (symbol=StrExtract(watchlist,i)) != ""; i++)
    {
        SetForeign (symbol);
			//===== Return of si ==========================		
			si=LinRegSlope(Close,89)*100;
			//===== Return of ri ==========================
			ri=Nz(-1+Close/Ref(Close,-144))*100;
			ri=EMA(ri,3);
			//===== Return of zi ==========================
			zi=(si * sloFactor) + (ri * (1.0-sloFactor));
        RestorePriceArrays();	
        StaticVarSet("si"+symbol,si);
        StaticVarSet("ri"+symbol,ri);
        StaticVarSet("zi"+symbol,zi);	
    }  	
	///////////////////////////////////////////////////////
	//                Generate Ranks                     //
    ///////////////////////////////////////////////////////
    StaticVarGenerateRanks("Rank","si",0,1224);
    StaticVarGenerateRanks("Rank","ri",0,1224);
    StaticVarGenerateRanks("Rank","zi",0,1224);
}
///////////////////////////////////////////////////////////
//                 Get Static Variables                  //
///////////////////////////////////////////////////////////
symbol=Name();
si=StaticVarGet("si"+symbol);	RankSi=StaticVarGet("RankSi"+symbol);
ri=StaticVarGet("ri"+symbol);	RankRi=StaticVarGet("RankRi"+symbol);
zi=StaticVarGet("zi"+symbol);	RankZi=StaticVarGet("RankZi"+symbol);
///////////////////////////////////////////////////////////
//           Specify Columns for Exploration             //
///////////////////////////////////////////////////////////
Filter = 1;
if ( Status( "actionex" ) == actionExplore ) 
{
	fgColor=ColorRGB(50,50,50);
	siColor=IIf(si>0,ColorRGB(123,177,117),colorRGB(217,175,182));	
	riColor=IIf(ri>0,ColorRGB(107,179,140),ColorRGB(201,122,140));
	ziColor=IIf(zi>0,colorSeaGreen,ColorCustom1);
///////////////////////////////////////////////////////////
SetOption("NoDefaultColumns",True);
siNegColor=
	IIf(si>0,ColorRGB(123,177,117),colorRGB(217,175,182));	
riNegColor=
	IIf(ri>0,ColorRGB(107,179,140),ColorRGB(201,122,140));
ziNegColor=
	IIf(zi>0,ColorRGB(107,179,140),ColorRGB(201,122,140));
SetSortColumns(-7);
AddTextColumn(Name(),"TestRank",1.0,-1,-1,55);
AddColumn(DateTime()," ",formatDateTime,-1,-1,85);
AddColumn(si,"(Slope)",1.4,fgColor,siColor,60);
AddColumn(RankSi," ",1.0,ColorDefault,colorDefault,25);
AddColumn(ri,"(RS  )",1.4,fgColor,riColor,60);
AddColumn(RankRi," ",1.0,colorDefault,colorDefault,25);
AddColumn(zi,"(zi)",1.4,fgColor,ziColor,60);
AddColumn(RankZi," ",1.0,colorDefault,colorDefault,25);
AddTextColumn("",".",1.0,colorDefault,colorGrey50,3);
AddTextColumn(FullName(),"Full Name",0,-1,-1,290);
}
_SECTION_END();

FYI, you have a static variable with name "zi"+symbol that you do not clear before new run.
Instead you have old unrelated "zombie" code.

15

3 Likes

Oh my gosh fxshrat, I missed something so basic! Thank you for pointing out my error that, once corrected, completely solved my issue and provided me with a good coding lesson.

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