mirror of
https://notabug.org/scuti/amort
synced 2024-11-25 16:28:50 +05:30
merge branch publish
This commit is contained in:
parent
53029f9925
commit
a1cdd69c42
2
Makefile
2
Makefile
@ -3,5 +3,5 @@ reference:
|
|||||||
amortize -P 100000 -r 0.05 -f monthly -n 36 -s > ref.txt
|
amortize -P 100000 -r 0.05 -f monthly -n 36 -s > ref.txt
|
||||||
|
|
||||||
test:
|
test:
|
||||||
python amort.py -p 100000 -i 5.0 -t 3 -ot "{\"payment_number\":13,\"amount\":5000}"
|
python src/amort.py -p 100000 -i 5.0 -t 3 -ot "{\"payment-number\":13,\"amount\":5000}"
|
||||||
|
|
||||||
|
45
README.md
Normal file
45
README.md
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
|
||||||
|
# amort
|
||||||
|
|
||||||
|
Generates an amortization schedule based on the principal, interest rate, term, and any additional payments towards principal.
|
||||||
|
|
||||||
|
## requirements
|
||||||
|
|
||||||
|
Only imports `argparse` and `json` for the main block of the script.
|
||||||
|
|
||||||
|
* saves results to a file `schedule.csv`
|
||||||
|
* does not access the web or internet
|
||||||
|
|
||||||
|
## usage
|
||||||
|
|
||||||
|
Invoke `make test` to generate a schedule based on loan of 100,000 over a 3 year term given an interest rate of 5.0% with a one-time payment towards principal of 5000 at the 13th month.
|
||||||
|
|
||||||
|
### extra payments
|
||||||
|
|
||||||
|
For extra payments towards principal, invoking the script will look like:
|
||||||
|
|
||||||
|
python src/amort.py -p {principal} -i {interest rate} -t {years} --extra-payments extra_payments.json
|
||||||
|
|
||||||
|
A valid json file contains a list named `extra-payments`. Each element is an object with the attributes `payment-number` and `amount`.
|
||||||
|
|
||||||
|
{
|
||||||
|
"extra-payments" : [
|
||||||
|
{
|
||||||
|
"payment-number": 1,
|
||||||
|
"amount": 500
|
||||||
|
}, {
|
||||||
|
"payment-number": 2,
|
||||||
|
"amount": 500
|
||||||
|
}, {
|
||||||
|
"payment-number": 3,
|
||||||
|
"amount": 500
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
* `payment-number` is the month when the payment has been made.
|
||||||
|
* `amount` is self-explanatory
|
||||||
|
|
||||||
|
## misc
|
||||||
|
|
||||||
|
This originated from me playing with ChatGPT. I asked it a question and it blurted out a semi-functional script as an answer. Then I made this repo to track the errors I fixed.
|
@ -1,6 +1,4 @@
|
|||||||
|
|
||||||
import argparse, json
|
|
||||||
|
|
||||||
def generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments=[]):
|
def generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments=[]):
|
||||||
# Convert interest rate to decimal and calculate periodic interest rate
|
# Convert interest rate to decimal and calculate periodic interest rate
|
||||||
monthly_interest_rate = interest_rate / 12 / 100
|
monthly_interest_rate = interest_rate / 12 / 100
|
||||||
@ -29,7 +27,7 @@ def generate_amortization_schedule(principal, interest_rate, loan_term, extra_pa
|
|||||||
principal_payment = round(monthly_payment - interest_payment, 2)
|
principal_payment = round(monthly_payment - interest_payment, 2)
|
||||||
|
|
||||||
# Apply one-time payment if provided
|
# Apply one-time payment if provided
|
||||||
if one_time_payment and payment_number == one_time_payment['payment_number']:
|
if one_time_payment and payment_number == one_time_payment['payment-number']:
|
||||||
principal_payment += one_time_payment['amount']
|
principal_payment += one_time_payment['amount']
|
||||||
if extra_payments != []:
|
if extra_payments != []:
|
||||||
one_time_payment = extra_payments.pop(0)
|
one_time_payment = extra_payments.pop(0)
|
||||||
@ -72,37 +70,14 @@ def get_totals(amortization_schedule, func=None):
|
|||||||
func(messages)
|
func(messages)
|
||||||
return total_paid, total_interest_paid, total_principal_paid
|
return total_paid, total_interest_paid, total_principal_paid
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
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}")
|
|
||||||
p.add_argument("--extra-payments", "-ep", type=str,\
|
|
||||||
help="facts in multiple one time payments (json file name)")
|
|
||||||
args = p.parse_args()
|
|
||||||
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
|
|
||||||
|
|
||||||
def compare(with_extra_payments, without):
|
def compare(with_extra_payments, without):
|
||||||
x,y,z = get_totals(with_extra_payments)
|
x,y,z = get_totals(with_extra_payments)
|
||||||
a,b,c = get_totals(without)
|
a,b,c = get_totals(without)
|
||||||
|
term_length = len(without) - len(with_extra_payments)
|
||||||
print(chr(916), "paid: ", round(x-a,2))
|
print(chr(916), "paid: ", round(x-a,2))
|
||||||
print(chr(916), "interest paid: ", round(y-b,2))
|
print(chr(916), "interest paid: ", round(y-b,2))
|
||||||
print(chr(916), "principal paid: ", round(abs(z-c),2))
|
print(chr(916), "principal paid: ", round(abs(z-c),2))
|
||||||
|
print(chr(916), "term (months): ", term_length)
|
||||||
|
|
||||||
def display(table):
|
def display(table):
|
||||||
print("id, paid, interest payment, principal payment, remaining")
|
print("id, paid, interest payment, principal payment, remaining")
|
||||||
@ -116,6 +91,34 @@ if __name__ == "__main__":
|
|||||||
print(row, file=f)
|
print(row, file=f)
|
||||||
print("wrote to file", filename)
|
print("wrote to file", filename)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import argparse, json
|
||||||
|
|
||||||
|
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}")
|
||||||
|
p.add_argument("--extra-payments", "-ep", type=str,\
|
||||||
|
help="facts in multiple one time payments (json file name)")
|
||||||
|
args = p.parse_args()
|
||||||
|
l = []
|
||||||
|
if args.extra_payments is not None:
|
||||||
|
with open(args.extra_payments) as f:
|
||||||
|
l = json.loads(f.read())
|
||||||
|
extra = []
|
||||||
|
if "extra-payments" in l:
|
||||||
|
extra = l["extra-payments"]
|
||||||
|
extra.sort(key=lambda k: k["payment-number"])
|
||||||
|
if args.one_time is not None:
|
||||||
|
extra.append(json.loads(args.one_time))
|
||||||
|
return args.principal, args.interest_rate, args.term, extra
|
||||||
|
|
||||||
principal, interest_rate, loan_term, extra_payments = get_arguments()
|
principal, interest_rate, loan_term, extra_payments = get_arguments()
|
||||||
|
|
||||||
schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments)
|
schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments)
|
Loading…
Reference in New Issue
Block a user