Display unique rank

I am using the below formula to generate relative strength ranking similar to Investors Business Daily. I am seeing many stocks have same rank, is there a way i can ensure only one number is assigned to one stock?


I can see that the ranking part is coming from the below snippet but i do not know what i can do to get my required outcome.

	for (n = 0; (Symbol = StrExtract(List, n))  != "";  n++) 
		Rank  = StaticVarGet ("RankRSraw_" +  Symbol); 
		RSpctile = 100 - 100*Rank/ListQty;
		StaticVarSet("RS_" + Symbol, RSpctile, True);

Complete AFL

// https://forum.amibroker.com/t/how-to-imitate-ibd-relative-strength-percentile-ranking-of-stocks/6068/15
// Relative Strength ranking of stocks similar to Marketsmith India
// watchlist should contain all symbols included in the test

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
ListQty = StrCount(List, ",") + 1; 
// Determine the quantity of stocks to rank
StaticVarSet("UniListTotal", ListQty, True);
if( Status( "stocknum" ) == 0 )
	// Clear the static vars from last run
	StaticVarRemove( "RS_*" );

	// Generate the raw RS score for every stock and store in a static var
	for (n = 0; (Symbol = StrExtract(List, n)) != "";  n++) 
		SetForeign (Symbol,0); // Restoring SetForeign as without it, all stocks are getting an RS of 100.
		RSraw = 0;
		// relative strength IBD style
		ThreeMthRS  = 2*(C /Ref(C,-63));
		SixMthRS    = (C /Ref(C,-126));
		NineMthRS   = (C /Ref(C,-189));
		TwelveMthRS = (C /Ref(C,-252));
		RSraw = ThreeMthRS + SixMthRS + NineMthRS + TwelveMthRS; 
		StaticVarSet("RSraw_"  +  Symbol, RSraw); 

	// rank the stocks using this extremely useful function!
	StaticVarGenerateRanks("Rank", "RSraw_", 0, 1224); 

	for (n = 0; (Symbol = StrExtract(List, n))  != "";  n++) 
		Rank  = StaticVarGet ("RankRSraw_" +  Symbol); 
		RSpctile = 100 - 100*Rank/ListQty;
		StaticVarSet("RS_" + Symbol, RSpctile, True);


Filter = 1;
Rank  = StaticVarGet ("RS_" +  Name()); 

// exploration code for verification
//AddColumn(Close, "Close", 1.2);
//AddColumn(Volume, "Volume", 1.0);
AddColumn(Rank, "Rank",1);

Read the manual:

and change the TieMode parameter as documented from 1224 to 1234.

Fourth argument (tiemode) defines how ties are ranked. Supported modes are 1234 and 1224. In 1224 mode ties are numbered with equal rank.

Also you don't really need to use StaticVarGenerateRanks for such simple one column ranking.

If you read the manual on Ranking functionality

you would find that you could use AddRankColumn instead AFL Function Reference - ADDRANKCOLUMN


@erukumk if I understand your concern correctly, multiple names sharing the same relative strength (if indeed similar to IBD) is not unusual. If you look through IBD you will see that many of them share the same rankings. One other thing I might mention, not that it is really that big of deal, is that I have never seen a ticker get a 100 on IBD's relative strength. The highest I have seen is 99. Whether it is possible to get a 100 or not I cant say but what little I do know about it, I don't think so. Overall, I think what you have is fine. What I would do is compare your ratings with that of IBD. I think that would make the most sense.

1 Like

@MCassICT all your observations are true. I take the exploration results to IBD for further analysis.

My concern that forced me to take a relook at the code was, when I initially adopted the code, there was not much difference in value on IBD and from AFL, now I see as much difference as 10 so I wondered if enforcing unique ranking will bring the score closer to IBD score.

Will try the suggested solution to see if there is any improvement.

1 Like

@erukumk do you by chance subscribe to IBD digital? There are some great post on this forum of a couple ideas people have used to scrape the IBD data into Amibroker. @rocketpower has done an amazing job coming up with a way to do that using their eTables and then another user took that idea and does it using the IBD screener. Ive used @rocketpower's to build out mine. Check out my interpretation panel and exploration.....

Screen Shot 2022-08-09 at 8.50.28 PM


It is exhaustive, i love the information that is being shown here. I trade Indian stocks and have Marketsmith India membership, and the information and screeners are not as exhaustive as IBD in USA.

I would love to do all the analysis from AmiBroker itself, where do i begin for scraping and Interpretation?

I would start here.... Using eTables with Amibroker - #6 by rocketPower


Also, if you insist on calculating RS yourself, I also have a post on that here.



Thank you for sharing the afl @rocketPower, i have been using the same afl for my calculations.


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