Short Float for Finviz

Hello,
Starting from the code of @fxshrat :

I want to adapt it for :
https://finviz.com/screener.ashx?v=131&f=geo_usa,idx_sp500

I have a little problem when I pull data from Finviz for Short Float.
In the .afl I have added some lines to detect the color change.
I do it looking for the number of characters, but the value does not come out.

Thanks for your help.
Best regards,
Carlos

// Short Float for Finviz
// https://forum.amibroker.com/t/adding-a-yahoo-fundamental-data-piece-to-norgate-database/17549

// Apply to --> *Current
// Range --> 1 recent bar(s)

/// Save ""Shares Outstanding" fundamentals from Yahoo finance to text file
/// Replace tickerlist by your own symbol list or single symbol
/// AFL code by fxshrat@gmail.com
/// @link https://forum.amibroker.com/t/adding-a-yahoo-fundamental-data-piece-to-norgate-database/17549/3

EnableTextOutput(0);

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

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


what3 = ">Short Float<";
short_float = "";


Filter = 0;
trigger = ParamTrigger( "Create Shares out", "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_repl3 = "";
				str_repl31 = "";
				num_str_ext3 = 0;
				while( ( str = InternetReadString( ih ) ) != "" ) {
					if ( StrFind(str, what3) ) 
						str_repl3 = StrReplace(str, what3, "\n");
					if ( StrFind(str, what3) ) 
						str_repl31 = StrReplace(str, what3, "\n");					

				}
				InternetClose( ih );
				
				str_ext3 = StrExtract(str_repl3, 1, '\n');				
				str_ext3 = StrExtract(str_ext3, 3, '>');

				// 3 color block OK
				// 4 o 5 o 6 ... para otro color PERO NO FUNCIONA
				// I have tried different numbers, but it doesn't work, when it's green or red.
				num_str_ext3 = Iif( StrLen(str_ext3) > 0, 3, 4 );
				
				if (num_str_ext3 == 3) 
				{
				str_ext3 = StrExtract(str_repl3, 1, '\n');				
				str_ext3 = StrExtract(str_ext3, num_str_ext3, '>');
				}
				else
				{
				str_ext31 = StrExtract(str_repl31, 1, '\n');				
				str_ext3 = StrExtract(str_ext31, num_str_ext3, '>');			
				}
							
				short_float += nm + ";" + StrExtract(str_ext3, 0, '<')+"\n";
			}		
		}

		_TRACE(short_float);

		// save to file        
		fh = fopen("C:\\Users\\Carlos\\Desktop\\Nueva carpeta\\Short_Float_Finviz_TOTAL_" + ListName + ".csv", "w" );
		if ( fh ) {
			// Cabecera
			fputs( "Ticker;short_float\n", fh );
			fputs( short_float, fh );
			fclose( fh );
		} else
			Error( "ERROR: file can not be open" );
	}

}

You are over-complicating this a bit.
You just need to extract once you have found key expression.
And if there is "color" expression found then you use different extraction field.

// Short Float for Finviz
// https://forum.amibroker.com/t/adding-a-yahoo-fundamental-data-piece-to-norgate-database/17549

// Apply to --> *Current
// Range --> 1 recent bar(s)

/// Save ""Shares Outstanding" fundamentals from Yahoo finance to text file
/// Replace tickerlist by your own symbol list or single symbol
/// AFL code by fxshrat@gmail.com
/// @link https://forum.amibroker.com/t/adding-a-yahoo-fundamental-data-piece-to-norgate-database/17549/3

EnableTextOutput(0);

// watchlist should contain all symbols included in the test
// 2 --> USA Total | 8 --> SP500 | 10 --> Nasdaq100 | 12 --> Wilshire5000
wlnum = 8; // 2; // 8; // 10; // 12; // GetOption( "FilterIncludeWatchlist" );
list = "AAL,AAPL";//CategoryGetSymbols(categoryWatchlist, wlnum);
ListName = CategoryGetName( categoryWatchlist, wlnum );  // 2 - 8 - 10 - 12

what3 = ">Short Float<";
short_float = "";

Filter = 0;
trigger = ParamTrigger( "Create Shares out", "CLICK HERE" ) OR Status("action") == actionExplore;

if ( trigger ) {

	if ( Status( "stocknum" ) == 0 ) {	
		for ( i = 0; ( nm = StrExtract(list, i, ',' ) ) != ""; i++ ) {
			ih = InternetOpenURL( "https://finviz.com/quote.ashx?t="+nm+"&ty=c&p=d&b=1" );			
			if ( ih ) {	
				str_ext = "";
				while( ( str = InternetReadString( ih ) ) != "" ) {
					if ( StrFind(str, what3) ) {
						if ( StrFind(str, "<span style=\"color:"))
							str_ext = StrExtract(str, 5, '>');
						else
							str_ext = StrExtract(str, 4, '>');
					}
				}
				InternetClose( ih );			

				short_float += nm + ";" + StrExtract(str_ext, 0, '<')+"\n";
			}		
		}

		_TRACE(short_float);

		// save to file        
		fh = fopen("C:\\Short_Float_Finviz_TOTAL_" + ListName + ".csv", "w" );
		if ( fh ) {
			// Cabecera
			fputs( "Ticker;short_float\n", fh );
			fputs( short_float, fh );
			fclose( fh );
		} else
			Error( "ERROR: file can not be open" );
	}
}

15

4 Likes

Hi @fxshrat,

I hope to understand the concept for another search.

As always fast and effective with your response.
Thank you very much.
Best regards,
Carlos