Bug #640347: Adding Example Skripts for historic Stock Quotes

This patch adds the example skripts from
http://wiki.gnucash.org/wiki/Stocks/get_prices
to the directory python-bindings/example_scripts.

Patch by Christoph Holtermann.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@20265 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2011-02-10 19:37:23 +00:00
parent d16ef1765e
commit 0f0b1e662a
2 changed files with 151 additions and 0 deletions

View File

@ -0,0 +1,41 @@
#!/usr/bin/perl -w
# get_quotes.pl -- Addition to example Script quotes_historc.py. Reads online stock quotes to file INTC.
#
## @file
# @brief Addition to example Script quotes_historic.py. Reads online stock quotes to file INTC.
# @author Peter Holtermann
# @date January 2011
# @ingroup python_bindings_examples
#
# Call this script before calling @code
# python quotes_historic.py
# @endcode
#
# For explanation of use have a look at the wiki:
# http://wiki.gnucash.org/wiki/Stocks/get_prices
#
# @cond PERL
use Finance::QuoteHist;
print "Will get stock quotes of $ARGV[0] and save it into the file $ARGV[0]\n";
$fname = $ARGV[0];
open (MYFILE, ">$fname");
$q = Finance::QuoteHist->new
(
symbols => [($ARGV[0])],
start_date => '01/01/2000',
end_date => 'today',
);
print "name,date, open, high, low, close, volume\n";
foreach $row ($q->quotes()) {
($name,$date, $open, $high, $low, $close, $volume) = @$row;
print MYFILE "$name,$date, $open, $high, $low, $close, $volume\n";
}
close(MYFILE);
## @endcond

View File

@ -0,0 +1,110 @@
#!/usr/bin/env python
# quotes_historic.py -- Example Script to read historic quote data into gnucash
#
## @file
# @brief Example Script to read historic stock data into gnucash
# @author Peter Holtermann
# @date January 2011
# @ingroup python_bindings_examples
#
# Call the perl-script @code
# ./get_quotes.pl INTC
# @endcode first to achieve data into file INTC which can thereafter be imported to GnuCash using this script.
#
# For explanation of use have a look at the wiki:
# http://wiki.gnucash.org/wiki/Stocks/get_prices
#
from gnucash import Session, Account, Split
import gnucash
import datetime
from fractions import Fraction
# Function definition from Christoph Holtermann
def findAccount(account,name,account_list=None):
"""Searches full names of account and descendents
returns a list of accounts which contain name."""
if not account_list:
account_list=[]
for child in account.get_children():
child=Account(instance=child)
account_list=findAccount(child,name,account_list)
Account_name=account.GetName()
if name in Account_name:
account_list.append(account)
return account_list
FILE = "./test.gnucash"
url = "xml://"+FILE
# Read data from file
f = open('INTC')
data = []
while 1:
tmp = f.readline()
if(len(tmp)<2):
break
data.append(tmp)
f.close()
stock_date = []
stock_price = []
for i in range(1,len(data)):
year = int(data[i].rsplit(',')[1].rsplit('/')[0])
month = int(data[i].rsplit(',')[1].rsplit('/')[1])
day = int(data[i].rsplit(',')[1].rsplit('/')[2])
stock_date.append(datetime.datetime(year,month,day))
stock_price.append(float(data[i].rsplit(',')[5]))
# Initialize Gnucash session
session = Session(url, True, False, False)
root = session.book.get_root_account()
book = session.book
account = book.get_root_account()
pdb = book.get_price_db()
comm_table = book.get_table()
ac = findAccount(account,'Intel')[0]
stock = ac.GetCommodity()
# Add the prices
pdb = book.get_price_db()
if len(ac.GetSplitList())<1:
print 'Need at least one Split to get currency info ... '
raise SystemExit
cur = ac.GetSplitList()[0].GetParent().GetCurrency()
# Get stock data
pl = pdb.get_prices(stock,cur)
if len(pl)<1:
print 'Need at least one database entry to clone ...'
raise SystemExit
pl0 = pl[0]
for i in range(1,len(pl)):
pdb.remove_price(pl[i])
for i in range(0,len(stock_date)):
p_new = pl0.clone(book)
p_new = gnucash.GncPrice(instance=p_new)
print 'Adding',i,stock_date[i],stock_price[i]
p_new.set_time(stock_date[i])
v = p_new.get_value()
v.num = int(Fraction.from_float(stock_price[i]).limit_denominator(100000).numerator)
v.denom = int(Fraction.from_float(stock_price[i]).limit_denominator(100000).denominator)
p_new.set_value(v)
p_new.set_source("Finance::Quotes::Historic")
pdb.add_price(p_new)
# Clean up
session.save()
session.end()
session.destroy()