Code for Ticker, Company, Sector, Industry

372/5000

Hi @fxshrat,

Following your advice from your last help:

I have written this code to get the data out of Finviz:
Ticker, Company, Sector, Industry

The file generates it well, but I would like if you can take a look at the code to improve its performance.

Thank you very much for your help.

Best regards,
Carlos

// Datos_Finviz_Stocks_Descripcion

EnableTextOutput(0);

// watchlist should contain all symbols included in the test
// 2 --> USA Total | 3 --> USA Wall Street | 8 --> SP500 | 10 --> Nasdaq100 | 12 --> Wilshire5000
wlnum = 3; // 2; // 3; // 8; // 10; // 12; // GetOption( "FilterIncludeWatchlist" );
listnum = CategoryGetSymbols( categoryWatchlist, wlnum ) ;

ListName = CategoryGetName( categoryWatchlist, wlnum );  // 2 - 8 - 10 - 12
Datos_Finviz_Stocks_Descripcion = "";

// ticker_Finviz
what1 = "class=\"fullview-ticker";
ticker_Finviz = "";

//Mer_Finviz
what2 = "class=\"fullview-ticker";
Mer_Finviz = "";

//Nom_Finviz
what6 = "\" target=\"_blank\" class=\"tab-link\"><b";
Nom_Finviz = "";

//Sector_Finviz
what3 = "<tr><td align=\"center\" class=\"fullview-links";
Sector_Finviz = "";

//Industria_Finviz
what4 = "<tr><td align=\"center\" class=\"fullview-links";
Industria_Finviz = "";

Filter = 0;
trigger = ParamTrigger( "Crear Descripción", "CLICK HERE" ) OR Status("action") == actionExplore;

if ( trigger ) {

	if ( Status( "stocknum" ) == 0 ) {	
		for ( i = 0; ( nm = StrExtract(listnum, i, ',' ) ) != ""; i++ ) {
			ih = InternetOpenURL( "https://finviz.com/quote.ashx?t="+nm+"&ty=c&p=d&b=1" );			
			if ( ih ) {	
				str_repl1 = "";
				str_repl2 = "";
				str_repl3 = "";
				str_repl4 = "";
				str_repl6 = "";
				while( ( str = InternetReadString( ih ) ) != "" ) {
				
				// ticker_Finviz
				if ( StrFind(str, what1) ) 
						str_repl1 = StrReplace(str, what1, "\n");
				
				//Mer_Finviz
				if ( StrFind(str, what2) ) 
						str_repl2 = StrReplace(str, what2, "\n");

				//Nom_Finviz
				if ( StrFind(str, what6) ) 
						str_repl6 = StrReplace(str, what6, "\n");
					
				//Sector_Finviz
				if ( StrFind(str, what3) ) 
						str_repl3 = StrReplace(str, what3, "\n");

				
				//Industria_Finviz	
				if ( StrFind(str, what4) ) 
						str_repl4 = StrReplace(str, what4, "\n");

				}
				
				InternetClose( ih );			
				
				// ticker_Finviz
				str_ext1 = StrExtract(str_repl1, 1, '\n');				
				str_ext1 = StrExtract(str_ext1, 1, '>');
				
				// Mercado_Finviz
				str_ext2 = StrExtract(str_repl2, 1, '\n');				
				str_ext2 = StrExtract(str_ext2, 3, '>');
				
				// Nom_Finviz
				str_ext6 = StrExtract(str_repl6, 1, '\n');				
				str_ext6 = StrExtract(str_ext6, 1, '>');
				str_ext6 = StrReplace(str_ext6, "&amp;", "&" );
			
				// Sector_Finviz
 				str_ext3 = StrExtract(str_repl3, 1, '\n');				
				str_ext3 = StrExtract(str_ext3, 2, '>');
               
               	// Industria_Finviz
 				str_ext4 = StrExtract(str_repl4, 1, '\n');				
				str_ext4 = StrExtract(str_ext4, 4, '>');
				str_ext4 = StrReplace(str_ext4, "&amp;", "&" );

                /*
                //str_ext5 = StrExtract(str_repl5, 1, '\n');                                                             
                //str_ext5 = StrExtract(str_ext5, 3, '>');
                str_ext5 = StrExtract(str_repl5, 1, '\n');                                                 
                str_ext5_x = StrExtract(str_ext5, 3, '>');
                    if (StrRight(str_ext5_x,3) == "</b")
                            str_ext5 = str_ext5_x;
                    else
                            str_ext5 = StrExtract(str_ext5, 4, '>');
				*/
				
				Datos_Finviz_Stocks_Descripcion += nm + ";" + StrExtract(str_ext1, 0, '<') + ";" + StrExtract(str_ext2, 0, '<') + ";" + StrExtract(str_ext6, 0, '<') + ";" + StrExtract(str_ext3, 0, '<') + ";" + StrExtract(str_ext4, 0, '<') + "\n" ;

			}		
		}

		_TRACE(Datos_Finviz_Stocks_Descripcion);

		// save to file        
		fh = fopen("C:\\Users\\Carlos\\Documents\\Amibroker\\Listas Mercados\\Indicador Warren Buffett\\Datos Finviz\\Datos_Finviz_Stocks_Descripcion_" + ListName + ".csv", "w" );
		if ( fh ) {
			// Cabecera
			fputs( "Ticker_AB;ticker_Finviz;Mercado;Nom_Finviz;Sector_Finviz;Industria_Finviz\n", fh );
			fputs( Datos_Finviz_Stocks_Descripcion, fh );
			fclose( fh );
		} else
			Error( "ERROR: file can not be open" );
	}
}

Yes, the code can be simplified much more -> ~40% less code.
And you can use StrExtract all the way inside loop (10 calls and 6 lines).
Also you only need single StrFind line (line != call). Single line, three calls.
And the one and only single time that you need StrReplace is outside of loop.

// Single time (outside of loops)
Datos_Finviz_Stocks_Descripcion = StrReplace(Datos_Finviz_Stocks_Descripcion, "&amp;", "&");

Good luck.

71/5000

Thanks for your quick response.
I'm going to investigate, see if I get it.

Best regards,
Carlos