1999-01-17 11:18:52 -06:00
|
|
|
#!/usr/bin/perl -w
|
2011-02-10 13:40:38 -06:00
|
|
|
##@file
|
|
|
|
# @brief
|
1999-01-02 00:39:00 -06:00
|
|
|
# example script showing how to use the Quote perl module.
|
1999-01-02 03:31:19 -06:00
|
|
|
# gets prices for some stocks, for some mutual funds
|
2011-02-10 13:40:38 -06:00
|
|
|
#
|
2001-03-16 16:49:12 -06:00
|
|
|
# Note that this example uses the meta-level "fetch" command. We do
|
|
|
|
# NOT used that in Gnucash because it's behavior is unpredictable If
|
|
|
|
# the given method/exchange doesn't work, it'll fall back to other
|
|
|
|
# methods, and I've seen no guarantee that all exchanges treat all
|
|
|
|
# symbols the same. So in Gnucash, we use the backend methods
|
|
|
|
# directly, i.e. $quoter->fidelity_direct("IBM", "LNUX");, etc. The
|
|
|
|
# documentation page for each Finance::Quote sub-module describes how
|
|
|
|
# to call it directly without fallbacks.
|
2011-02-10 13:40:38 -06:00
|
|
|
#
|
|
|
|
# @cond PERL
|
2001-03-16 16:49:12 -06:00
|
|
|
|
2000-08-19 14:45:41 -05:00
|
|
|
use Finance::Quote;
|
|
|
|
|
|
|
|
# Create a quote object.
|
|
|
|
my $quoter = Finance::Quote->new();
|
1999-01-02 00:39:00 -06:00
|
|
|
|
|
|
|
# -----------------------------------
|
|
|
|
# get quotes for two stocks ...
|
2018-01-02 12:43:49 -06:00
|
|
|
%quotes = $quoter->fetch("alphavantage","IBM", "SGI");
|
1999-01-02 00:39:00 -06:00
|
|
|
|
|
|
|
# print some selected values
|
2018-01-02 12:43:49 -06:00
|
|
|
print "NYSE by Alphavantage: ", $quotes {"IBM", "name"},
|
1999-01-02 00:39:00 -06:00
|
|
|
" last price: ", $quotes {"IBM", "last"}, "\n";
|
2018-01-02 12:43:49 -06:00
|
|
|
print "NYSE by Alphavantage: ", $quotes {"SGI", "name"},
|
1999-01-02 00:39:00 -06:00
|
|
|
" last price: ", $quotes {"SGI", "last"}, "\n";
|
|
|
|
|
|
|
|
# loop over and print all values.
|
|
|
|
# Notes that values are stored ion a multi-dimensional associative array
|
|
|
|
foreach $k (sort (keys %quotes)) {
|
|
|
|
($sym, $attr) = split ($;, $k, 2);
|
|
|
|
$val = $quotes {$sym, $attr};
|
|
|
|
# $val = $quotes {$k}; # this also works, if desired ...
|
|
|
|
print "\t$sym $attr =\t $val\n";
|
|
|
|
}
|
|
|
|
print "\n\n";
|
|
|
|
|
|
|
|
# -----------------------------------
|
|
|
|
# get quotes from Fidelity Investments
|
|
|
|
@funds = ("FGRIX", "FNMIX", "FASGX", "FCONX");
|
2000-08-19 14:45:41 -05:00
|
|
|
%quotes = $quoter->fetch("fidelity",@funds);
|
1999-01-02 00:39:00 -06:00
|
|
|
|
|
|
|
foreach $f (@funds) {
|
|
|
|
$name = $quotes {$f, "name"};
|
|
|
|
$nav = $quotes {$f, "nav"};
|
|
|
|
print "Fidelity Fund $f $name \tNAV = $nav\n";
|
|
|
|
}
|
|
|
|
print "\n\n";
|
|
|
|
|
|
|
|
# -----------------------------------
|
|
|
|
@funds = ("FGRXX");
|
2000-08-19 14:45:41 -05:00
|
|
|
%quotes = $quoter->fetch("fidelity",@funds);
|
1999-01-02 00:39:00 -06:00
|
|
|
|
2017-01-16 16:03:50 -06:00
|
|
|
print "Not all funds have a NAV; some have Yields:\n";
|
1999-01-02 00:39:00 -06:00
|
|
|
foreach $f (@funds) {
|
|
|
|
$name = $quotes {$f, "name"};
|
|
|
|
$yield = $quotes {$f, "yield"};
|
|
|
|
print "\tFidelity $f $name 30-day Yield = $yield percent\n";
|
|
|
|
}
|
|
|
|
print "\n\n";
|
|
|
|
|
|
|
|
# -----------------------------------
|
|
|
|
# demo T. Rowe Price -- same as above
|
|
|
|
@funds = ("PRFDX", "PRIDX");
|
2000-08-19 14:45:41 -05:00
|
|
|
%quotes = $quoter->fetch("troweprice",@funds);
|
1999-01-02 00:39:00 -06:00
|
|
|
|
|
|
|
foreach $f (@funds) {
|
|
|
|
$nav = $quotes {$f, "nav"};
|
|
|
|
$dayte = $quotes {$f, "date"};
|
|
|
|
print "T. Rowe Price $f NAV = $nav as of $dayte\n";
|
|
|
|
}
|
|
|
|
print "\n\n";
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------
|
2000-03-29 18:49:54 -06:00
|
|
|
|
|
|
|
# demo for ASX. Grab the price of Coles-Myer and Telstra
|
|
|
|
@funds = ("CML","TLS");
|
2000-08-19 14:45:41 -05:00
|
|
|
%quotes = $quoter->fetch("australia",@funds);
|
2000-03-29 18:49:54 -06:00
|
|
|
foreach $f (@funds) {
|
|
|
|
print "ASX Price of $f is ".$quotes{$f,"last"}." at ".
|
|
|
|
$quotes{$f,"date"}."\n";
|
|
|
|
}
|
|
|
|
print "\n\n";
|
2011-02-10 13:40:38 -06:00
|
|
|
##@endcond Perl
|