/
mortgageMaxAmount.ts
283 lines (261 loc) · 9.14 KB
/
mortgageMaxAmount.ts
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import mortgageInsurancePremium from "./mortgageInsurancePremium.js"
/**
* Calculates the maximum purchase price (and other variables) for a property a person can afford and the related mortgage it would qualify for based on annual income, down payment, mortgage interest rate, and additional options.
*
* ```ts
* // With an annual income of $100,000, a down payment of $25,000, and a rate of 5.25%.
* const results = maxMortgageAmount(100_000, 25_000, 5.25)
* // results = {
* // annualIncome: 100000,
* // downPayment: 25000,
* // rate: 5.25,
* // rateTested: 7.25,
* // purchasePrice: 307000,
* // mortgageAmount: 293280,
* // insurancePremium: 11280,
* // monthlyMortgagePayment: 2099.65,
* // grossDebtServiceRatio: 0.32,
* // totalDebtServiceRatio: 0.32,
* // reason: "Gross debt service ratio would be above threshold of 0.32 with a bigger amount",
* // monthlyDebtPayment: 0,
* // monthlyHeating: 175,
* // isHeatingEstimate: true,
* // monthlyTax: 385,
* // isTaxEstimate: true,
* // monthlyCondoFees: 0,
* // }
* ```
*
* @param annualIncome - The annual income of the borrower.
* @param downPayment - The amount of money paid upfront.
* @param rate - The mortgage interest rate.
* @param options - Additional options such.
* @param options.monthlyDebtPayment - The monthly debt payment of the borrower. Defaults to 0.
* @param options.monthlyHeating - The monthly heating cost. Defaults to $175, like Royal Bank of Canada does.
* @param options.monthlyTax - The monthly property tax. Default to 1.5% of the purchase price, like Royal Bak of Canada does.
* @param options.monthlyCondoFees - The monthly condo fees. Defaults to 0.
*
* @category Finance
**/
export default function mortgageMaxAmount(
annualIncome: number,
downPayment: number,
rate: number,
options: {
monthlyDebtPayment?: number
monthlyHeating?: number
monthlyTax?: number
monthlyCondoFees?: number
} = {}
): {
annualIncome: number
downPayment: number
rate: number
rateTested: number
purchasePrice: number
mortgageAmount: number
insurancePremium: number
monthlyMortgagePayment: number
grossDebtServiceRatio: number
totalDebtServiceRatio: number
reason: string
monthlyDebtPayment: number
monthlyHeating: number
isHeatingEstimate: boolean
monthlyTax: number
isTaxEstimate: boolean
monthlyCondoFees: number
} {
const monthlyIncome = annualIncome / 12
// For the stress test, the rate should be the highest value between rate+2 or 5.25
const rateTested = parseFloat(Math.max(rate + 2, 5.25).toFixed(2))
// Default monthlyDebtPayment is 0
const monthlyDebtPayment = options.monthlyDebtPayment ?? 0
// Default monthly heating is $175, like Royal Bank of Canada.
const monthlyHeating = options.monthlyHeating ?? 175
const isHeatingEstimate =
typeof options.monthlyHeating === "number" ? false : true
// Default condo fees is $0
const monthlyCondoFees = options.monthlyCondoFees ?? 0
// We calculate variables for the monthly payment. Formula comes from the mortgagePayment function and is applicable only for fixed mortgage rates in Canada.
const nominalRate = rateTested / 100
const annualEffectiveRate = Math.pow(1 + nominalRate / 2, 2) // Compounded two times per year
const monthlyRate = Math.pow(annualEffectiveRate, 1 / 12) - 1
const amortizationPeriodinMonths = 25 * 12 // Amortization period of 25 years
// We will return an object. We define it here.
const results = {
annualIncome,
downPayment,
rate,
rateTested,
purchasePrice: downPayment,
mortgageAmount: 0,
insurancePremium: 0,
monthlyMortgagePayment: 0,
grossDebtServiceRatio: 0,
totalDebtServiceRatio: 0,
reason: "",
monthlyDebtPayment,
monthlyHeating,
isHeatingEstimate,
monthlyTax: 0,
isTaxEstimate: false,
monthlyCondoFees,
}
// We start with the down payment and increment by $100,000
findMaxAmount(
downPayment,
100_000,
monthlyIncome,
downPayment,
monthlyRate,
amortizationPeriodinMonths,
monthlyHeating,
monthlyCondoFees,
monthlyDebtPayment,
options,
results
)
// Then with the purchase price previously found but we increment by $10,000
findMaxAmount(
results.purchasePrice,
10_000,
monthlyIncome,
downPayment,
monthlyRate,
amortizationPeriodinMonths,
monthlyHeating,
monthlyCondoFees,
monthlyDebtPayment,
options,
results
)
// Again but $1,000 increment
findMaxAmount(
results.purchasePrice,
1_000,
monthlyIncome,
downPayment,
monthlyRate,
amortizationPeriodinMonths,
monthlyHeating,
monthlyCondoFees,
monthlyDebtPayment,
options,
results
)
if (results.purchasePrice === 10_000_000) {
results.reason = "max purchase price"
}
// We return the results.
return results
}
// A function where we loop and test mortgage amounts.
function findMaxAmount(
startPrice: number,
increment: number,
monthlyIncome: number,
downPayment: number,
monthlyRate: number,
amortizationPeriodinMonths: number,
monthlyHeating: number,
monthlyCondoFees: number,
monthlyDebtPayment: number,
options: { monthlyTax?: number },
results: {
annualIncome: number
downPayment: number
rate: number
rateTested: number
purchasePrice: number
mortgageAmount: number
insurancePremium: number
monthlyMortgagePayment: number
grossDebtServiceRatio: number
totalDebtServiceRatio: number
reason: string
monthlyDebtPayment: number
monthlyHeating: number
isHeatingEstimate: boolean
monthlyTax: number
isTaxEstimate: boolean
monthlyCondoFees: number
}
) {
// We start with zeros
let grossDebtServiceRatio = 0
let totalDebtServiceRatio = 0
// We test amount up to $10,000,000
let downPaymentTooLow = false
for (
let purchasePrice = startPrice;
purchasePrice <= 10_000_000;
purchasePrice += increment
) {
// We check that the down payment is enough
if (purchasePrice <= 500_000) {
const downPaymentMin = purchasePrice * 0.05
if (downPayment < downPaymentMin) {
downPaymentTooLow = true
}
} else if (purchasePrice > 500_000 && purchasePrice < 1_000_000) {
const downPaymentMin = 25_000 + (purchasePrice - 500_000) * 0.1
if (downPayment < downPaymentMin) {
downPaymentTooLow = true
}
} else if (purchasePrice >= 1_000_000) {
const downPaymentMin = purchasePrice * 0.2
if (downPayment < downPaymentMin) {
downPaymentTooLow = true
}
}
if (downPaymentTooLow) {
results.reason = `downPayment limit`
break
}
// We calculate the insurance premium
const insurancePremium = mortgageInsurancePremium(
purchasePrice,
downPayment
)
// Then the actual mortgage amount
const mortgageAmount =
Math.max(purchasePrice - downPayment, 0) + insurancePremium
// And monthly mortgage payment
const monthlyMortgagePayment = Math.round(
(monthlyRate * mortgageAmount) /
(1 - Math.pow(1 + monthlyRate, -amortizationPeriodinMonths))
)
// The default annual tax rate is 1.5% of purchase price, like Royal Bank of Canada.
const monthlyTax =
options.monthlyTax ?? Math.round((purchasePrice * 0.015) / 12)
results.monthlyTax = monthlyTax
results.isTaxEstimate =
typeof options.monthlyTax === "number" ? false : true
const monthlyHomeExpenses =
monthlyMortgagePayment +
monthlyHeating +
monthlyTax +
monthlyCondoFees
// We update the ratios.
grossDebtServiceRatio = monthlyHomeExpenses / monthlyIncome
totalDebtServiceRatio =
(monthlyHomeExpenses + monthlyDebtPayment) / monthlyIncome
// If the GDS or TDS are equal or above the thresholds established by the Financial Consumer Agency of Canada, we break. Otherwise, we update the results values.
if (grossDebtServiceRatio >= 0.32 || totalDebtServiceRatio >= 0.4) {
results.reason = `debt limit`
break
} else {
results.purchasePrice = purchasePrice
results.mortgageAmount = mortgageAmount
results.insurancePremium = insurancePremium
results.monthlyMortgagePayment = monthlyMortgagePayment
results.grossDebtServiceRatio = parseFloat(
grossDebtServiceRatio.toFixed(2)
)
results.totalDebtServiceRatio = parseFloat(
totalDebtServiceRatio.toFixed(2)
)
}
}
}