Yahoo Stock Screening Using Perl
So you need to screen stocks? And you want to automate the process?
Well I have not found much out there in the “Free” world that lets you do this. Yahoo seems to be about the best screening tool especially for Trend Traders as it allows you to filter on BETA. The only thing I don’t like is it doesn’t screen for Average Daily Volume. However with the script below, you could get a list of high beta stocks and then loop through them to return AVG Daily Volume.
This script parses the Yahoo Screen page by page and returns the values sorted by BETA (descending order). If you make it better, please let me know and I can post it.
In my case I only cared about the price range and a BETA > 2. However if you create your own screen you could just hack up the URL to your liking.
#!c:\\perl\\bin
use strict;
use HTML::TableExtract;
use LWP::UserAgent::Determined;
our %STOCKBETAS = ();
###These could be changed into arguments....Set them to what you wish.
my ($minprice,$maxprice,$minbeta) = (10,50,2);
####THE SCRIPT###############
my $url = 'http://screen.yahoo.com/b?pr=' . "$minprice/" . "$maxprice" . "&beta=$minbeta" . "/&s=nm&vw=1&db=stocks&b=1";
my $capture = GetStockScreen($url);
my $count = extractCount($capture); ###gets the count of symbols from the first page of the screen (i.e showing 20 of 341 symbols);
GetSymbols($capture);
for (my $x = 20;$x <= $count; $x+=20 ) { ###Loop through pages to get stock symbols
my $b = $x + 1;
my $MYURL = 'http://screen.yahoo.com/b?pr=' . "$minprice/" . "$maxprice" . "&beta=$minbeta" . "/&s=nm&vw=1&db=stocks&b=" . "$b";
$capture = GetStockScreen($MYURL);
GetSymbols($capture);
}
###Print the Results by Beta Decending.
open(SCREEN, ">YahooStockScreen.txt”);
foreach my $key (sort hashValueDescendingNum (keys(%STOCKBETAS))) {
print SCREEN “$key\|$STOCKBETAS{$key}\n”;
}
close (SCREEN);
#END OF THE SCRIPT########################################
###Retrieve the count from the first page of the yahoo screen.
sub extractCount {
my ($capture) = @_;
my $count = 1;
my $te = HTML::TableExtract->new( depth => 0, count => 0 );
$te->parse($capture);
my $table = $te->first_table_found;
foreach my $ts ($te->tables)
{
#print “Table found at “, join(’,', $ts->coords), “:\n”;
foreach my $row ($ts->rows) {
if ($count eq 1) {
$a = substr($row->[0],index($row->[0],”of”)+3, length($row->[0]));
$count = substr($a,0,index($a,”)”));
}
}
}
return $count;
}
####Retrieve the list of symbols and Betas from yahoo page
sub GetSymbols {
my ($capture) = @_;
my @symbols = ();
my $te = HTML::TableExtract->new( depth => 1, count => 0 );
$te->parse($capture);
my $table = $te->first_table_found;
foreach my $ts ($te->tables)
{
#print “Table found at “, join(’,', $ts->coords), “:\n”;
foreach my $row ($ts->rows) {
my $a = $row->[0];
my $b = $row->[3];
$STOCKBETAS{$a}=$b;
print “$a,$b\n”;
}
}
return @symbols;
}
###go to login page and login.
sub GetStockScreen {
my ($u) = @_;
my $response = getURL($u);
$capture = $response ->content;
return $capture;
}
###Download the URL it is passed. Uses LWP::UserAgent::Determined so its on a retry interval
sub getURL {
my ($url) = @_;
print “GETTING: $url\n”;
my $browser = LWP::UserAgent::Determined->new;
$browser->timing( “10,15,20,30,30,30,30,30″ ); ###get url on 10,15,20,30 second retry intervals.
my $response = $browser->get($url);
return $response;
}
####Sort the output in decending order.
sub hashValueDescendingNum {
$STOCKBETAS{$b} <=> $STOCKBETAS{$a};
}


Leave a Reply