[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fundamentals in a TXT file



PureBytes Links

Trading Reference Links

Hi Jim!

On Fri, 25 Apr 2003, Jim Bronke wrote:

> thanks Mike,
> 
> I haven't used Perl, but I suppose I could learn. It would be good
> especially if I could save some money. Does this basicly mean I would run a
> loop that would enter the symbol and then write the response to my own text
> file?
> 
> ----- Original Message -----
> From: "Mike Eggleston" <mikee@xxxxxxxxxxxx>
> To: "Jim Bronke" <jvbronke@xxxxxxxxxxx>
> Cc: <omega-list@xxxxxxxxxx>
> Sent: Friday, April 25, 2003 10:00 AM
> Subject: Re: Fundamentals in a TXT file
> 
> 
> : On Fri, 25 Apr 2003, Jim Bronke wrote:
> :
> : >
> : > Does anyone know of a good data service provider that can give their
> : > customers fundamental data for stocks in a text file. I would want it to
> be
> : > all in one file.
> : >
> :
> : Is yahoo's fundamental stock data good enough? You can easily pull the
> : data using perl, then write the data to disk.
> :
> : Mike
> 
> 
> 

Here is the basic code. This code accepts on the command line a list of
symbols you want to retrieve the fundamental data for. The script
pulls the data then writes that data to a local database. For what you
mentioned the latter part of the script is changed to write to a file
instead of to the database.

First the unmodified script:

-----------------
#!/usr/bin/perl

# $Id: gi.pl,v 1.1 2001/04/24 12:17:23 mikee Exp mikee $
# $Log: gi.pl,v $
# Revision 1.1  2001/04/24 12:17:23  mikee
# Initial revision
#

use Finance::Quote;
use Data::Dumper;
use DBI;
use Date::Manip;

package gi;

sub expand {
	my($val) = shift;
	$val =~ s/^\+//o;
	if($val =~ /\d+B$/) {
		$val =~ s/[,A-z]//g;
		$val *= 1000000000;
	} elsif($val =~ /\d+M$/) {
		$val =~ s/[,A-z]//g;
		$val *= 1000000;
	} elsif($val =~ /\d+K$/) {
		$val =~ s/[,A-z]//g;
		$val *= 1000;
	} elsif($val =~ /\d+\%$/) {
		$val =~ s/[,A-z%]//g;
		$val /= 100;
	}
	$val
}

$dbh = DBI->connect("DBI:Pg:dbname=stocks", undef, undef, {'RaiseError' => 1});

$argv = join(' ', @ARGV);
$argv =~ tr/[a-z]/[A-Z]/;
@ARGV = split(' ', $argv);
$stmt = $dbh->prepare("select id from info where symbol = ?");
foreach $symbol (@ARGV) {
	$rc = $stmt->execute($symbol);
	if($rc > 0) {
		warn "symbol '$symbol' found; symbol not inserted";
	} else {
		push(@symbols, $symbol);
	}
}
$stmt->finish();

$q = Finance::Quote->new();
%quotes = $q->yahoo(@symbols);

$stmt = $dbh->prepare("insert into info (id, symbol, name, pe, eps, cap, low52, high52, "
	. "dividend, earnings, avg_vol, pct_chg, net_chg, ask, bid, lastbid) "
	. "values (nextval('infoseq'), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

foreach $symbol (@symbols) {
	print "$symbol\n";
	@a = split(/\s*-\s*/o, $quotes{$symbol, 'year_range'});
	$low52 = $a[0] =~ /^\d+\.\d+$/o ? $a[0] : 0;
	$high52 = $a[1] =~ /^\d+\.\d+$/o ? $a[1] : 0;
	my $key;
	foreach $key (keys %quotes) {
		$quotes{$key} = &expand($quotes{$key});
		#print "$key = $quotes{$key}\n";
	}
	#print "\n";
	$stmt->execute($symbol, $quotes{$symbol, 'name'}, $quotes{$symbol, 'pe'},
		$quotes{$symbol, 'eps'},
		$quotes{$symbol, 'cap'}, $low52, $high52, $quotes{$symbol, 'div'},
		0, $quotes{$symbol, 'avg_vol'}, $quotes{$symbol, 'p_change'},
		$quotes{$symbol, 'net'},
		$quotes{$symbol, 'ask'} ? $quotes{$symbol, 'ask'} : 0,
		$quotes{$symbol, 'bid'} ? $quotes{$symbol, 'bid'} : 0,
		$quotes{$symbol, 'last'});

	$stmt->finish();
}
$dbh->disconnect();

-----------------

Then the modified script. I have not tested this script.

-----------------
#!/usr/bin/perl

# $Id: gi.pl,v 1.1 2001/04/24 12:17:23 mikee Exp mikee $
# $Log: gi.pl,v $
# Revision 1.1  2001/04/24 12:17:23  mikee
# Initial revision
#

use Finance::Quote;
use Data::Dumper;
use Date::Manip;

package gi;

sub expand {
	my($val) = shift;
	$val =~ s/^\+//o;
	if($val =~ /\d+B$/) {
		$val =~ s/[,A-z]//g;
		$val *= 1000000000;
	} elsif($val =~ /\d+M$/) {
		$val =~ s/[,A-z]//g;
		$val *= 1000000;
	} elsif($val =~ /\d+K$/) {
		$val =~ s/[,A-z]//g;
		$val *= 1000;
	} elsif($val =~ /\d+\%$/) {
		$val =~ s/[,A-z%]//g;
		$val /= 100;
	}
	$val
}

$q = Finance::Quote->new();
%quotes = $q->yahoo(@ARGV);

foreach $symbol (@ARGV) {
	open(OUT, "> $symbol.fd") or die "$0: unable to write to file $symbol.df: $!";

	@a = split(/\s*-\s*/o, $quotes{$symbol, 'year_range'});
	$low52 = $a[0] =~ /^\d+\.\d+$/o ? $a[0] : 0;
	$high52 = $a[1] =~ /^\d+\.\d+$/o ? $a[1] : 0;
	my $key;
	foreach $key (keys %quotes) {					# convert M to millions
		$quotes{$key} = &expand($quotes{$key});
		#print "$key = $quotes{$key}\n";
	}
	print OUT join("\n",
			'Name ' . $quotes{$symbol, 'name'},
			'PE ' . $quotes{$symbol, 'pe'},
			'EPS ' . $quotes{$symbol, 'eps'},
			'Cap ' . $quotes{$symbol, 'cap'},
			'Low52 ' . $low52,
			'High52 ' . $high52,
			'Dividend ' . $quotes{$symbol, 'div'},
			'AvgVol ' . $quotes{$symbol, 'avg_vol'},
			'PctChg ' . $quotes{$symbol, 'p_change'},
			'NetChg ' . $quotes{$symbol, 'net'},
			'Ask ' . $quotes{$symbol, 'ask'} ? $quotes{$symbol, 'ask'} : 0,
			'Bid ' . $quotes{$symbol, 'bid'} ? $quotes{$symbol, 'bid'} : 0,
			'Last ' . $quotes{$symbol, 'last'}), "\n";

	close OUT;
}
-----------------