Archive

Archive for the ‘Finance’ Category

Yahoo Stock Screening Using Perl

June 19th, 2008

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

Greg Finance, Perl

Yahoo Finance Economic Events to XML with Perl

May 23rd, 2008

This is a very simple script which gets yahoo economic events from the yahoo finance website and converts it to XML.


#!c:\perl\bin\perl.exe

use LWP::Simple;
use LWP::UserAgent;
use HTML::TableExtract;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = new CGI;

my $url = “http://biz.yahoo.com/c/e.html”;
my $capture = get($url);
#http://www.treasurydirect.gov/xml/PendingAuctions.xml
print $cgi->header(-type => ‘application/xml’); # make browser expect xml

$te = HTML::TableExtract->new( depth => 0, count => 5 );
print “\n”;
$te->parse($capture);
my $table = $te->first_table_found;
foreach my $ts ($te->tables)
{
#print “Table found at “, join(’,', $ts->coords), “:\n”;
#print @$row;
$count = 0;
foreach $row ($ts->rows) {
$a = @$row->[0];
$b = @$row->[1];
$c = @$row->[2];
$d = @$row->[3];
$e = @$row->[4];
$f = @$row->[5];
$g = @$row->[6];
$h = @$row->[7];
$i = @$row->[8];
#print “$a,$b,$c,$d,$e,$f,$g,$h,$i\n”;
if ($count > 0) { ###skip first line
printXML();
}
$count++
#print join(’,', @$row), “\n”;
}
}

print “\n”;
sub printXML {
print “\t\n”;
print “\t\t$a\n” .
“\t\t\n” .
“\t\t$c\n” .
“\t\t$d\n” .
“\t\t$e\n” .
“\t\t$f\n” .
“\t\t$g\n” .
“\t\t

$h\n” .
“\t\t$i\n”;
print “\t\n”
}sub printXMLHeader {
print $cgi->header(-type => ‘application/xml’); # make browser expect xml
#print “\<\?xml version=\”1.0\” encoding=\”UTF-8\”\?>\n”;
}

Greg Finance, Perl

Yahoo Finance Get Industry and Sector using Perl

May 20th, 2008

This script parses yahoo finance an pulls the Industry and sector for any ticker symbol you pass it. I used it to create a stock screener I am working on. Hope it becomes useful for you.


#!c:\perl\bin\perl.exe
use LWP::Simple;
use LWP::UserAgent;
use HTML::TableExtract;

if ($#ARGV != 0) {
print “$#ARGV usage: need to pass in a symbol\n”;
exit;
}
$sym = $ARGV[0];

my $url = “http://finance.yahoo.com/q/in?s=$sym”;
my $capture = get($url);

$te = HTML::TableExtract->new( depth => 2, count => 0 );
#print “$depth,$i\n”;
$te->parse($capture);
my $table = $te->first_table_found;
foreach $ts ($te->tables) {
#print “Table found at “, join(’,', $ts->coords), “:\n”;
foreach $row ($ts->rows)
{
$a = @$row->[0];
$b = @$row->[1];
print “$a $b\n”;
}
}

Greg Finance, Perl

Dividend Reinvestment Calculator

May 15th, 2008

I have been a moderate investor in Dividend Reinvestment Plans. aka (DRIPS) If you are not sure what that is, check out http://www.fool.com/school/drips.htm as they do a pretty good job explaining it.

But basically it allows investors to buy shares of a particular equity on a periodic basis (bi-weekly, monthly,quarterly etc). Dividends on that equitiy are automatically reinvested when they are distributed, thus buying fractional shares of the company.

My calculator below simulate a DRIP over time.

View code
Title: DRIP Calculator
Description: Click the link to see it full screen

I have not found a tool out there that really tracks how a stock + dividend reinvestment performs over time. So I wrote this little tool to track it. You can find it at http://www.gregjessup.com/DRIP If you know of something similar, please post a comment with the link. If you think my math is wrong, also post a comment and challenge me.

If you want to see some changes made to it, send me an email. I’ll have a look and try to post a change.

Happy DRIPing!

By the way..This is by no means guaranteed to be accurate. Although I use the tool myself, and trust my work…it is not investment advice. Use it at your own risk. Persons seeking investment advice should consult a financial professional. Data not guaranteed to be accurate.

Greg FLEX, Finance