def test_amortization_amount(): principal = 150000 period = 36 interest_rate = 0.1 amortization = (principal * (interest_rate * (1 + interest_rate)**period) / ((1 + interest_rate)**period - 1)) assert (calculate_amortization_amount(principal, interest_rate, period) == amortization)
def main(): # pragma: no cover import argparse from tabulate import tabulate parser = argparse.ArgumentParser( description= 'Python library for calculating amortizations and generating amortization schedules' ) # required parameters required = parser.add_argument_group('required arguments') required.add_argument('-P', '--principal', dest='principal', type=float, required=True, help='Principal amount') required.add_argument('-n', '--period', dest='period', type=int, required=True, help='Total number of periods') required.add_argument('-r', '--interest-rate', dest='interest_rate', type=float, required=True, help='Interest rate per period') # optional parameters parser.add_argument('-s', '--schedule', dest='schedule', default=False, action='store_true', help='Generate amortization schedule') arguments = parser.parse_args() if arguments.schedule: table = (x for x in amortization_schedule( arguments.principal, arguments.interest_rate, arguments.period)) print( tabulate(table, headers=[ "Number", "Amount", "Interest", "Principal", "Balance" ], floatfmt=",.2f", numalign="right")) else: amount = calculate_amortization_amount(arguments.principal, arguments.interest_rate, arguments.period) print("Amortization amount: {:,.2f}".format(amount))
def amortization_schedule(principal, interest_rate, period): """ Generates amortization schedule :param principal: Principal amount :param interest_rate: Interest rate per period :param period: Total number of periods :return: Rows containing period, interest, principal, balance, etc """ amortization_amount = calculate_amortization_amount( principal, interest_rate, period) number = 1 balance = principal while number <= period: interest = balance * interest_rate principal = amortization_amount - interest balance -= principal yield number, amortization_amount, interest, principal, balance if balance > 0 else 0 number += 1
def amortization_schedule( principal: float, interest_rate: float, period: int) -> Iterator[Tuple[int, float, float, float, float]]: """ Generates amortization schedule :param principal: Principal amount :param interest_rate: Interest rate per period :param period: Total number of periods :return: Rows containing period, amount, interest, principal, balance, etc """ amortization_amount = calculate_amortization_amount( principal, interest_rate, period) adjusted_interest = interest_rate / 12 balance = principal for number in range(1, period + 1): interest = round(balance * adjusted_interest, 2) if number < period: principal = amortization_amount - interest balance -= principal else: principal, amortization_amount, balance = balance, balance + interest, 0 yield number, amortization_amount, interest, principal, balance
def test_amortization_schedule(): principal = 150000 period = 36 interest_rate = 0.1 amortization_amount = calculate_amortization_amount( principal, interest_rate, period) number = 1 balance = principal for n, a, i, p, b in amortization_schedule(principal, interest_rate, period): interest = balance * interest_rate principal = amortization_amount - interest balance -= principal assert number == n assert amortization_amount == a assert interest == i assert principal == p assert balance == b number += 1
def _compute_monthly_20(self): for rec in self: rec.monthly_20 = calculate_amortization_amount( rec.loanable_amount, rec.interest_rate / 12, 240)
def main(): # pragma: no cover import argparse from tabulate import tabulate parser = argparse.ArgumentParser( description= "Python library for calculating amortizations and generating amortization schedules" ) # required parameters required = parser.add_argument_group("required arguments") required.add_argument( "-P", "--principal", dest="principal", type=float, required=True, help="Principal amount", ) required.add_argument( "-n", "--period", dest="period", type=int, required=True, help="Total number of periods", ) required.add_argument( "-r", "--interest-rate", dest="interest_rate", type=float, required=True, help="Interest rate per period", ) # optional parameters parser.add_argument( "-s", "--schedule", dest="schedule", default=False, action="store_true", help="Generate amortization schedule", ) arguments = parser.parse_args() if arguments.schedule: table = (x for x in amortization_schedule( arguments.principal, arguments.interest_rate, arguments.period)) print( tabulate( table, headers=[ "Number", "Amount", "Interest", "Principal", "Balance" ], floatfmt=",.2f", numalign="right", )) else: amount = calculate_amortization_amount(arguments.principal, arguments.interest_rate, arguments.period) print("Amortization amount: {:,.2f}".format(amount))
def main() -> None: # pragma: no cover import argparse from tabulate import tabulate parser = argparse.ArgumentParser( description= "Python library for calculating amortizations and generating amortization schedules" ) # required parameters required = parser.add_argument_group("required arguments") required.add_argument( "-P", "--principal", dest="principal", type=float, required=True, help="Principal amount", ) required.add_argument( "-r", "--interest-rate", dest="interest_rate", type=float, required=True, help="Interest rate per period", ) # optional parameters parser.add_argument( "-s", "--schedule", dest="schedule", default=False, action="store_true", help="Generate amortization schedule", ) mutually_exclusive = parser.add_mutually_exclusive_group(required=True) mutually_exclusive.add_argument( "-n", "--period", dest="period", type=int, help="Total number of periods", ) mutually_exclusive.add_argument( "-a", "--amount", dest="amount", type=float, help="Amortization amount per period", ) arguments = parser.parse_args() if arguments.schedule: if arguments.period is None: parser.error("-s/--schedule requires -n/--period") total_paid = total_interest = total_principal = 0.0 table: List[Any] = [] for row in amortization_schedule(arguments.principal, arguments.interest_rate, arguments.period): table.append(row) total_paid += row[1] total_interest += row[2] total_principal += row[3] table.append(("Totals", total_paid, total_interest, total_principal)) print( tabulate( table, headers=[ "Number", "Amount", "Interest", "Principal", "Balance" ], floatfmt=",.2f", numalign="right", )) elif arguments.amount: period = calculate_amortization_period(arguments.principal, arguments.interest_rate, arguments.amount) print("Amortization period: {}".format(period)) else: amount = calculate_amortization_amount(arguments.principal, arguments.interest_rate, arguments.period) print("Amortization amount: {:,.2f}".format(amount))