Rank of Rank with two pass loop?

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );  

if ( Status("stocknum") == 0 ) // Generate ranking when we are on the very first symbol
{
     StaticVarRemove( "values*" );

     for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
     {
         SetForeign ( symbol );
        
         // first criteria used for scoring
         values1 = 1000 + Criteria1  ;
         
        
         // the other criteria used for scoring,
         values2 =  1000 - criteria2;
         
         values3 = 1000 - criteria3;
         
         
         
          inIndex = NorgateIndexConstituentTimeSeries(tkindex);
          RestorePriceArrays();
          
          // here we write static variables holding the values
         StaticVarSet (  "values1"  +  symbol, IIf(inIndex,values1,-1e9));
         StaticVarSet (  "values2"  +  symbol, IIf(inIndex,values2,-1e9));
         StaticVarSet (  "values3"  +  symbol, IIf(inIndex,values3,-1e9));
        
     }

     StaticVarGenerateRanks( "rank", "values1", 0, 1234 );
     StaticVarGenerateRanks( "rank", "values2", 0, 1234 );
     StaticVarGenerateRanks( "rank", "values3", 0, 1234 );
     
}

        rank1 = StaticVarGet ( "rankvalues1" +  symbol );
		rank2 = StaticVarGet ( "rankvalues2" +  symbol );
		rank3 = StaticVarGet ( "rankvalues3" +  symbol );
        ranktemp = 0.33 * rank1 + 0.33 * rank2 + 0.33 * rank3;

My objective is to normalize ranktemp so that the first stock (according to criteria) has rank 1.

Is correct to use two loop (as in the following code) or does exist a better code?

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum );  //50
//SetOption("MaxOpenPositions", 3);

if ( Status("stocknum") == 0 ) // Generate ranking when we are on the very first symbol
{
     StaticVarRemove( "values*" );

     for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
     {
         SetForeign ( symbol );
        
         // first criteria used for scoring
         values1 = 1000 + Criteria1  ;
         
        
         // the other criteria used for scoring,
         values2 =  1000 - criteria2;
         
         values3 = 1000 - criteria3;
         
         
         
          inIndex = NorgateIndexConstituentTimeSeries(tkindex);
          RestorePriceArrays();
          
          // here we write static variables holding the values
         StaticVarSet (  "values1"  +  symbol, IIf(inIndex,values1,-1e9));
         StaticVarSet (  "values2"  +  symbol, IIf(inIndex,values2,-1e9));
         StaticVarSet (  "values3"  +  symbol, IIf(inIndex,values3,-1e9));
        
     }

     StaticVarGenerateRanks( "rank", "values1", 0, 1234 );
     StaticVarGenerateRanks( "rank", "values2", 0, 1234 );
     StaticVarGenerateRanks( "rank", "values3", 0, 1234 );
     
}


if ( Status("stocknum") == 0 ) // Generate ranking when we are on the very first symbol
{
     //StaticVarRemove( "values*" );

     for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
     {
         SetForeign ( symbol );
        
        rank1 = StaticVarGet ( "rankvalues1" +  symbol );
		rank2 = StaticVarGet ( "rankvalues2" +  symbol );
		rank3 = StaticVarGet ( "rankvalues3" +  symbol );
        ranktemp = 0.33 * rank1 + 0.33 * rank2 + 0.33 * rank3; 
         
          inIndex = NorgateIndexConstituentTimeSeries(tkindex);
          RestorePriceArrays();
          
          // here we write static variables holding the values
         StaticVarSet (  "ranktemp"  +  symbol, IIf(inIndex,1000/ranktemp,-1e9));
         
     }

     StaticVarGenerateRanks( "rank", "ranktemp", 0, 1234 );

     
}

Please can I solicit confirmation of the correctness of the second script posted above? The one with the double loop?
This solution is very useful when you have a composite rank and you want use BUY | SELL structure instead of the rotational one.
Thank you

Keep in mind that NON-rotational mode support PositionScore TOO. Scoring and ranking is NOT limited to rotational trading. http://www.amibroker.com/guide/h_ranking.html

Thank you Tomasz.
Yes i know that ranking is not limited to rotational trading.
I just wanted to be sure that double looping was not a blatantly wrong practice.

Suppose I want to use the staticvar technique for ranking and at the same time want to buy a maximum of 20 stocks according to my multiple criteria.
I can do it using the condition BUY = rank <20;
For this to make sense, the rank vector must have a cardinal structure: rank1 = best stock based on my aggregate criteria, rank2 = second best stock, etc.

Therefore my question is: is the practice of the double loop correct?

You can rank as many times as you won't. The only problem wit your second loop is that you are calling SetForeign for NO REASON.
You are NOT accessing prices, just static variables. Static variables work regardless of what symbol is selected so you can just REMOVE SetForeign/RestorePriceArrays from second loop.

1 Like

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