# # Analysis tool for stock performance. Similar to the built-in advanced portfolio # view, but does a better job at figuring out fees, taxes and dividens. Also # computes lifetime wins/losses and yearly increas percentages to ease comparisons # between stocks that you hold for different durations of time. import codecs import gnucash import math import sys from gnucashutil import full_acc_name out = codecs.getwriter('UTF-8')(sys.stdout) if len(sys.argv) == 1: sys.stderr.write("Invocation: %s gnucash_filename\n" % sys.argv[0]) sys.exit(1) data = gnucash.read_file(sys.argv[1]) def analyze_transactions(acc): sum_shares = 0 sum_invest = 0.0 sum_costs = 0.0 sum_fees = 0.0 sum_div_value = 0.0 sum_div_fees = 0.0 sum_div = 0.0 first_in = None last_sell = None splits = sorted(acc.splits, key=lambda x: x.transaction.post_date) processed_transactions = set()
def get_latest_share_value(acc, shares): commodity = acc.commodity prices = commodity.prices if len(prices) == 0: return float('NaN') last_price = prices[-1] value = shares * last_price.value return (value, last_price.value, last_price.date) out = sys.stdout if len(sys.argv) == 1: sys.stderr.write("Invocation: %s gnucash_filename\n" % sys.argv[0]) sys.exit(1) data = gnucash.read_file(sys.argv[1]) # Report gdiv_value = 0.0 gdiv_fees = 0.0 grealized_gain = 0.0 gunrealized_gain = 0.0 gfees = 0.0 for acc in data.accounts.values(): if acc.type != "STOCK" and acc.type != "MUTUAL": # out.write("IGNORE %s: %-40s\n" % (acc.type, full_acc_name(acc, 3))) continue name = full_acc_name(acc, 3) out.write("== %s ==\n" % (name,)) costs, invest, fees, shares, div_value, div_fees, first_in, last_sell = \
file_descriptor.close() else: file_descriptor = "" try: file_descriptor = open(args.outfile, "x") file_descriptor.close() except FileExistsError: temp = "File {} already exists, overwrite with '-x' option".format( args.outfile) print(temp) return file_descriptor = open(args.outfile, "w") file_descriptor.write(ledger_string()) file_descriptor.close() def main(): """Exports Gnucash data to Ledger format""" if args.outfile: write_ledger_file() else: print(ledger_string()) if __name__ == '__main__': """Reads command line arguments, converts data from Gnucash file to global variable, and calls the main function""" args = parse_arguments() data = gnucash.read_file(args.FILENAME) main()