Last active
March 17, 2020 17:45
-
-
Save FreddieLindsey/38610e37e2b801c6e6d1bd2c0eba1bde to your computer and use it in GitHub Desktop.
Mortgage Calculator (inc. balance remaining after a number of payments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import sys | |
mortgage_value = float(sys.argv[1]) | |
mortgage_rate = float(sys.argv[2]) / 100 | |
mortgage_term = float(sys.argv[3]) | |
mortgage_fixed_term_m = int(sys.argv[4]) if len(sys.argv) > 4 else mortgage_term * 12 | |
monthly_interest_rate = mortgage_rate / 12 | |
payments = mortgage_term * 12 | |
total_repayment = payments * mortgage_value * (monthly_interest_rate * pow(1 + monthly_interest_rate, payments)) / (pow(1 + monthly_interest_rate, payments) - 1) | |
total_repayment_m = total_repayment / payments | |
total_interest = total_repayment - mortgage_value | |
average_interest = total_interest / payments | |
average_repayment = total_repayment_m - average_interest | |
def print_line_groups(line_groups): | |
for line_group in line_groups: | |
print('') | |
for line in line_group: | |
print('{:>40s}:{:>4s}{:>12.2f}{:>2s}'.format(line[0], line[1], line[2], line[3])) | |
print_line_groups([[ | |
('Total interest', '£', total_interest, ''), | |
('Average interest / m', '£', average_interest, ''), | |
],[ | |
('Repayment / m', '£', average_repayment, ''), | |
('Total cost', '£', total_repayment, ''), | |
('Total cost / m', '£', total_repayment_m, ''), | |
]]) | |
calculation_payments = min(mortgage_fixed_term_m, mortgage_term * 12) | |
repayment_interest_amount_paid = 0 | |
eom_amount = mortgage_value | |
for i in range(calculation_payments): | |
interest = eom_amount * monthly_interest_rate | |
eom_amount -= (total_repayment_m - interest) | |
repayment_interest_amount_paid += interest | |
if calculation_payments < mortgage_term * 12: | |
print('\nAfter {} payments, there is £{:10.2f} left to repay'.format(mortgage_fixed_term_m, eom_amount)) | |
interest_only_amount_paid = monthly_interest_rate * mortgage_value * calculation_payments | |
print_line_groups([[ | |
('Repayment mortgage interest', '£', repayment_interest_amount_paid, ''), | |
('Interest-only mortgage interest', '£', interest_only_amount_paid, ''), | |
], [ | |
('Saving with repayment mortgage', '£', interest_only_amount_paid - repayment_interest_amount_paid, ''), | |
('', ' ', 100 * (interest_only_amount_paid - repayment_interest_amount_paid) / interest_only_amount_paid, '%'), | |
]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are taking out a £400,000 mortgage at a rate of 1.41% over a 5 year period (for the first 63 payments usually) you'd execute the following.