| 1 |
#!/usr/bin/perl -w |
| 2 |
# |
| 3 |
# example script showing how to use the Quote perl module. |
| 4 |
# gets prices for some stocks, for some mutual funds |
| 5 |
# |
| 6 |
# This script was originally part of GnuCash. |
| 7 |
|
| 8 |
use lib '../lib'; |
| 9 |
use Finance::Quote; |
| 10 |
|
| 11 |
my $q = Finance::Quote->new(); |
| 12 |
|
| 13 |
# ----------------------------------- |
| 14 |
# get quotes for two stocks ... |
| 15 |
%quotes = $q->yahoo ("IBM", "SGI"); |
| 16 |
|
| 17 |
# print some selected values |
| 18 |
print "NYSE by Yahoo: ", $quotes {"IBM", "name"}, |
| 19 |
" last price: ", $quotes {"IBM", "last"}, "\n"; |
| 20 |
print "NYSE by Yahoo: ", $quotes {"SGI", "name"}, |
| 21 |
" last price: ", $quotes {"SGI", "last"}, "\n"; |
| 22 |
|
| 23 |
# loop over and print all values. |
| 24 |
# Notes that values are stored ion a multi-dimensional associative array |
| 25 |
foreach $k (sort (keys %quotes)) { |
| 26 |
($sym, $attr) = split ($;, $k, 2); |
| 27 |
$val = $quotes {$sym, $attr}; |
| 28 |
# $val = $quotes {$k}; # this also works, if desired ... |
| 29 |
print "\t$sym $attr =\t $val\n"; |
| 30 |
} |
| 31 |
print "\n\n"; |
| 32 |
|
| 33 |
# ----------------------------------- |
| 34 |
# get quotes from Fidelity Investments |
| 35 |
@funds = ("FGRIX", "FNMIX", "FASGX", "FCONX"); |
| 36 |
%quotes = $q->fidelity (@funds); |
| 37 |
|
| 38 |
foreach $f (@funds) { |
| 39 |
$name = $quotes {$f, "name"}; |
| 40 |
$nav = $quotes {$f, "nav"}; |
| 41 |
print "Fidelity Fund $f $name \tNAV = $nav\n"; |
| 42 |
} |
| 43 |
print "\n\n"; |
| 44 |
|
| 45 |
# ----------------------------------- |
| 46 |
@funds = ("FGRXX"); |
| 47 |
%quotes = $q->fidelity (@funds); |
| 48 |
|
| 49 |
print "Not all funds have a NAV; some have Yeilds:\n"; |
| 50 |
foreach $f (@funds) { |
| 51 |
$name = $quotes {$f, "name"}; |
| 52 |
$yield = $quotes {$f, "yield"}; |
| 53 |
print "\tFidelity $f $name 30-day Yield = $yield percent\n"; |
| 54 |
} |
| 55 |
print "\n\n"; |
| 56 |
|
| 57 |
# ----------------------------------- |
| 58 |
# demo T. Rowe Price -- same as above |
| 59 |
@funds = ("PRFDX", "PRIDX"); |
| 60 |
%quotes = $q->troweprice (@funds); |
| 61 |
|
| 62 |
foreach $f (@funds) { |
| 63 |
$nav = $quotes {$f, "nav"}; |
| 64 |
$dayte = $quotes {$f, "date"}; |
| 65 |
print "T. Rowe Price $f NAV = $nav as of $dayte\n"; |
| 66 |
} |
| 67 |
print "\n\n"; |
| 68 |
|
| 69 |
|
| 70 |
# ----------------------------------- |
| 71 |
|
| 72 |
# demo for ASX. Grab the price of Coles-Myer and Telstra |
| 73 |
@funds = ("CML","TLS"); |
| 74 |
%quotes = $q->asx(@funds); |
| 75 |
foreach $f (@funds) { |
| 76 |
print "ASX Price of $f is ".$quotes{$f,"last"}." at ". |
| 77 |
$quotes{$f,"date"}."\n"; |
| 78 |
} |
| 79 |
print "\n\n"; |
| 80 |
|
| 81 |
# Demo for TIAA-CREF. |
| 82 |
@funds = qw/CREFstok BOGOname TIAAreal CREFmony/; |
| 83 |
%quotes = $q->tiaacref(@funds); |
| 84 |
foreach $f (@funds) { |
| 85 |
if ($quotes{$f,"success"} == 1) { |
| 86 |
print "TIAA-CREF Price of ".$quotes{$f,"name"}." is ".$quotes{$f,"nav"}. |
| 87 |
" at ".$quotes{$f,"date"}."\n"; |
| 88 |
} else { |
| 89 |
print "Error: ".$quotes{$f,"errormsg"}." for ".$f."\n"; |
| 90 |
} |
| 91 |
} |
| 92 |
print "\n\n"; |