Skip to content

Commit

Permalink
make it easier to update prices.db
Browse files Browse the repository at this point in the history
  • Loading branch information
John Beppu committed Sep 21, 2010
1 parent 9247304 commit c85d289
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions djia-vs-gold-ratio/djia-load.pl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
use Text::CSV_XS;
use DBI;

require 'utils.pl';

my $dbh = DBI->connect("dbi:SQLite:dbname=prices.db", "", "") or die DBI->errstr;
my $last_date = last_date($dbh, 'DJIA');

for my $file (@ARGV) {
my $csv = Text::CSV_XS->new({ binary => 1 })
or die "Cannot use CSV: " . Text::CSV->error_diag();
open my $fh, "<:encoding(utf8)", $file or die "$file: $!";
$csv->getline($fh); # throw away first line
while (my $row = $csv->getline($fh)) {
my ($date, $open, $high, $low, $close) = @$row;
next unless $date gt $last_date;
$dbh->do(
"INSERT INTO price (symbol, value, day) VALUES ('DJIA', ?, ?)", {},
$close, $date
Expand Down
7 changes: 6 additions & 1 deletion djia-vs-gold-ratio/gold-load.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use DBI;
use Data::Dump 'pp';

require 'utils.pl';

my %month_number = (
Jan => '01',
Feb => '02',
Expand All @@ -32,6 +34,7 @@ sub date {
}

my $dbh = DBI->connect("dbi:SQLite:dbname=prices.db", "", "") or die DBI->errstr;
my $last_date = last_date($dbh, 'XAU');

for (@ARGV) {
my $parser = Spreadsheet::ParseExcel->new();
Expand All @@ -42,11 +45,13 @@ sub date {
for my $row ($min .. $max) {
my $date_cell = $daily_gold->get_cell($row, 0);
my $usd_cell = $daily_gold->get_cell($row, 1);
my $date = date($date_cell->value);
next unless $date gt $last_date;
#print date($date_cell->value), " -- ", $usd_cell->value, "\n";
$dbh->do(
"INSERT INTO price (symbol, value, day) VALUES ('XAU', ?, ?)", {},
$usd_cell->value,
date($date_cell->value)
$date
);
}
}
Binary file modified djia-vs-gold-ratio/prices.db
Binary file not shown.
11 changes: 11 additions & 0 deletions djia-vs-gold-ratio/utils.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use strict;
use warnings;
use DBI;

sub last_date {
my ($dbh, $symbol) = @_;
my ($last) = $dbh->selectall_arrayref("SELECT max(day) as day FROM price WHERE symbol = ?", { Slice => {} }, $symbol);
$last->[0]{day};
}

1;

0 comments on commit c85d289

Please sign in to comment.