GnuCash 2.4.99
Quote_example.pl
Go to the documentation of this file.
00001 #!/usr/bin/perl -w
00002 ##@file
00003 # @brief
00004 # example script showing how to use the Quote perl module.
00005 # gets prices for some stocks, for some mutual funds
00006 #
00007 # Note that this example uses the meta-level "fetch" command.  We do
00008 # NOT used that in Gnucash because it's behavior is unpredictable If
00009 # the given method/exchange doesn't work, it'll fall back to other
00010 # methods, and I've seen no guarantee that all exchanges treat all
00011 # symbols the same.  So in Gnucash, we use the backend methods
00012 # directly, i.e. $quoter->fidelity_direct("IBM", "LNUX");, etc.  The
00013 # documentation page for each Finance::Quote sub-module describes how
00014 # to call it directly without fallbacks.
00015 #
00016 # @cond PERL
00017 
00018 use Finance::Quote;
00019 
00020 # Create a quote object.
00021 my $quoter = Finance::Quote->new();
00022 
00023 # -----------------------------------
00024 # get quotes for two stocks ...
00025 %quotes = $quoter->fetch("yahoo","IBM", "SGI"); 
00026 
00027 # print some selected values 
00028 print "NYSE by Yahoo: ", $quotes {"IBM", "name"},  
00029        " last price: ", $quotes {"IBM", "last"},  "\n";
00030 print "NYSE by Yahoo: ", $quotes {"SGI", "name"},   
00031        " last price: ", $quotes {"SGI", "last"},  "\n";
00032        
00033 # loop over and print all values.
00034 # Notes that values are stored ion a multi-dimensional associative array
00035 foreach $k (sort (keys %quotes)) {
00036      ($sym, $attr) = split ($;, $k, 2);
00037      $val = $quotes {$sym, $attr};
00038      # $val = $quotes {$k};     # this also works, if desired ...
00039      print "\t$sym $attr =\t $val\n";
00040 } 
00041 print "\n\n";
00042 
00043 # -----------------------------------
00044 # get quotes from Fidelity Investments
00045 @funds = ("FGRIX", "FNMIX", "FASGX", "FCONX");
00046 %quotes = $quoter->fetch("fidelity",@funds);
00047 
00048 foreach $f (@funds) {
00049      $name = $quotes {$f, "name"};
00050      $nav = $quotes {$f, "nav"};
00051      print "Fidelity Fund $f $name \tNAV = $nav\n";
00052 }
00053 print "\n\n";
00054 
00055 # -----------------------------------
00056 @funds = ("FGRXX");
00057 %quotes = $quoter->fetch("fidelity",@funds);
00058 
00059 print "Not all funds have a NAV; some have Yeilds:\n";
00060 foreach $f (@funds) {
00061      $name = $quotes {$f, "name"};
00062      $yield = $quotes {$f, "yield"};
00063      print "\tFidelity $f $name 30-day Yield = $yield percent\n";
00064 }
00065 print "\n\n";
00066 
00067 # -----------------------------------
00068 # demo T. Rowe Price -- same as above
00069 @funds = ("PRFDX", "PRIDX");
00070 %quotes = $quoter->fetch("troweprice",@funds);
00071 
00072 foreach $f (@funds) {
00073      $nav = $quotes {$f, "nav"};
00074      $dayte = $quotes {$f, "date"};
00075      print "T. Rowe Price $f NAV = $nav as of $dayte\n";
00076 }
00077 print "\n\n";
00078 
00079 
00080 # -----------------------------------
00081 
00082 # demo for ASX.  Grab the price of Coles-Myer and Telstra
00083 @funds = ("CML","TLS");
00084 %quotes = $quoter->fetch("australia",@funds);
00085 foreach $f (@funds) {
00086         print "ASX Price of $f is ".$quotes{$f,"last"}." at ".
00087               $quotes{$f,"date"}."\n";
00088 }
00089 print "\n\n";
00090 ##@endcond Perl
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines