From 8f3dadb7178a07ebadf782507cf2af5995321c71 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 2 Jan 2024 19:43:21 +0100 Subject: Initial commit --- mortgage.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 mortgage.py (limited to 'mortgage.py') diff --git a/mortgage.py b/mortgage.py new file mode 100644 index 0000000..3b9b958 --- /dev/null +++ b/mortgage.py @@ -0,0 +1,55 @@ +import argparse +import math +import sys +from tabulate import tabulate, SEPARATING_LINE + + +def calc_payments(principal, annual_interest, installments): + p = principal + i = annual_interest / 100 / 12 # Monthly equivalent. + n = installments + + def fmt(x): + return f"{math.ceil(x*100)/100:.2f}" + + table = [["Time", "Outstanding Debt", "Monthly Installment", "Interest Paid", "Amount Amortised"]] + + monthly_installment = p * (i * (1 + i)**n) / ((1 + i)**n - 1) + debt = principal + total_interest = 0 + total_amortised = 0 + + for k in range(n): + interest = i * debt + amortised = monthly_installment - interest + table.append([k] + [fmt(x) for x in [debt, monthly_installment, interest, amortised]]) + + total_interest += interest + total_amortised += amortised + debt -= amortised + + total_interest_pct = total_interest / principal * 100 + + table.append(SEPARATING_LINE) + table.append(["Total", "", "Amount Paid", "Interest Paid", "Amount Amortised"]) + table.append(["", ""] + [ + fmt(monthly_installment * n), + fmt(total_interest) + f" ({fmt(total_interest_pct)}%)", + fmt(total_amortised)]) + return table + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("-p", "--principal", type=float, default=100000, help="Principal") + parser.add_argument("-i", "--interest", type=float, default=5, help="Annual interest %") + parser.add_argument("-n", "--installments", type=int, default=300, help="Number of installments") + args = parser.parse_args() + + table = calc_payments(args.principal, args.interest, args.installments) + print(tabulate(table)) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) -- cgit v1.2.3