summaryrefslogtreecommitdiff
path: root/mortgage.py
diff options
context:
space:
mode:
Diffstat (limited to 'mortgage.py')
-rw-r--r--mortgage.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/mortgage.py b/mortgage.py
new file mode 100644
index 0000000..3b9b958
--- /dev/null
+++ b/mortgage.py
@@ -0,0 +1,55 @@
1import argparse
2import math
3import sys
4from tabulate import tabulate, SEPARATING_LINE
5
6
7def calc_payments(principal, annual_interest, installments):
8 p = principal
9 i = annual_interest / 100 / 12 # Monthly equivalent.
10 n = installments
11
12 def fmt(x):
13 return f"{math.ceil(x*100)/100:.2f}"
14
15 table = [["Time", "Outstanding Debt", "Monthly Installment", "Interest Paid", "Amount Amortised"]]
16
17 monthly_installment = p * (i * (1 + i)**n) / ((1 + i)**n - 1)
18 debt = principal
19 total_interest = 0
20 total_amortised = 0
21
22 for k in range(n):
23 interest = i * debt
24 amortised = monthly_installment - interest
25 table.append([k] + [fmt(x) for x in [debt, monthly_installment, interest, amortised]])
26
27 total_interest += interest
28 total_amortised += amortised
29 debt -= amortised
30
31 total_interest_pct = total_interest / principal * 100
32
33 table.append(SEPARATING_LINE)
34 table.append(["Total", "", "Amount Paid", "Interest Paid", "Amount Amortised"])
35 table.append(["", ""] + [
36 fmt(monthly_installment * n),
37 fmt(total_interest) + f" ({fmt(total_interest_pct)}%)",
38 fmt(total_amortised)])
39 return table
40
41
42def main():
43 parser = argparse.ArgumentParser()
44 parser.add_argument("-p", "--principal", type=float, default=100000, help="Principal")
45 parser.add_argument("-i", "--interest", type=float, default=5, help="Annual interest %")
46 parser.add_argument("-n", "--installments", type=int, default=300, help="Number of installments")
47 args = parser.parse_args()
48
49 table = calc_payments(args.principal, args.interest, args.installments)
50 print(tabulate(table))
51 return 0
52
53
54if __name__ == "__main__":
55 sys.exit(main())