gnucash/libgnucash/quotes/Quote_example.pl

91 lines
2.7 KiB
Perl
Raw Normal View History

#!/usr/bin/perl -w
##@file
# @brief
# example script showing how to use the Quote perl module.
# gets prices for some stocks, for some mutual funds
#
Rob Browning's patch. * src/scm/utilities.scm (item-list->hash!): new - fancy (possibly over-customizable) list->hash converter function. (filteroutnulls): simplify via SRFI-1. (flatten): speedup - avoid intermediate appends. (striptrailingwhitespace): speedup - avoid intermediate substrings. (string-join): speedup - (maybe), trade string-appends for conses. * src/scm/process.scm (gnc:run-sub-process): renamed from run-sub-process. (gnc:cleanup-sub-process): new - take various steps to make sure we don't leave a zombie lying around. * src/scm/price-quotes.scm: substantial rework - now much smarter, though still a bit too obfuscated. * src/scm/engine-utilities.scm: various cleanups, speedups, and new functions. (gnc:filename->book): deleted. (gnc:call-with-account-data-from-file): deleted. (gnc:url->loaded-book): new function. * src/scm/command-line.scm (gnc:*arg-defs*): add --add-price-quotes option. * src/quotes/finance-quote-helper.in: new file - renamed from price-quote-helper.in. * src/quotes/README: various updates. * src/quotes/Quote_example.pl: document that we *don't* use the "fetch" method in gnucash. * src/quotes/gnc-prices: new file -- replaces gnc-prices.in and is now just a wrapper for "gnucash --add-price-quotes". * src/quotes/Makefile.am: accomodated renaming of price-quote-helper to finance-quote-helper. Also moved finance-quote-helper to GNC_SHAREDIR from pkgdatadir. (gncsharedir): grab GNC_SHAREDIR. (gncshare_SCRIPTS): put finance-quote-helper in GNC_SHAREDIR. * src/guile/gnc.gwp: wraped new quote timezone API. Added new gnc-book wrappers and new pricedb wrappers -- still not finished. * src/gnome/gnc-dialogs.glade: add quote timezone menu. * src/gnome/dialog-utils.h: added prototypes for timezone menu funcs. * src/gnome/dialog-utils.c: (known_timezones): new - list of quote timezones we support. (gnc_find_timezone_menu_position): new. (gnc_timezone_menu_position_to_string): new. (gnc_ui_quote_tz_menu_create): new. * src/gnome/dialog-account.c (struct _AccountWindow): add quote_tz_menu. (gnc_account_to_ui): support quote_tz_menu. (gnc_ui_to_account): support quote_tz_menu. (gnc_account_window_create): create quote_tz_menu. * src/gnc-ui-util.h: warn to update price-quotes.scm when PriceSourceCode changes. * src/engine/io-gncbin-r.c (cvt_potential_prices_to_pricedb_and_cleanup): Timespec* -> Timespec for price time function(s). * src/engine/gnc-pricedb.h: Timespec* -> Timespec for time functions. * src/engine/gnc-pricedb.c (gnc_price_clone): re-enable, and Timespec* -> Timespec (gnc_price_set_time): Timespec* -> Timespec (gnc_price_get_time): Timespec* -> Timespec (compare_prices_by_date): Timespec* -> Timespec (gnc_pricedb_lookup_at_time): Timespec* -> Timespec (gnc_price_print): minor fixes (still lame). (gnc_price_print_stdout): new (for lame guile side debugging). * src/engine/gnc-pricedb-xml-v1.c: change price time args/results from Timespec* to Timespec (to be more guile friendly). * src/engine/gnc-book.c (gnc_book_init): be more thorough. * src/engine/Backend.h: warn to update gnc.gwp on some mods. * src/engine/Account.h: support new funcs and update docs. * src/engine/Account.c (xaccAccountSetQuoteTZ): new. (xaccAccountGetQuoteTZ): new. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3788 57a11ea4-9604-0410-9ed3-97b8803252fd
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.
#
# @cond PERL
Rob Browning's patch. * src/scm/utilities.scm (item-list->hash!): new - fancy (possibly over-customizable) list->hash converter function. (filteroutnulls): simplify via SRFI-1. (flatten): speedup - avoid intermediate appends. (striptrailingwhitespace): speedup - avoid intermediate substrings. (string-join): speedup - (maybe), trade string-appends for conses. * src/scm/process.scm (gnc:run-sub-process): renamed from run-sub-process. (gnc:cleanup-sub-process): new - take various steps to make sure we don't leave a zombie lying around. * src/scm/price-quotes.scm: substantial rework - now much smarter, though still a bit too obfuscated. * src/scm/engine-utilities.scm: various cleanups, speedups, and new functions. (gnc:filename->book): deleted. (gnc:call-with-account-data-from-file): deleted. (gnc:url->loaded-book): new function. * src/scm/command-line.scm (gnc:*arg-defs*): add --add-price-quotes option. * src/quotes/finance-quote-helper.in: new file - renamed from price-quote-helper.in. * src/quotes/README: various updates. * src/quotes/Quote_example.pl: document that we *don't* use the "fetch" method in gnucash. * src/quotes/gnc-prices: new file -- replaces gnc-prices.in and is now just a wrapper for "gnucash --add-price-quotes". * src/quotes/Makefile.am: accomodated renaming of price-quote-helper to finance-quote-helper. Also moved finance-quote-helper to GNC_SHAREDIR from pkgdatadir. (gncsharedir): grab GNC_SHAREDIR. (gncshare_SCRIPTS): put finance-quote-helper in GNC_SHAREDIR. * src/guile/gnc.gwp: wraped new quote timezone API. Added new gnc-book wrappers and new pricedb wrappers -- still not finished. * src/gnome/gnc-dialogs.glade: add quote timezone menu. * src/gnome/dialog-utils.h: added prototypes for timezone menu funcs. * src/gnome/dialog-utils.c: (known_timezones): new - list of quote timezones we support. (gnc_find_timezone_menu_position): new. (gnc_timezone_menu_position_to_string): new. (gnc_ui_quote_tz_menu_create): new. * src/gnome/dialog-account.c (struct _AccountWindow): add quote_tz_menu. (gnc_account_to_ui): support quote_tz_menu. (gnc_ui_to_account): support quote_tz_menu. (gnc_account_window_create): create quote_tz_menu. * src/gnc-ui-util.h: warn to update price-quotes.scm when PriceSourceCode changes. * src/engine/io-gncbin-r.c (cvt_potential_prices_to_pricedb_and_cleanup): Timespec* -> Timespec for price time function(s). * src/engine/gnc-pricedb.h: Timespec* -> Timespec for time functions. * src/engine/gnc-pricedb.c (gnc_price_clone): re-enable, and Timespec* -> Timespec (gnc_price_set_time): Timespec* -> Timespec (gnc_price_get_time): Timespec* -> Timespec (compare_prices_by_date): Timespec* -> Timespec (gnc_pricedb_lookup_at_time): Timespec* -> Timespec (gnc_price_print): minor fixes (still lame). (gnc_price_print_stdout): new (for lame guile side debugging). * src/engine/gnc-pricedb-xml-v1.c: change price time args/results from Timespec* to Timespec (to be more guile friendly). * src/engine/gnc-book.c (gnc_book_init): be more thorough. * src/engine/Backend.h: warn to update gnc.gwp on some mods. * src/engine/Account.h: support new funcs and update docs. * src/engine/Account.c (xaccAccountSetQuoteTZ): new. (xaccAccountGetQuoteTZ): new. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@3788 57a11ea4-9604-0410-9ed3-97b8803252fd
2001-03-16 16:49:12 -06:00
use Finance::Quote;
# Create a quote object.
my $quoter = Finance::Quote->new();
# -----------------------------------
# get quotes for two stocks ...
%quotes = $quoter->fetch("alphavantage","IBM", "SGI");
# print some selected values
print "NYSE by Alphavantage: ", $quotes {"IBM", "name"},
" last price: ", $quotes {"IBM", "last"}, "\n";
print "NYSE by Alphavantage: ", $quotes {"SGI", "name"},
" 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");
%quotes = $quoter->fetch("fidelity",@funds);
foreach $f (@funds) {
$name = $quotes {$f, "name"};
$nav = $quotes {$f, "nav"};
print "Fidelity Fund $f $name \tNAV = $nav\n";
}
print "\n\n";
# -----------------------------------
@funds = ("FGRXX");
%quotes = $quoter->fetch("fidelity",@funds);
2017-01-16 16:03:50 -06:00
print "Not all funds have a NAV; some have Yields:\n";
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");
%quotes = $quoter->fetch("troweprice",@funds);
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";
# -----------------------------------
# demo for ASX. Grab the price of Coles-Myer and Telstra
@funds = ("CML","TLS");
%quotes = $quoter->fetch("australia",@funds);
foreach $f (@funds) {
print "ASX Price of $f is ".$quotes{$f,"last"}." at ".
$quotes{$f,"date"}."\n";
}
print "\n\n";
##@endcond Perl