Displaying multiple stocks' rank in "Explore"

Hello everyone. I am doing a portfolio backtest and would like to rank the top 10 stocks and rotationally trade them. I try to display the ranking of these stocks in Explore but turned out I found that no rank is generated. May I ask where went wrong in the codes? Thanks.

SetPositionSize(100,spsPercentOfEquity);
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "ActivateStopsImmediately", True );
SetOption( "AllowSameBarExit", False); 
SetOption("MaxOpenPositions",10);
SetOption("AllowPositionShrinking", True);

symlist = "AAPL US Equity,ADBE US Equity,AMZN US Equity,BRO US Equity,AON US Equity,AMAT US Equity,ACN US Equity,AVGO US Equity,AMD US Equity,AJG US Equity";

// delete static variables - DO NOT forget the asterisk (wildcard) at the end
StaticVarRemove( "ValuesToSort*" );
StaticVarRemove( "rank*" );
StaticVarRemove( "top*" );

for( i = 0; ( sym = StrExtract(symlist,i)) != ""; i++)
{
SetForeign(sym );
Value =  (MA(C,2) - MA(C,150)) / MA(C,150);
RestorePriceArrays();
StaticVarSet( "ValuesToSort" + sym, Value );
AddColumn(StaticVarGet( "ValuesToSort" + sym ),"ValuesToSort" + sym);
}

//perform ranking
StaticVarGenerateRanks( "top", "ValuesToSort", 10, 1234 ); // normal rank mode


// read ranking
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{

	Plot( StaticVarGet( "topValuesToSort" + sym ), sym, colorCustom10 + i );
	AddColumn(StaticVarGet( "topValuesToSort" + sym ),"top" + sym);
}

Below is the attached picture as the empty Explore I generated.

TopRank

You forgot Exploration Filter.
Then wrong call of Static Var name.
You forgot StaticVarGetRankedSymbols to output top list.
Also you should run on single symbol.

NOTE: Top mode returns list at single date!

SetPositionSize(100,spsPercentOfEquity);
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "ActivateStopsImmediately", True );
SetOption( "AllowSameBarExit", False); 
SetOption("MaxOpenPositions",10);
SetOption("AllowPositionShrinking", True);

symlist = "AAPL US Equity,ADBE US Equity,AMZN US Equity,BRO US Equity,AON US Equity,AMAT US Equity,ACN US Equity,AVGO US Equity,AMD US Equity,AJG US Equity";

sdt = SelectedValue( DateTime() );

Filter = DateTimeDiff(sdt, DateTime()) == 0;

// delete static variables - DO NOT forget the asterisk (wildcard) at the end
if ( Status( "stocknum") == 0 ) {
	StaticVarRemove( "ValuesToSort*" );
	StaticVarRemove( "rank*" );
	StaticVarRemove( "top*" );

	for( i = 0; ( sym = StrExtract(symlist,i)) != ""; i++)
	{
		SetForeign(sym );
		Value =  (MA(C,2) - MA(C,150)) / MA(C,150);
		RestorePriceArrays();
		StaticVarSet( "ValuesToSort" + sym, Value );
		//AddColumn(StaticVarGet( "ValuesToSort" + sym ),"ValuesToSort" + sym);
	}

	//perform ranking
	StaticVarGenerateRanks( "top", "ValuesToSort", 10, 1234 ); // normal rank mode
	
	symlist = StaticVarGetRankedSymbols("top", "ValuesToSort", sdt);

	// read ranking
	for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
	{
		Plot( StaticVarGet( "ValuesToSort" + sym ), sym, colorCustom10 + i );
		AddColumn(StaticVarGet( "ValuesToSort" + sym ),"top" + sym, 1.2);
	}
}
1 Like

Thank you fxshart. I amended the codes a bit as I tried to display the exact top 10 ranking instead of the criteria value.

	// read ranking
	for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
	{
		Plot( StaticVarGet( "ValuesToSort" + sym ), sym, colorCustom10 + i );
		//AddColumn(StaticVarGet( "ValuesToSort" + sym ),"top" + sym, 1.2);
		AddColumn(StaticVarGet( "topValuesToSort" + sym ),"top" + sym, 1.2);
	}
}

However the ranking in Explore is still empty. If I understand correctly, I already got the "topValuesToSort" prefix + suffix in order to get the exact ranking of the static variable for each stock.

You should not amend but use exact code I posted.
What you do is once again incorrect.

Re-read my post slowly!

This is not correct.

Name(s) would be StaticVarGet( "topValuesToSort1" ), StaticVarGet( "topValuesToSort2" ), ... , StaticVarGet( "topValuesToSort10" )

// INCOMPLETE Snippet
sdt = SelectedValue( DateTime() );
topranked= StaticVarGetRankedSymbols("top", "ValuesToSort", sdt);

for ( i = 0; ( sym = StrExtract( topranked, i ) ) != ""; i++ )
{
	Plot( StaticVarGet( "topValuesToSort"+(i+1) ), "rank"+(i+1)+" "+sym, colorCustom10 + i );
	AddColumn(StaticVarGet( "topValuesToSort"+(i+1) ),"top" + sym, 1.2);
}

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