1 |
apollock |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
use Pg; |
4 |
|
|
use Finance::Quote; |
5 |
|
|
|
6 |
|
|
$start = 0; |
7 |
|
|
$db = "stock"; |
8 |
|
|
$db_user = "postgres"; |
9 |
|
|
$db_password = "ybgz004"; |
10 |
|
|
|
11 |
|
|
$conn = Pg::connectdb("dbname=$db user=$db_user password=$db_password"); |
12 |
|
|
$asx = Finance::Quote->new; |
13 |
|
|
|
14 |
|
|
# Work out what stock we need to get prices for |
15 |
|
|
|
16 |
|
|
$query = "SELECT DISTINCT code FROM stock"; |
17 |
|
|
|
18 |
|
|
$result = $conn->exec($query); |
19 |
|
|
|
20 |
|
|
if ($result) { |
21 |
|
|
# Stuff those stock into an array |
22 |
|
|
while ($code = $result->fetchrow) { |
23 |
|
|
push(@codes, $code); |
24 |
|
|
print $code, "\n"; |
25 |
|
|
} |
26 |
|
|
# Fetch the price info for what's in the array |
27 |
|
|
foreach $code (@codes) { |
28 |
|
|
%stockinfo = $asx->fetch("asx", $code); |
29 |
|
|
$open = $stockinfo{$code, 'open'} * 100; |
30 |
|
|
$high = $stockinfo{$code, 'high'} * 100; |
31 |
|
|
$low = $stockinfo{$code, 'low'} * 100; |
32 |
|
|
$close = $stockinfo{$code, 'last'} * 100; |
33 |
|
|
$volume = $stockinfo{$code, 'volume'}; |
34 |
|
|
|
35 |
|
|
# Poke it into the database; |
36 |
|
|
|
37 |
|
|
$query = "INSERT INTO market VALUES ('$code', now(), $open, $high, $low, $close, $volume)"; |
38 |
|
|
print $query; |
39 |
|
|
$result = $conn->exec($query); |
40 |
|
|
if ($result) { |
41 |
|
|
print "..." . $result->resultStatus . "\n"; |
42 |
|
|
} |
43 |
|
|
} |
44 |
|
|
} else { |
45 |
|
|
die "It's all gone to custard\n"; |
46 |
|
|
} |