Hello,
I search the forum and tried several code that have similar problem with timeframeset + rank function like this link for instance Help for using TimeFrameSet with Static Variables
but find that my stock rank is still change in each candle in daily timeframe, it should changed after the month change in daily timeframe
Here is my attempt based on fxshrat code , can someone direct me where is the wrong is?
`// Relative Strength ranking of stocks
// This code must be run in Analysis Engine with Monthly timeframe.
// 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_*" );
StaticVarRemove("RSraw_*");
StaticVarRemove("RankRSraw*_");
// 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);
RSraw = 0;
TimeFrameSet(inMonthly);
// relative strength IBD style
ThreeMthRS = 0.4*(C/Ref(C,-3));
SixMthRS = 0.2*(C/Ref(C,-6));
NineMthRS = 0.2*(C/Ref(C,-9));
TwelveMthRS = 0.2*(C/Ref(C,-12));
RSraw = ThreeMthRS + SixMthRS + NineMthRS + TwelveMthRS;
TimeFrameRestore();
RSraw = TimeFrameExpand(RSraw, inMonthly, expandfirst);
RestorePriceArrays();
StaticVarSet("RSraw_" + Symbol, RSraw);
}
// rank the stocks using this extremely useful function!
StaticVarGenerateRanks("Rank", "RSraw_", 0, 1224);
/**** If you want to automaticlly stores the result in watch list, uncomment the following lines. ***/
//Create result watchlist based on market of first symbol on list. Default is US market
//firstSym=StrExtract(List, 0);
//marketSym=StrExtract(firstSym, 1, '.');
//resultwl = -1;
//if ((marketSym == "SZ") OR (marketSym == "SS"))
// resultwl = CategoryCreate( "Top Rank China Stocks", categoryWatchlist );
//else if (marketSym == "HK")
// resultwl = CategoryCreate( "Top Rank HK Stocks", categoryWatchlist );
//else
// resultwl = CategoryCreate( "Top Rank US Stocks", categoryWatchlist );
// Convert the static var ranks generated into a percentile score.
for (n = 0; (Symbol = StrExtract(List, n)) != ""; n++)
{
Rank = StaticVarGet ("RankRSraw_" + Symbol);
TimeFrameSet(inMonthly);
RSpctile = 100 - 100*Rank/ListQty;
TimeFrameRestore();
RSpctile = TimeFrameExpand(RSpctile, inMonthly, expandfirst);
StaticVarSet("RS_" + Symbol, RSpctile, True);
/**** If you want to automaticlly stores the result in watch list, uncomment the following lines. ***/
// use this opportunity to store the highest ranking stocks in a watchlist.
//if (LastValue(RSpctile) >= 95)
// CategoryAddSymbol(Symbol, categoryWatchlist, resultwl);
//else
// CategoryRemoveSymbol(Symbol, categoryWatchlist, resultwl);
}
}
Rank = StaticVarGet ("RS_" + Name());
function GetRS(Symbol)
{
// Retrieve relative strength static var
return StaticVarGet ( "RS_" + Symbol );
}
Filter= 1;
AddColumn( Close, "Close" );
rank = PercentRank( Close, 100 );
Color = ColorHSB( rank * 64/100, 255, 255 );
AddColumn( rank, "100-day percent rank", 1.2, colorDefault, Color, -1, rank );
if( Status( "Action" ) == actionExplore ) SetSortColumns(-4,1);
sdt = SelectedValue( DateTime() );
Title = "{{NAME}} -{{DATE}} - {{VALUES}} TOP: " + Rank+
" BOT: " + StaticVarGetRankedSymbols( "bot", "ValuesToSort", sdt ) ;
GfxSetOverlayMode(0);
GfxSetZOrder(-4);
GfxSelectFont("Tahoma", Status("pxheight")/5 );
GfxSetTextAlign( 6 );// center alignment
GfxSetTextColor( ColorRGB( 200, 200, 200 ) );
GfxSetBkMode(1); // transparent
GfxTextOut( WriteVal(rank,1,1), Status("pxwidth")/2, Status("pxheight")/12 );`