Pass the top 10 stocks from explore to the chart title

Hello,
I need to pass the top 10 stocks from explore to the chart title.

Yesterday @fxshrat gave me a solution for another similar problem was for the sectors of the SP500:

RSC__10 = LastValue (StaticVarGet ("RSCM" + "SPNY"));
RSC__15 = LastValue (StaticVarGet ("RSCM" + "SPLRCM"));
RSC__20 = LastValue (StaticVarGet ("RSCM" + "SPLRCI"));

Rank__10 = LastValue (StaticVarGet ("RankRSCM" + "RSCM" + "SPNY"));
Rank__15 = LastValue (StaticVarGet ("RankRSCM" + "RSCM" + "SPLRCM"));
Rank__20 = LastValue (StaticVarGet ("RankRSCM" + "RSCM" + "SPLRCI"));

This works fine for me, but now I only need the top 10.
What I want is to see the result of the explore (10 best stocks of the 500 of the SP500) in the title of the chart.

Thanks for your help,
Carlos

ListWL_SP500 = CategoryGetSymbols( categoryWatchlist, 8 ); // Sotcks SP500
numtickers = 1 + StrCount( ListWL_SP500, "," ); 

// Asignando Market Cap TOTAL SP500
if( Status( "stocknum" ) == 0 )
{
    // cleanup variables created in previous runs
    StaticVarRemove( "~SymbolCount*" );
	StaticVarRemove( "~AboveALL*" );
    StaticVarRemove( "values*" ); 

    for( n = 0; ( Symbol = StrExtract( ListWL_SP500, n ) )  != "";  n++ )
    {
        SetForeign( symbol );
        AboveALL = GetFnData("SharesOut")*C ;
        StaticVarAdd( "~SymbolCount", 1 );
        StaticVarAdd( "~AboveALL", AboveALL );

        valuesMC = Close * GetFnData( "SharesOut" );

        RestorePriceArrays();
        
        StaticVarSet (  "valuesMC"  +  symbol, valuesMC ); 

    }
    
       StaticVarRemove( "values*" ); 

    for ( n = 0; ( Symbol = StrExtract( ListWL_SP500, n ) )  != "";  n++    ) 
    { 
        SetForeign ( symbol ); 
        values = Close * GetFnData( "SharesOut" ) / StaticVarGet( "~AboveALL" );
        RestorePriceArrays(); 

        StaticVarSet (  "values"  +  symbol, values ); 
    } 

    StaticVarGenerateRanks( "rank", "values", 0, 1224 ); 
}

symbol = Name(); 

MarCap = StaticVarGet ( "valuesMC" + symbol ); 
Peso = StaticVarGet ( "values" + symbol ); 
rank = StaticVarGet ( "rankvalues" + symbol ); 

MaxStock = Param("Máx. x Capitalización 1-10",10,1,10,1);

if ( GetOption("ApplyTo") == 2 ) {

Filter = rank <= MaxStock; // 1;
SetOption("NoDefaultColumns", True ); 
SetSortColumns( 6 );

AddColumn( DateTime(), "Date", formatDateTime );
AddTextColumn(Name(),"Ticker");
AddTextColumn(FullName(),"Nombre");
AddColumn ( Peso*100, "Peso %", 1.2 ); 
AddColumn ( Close * GetFnData( "SharesOut" ), "MarketCap", 1.0 ); 
AddColumn ( rank, "Rank Peso", 1.0, colorWhite, colorLightBlue); 


} else 
	if ( Status( "action" ) == actionExplore )
		Error("Seleccionar en 'Apply to' --> *Filter --> WatchList: SP500");


// Buscamos los 10 mejores stocks
// I'm looking for the 10 best stocks
List_Pond = "";

for( i = 0; ( Symbol_Pond = StrExtract( ListWL_SP500, i ) ) != ""; i++ )
{

Var_01 = LastValue(StaticVarGet ( "RankValues" + Symbol_Pond ));

if( Var_01  >= 1 OR  Var_01 <= 10)
{
  List_Pond += Symbol_Pond + ",";

}
else
{
  List_Pond = List_Pond;
}

}

Title = "Ponderación SP-500" +
		"\n          Ticker       Capitalización      Peso %" +
		"\n  1 -   " + StrExtract(List_Pond,0) +
		"\n  2 -   " + StrExtract(List_Pond,1) +
		"\n  3 -   " + StrExtract(List_Pond,2) +
		"\n  4 -   " + StrExtract(List_Pond,3) +
		"\n  5 -   " + StrExtract(List_Pond,4) +
		"\n  6 -   " + StrExtract(List_Pond,5) +
		"\n  7 -   " + StrExtract(List_Pond,6) +
		"\n  8 -   " + StrExtract(List_Pond,7) +
		"\n  9 -   " + StrExtract(List_Pond,8) +
		"\n10 -   " + StrExtract(List_Pond,9) ;

 
// This is not the correct one, but it is what I need.
// I can't search one by one.
// it does not work correctly.
 Stock01 = LastValue(StaticVarGet ( "RankValues" + "AAPL" ));
 MarCap01 = NumToStr(LastValue(StaticVarGet ( "ValuesMC" + "AAPL" )),1.0);
 Peso01 = NumToStr(LastValue(StaticVarGet ( "Values" + "AAPL" ))*100,1.2);
 Stock02 = LastValue(StaticVarGet ( "RankValues" + "MSFT" ));
 MarCap02 = NumToStr(LastValue(StaticVarGet ( "ValuesMC" + "MSFT" )),1.0);
 Peso02 = NumToStr(LastValue(StaticVarGet ( "Values" + "MSFT" ))*100,1.2);
  
Title = Title + "\n\n" +
		"\n " + Stock01 + " -   " + "AAPL   " + MarCap01 + "      " + Peso01 +
		"\n " + Stock02 + " -   " + "MSFT   " + MarCap02 + "       " + Peso02 +
		"\netc..." ;
		

StaticVarAdd is not suppposed to be run inside loop.

Also don't use StaticVarAdd( "~SymbolCount", 1 ); inside loop and SetForeign to count symbols.
It is non sense code. (Besides it is not array. So it makes no sense). BTW you already have symbol count variable before that - > numtickers.

And to get top rank list use

StaticVarGenerateRanks("top", ....);
StaticVarGetRankedSymbols("top", ...);

Please read documentation
https://www.amibroker.com/guide/afl/staticvargenerateranks.html
https://www.amibroker.com/guide/afl/staticvargetrankedsymbols.html

As for "fix"...
Here you go:

/// original code by carlosrodrgueznieto at
/// @link https://forum.amibroker.com/t/pass-the-top-10-stocks-from-explore-to-the-chart-title/20694
/// "fix" by fxshrat@gmail.com at
/// @link https://forum.amibroker.com/t/pass-the-top-10-stocks-from-explore-to-the-chart-title/20694/2
ListWL_SP500 = CategoryGetSymbols( categoryWatchlist, 8 ); // Sotcks SP500
numtickers = 1 + StrCount( ListWL_SP500, "," ); 
MaxStock = Param("Máx. x Capitalización 1-10",10,1,10,1);

// Asignando Market Cap TOTAL SP500
if ( Status( "stocknum" ) == 0 ) {
    // cleanup variables created in previous runs
    StaticVarRemove( "values*" ); 

    cs_mkt_cap  = 0;
    for ( n = 0; ( Symbol = StrExtract( ListWL_SP500, n ) )  != "";  n++ ) {
        share_out = GetFnDataForeign("SharesOut", symbol);
        price = Foreign(symbol, "C");
        cs_mkt_cap += Nz(price*share_out);   
    }  

    for ( n = 0; ( Symbol = StrExtract( ListWL_SP500, n ) )  != "";  n++ ) { 
        share_out = GetFnDataForeign("SharesOut", symbol);
        price = Foreign(symbol, "C");
        mkt_cap = price * share_out;
        values = mkt_cap / cs_mkt_cap;
        StaticVarSet("values" + symbol, values); 
        VarSet("MC" + symbol, mkt_cap); 
    } 
    StaticVarGenerateRanks( "rank", "values", 0, 1224 ); 
    StaticVarGenerateRanks( "top", "values", MaxStock, 1224 ); // top-N mode 
}

symbol = Name(); 
MarCap = VarGet( "MC" + symbol ); 
Peso = StaticVarGet( "values" + symbol ); 
rank = StaticVarGet( "rankvalues" + symbol ); 

if ( GetOption("ApplyTo") == 2 ) {
	Filter = rank <= MaxStock; // 1;
	SetOption("NoDefaultColumns", True ); 
	SetSortColumns( 6 );
	AddColumn( DateTime(), "Date", formatDateTime );
	AddTextColumn(Name(),"Ticker");
	AddTextColumn(FullName(),"Nombre");
	AddColumn ( Peso*100, "Peso %", 1.2 ); 
	AddColumn ( Close * GetFnData( "SharesOut" ), "MarketCap", 1.0 ); 
	AddColumn ( rank, "Rank Peso", 1.0, colorWhite, colorLightBlue); 

} else if ( Status( "action" ) == actionExplore )
	Error("Seleccionar en 'Apply to' --> *Filter --> WatchList: SP500");

// Buscamos los 10 mejores stocks
// I'm looking for the 10 best stocks
sdt = SelectedValue( DateTime() );
top_list = StaticVarGetRankedSymbols("top", "values", sdt );
printf( "topList: %s", top_list);

List_Pond = "";
for( i = 0; (Symbol_Pond = StrExtract( top_list, i ) ) != ""; i++ ) {
	Stock01 = SelectedValue(StaticVarGet("rankvalues" + Symbol_Pond));
	MarCap01 = SelectedValue(VarGet("MC" + Symbol_Pond));
	Peso01 = SelectedValue(StaticVarGet("values" + Symbol_Pond)) * 100;
	List_Pond += StrFormat("\n %1.0f -   %s   %1.0f      %1.2f", Stock01, StrToUpper(Symbol_Pond), MarCap01, Peso01);
}
Title = "Ponderación SP-500" +
		"\n          Ticker       Capitalización      Peso %" + List_Pond;

14

2 Likes

Thanks @fxshrat for your quick reply.
Then I will try it and answer it, but I think that as always it will work well.
Best regards,
Carlos

@fxshrat

I've tried it and it's perfect, as always.
Again thank you very much for your help.

Best regards,
Carlos

1 Like