2024-02-09 10:44:16 +05:30
|
|
|
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)
|
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 = []
|
|
|
|
|
|
|
|
for payment_number in range(1, num_payments + 1):
|
|
|
|
# Calculate interest for the current period
|
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']
|
|
|
|
remaining_balance -= one_time_payment['amount']
|
|
|
|
|
|
|
|
# Update remaining balance
|
|
|
|
remaining_balance -= principal_payment
|
|
|
|
|
|
|
|
# Create a dictionary with payment details and add it to the amortization schedule
|
|
|
|
payment_details = {
|
|
|
|
'Payment Number': payment_number,
|
|
|
|
'Payment Amount': monthly_payment,
|
|
|
|
'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-09 11:35:35 +05:30
|
|
|
if __name__ == "__main__":
|
|
|
|
# Example usage
|
|
|
|
principal = 100000
|
|
|
|
interest_rate = 5.0
|
|
|
|
loan_term = 3
|
2024-02-09 10:44:16 +05:30
|
|
|
|
2024-02-09 11:35:35 +05:30
|
|
|
one_time_payment = {
|
|
|
|
'payment_number': 13,
|
|
|
|
'amount': 5000
|
|
|
|
}
|
2024-02-09 10:44:16 +05:30
|
|
|
|
2024-02-09 11:35:35 +05:30
|
|
|
schedule = generate_amortization_schedule(principal, interest_rate, loan_term, one_time_payment)
|
2024-02-09 10:44:16 +05:30
|
|
|
|
2024-02-09 11:35:35 +05:30
|
|
|
# Display the amortization schedule
|
|
|
|
for payment in schedule:
|
|
|
|
# print(payment)
|
|
|
|
attrs = [payment[key] for key in payment]
|
|
|
|
print(attrs)
|