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.
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);
}
}
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.