GnuCash 2.4.99
quotes_historic.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # quotes_historic.py -- Example Script to read historic quote data into gnucash
00004 #
00005  
00006 ##  @file
00007 #   @brief Example Script to read historic stock data into gnucash
00008 #   @author Peter Holtermann
00009 #   @date January 2011
00010 #   @ingroup python_bindings_examples
00011 #  
00012 #   Call the perl-script @code 
00013 #   ./get_quotes.pl INTC 
00014 #   @endcode first to achieve data into file INTC which can thereafter be imported to GnuCash using this script.
00015 # 
00016 #   For explanation of use have a look at the wiki:
00017 #   http://wiki.gnucash.org/wiki/Stocks/get_prices
00018 #
00019 
00020 from gnucash import Session, Account, Split
00021 import gnucash
00022 import datetime
00023 from fractions import Fraction
00024 from gnc_convenience import find_account
00025 
00026 FILE = "./test.gnucash"
00027 url = "xml://"+FILE
00028 
00029 # Read data from file
00030 f = open('INTC')
00031 data = []
00032 while 1:
00033     tmp = f.readline()
00034     if(len(tmp)<2):
00035         break
00036     
00037     data.append(tmp)
00038 
00039 f.close()
00040 
00041 stock_date = []
00042 stock_price = []
00043 for i in range(1,len(data)):
00044     year = int(data[i].rsplit(',')[1].rsplit('/')[0])
00045     month = int(data[i].rsplit(',')[1].rsplit('/')[1])
00046     day = int(data[i].rsplit(',')[1].rsplit('/')[2])
00047     stock_date.append(datetime.datetime(year,month,day))
00048     stock_price.append(float(data[i].rsplit(',')[5]))
00049 
00050 # Initialize Gnucash session
00051 session = Session(url, True, False, False)
00052 root = session.book.get_root_account()
00053 book = session.book
00054 account = book.get_root_account()
00055 pdb = book.get_price_db()
00056 comm_table = book.get_table()
00057 ac = find_account(account,'Intel')[0] 
00058 
00059 stock = ac.GetCommodity()
00060 # Add the prices
00061 pdb = book.get_price_db()
00062 if len(ac.GetSplitList())<1:
00063   print 'Need at least one Split to get currency info ... '
00064   raise SystemExit
00065 cur = ac.GetSplitList()[0].GetParent().GetCurrency()
00066 
00067 # Get stock data
00068 pl = pdb.get_prices(stock,cur)
00069 if len(pl)<1:
00070   print 'Need at least one database entry to clone ...'
00071   raise SystemExit
00072 
00073 pl0 = pl[0]
00074 for i in range(1,len(pl)):
00075   pdb.remove_price(pl[i])
00076 
00077 for i in range(0,len(stock_date)):
00078   p_new = pl0.clone(book)
00079   p_new = gnucash.GncPrice(instance=p_new)
00080   print 'Adding',i,stock_date[i],stock_price[i]
00081   p_new.set_time(stock_date[i])
00082   v = p_new.get_value()
00083   v.num = int(Fraction.from_float(stock_price[i]).limit_denominator(100000).numerator)
00084   v.denom = int(Fraction.from_float(stock_price[i]).limit_denominator(100000).denominator)
00085   p_new.set_value(v)
00086   p_new.set_source("Finance::Quotes::Historic")
00087   pdb.add_price(p_new)
00088 
00089 # Clean up
00090 session.save()
00091 session.end()
00092 session.destroy()
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines