diff options
author | 3gg <3gg@shellblade.net> | 2024-01-02 19:43:21 +0100 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2024-01-02 19:43:21 +0100 |
commit | 8f3dadb7178a07ebadf782507cf2af5995321c71 (patch) | |
tree | 69081999c2845fb8d15013b3f1fde9ebe4cddf92 /mortgage.py |
Initial commitmain
Diffstat (limited to 'mortgage.py')
-rw-r--r-- | mortgage.py | 55 |
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 @@ | |||
1 | import argparse | ||
2 | import math | ||
3 | import sys | ||
4 | from tabulate import tabulate, SEPARATING_LINE | ||
5 | |||
6 | |||
7 | def 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 | |||
42 | def 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 | |||
54 | if __name__ == "__main__": | ||
55 | sys.exit(main()) | ||