GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Quote_example.pl
Go to the documentation of this file.
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 
18 use Finance::Quote;
19 
20 # Create a quote object.
21 my $quoter = Finance::Quote->new();
22 
23 # -----------------------------------
24 # get quotes for two stocks ...
25 %quotes = $quoter->fetch("yahoo","IBM", "SGI");
26 
27 # print some selected values
28 print "NYSE by Yahoo: ", $quotes {"IBM", "name"},
29  " last price: ", $quotes {"IBM", "last"}, "\n";
30 print "NYSE by Yahoo: ", $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
35 foreach $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 }
41 print "\n\n";
42 
43 # -----------------------------------
44 # get quotes from Fidelity Investments
45 @funds = ("FGRIX", "FNMIX", "FASGX", "FCONX");
46 %quotes = $quoter->fetch("fidelity",@funds);
47 
48 foreach $f (@funds) {
49  $name = $quotes {$f, "name"};
50  $nav = $quotes {$f, "nav"};
51  print "Fidelity Fund $f $name \tNAV = $nav\n";
52 }
53 print "\n\n";
54 
55 # -----------------------------------
56 @funds = ("FGRXX");
57 %quotes = $quoter->fetch("fidelity",@funds);
58 
59 print "Not all funds have a NAV; some have Yeilds:\n";
60 foreach $f (@funds) {
61  $name = $quotes {$f, "name"};
62  $yield = $quotes {$f, "yield"};
63  print "\tFidelity $f $name 30-day Yield = $yield percent\n";
64 }
65 print "\n\n";
66 
67 # -----------------------------------
68 # demo T. Rowe Price -- same as above
69 @funds = ("PRFDX", "PRIDX");
70 %quotes = $quoter->fetch("troweprice",@funds);
71 
72 foreach $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 }
77 print "\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);
85 foreach $f (@funds) {
86  print "ASX Price of $f is ".$quotes{$f,"last"}." at ".
87  $quotes{$f,"date"}."\n";
88 }
89 print "\n\n";
90 ##@endcond Perl