1#!/usr/bin/perl -w
2##@file
3# @brief
4# example script showing how to use the Quote perl module.
5# gets prices for some stocks, for some mutual funds
6#
7# Note that this example uses the meta-level "fetch" command.  We do
8# NOT used that in Gnucash because it's behavior is unpredictable If
9# the given method/exchange doesn't work, it'll fall back to other
10# methods, and I've seen no guarantee that all exchanges treat all
11# symbols the same.  So in Gnucash, we use the backend methods
12# directly, i.e. $quoter->fidelity_direct("IBM", "LNUX");, etc.  The
13# documentation page for each Finance::Quote sub-module describes how
14# to call it directly without fallbacks.
15#
16# @cond PERL
17
18use Finance::Quote;
19
20# Create a quote object.
21my $quoter = Finance::Quote->new();
22
23# -----------------------------------
24# get quotes for two stocks ...
25%quotes = $quoter->fetch("alphavantage","IBM", "SGI");
26
27# print some selected values
28print "NYSE by Alphavantage: ", $quotes {"IBM", "name"},
29       " last price: ", $quotes {"IBM", "last"},  "\n";
30print "NYSE by Alphavantage: ", $quotes {"SGI", "name"},
31       " last price: ", $quotes {"SGI", "last"},  "\n";
32
33# loop over and print all values.
34# Notes that values are stored ion a multi-dimensional associative array
35foreach $k (sort (keys %quotes)) {
36     ($sym, $attr) = split ($;, $k, 2);
37     $val = $quotes {$sym, $attr};
38     # $val = $quotes {$k};     # this also works, if desired ...
39     print "\t$sym $attr =\t $val\n";
40}
41print "\n\n";
42
43# -----------------------------------
44# get quotes from Fidelity Investments
45@funds = ("FGRIX", "FNMIX", "FASGX", "FCONX");
46%quotes = $quoter->fetch("fidelity",@funds);
47
48foreach $f (@funds) {
49     $name = $quotes {$f, "name"};
50     $nav = $quotes {$f, "nav"};
51     print "Fidelity Fund $f $name \tNAV = $nav\n";
52}
53print "\n\n";
54
55# -----------------------------------
56@funds = ("FGRXX");
57%quotes = $quoter->fetch("fidelity",@funds);
58
59print "Not all funds have a NAV; some have Yields:\n";
60foreach $f (@funds) {
61     $name = $quotes {$f, "name"};
62     $yield = $quotes {$f, "yield"};
63     print "\tFidelity $f $name 30-day Yield = $yield percent\n";
64}
65print "\n\n";
66
67# -----------------------------------
68# demo T. Rowe Price -- same as above
69@funds = ("PRFDX", "PRIDX");
70%quotes = $quoter->fetch("troweprice",@funds);
71
72foreach $f (@funds) {
73     $nav = $quotes {$f, "nav"};
74     $dayte = $quotes {$f, "date"};
75     print "T. Rowe Price $f NAV = $nav as of $dayte\n";
76}
77print "\n\n";
78
79
80# -----------------------------------
81
82# demo for ASX.  Grab the price of Coles-Myer and Telstra
83@funds = ("CML","TLS");
84%quotes = $quoter->fetch("australia",@funds);
85foreach $f (@funds) {
86	print "ASX Price of $f is ".$quotes{$f,"last"}." at ".
87	      $quotes{$f,"date"}."\n";
88}
89print "\n\n";
90##@endcond Perl
91