amort/amort.py

99 lines
3.8 KiB
Python

import argparse, json
def generate_amortization_schedule(principal, interest_rate, loan_term, one_time_payment=None):
# Convert interest rate to decimal and calculate periodic interest rate
monthly_interest_rate = interest_rate / 12 / 100
# Calculate the number of monthly payments
num_payments = loan_term * 12
# Calculate the fixed monthly payment using the amortization formula
monthly_payment = (principal * monthly_interest_rate) / (1 - (1 + monthly_interest_rate) ** -num_payments)
monthly_payment = round(monthly_payment, 2)
# Initialize variables
remaining_balance = principal
amortization_schedule = []
for payment_number in range(1, num_payments + 1):
# Calculate interest for the current period
interest_payment = round(remaining_balance * monthly_interest_rate, 2)
# Calculate principal payment
principal_payment = round(monthly_payment - interest_payment, 2)
# Apply one-time payment if provided
if one_time_payment and payment_number == one_time_payment['payment_number']:
principal_payment += one_time_payment['amount']
remaining_balance -= one_time_payment['amount']
# Update remaining balance
remaining_balance -= principal_payment
if remaining_balance < 0:
principal_payment = principal_payment + remaining_balance
monthly_payment = monthly_payment + remaining_balance
remaining_balance = 0
# Create a dictionary with payment details and add it to the amortization schedule
payment_details = {
'Payment Number': payment_number,
'Payment Amount': round(monthly_payment,2),
'Interest Payment': interest_payment,
'Principal Payment': round(principal_payment,2),
'Remaining Balance': round(remaining_balance,2)
}
amortization_schedule.append(payment_details)
return amortization_schedule
if __name__ == "__main__":
# Example usage
# principal = 100000
# interest_rate = 5.0
# loan_term = 3
def get_arguments():
p = argparse.ArgumentParser()
p. add_argument("--principal", "-p", type=float, \
help="set value for principal")
p.add_argument("---interest-rate", "-i", type=float,\
help="set the value for interest rate (percentage)")
p.add_argument("--term", "-t", type=int,\
help="sets the term (years)")
p.add_argument("--one-time", "-ot", type=str,\
help="factors in a one-time payment (json, example: {\"payment_number\":13,\"amount\":5000}")
args = p.parse_args()
return args.principal, args.interest_rate, args.term, args.one_time
principal, interest_rate, loan_term, one_time = get_arguments()
if one_time is not None:
one_time_payment = json.loads(one_time)
else: one_time_payment = None
# one_time_payment = {
# 'payment_number': 13,
# 'amount': 5000
# }
schedule = generate_amortization_schedule(principal, interest_rate, loan_term, one_time_payment)
total_paid = 0
total_interest_paid = 0
total_principal_paid = 0
# Display the amortization schedule
print("id, paid, interest payment, principal payment, remaining")
for payment in schedule:
# print(payment)
total_paid += payment["Payment Amount"]
total_interest_paid += payment["Interest Payment"]
total_principal_paid += payment["Principal Payment"]
if payment["Remaining Balance"] < 0:
break
attrs = [payment[key] for key in payment]
print("%s" % ", ".join([str(attr) for attr in attrs]))
print("total paid: ", round(total_paid,2))
print("total interest paid: ", round(total_interest_paid,2))
print("total principal paid: ", round(total_principal_paid,2))