Ranking by volume

Hi!

I have code that ranks all the symbols in my DB according to volume for each day. It looks like everything is working fine, but when I look at the result list, there are always some positions in the ranking that are missing and I just can't figure out why.

Here is the code:


List = CategoryGetSymbols( categoryAll, 0 ) ;

symbol = "";

if( Status( "stocknum" ) == 0 )
{
    StaticVarRemove( "rank*" );
    StaticVarRemove( "values*" );

    for( n = 0; ( symbol = StrExtract( List, n ) ) != "";  n++ ) {
        SetForeign( symbol );
        value = MA( Ref(Volume, -1), 20 );
        RestorePriceArrays();
        StaticVarSet( "value" + symbol, value );        // write ranked values to a static variable
    }

	StaticVarGenerateRanks( "rank", "value", 0, 1224 ); 
}

symbol = Name();

values = StaticVarGet( "value" + symbol );
rank = StaticVarGet( "rank" + "value" + symbol );

AddColumn( values, "Value" );
AddColumn( rank, "rank" );

Filter = rank <= 10000;

if( Status( "Action" ) == actionExplore ) SetSortColumns( 4, 2 );

And here is a screenshot of the exploration output. As you can see, lots of positions in the list are missing, for example #1 to #4, #14 etc. Can anyone help me understand why some tickers appear missing?

Thanks!

Untitled

@Muzzle, maybe some of your tickers, for the selected date, do not have valid quotes.

Try setting "Pad and align" in the Analysis settings" dialog (as a reference, please, select a symbol that has no data holes).

image

2 Likes

@Muzzle, in addition to what @beppe suggested. Your ranking is messed up because,

Should be

StaticVarRemove( "value*" );

Also using SetForeign inside loop is not correct use (as already mentioned many times on this forum).

See create-custom-index-from-specified-tickers.

Search forum for other examples if need be.

4 Likes

The expression can be priceless when one realizes that a "typo" is the source of the problem :smiley:

1 Like

Thank you! That was exactly the problem :smiley:

@Muzzle, in any case, please, also fix your code as suggested by @TrendSurfer to clear the (previously used) static variables.

In your case, repeating the exploration using "all symbols" will simply overwrite previous static variables (never deallocating them - a waste of memory).
In other situations, doing the same exploration using different watchlists, without properly clearing the static vars that you plan to reuse, could lead to some weird results.

1 Like

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