How to filter some stocks in a ranking

Good morning,

I have been looking for the answer but I don't find the correct key words that could solve what I want to do.

I'd be glad if someone can point where to look.

I want to create a list to be ranked for each ICB. The purpose of this ranking is to use it as an indicator.

But I want to avoid certain symbols of the list like those delisted stocks or those that don't fullfill some conditions.

This is the code I'm using

_SECTION_BEGIN("RSPercentile%2");
RestorePriceArrays();
period = Param("Periodo", 52, 1, 500, 1);
listIcb = CategoryGetSymbols(categoryICB, StrToNum(IcbID(0)));

// Create the full list of stocks to be ranked
StaticRank=0;
List    = listIcb;
ListQty = StrCount(List, ",") + 1;
RSpctile2=0;
if (ListQty<1000 AND StrToNum(IcbID(0))!=0)
{
if ( Status( "stocknum" ) == 0 OR Status("stocknum") == -1 )

{

    StaticVarRemove( "RSC*"    );
    StaticVarRemove( "RSSig*"    );
	StaticVarRemove( "Rank_*"   );
	StaticVarRemove( "TOPRank_*"   );
     
    for ( i = 0; ( symbol = StrExtract( List, i ) )  != "";  i++ )
    {    
	SetForeign ( symbol );
       
        period = Param("Periodo", 52, 1, 500, 1);
		rsSymbol = ParamStr( "Base Index Symbol", "^SPX");

		Cociente = C / Foreign(rsSymbol, "C");
		CountR = Sum(Cociente , period);
		Baseprice = CountR / period ;
		RSC = ((Cociente / Baseprice ) - 1)*10;
		
		Cond   = !IsNull(GetFnData("DelistingDate")) AND (BarIndex() >= (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData("DelistingDate") );
		
		RSCFiltered = IIf(Cond,RSC,-1000);
       
        RestorePriceArrays();
        StaticVarSet( "RSC" + symbol, RSCFiltered  );
    }  
StaticVarGenerateRanks( "Rank_", "RSC", 0, 1224 );
StaticVarGenerateRanks( "TOPRank_", "RSC", 20, 1224 ); // top-N mode
for ( i = 0; ( symbol = StrExtract( List, i ) )  != "";  i++ )
 {
 //Plot( StaticVarGet( "Rank_RSC" + symbol ), symbol, colorCustom10 + i );
 }

 sdt = SelectedValue( DateTime() );

Title = "{{NAME}} -{{DATE}} - {{VALUES}} > > TOP RANK > > " + StaticVarGetRankedSymbols( "TOPRank_", "RSC", sdt );
}
RestorePriceArrays();
symbol= Name();
 
//staticRSC= StaticVarGet( "RSC"    + symbol);
staticRank= StaticVarGet( "Rank_RSC" + symbol);

RSpctile2 = 100*(1-(staticRank-1)/(ListQty));

Plot( ListQty+1-staticRank, "Ranking", IIf(rspctile2<20, colorRed,IIf(rspctile2>70,colorGreen,colorOrange)), styleLine + styleThick| styleNoLabel | styleOwnScale);
Plot( ListQty, "de", colorOrange, styleLine | stylenoLine | stylenolabel | styleOwnScale);
Plot( RSpctile2, "%Percentil", IIf(rspctile2<20, colorRed,IIf(rspctile2>70,colorGreen,colorOrange)), styleDots+stylethick | styleNoLine | stylenolabel| styleOwnScale );
}

Thanks in advance

Put unwanted symbols into watch list and use "Exclude" filter in Analysis settings.
It is important to understand that time consuming operations such as ranking/foreign should NOT be placed in chart code, only in Analysis and run once, not with every chart refresh.

2 Likes

Thanks for your answer, I won't use it as an indicator.

Anyway and to improve my programming skills, would it be possible to exclude that new Watchlist from my code? I'm doing something wrong as my Cond is not doing what I thought it would do

Thanks

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