金融贷款等额本息计算每期还款金额

最近转战金融贷款业务,接口自动化用的sosotest,感觉挺好用,关于等额本金计算还款金额代码如下

# 前n期的installment
def getInstallment(interestRate, tenor, loanAmt, tenorNum):
if tenorNum != tenor:
installment = numpy.around(numpy_financial.pmt(interestRate / 12, tenor, -loanAmt),2)
else:
principalLastTenor = numpy.around(getPrincipleForEachTenor(loanAmt, interestRate, tenor, tenorNum),2)
interestLastTenor = getInterestForEachTenor(loanAmt, interestRate, tenorNum)
installment = numpy.around((principalLastTenor + interestLastTenor),2)
return numpy.around(installment, 2)

# 前tenorNum期principal总和
def getPrincipleSumBefore(interestRate, tenorNum, loanAmt):
principleList = []
for i in range(1, tenorNum+1):
if i == 1:
installment = getInstallment(interestRate, tenor, loanAmt, tenorNum)
interest = numpy.around(loanAmt * interestRate / 12, 2)
principle = installment - interest
principleList.append(principle)
# print("第一期本金", principle)
else:
installment = getInstallment(interestRate, tenor, loanAmt, tenorNum)
interest = numpy.around((loanAmt - getPrincipleSumBefore(interestRate, i - 1, loanAmt)) * interestRate / 12, 2)
principle = installment - interest
# print("二期", principle)
principleList.append(principle)
#print("sum(principleList)=",i,sum(principleList))
return sum(principleList)

 


def getInterestForEachTenor(loanAmt, interestRate, tenorNum):
if tenorNum == 1:
interest = numpy.around(loanAmt * interestRate / 12, 2)
else:
interest = numpy.around((loanAmt - getPrincipleSumBefore(interestRate, tenorNum - 1, loanAmt))*interestRate/12, 2)
print("实际principle sum", tenorNum, getPrincipleSumBefore(interestRate, tenorNum - 1, loanAmt))
return interest

def getInterestSum(loanAmt, interestRate, tenorNum):
log(loanAmt)
log(interestRate)
log(tenorNum)
tenorSum = 0
for i in range(1, tenorNum+1):
tenorSum = tenorSum + getInterestForEachTenor(loanAmt, interestRate, i)
return numpy.around(tenorSum, 2)

def getPrincipleForEachTenor(loanAmt, interestRate,tenor, tenorNum):
if tenorNum != tenor:
installment = getInstallment(interestRate, tenor, loanAmt, tenorNum)
# print(installment)
interest = getInterestForEachTenor(loanAmt, interestRate, tenorNum)
#print(interest)
principle = installment - interest
else:
principle = loanAmt - getPrincipleSumBefore(interestRate, tenor - 1, loanAmt)
log(principle)

return principle

# 前tenor-1期 dst总和
def getDstSumBefore(feeAmount, tenor, tenorNum):
dstList = []
for i in range(1, tenorNum+1):
if i != tenor:
dst = numpy.around(feeAmount / tenor, 2)
dstList.append(dst)
return sum(dstList)


def getDstForEachTenor(feeAmount, tenor, tenorNum):
if tenor != tenorNum:
dst = numpy.around(feeAmount / tenor, 2)
else:
dst = numpy.around(feeAmount - getDstSumBefore(feeAmount, tenor, tenorNum - 1), 2)
# log("DST=")
# log(dst)
return dst

#feeAmt 为总DST
def getTotalRepayAmtForEachTenor(loanAmt, interestRate,tenor, tenorNum, feeAmt):
# log("getTotalRepayAmtForEachTenor方法传输入loanAmt"+ str(loanAmt))
# log(interestRate)
# log(tenor)
# log(tenorNum)
#log(feeAmt)
installment = numpy.around(getInstallment(interestRate, tenor, loanAmt, tenorNum), 2)
log("installment:")
log(installment)
dst = getDstForEachTenor(feeAmt, tenor, tenorNum)
log("dst:")
log(dst)
return installment + dst

 

#获取前tenorNum(含tenorNum期)期贷款总金额
def getBeforeRepayAmt(loanAmt, interestRate,tenor, tenorNum, feeAmount):
repayAmt = 0
for i in range(1, tenorNum+1):
repayAmt = repayAmt + getTotalRepayAmtForEachTenor(loanAmt, interestRate,tenor, i, feeAmount)
return repayAmt

posted @ 2022-10-26 11:59  Lynne~  阅读(155)  评论(0编辑  收藏  举报