Using function StaticVarGenerateRanks

I am trying to learn to use the function “StaticVarGenerateRanks”. I used the template from manual and add “for” loop to generate the following simple afl. I used DJ30 watchlist to test the script. The result tells that there is error in the script. Can someone please help me to figure out where is the problem?


wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
Prd=Param("Prdiods",65,5,260,5);

if ( Status("stocknum") == 0 ) // GENERATE RANKING WHEN WE ARE ON VERY FIRST SYMBOL
{
    StaticVarRemove( "values*" );
	for (i=0; i<=13; i++) 
	{   
		for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
		{
			SetForeign ( symbol );
			values = Ref(C, -Prd) ;
			RestorePriceArrays();
			StaticVarSet ("values"+symbol+i, values );
		}
		StaticVarGenerateRanks( "rank", "values"+symbol+i, 0, 1224 );
	}
}
symbol = Name();
for (i=0; i<=13; i++) 
{   
	rank = StaticVarGet ( "rankvalues"+symbol+i );
	AddColumn ( rank, "rank"+i, 1.0, colorDefault, colorDefault, 60 );
}
Filter = 1;
type or paste code here

image

Why are you adding the extra for loop?

Why not just follow the examples in the User's Guide?

StaticVarGenerateRanks - Examples.

I am sorry that there is some mistake in my original post.
the corrected one is as below. You will notice that my purpose of "for" loop is to calculate the same factor (C in here. it can be others like ROC, RSI etc) for value 5, 10, 15 ... days ago.

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
if ( Status("stocknum") == 0 ) // GENERATE RANKING WHEN WE ARE ON VERY FIRST SYMBOL
{
    StaticVarRemove( "values*" );
	for (i=0; i<=13; i++) 
	{   
		for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
		{
			SetForeign ( symbol );
			prd = 5*i ;
			values = Ref(C, -Prd) ;
			RestorePriceArrays();
			StaticVarSet ("values"+symbol+i, values );
		}
		StaticVarGenerateRanks( "rank", "values"+symbol+i, 0, 1224 );
	}
}
symbol = Name();
for (i=0; i<=13; i++) 
{   
	rank = StaticVarGet ( "rankvalues"+symbol+i );
	AddColumn ( rank, "Rank"+i, 1.0, colorDefault, colorDefault, 60 );
}
Filter = 1;type or paste code here

image

Your code is very inefficient, but that isn't what's causing your bad output. There are (at least) two things that you need to fix:

  1. You should not include the symbol when you call StaticVarGenerateRanks. Please review the documentation.
  2. The value of your variable "i" should come immediately after "values", not after the symbol name. The symbol name needs to be the LAST thing in the static variable name.

So when you call StaticVarSet, do something like this:

StaticVarSet ("values"+i+symbol, values );

And when you call StaticVarGenerateRanks, like this:

StaticVarGenerateRanks( "rank", "values"+i, 0, 1224 );
3 Likes

Thank you very much for the replay. This help.
PS. Sorry for the delayed response. I was traveling and away from internet.

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