Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

AFL Library

This is read-only version of AFL library entry. Ability to add user formulas and comment is only available from members-only area.

Details:

Formula name: Ranking and sorting stocks
Author/Uploader: Herman van en Bergen - (email hidden)
Date/Time added: 2005-03-01 06:33:36
Origin:
Keywords: Ranking Scoring Sorting
Level: semi-advanced
Flags: function

DISCLAIMER: Most formulas present in AFL on-line library are submitted by the users and are provided here on an "as is" and "as available" basis. AmiBroker.com makes no representations or warranties of any kind to the contents or the operation of material presented here. We do not maintain nor provide technical support for 3rd party formulas.
Description:

A function to rank stocks according to your scoring formula. The top-tickers can be listed on a chart for demo or the tickers/scores strings can be read by your code for other use. Slow on large watchlists!

Formula:

function GetScore( )
	{
//	return RSI();
	return C; 									// substitute C for easy verification
	}

function padString( InString, Length )
	{
	SpaceFill= "          ";
	SL 		= StrLen(InString);
	NS			= StrLeft(SpaceFill, Length-SL);
	return NS+Instring;
	}

procedure getPositionScores( WatchList, BarNum, NumTickers )
	{
	global toptickerlist, TickerList;
	TickerList		= GetCategorySymbols( categoryWatchlist, WatchList);

	// Create zero-based Scores array in wrt to current data
	for( n=0; (Ticker=StrExtract( TickerList, n)) != ""; n++)
		{
		SetForeign(Ticker);
		Temp = LastValue(GetScore());
		RestorePriceArrays();
		TickerScores[n] 	= Temp;
		TickerIndex[n] 	= n;
		}

	TickerCount= n;

	// Sort Tickers by rank
	for(i = n; i>=0; i--)
		{
		for (j = 1; j <= i; j++)
			{
			if (TickerScores[j-1] > TickerScores[j])
				{
				temp = TickerScores[j-1]; TickerScores[j-1] = TickerScores[j];
				TickerScores[j] = temp;
				temp = TickerIndex[j-1]; TickerIndex[j-1] = TickerIndex[j];
				TickerIndex[j] = temp;
				}
			}
		}

	TopTickerList  = TopTickerNum = TopTickerScore = Toptickerlist 	= "";

	if( NumTickers > TickerCount ) NumTickers = TickerCount;

	// Format Top Tickers for Indicator Title verification
	TickersCounted = 0; i=0; P=8; 
	for(n=TickerCount; n>=TickerCount-NumTickers; n--)
		{
		T5[i++] 		= TickerIndex[n];
		Ticker 		= StrExtract(Tickerlist,T5[i-1]);
		Score  		= NumToStr(TickerScores[n],1.3);
		TickerNum 	= NumToStr(TickerIndex[n],1.0);
		StaticVarSet(Ticker+"Score",TickerScores[n]);
		StaticVarSet(Ticker+"Number",TickerIndex[n]);
		StaticVarSet(Ticker+"Rank",n);
		TopTickerList 	= TopTickerList + PadString(Ticker,P)+",";
		TopTickerScore = TopTickerScore + PadString(Score,P)+",";
		TopTickerNum 	= TopTickerNum + PadString(TickerNum,P)+",";
		}

	StaticVarSetText("TopTickerList",TopTickerList);
	StaticVarSetText("TopTickerScore",TopTickerScore );
	StaticVarSetText("TopTickerNum ",TopTickerNum );
	}


BarNum = BarCount-1;
Watchlist = Param("Watchlist Number",0,0,100,1);
NumTickers = Param("Number Of symbols to show On the Chart",2,2,10,1);

getPositionScores( WatchList, BarNum, NumTickers );

TopTickerScore =StaticVarGetText("TopTickerScore");
TopTickerList	=StaticVarGetText("TopTickerList");
TopTickerNum 	=StaticVarGetText("TopTickerNum ");

Plot(C,"",1,128);

Tstr =
"\nWatchlist Number: "+NumToStr(WatchList,1.0)+
"\nPosition Scores: \n"+TopTickerList + "\n"+TopTickerScore;
Title = StrLeft(Tstr,1000);

Filter = Status("LastBarInRange");
SetOption("NoDefaultColumns",False);
AddColumn(getScore(),"Score",1.3);

Comments:

Marc Poussard
marcmeg [at] europe.com
2005-03-01 11:03:39
I am new to Amibroker and am trying to figure out where I would insert this formular into. I tried opening an edit formula window for an indicator but nothing happened. Would you mind walking me through some of the basics involved. Thanks
dave
david346 [at] yahoo.com
2005-03-06 18:19:54
hi,get a error message,
line 120 column 21
error # 7,
sub ,out of range.
can you
HELP me
thanks dave
Dennis

2005-09-16 02:25:21
It is great to see all the possibilities but could someone please make a simpler example?

Something like:

Find 10 stocks with highest closing price & add to watchlist #10.
Ralph

2006-11-29 02:37:53
You might like Tickerank.com too.
jim wiehe

2007-06-23 11:10:07
Multiple variables used without being initialized appears 7 times.
Budy

2007-12-07 17:02:16
Did anyone managed to run this code?
KH Tang

2008-01-13 21:36:29
This is possibly one of the Most Useful Tool that it can be found here...!!!
There is no error and can be use immediately.
As the Note in the GetScore Function hints that the return C is just for Easy Verification.
User would need to choose to change it back to RSI() or anything he like;
I would suggest to modify it to RSI()*ADX(), which will differentiate which stock has a strong trend. THANKS to Herman for your generousity and great job! Yes. There is MONEY inside!!!
KH Tang

2008-01-13 21:48:14
Oh! Sorry...
I means IFT_RSI*ADX() [not RSI()*ADX()]

function GetScore( )
{

// IFT_RSI
periodRSI=8;// 5
PeriodMA =9;// 9

PeriodRSI=Param("PeriodRSI",PeriodRSI,2,15,1);
PeriodMA =Param("PeriodMA",PeriodMA,2,20,1);

Value1 = 0.1 * ( RSI( PeriodRSI ) - 50 );
Value2 = WMA( Value1, PeriodMA );
e2y = exp( 2 * Value2 );
IFT_RSI=( e2y - 1 )/( e2y + 1 );

return IFT_RSI*ADX();
// return C; // substitute C for easy verification
}
m3trail
m3trail [at] yahoo.com
2008-05-28 15:09:03
"Multiple variables used without being initialized appears 7 times" same error for me

HELP KH Tang

Can you upload your complete formula
m3trail
m3trail [at] yahoo.com
2008-05-28 15:09:22
"Multiple variables used without being initialized appears 7 times" same error for me

HELP KH Tang

Can you upload your complete formula
Rene
rijnaa [at] live.nl
2008-07-12 01:56:35
Try to run your script which works well for about 85 tickers then i get a message

SetForeign(Ticker);

Temp = LastValue(GetScore());

RestorePriceArrays();

TickerScores[n]
----------------^

Error 10.
Subscript out of range.
You must not access array elements outside 0..(BarCount-1) range.

can you help me


About | Privacy | Terms of Use | Contact information
Copyright © 2001 AMIBROKER.COM