I have a working code to get annual relative strength ranking, i want to generate similar ranking for quarterly time frame.
When i explore, rank for quarterly column is blank.
Below is the code:
// 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
GetOption("PadAndAlignToReference");
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_*" );
StaticVarRemove( "QRS_*" );
// 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;
QRSraw = 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;
QRSraw = ThreeMthRS/2;
// Making changes to show RS performance of last 3 months only
//ThreeMthRS = (C /Ref(C,-63));
//RSraw = ThreeMthRS ;
RestorePriceArrays();
StaticVarSet("RSraw_" + Symbol, RSraw);
StaticVarSet("QRSraw_" + Symbol, QRSraw);
}
// rank the stocks using this extremely useful function!
StaticVarGenerateRanks("Rank", "RSraw_", 0, 1234);
StaticVarGenerateRanks("QRank", "QRSraw_", 0, 1234);
for( n = 0; ( Symbol = StrExtract( List, n ) ) != ""; n++ )
{
Rank = StaticVarGet( "RankRSraw_" + Symbol );
RSpctile = 100 - 100 * Rank / ListQty;
StaticVarSet( "RS_" + Symbol, RSpctile, True );
QRank = StaticVarGet( "RankQRSraw_" + Symbol );
QRSpctile = 100 - 100 * QRank / ListQty;
StaticVarSet( "QRS_" + Symbol, QRSpctile, True );
// use this opportunity to store the highest ranking stocks in a watchlist.
//if( LastValue( RSpctile ) >= 60 )
// CategoryAddSymbol( Symbol, categoryWatchlist, 35 );
//else
// CategoryRemoveSymbol( Symbol, categoryWatchlist, 35 );
}
}
Filter = 1;
Rank = StaticVarGet ("RS_" + Name());
QRank = StaticVarGet ("QRS_" + Name());
// exploration code for verification
//AddColumn(Close, "Close", 1.2);
//AddColumn(Volume, "Volume", 1.0);
AddColumn(Rank, "Rank",1);
AddColumn(QRank, "QRank",1);
SetSortColumns(-3);
StaticVarRemove("RSraw_*");
StaticVarRemove("RankRSraw*_");
StaticVarRemove("QRSraw_*");
StaticVarRemove("QRankRSraw*_");