2024-02-10 06:17:56 +05:30
|
|
|
|
2024-02-09 12:59:32 +05:30
|
|
|
import argparse, json
|
|
|
|
|
2024-02-10 10:01:04 +05:30
|
|
|
def generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments=[]):
|
2024-02-09 10:44:16 +05:30
|
|
|
# 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)
|
2024-02-09 11:35:35 +05:30
|
|
|
monthly_payment = round(monthly_payment, 2)
|
2024-02-09 10:44:16 +05:30
|
|
|
|
|
|
|
# Initialize variables
|
|
|
|
remaining_balance = principal
|
|
|
|
amortization_schedule = []
|
|
|
|
|
2024-02-10 10:01:04 +05:30
|
|
|
one_time_payment = None
|
|
|
|
if extra_payments != []:
|
|
|
|
one_time_payment = extra_payments.pop(0)
|
2024-02-09 10:44:16 +05:30
|
|
|
for payment_number in range(1, num_payments + 1):
|
|
|
|
# Calculate interest for the current period
|
2024-02-11 00:28:05 +05:30
|
|
|
if remaining_balance == 0:
|
|
|
|
break
|
2024-02-09 11:35:35 +05:30
|
|
|
interest_payment = round(remaining_balance * monthly_interest_rate, 2)
|
2024-02-09 10:44:16 +05:30
|
|
|
|
|
|
|
# Calculate principal payment
|
2024-02-09 11:35:35 +05:30
|
|
|
principal_payment = round(monthly_payment - interest_payment, 2)
|
2024-02-09 10:44:16 +05:30
|
|
|
|
|
|
|
# Apply one-time payment if provided
|
|
|
|
if one_time_payment and payment_number == one_time_payment['payment_number']:
|
|
|
|
principal_payment += one_time_payment['amount']
|
2024-02-10 10:01:04 +05:30
|
|
|
if extra_payments != []:
|
|
|
|
one_time_payment = extra_payments.pop(0)
|
2024-02-09 10:44:16 +05:30
|
|
|
# Update remaining balance
|
|
|
|
remaining_balance -= principal_payment
|
|
|
|
|
2024-02-10 06:17:56 +05:30
|
|
|
if remaining_balance < 0:
|
|
|
|
principal_payment = principal_payment + remaining_balance
|
2024-02-10 09:15:45 +05:30
|
|
|
monthly_payment = monthly_payment + remaining_balance
|
2024-02-10 06:17:56 +05:30
|
|
|
remaining_balance = 0
|
|
|
|
|
2024-02-09 10:44:16 +05:30
|
|
|
# Create a dictionary with payment details and add it to the amortization schedule
|
|
|
|
payment_details = {
|
|
|
|
'Payment Number': payment_number,
|
2024-02-10 09:15:45 +05:30
|
|
|
'Payment Amount': round(monthly_payment,2),
|
2024-02-09 10:44:16 +05:30
|
|
|
'Interest Payment': interest_payment,
|
2024-02-09 11:35:35 +05:30
|
|
|
'Principal Payment': round(principal_payment,2),
|
|
|
|
'Remaining Balance': round(remaining_balance,2)
|
2024-02-09 10:44:16 +05:30
|
|
|
}
|
|
|
|
amortization_schedule.append(payment_details)
|
|
|
|
return amortization_schedule
|
|
|
|
|
2024-02-11 01:45:32 +05:30
|
|
|
def get_totals(amortization_schedule, func=None):
|
2024-02-11 00:28:05 +05:30
|
|
|
total_paid = 0
|
|
|
|
total_interest_paid = 0
|
|
|
|
total_principal_paid = 0
|
|
|
|
# Display the amortization schedule
|
2024-02-11 01:45:32 +05:30
|
|
|
messages = []
|
2024-02-11 00:28:05 +05:30
|
|
|
for payment in amortization_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]
|
2024-02-11 01:45:32 +05:30
|
|
|
if func is not None:
|
|
|
|
messages.append("%s" % ", ".join([str(attr) for attr in attrs]))
|
|
|
|
if func is not None:
|
|
|
|
func(messages)
|
2024-02-11 00:28:05 +05:30
|
|
|
return total_paid, total_interest_paid, total_principal_paid
|
|
|
|
|
2024-02-09 11:35:35 +05:30
|
|
|
if __name__ == "__main__":
|
2024-02-09 12:59:32 +05:30
|
|
|
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}")
|
2024-02-10 10:01:04 +05:30
|
|
|
p.add_argument("--extra-payments", "-ep", type=str,\
|
|
|
|
help="facts in multiple one time payments (json file name)")
|
2024-02-09 12:59:32 +05:30
|
|
|
args = p.parse_args()
|
2024-02-10 10:01:04 +05:30
|
|
|
l = []
|
|
|
|
if args.extra_payments is not None:
|
|
|
|
with open(args.extra_payments) as f:
|
|
|
|
l = json.loads(f.read())
|
|
|
|
# print(extra["extra-payments"])
|
|
|
|
if args.one_time is not None:
|
|
|
|
l.append(json.loads(args.one_time))
|
|
|
|
extra = l["extra-payments"]
|
|
|
|
extra.sort(key=lambda k: k["payment_number"])
|
|
|
|
return args.principal, args.interest_rate, args.term, extra
|
2024-02-11 00:28:05 +05:30
|
|
|
|
|
|
|
def compare(with_extra_payments, without):
|
|
|
|
x,y,z = get_totals(with_extra_payments)
|
|
|
|
a,b,c = get_totals(without)
|
2024-02-11 01:45:32 +05:30
|
|
|
print(chr(916), "paid: ", round(x-a,2))
|
|
|
|
print(chr(916), "interest paid: ", round(y-b,2))
|
|
|
|
print(chr(916), "principal paid: ", round(abs(z-c),2))
|
|
|
|
|
|
|
|
def display(table):
|
|
|
|
print("id, paid, interest payment, principal payment, remaining")
|
|
|
|
for row in table:
|
|
|
|
print(row)
|
|
|
|
|
|
|
|
def export(table, filename="schedule.csv"):
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
print("id, paid, interest payment, principal payment, remaining", file=f)
|
|
|
|
for row in table:
|
|
|
|
print(row, file=f)
|
|
|
|
print("wrote to file", filename)
|
2024-02-11 00:28:05 +05:30
|
|
|
|
2024-02-10 10:01:04 +05:30
|
|
|
principal, interest_rate, loan_term, extra_payments = get_arguments()
|
2024-02-09 10:44:16 +05:30
|
|
|
|
2024-02-10 10:01:04 +05:30
|
|
|
schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments)
|
2024-02-11 01:45:32 +05:30
|
|
|
paid, interest_paid, principal_paid = get_totals(schedule,export)
|
2024-02-09 10:44:16 +05:30
|
|
|
|
2024-02-11 00:28:05 +05:30
|
|
|
print("total paid: ", round(paid,2))
|
|
|
|
print("total interest paid: ", round(interest_paid,2))
|
|
|
|
print("total principal paid: ", round(principal_paid,2))
|
2024-02-10 09:15:45 +05:30
|
|
|
|
2024-02-11 00:28:05 +05:30
|
|
|
# without extra payments for comparison
|
|
|
|
compare(schedule, generate_amortization_schedule(
|
|
|
|
principal, interest_rate, loan_term)
|
|
|
|
)
|
2024-02-10 06:17:56 +05:30
|
|
|
|