-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_functions.py
176 lines (125 loc) · 5.85 KB
/
home_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import locale
from decimal import Decimal
from functools import lru_cache
# Practicing with LruCache.
# May not be too useful in this tool, but trying my first start to get it at least working
@lru_cache(maxsize=10)
# Homebuying inputs received through the command-line interface
def home_buy_cli():
locale.setlocale(locale.LC_ALL, '')
# Home Price
while True:
try:
home_price = Decimal(input("How expensive of a home do you plan to buy? "))
#home_price_comma = "{:,}".format(home_price)
#print(home_price_comma)
#print(f"Your home price is: ${home_price:.2f}")
# other print option
# print("Your home price is: $" + "{:,}".format(home_price))
formatted_home_price = locale.currency(home_price, grouping=True)
print("Your home price is:", formatted_home_price)
break
except ValueError:
print("There's an error with the home price. Try again. Just the number is needed, no special characters.")
# Downpayment
while True:
try:
choice_dp = Decimal(input("Do you want provide the downpayment as an amount (option 1) or a percentage (option 2)? "))
except ValueError:
choice_dp = 0
# Downpayment as an amount
if choice_dp == 1:
try:
down_payment = Decimal(input("How much downpayment do you want to put down as an amount? "))
break
except:
print("Invalid input. Try again.")
# Downpayment as a percentage
elif choice_dp == 2:
try:
down_payment = (Decimal(input("how much downpayment do you want to put down as a percentage? ")) / 100) * home_price
break
except:
print("Invalid input. Try again.")
#down_payment = 0
else:
print("No down payment option selected. Try again.")
down_payment_percent = (down_payment/home_price)*100
# Old display option
#print("Your down payment is: $" + "{:,}".format(down_payment) + ". Which is " + f"{down_payment_percent:.2f}" + "%." )
formatted_down_payment = locale.currency(down_payment, grouping=True)
print(f"Your down payment is: {formatted_down_payment}. Which is {down_payment_percent:.2f}%.")
# Interest Rate
while True:
try:
interest_rate = Decimal(input("What interest rate do you have? (Ex. 7 for 7%) "))
break
except ValueError:
print("Invalid input. Try again.")
print("Your interest rate is: " + f"{interest_rate:.2f}" + "%." )
# Term of the loan
while True:
try:
loan_term = int(input("What is the term of the loan that you are considering in years? "))
print("Your loan term is: " + f"{loan_term}" + " years.")
break
except ValueError:
print("There's an error with loan term you provide. Just the number in years is needed, no special characters.")
return home_price, down_payment, down_payment_percent, interest_rate , loan_term
# Mortgage Rate Calculation
def mortgage_rate_calc(home_price, down_payment, interest_rate, loan_term):
# Assumptions that should be handled by earlier inputs
# All values are greater than 0.
# All values are numbers.
# home_price >= down_payment.
if interest_rate == 0:
mortgage_rate = (home_price - down_payment) / (12*loan_term)
elif loan_term == 0:
# Could improve the else eqn to just handle this scenario on its own
mortgage_rate = (home_price - down_payment)
else :
numerator = (interest_rate/1200)*((1+(interest_rate/1200))**(12*loan_term))
denominator = ((1+(interest_rate/1200))**(12*loan_term)) - 1
mortgage_rate = (home_price - down_payment) * (numerator / denominator)
return mortgage_rate
# Interest and Principal payment calculation for an amortization schedule
def interest_payment(current_balance, rate, term, mortgage_rate):
monthly_interest_payment = current_balance * ((rate/100)/12)
monthly_principal_payment = mortgage_rate - monthly_interest_payment
# The long way to calculate the above.
# monthly_principal_payment = (((current_balance * ((rate/100)/12))*((1 + (rate/100)/12)**(term*12))) / ((1 + ((rate/100)/12))**(term*12)-1)) - monthly_interest_payment
return monthly_interest_payment, monthly_principal_payment
# Plottable Data
def plottable_data(principal, annual_interest_rate, loan_term_years,mortgage_rate):
monthly_interest_payment = [0]
monthly_principal_payment = [0]
monthly_total_payment = [0]
months = [0]
monthly_total_equity = [0]
monthly_total_spend = [0]
monthly_total_interest_spend = [0]
remaining_balance = principal
for n in range((loan_term_years*12)):
ip,pp = interest_payment(remaining_balance,annual_interest_rate,loan_term_years-(n/12),mortgage_rate)
remaining_balance -= pp
# Appending
monthly_interest_payment.append(ip)
monthly_principal_payment.append(pp)
monthly_total_payment.append(ip + pp)
months.append(n+1)
monthly_total_equity.append(sum(monthly_principal_payment))
monthly_total_spend.append(sum(monthly_interest_payment)+sum(monthly_principal_payment))
monthly_total_interest_spend.append(sum(monthly_interest_payment))
pd = [
monthly_interest_payment,
monthly_principal_payment,
monthly_total_payment,
months,
monthly_total_equity,
monthly_total_spend,
monthly_total_interest_spend
]
#return monthly_interest_payment, monthly_principal_payment, monthly_total_payment, months, monthly_total_equity, monthly_total_spend, monthly_total_interest_spend
return pd
if __name__ == "__main__":
home_buy_cli()