|
I use two classes to track accounts and transactions: class acct(object):
# bank and brokerage account
allacct = []
def __init__(self, name, balance, holdings=None):
self.name = name
self.balance = balance
self.holdings = holdings
self.xferamt = 0
self.accum = 0 # used to track cumulation ad hoc values
acct.allacct.append(self)
def buy(self, sec, amt, cost, day):
rec = trans(('buy', sec, amt), -cost, day, self)
self.balance -= cost
if not self.holdings.has_key(sec):
self.holdings[sec] = [0]
self.holdings[sec][0] += amt
self.holdings[sec].append(rec)
......
class trans(object):
# transaction record object track cash invovled in each transaction
allrec = []
def __init__(self, t, amt, day, where):
try:
self.type = t
self.amt = amt
self.date = date(year, month, day)
self.where = where
trans.allrec.append(self)
except Exception as err:
print t, amt, year, month, day, where
raise err
Then for each data line I just do any of the following: SOMEACCT.buy(...)
SOMEACCT.sell(...)
SOMEACCT.credit(...)
SOMEACCT.debit(...)
SOMEACCT.xfer(...)
# classified by tax categories:
SOMEACCT.int(...)
SOMEACCT.div(...)
SOMEACCT.fdiv(...)
SOMEACCT.mint(...)
SOMEACCT.mdiv(...)
Then it is easy to query and do computations in any way one cares to write a function to do. |