Skip to content

Commit

Permalink
Merge pull request #4 from mclaypool/FinishInitialLoanClass
Browse files Browse the repository at this point in the history
Finish initial loan class
  • Loading branch information
mclaypool authored Jan 16, 2018
2 parents fdd21a6 + 37d22a8 commit ed03643
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 99 deletions.
96 changes: 75 additions & 21 deletions sandbox_api/MinProd/server/controllers/loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,67 @@
class LoanController():
# Payments --------------------------------------------------------
@staticmethod
def get_monthly_payment(loan_terms):
def get_payment(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']

return LoanView.display_basic('Monthly Payment',
Loan.calc_monthly_payment(rate, years, amount))
return LoanView.display_basic('Payment',
Loan.calc_payment(rate, years, amount, yearly_compounds))


@staticmethod
def get_payment_ppart():
def get_payment_ppart(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']
current_period = loan_terms['current_period']

return LoanView.display_basic('Principle Part',
Loan.calc_payment_ppart())
Loan.calc_payment_ppart(
rate, years, amount, yearly_compounds, current_period))


@staticmethod
def get_payment_ipart():
def get_payment_ipart(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']
current_period = loan_terms['current_period']

return LoanView.display_basic('Interest Part',
Loan.calc_payment_ipart())
Loan.calc_payment_ipart(
rate, years, amount, yearly_compounds, current_period))


# Principle -------------------------------------------------------
@staticmethod
def get_principle_paid():
def get_principle_paid(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']
current_period = loan_terms['current_period']

return LoanView.display_basic('Principle Paid',
Loan.calc_principle_paid())
Loan.calc_principle_paid(
rate, years, amount, yearly_compounds, current_period))


@staticmethod
def get_principle_remaining():
def get_principle_remaining(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']
current_period = loan_terms['current_period']

return LoanView.display_basic('Principle Remaining',
Loan.calc_principle_remaining())
Loan.calc_principle_remaining(
rate, years, amount, yearly_compounds, current_period))


# Interest --------------------------------------------------------
Expand All @@ -46,47 +75,72 @@ def get_total_interest(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']

return LoanView.display_basic('Total Interest',
Loan.calc_total_interest(rate, years, amount))
Loan.calc_total_interest(
rate, years, amount, yearly_compounds))


@staticmethod
def get_interest_paid():
def get_interest_paid(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']
current_period = loan_terms['current_period']

return LoanView.display_basic('Interest Paid',
Loan.calc_interest_paid())
Loan.calc_interest_paid(
rate, years, amount, yearly_compounds, current_period))


@staticmethod
def get_interest_remaining():
def get_interest_remaining(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']
current_period = loan_terms['current_period']

return LoanView.display_basic('Interest Remaining',
Loan.calc_interest_remaining())
Loan.calc_interest_remaining(
rate, years, amount, yearly_compounds, current_period))


# Totals --------------------------------------------------------
@staticmethod
def get_total_cost(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']

return LoanView.display_basic('Total Cost',
Loan.calc_total_cost(rate, years, amount))
Loan.calc_total_cost(
rate, years, amount, yearly_compounds))


@staticmethod
def get_total_remaining(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']
current_period = loan_terms['current_period']

return LoanView.display_basic('Total Remaining',
Loan.calc_total_remaining(
rate, years, amount, current_period))
rate, years, amount, yearly_compounds, current_period))


# Charting --------------------------------------------------------
@staticmethod
def get_amortization_schedule():
return LoanView.display_amortization_schedule(
Loan.calc_amortization_schedule())
def get_amortization(loan_terms):
rate = loan_terms['apr']
years = loan_terms['years']
amount = loan_terms['amount']
yearly_compounds = loan_terms['yearly_compounds']

return LoanView.display_amortization(
Loan.amortize_loan(rate, years, amount, yearly_compounds))
149 changes: 122 additions & 27 deletions sandbox_api/MinProd/server/models/loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,68 +23,141 @@ class Loan(Base):

# Payments --------------------------------------------------------
@staticmethod
def calc_monthly_payment(apr, years, amount):
# pmt = (r * PV) / (1 - (1 + r)^-n)
payment = (apr/12)*amount / (1 - (1 + apr/12)**-(years*12))
def calc_payment(apr, years, amount, yearly_compounds):
# Formula is: pmt = (r * PV) / (1 - (1 + r)^-n)

# calc initial vars
period_rate = apr / yearly_compounds
count_periods = years * yearly_compounds

# calc payment
payment = period_rate * amount / \
(1 - (1 + period_rate) ** -count_periods)

return round(payment, 2)


@staticmethod
def calc_payment_ppart():
return None
def calc_payment_ppart(
apr, years, amount, yearly_compounds, current_period):

amort_sched = Loan.calc_amort_sched(
apr, years, amount, yearly_compounds)

return amort_sched[current_period][2]


@staticmethod
def calc_payment_ipart():
return None
def calc_payment_ipart(
apr, years, amount, yearly_compounds, current_period):

amort_sched = Loan.calc_amort_sched(
apr, years, amount, yearly_compounds)

return amort_sched[current_period][1]


# Principle -------------------------------------------------------
@staticmethod
def calc_principle_paid():
return None
def calc_principle_paid(
apr, years, amount, yearly_compounds, current_period):

amort_sched = Loan.calc_amort_sched(
apr, years, amount, yearly_compounds)

period = 1
principle_paid = 0

while (period <= current_period):
principle_paid = principle_paid + amort_sched[period][2]
period = period + 1

return principle_paid


@staticmethod
def calc_principle_remaining():
return None
def calc_principle_remaining(
apr, years, amount, yearly_compounds, current_period):

amort_sched = Loan.calc_amort_sched(
apr, years, amount, yearly_compounds)

period = current_period + 1
prin_remain = 0

while (period <= years * yearly_compounds):
prin_remain = prin_remain + amort_sched[period][2]
period = period + 1

return prin_remain


# Interest --------------------------------------------------------
@staticmethod
def calc_total_interest(apr, years, amount):
total_interest = Loan.calc_total_cost(apr, years, amount)
total_interest = total_interest - amount
return round(total_interest, 2)
def calc_total_interest(apr, years, amount, yearly_compounds):
total_interest = Loan.calc_total_cost(
apr, years, amount, yearly_compounds)

return round(total_interest - amount, 2)


@staticmethod
def calc_interest_paid():
return None
def calc_interest_paid(
apr, years, amount, yearly_compounds, current_period):

amort_sched = Loan.calc_amort_sched(
apr, years, amount, yearly_compounds)

period = 1
interest_paid = 0

while (period <= current_period):
interest_paid = interest_paid + amort_sched[period][1]
period = period + 1

return interest_paid

@staticmethod
def calc_interest_remaining():
return None
def calc_interest_remaining(
apr, years, amount, yearly_compounds, current_period):

amort_sched = Loan.calc_amort_sched(
apr, years, amount, yearly_compounds)

period = current_period + 1
interest_remain = 0

while (period <= years * yearly_compounds):
interest_remain = interest_remain + amort_sched[period][1]
period = period + 1

return round(interest_remain,2)


# Totals ----------------------------------------------------------
@staticmethod
def calc_total_cost(apr, years, amount):
total_cost = Loan.calc_monthly_payment(apr, years, amount)
total_cost = total_cost * 12 * years
def calc_total_cost(apr, years, amount, yearly_compounds):
total_cost = Loan.calc_payment(
apr, years, amount, yearly_compounds)

total_cost = total_cost * yearly_compounds * years
return round(total_cost, 2)


@staticmethod
def calc_total_remaining(apr, years, amount, current_period):
def calc_total_remaining(
apr, years, amount, yearly_compounds, current_period):
# http://financeformulas.net/Remaining_Balance_Formula.html
t = current_period

total_payments = Loan.calc_monthly_payment(apr, years, amount)
total_payments = Loan.calc_payment(
apr, years, amount, yearly_compounds)

total_payments = total_payments * ( ((1 + apr)**t - 1) / apr )

total_remaining = Loan.calc_total_cost(apr, years, amount)
total_remaining = Loan.calc_total_cost(
apr, years, amount, yearly_compounds)

total_remaining = total_remaining - total_payments

return round(total_remaining, 2)
Expand All @@ -98,5 +171,27 @@ def estimate_payoff_date():

# Charting --------------------------------------------------------
@staticmethod
def calc_amortization_schedule():
return None
def amortize_loan(apr, years, amount, yearly_compounds):
# TODO: Replace with pandas

# calc initial vars
pmt = Loan.calc_payment(apr, years, amount, yearly_compounds)
period_rate = apr / yearly_compounds
count_periods = years * yearly_compounds

# dict is the follow: {period, [pmt, ipmt, ppmt, balance]}
amort_sched = {}
amort_sched[0] = [0, 0, 0, amount]

period = 1
while (period <= count_periods):
# get period payment parts and balance
ipmt = round(amort_sched[period-1][3] * period_rate, 2)
ppmt = round(pmt - ipmt, 2)
balance = round(amort_sched[period-1][3] - ppmt, 2)

# save and move to next period
amort_sched[period] = [pmt, ipmt, ppmt, balance]
period = period + 1

return amort_sched
Loading

0 comments on commit ed03643

Please sign in to comment.