Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Excel VBA:计算法国摊销计划的近似利率,给定:起始本金、定期付款金额、年数_C#_Excel_Vba_Rate_Amortization - Fatal编程技术网

C# Excel VBA:计算法国摊销计划的近似利率,给定:起始本金、定期付款金额、年数

C# Excel VBA:计算法国摊销计划的近似利率,给定:起始本金、定期付款金额、年数,c#,excel,vba,rate,amortization,C#,Excel,Vba,Rate,Amortization,我需要计算(大约)法国分期偿还计划(一系列定期等额付款)的利率,如下所示: 起始校长 定期付款金额(本金和利息) 摊销计划的年数 没有直接/简单的方法可以用数学公式计算利率,我不喜欢汇率excel公式(),因为我需要一些可以用其他语言轻松移植的代码(我目前正在开发一个C#库来进行一些金融计算) 关于摊销的一些维基百科参考资料如下: 仅为年息提供计算应该没有实质性区别。事实上,从每年到每月,10年的利息差约为0005英镑,30年的利息差约为00025英镑 Excel VBA代码如下所示

我需要计算(大约)法国分期偿还计划(一系列定期等额付款)的利率,如下所示:

  • 起始校长
  • 定期付款金额(本金和利息)
  • 摊销计划的年数
没有直接/简单的方法可以用数学公式计算利率,我不喜欢汇率excel公式(),因为我需要一些可以用其他语言轻松移植的代码(我目前正在开发一个C#库来进行一些金融计算)

关于摊销的一些维基百科参考资料如下:


仅为年息提供计算应该没有实质性区别。事实上,从每年到每月,10年的利息差约为0005英镑,30年的利息差约为00025英镑

Excel VBA代码如下所示,如果有人有任何改进建议,欢迎使用

' compute the Annual Interest Rate of a french amortization schedule (a series of equal payments at regular intervals) given:
' > the Starting Principal
' > the Periodic Payment Amount (Principal And Interests)
' > the Yearly Nr Of payments
' > the Total Nr Of payments
'
' returns the interest truncated to 2 decimals (for 7,525% returns 0,0752)
' returns Null if the interest rate is not computable
'
' examples:
' > the Starting Principal = 10000
' > the Periodic Payment Amount (Principal And Interests) = 1423.78
' > the Yearly Nr Of payments = 1
' > the Total Nr Of payments = 10
' >>> returns 0,07   (i.e. 7%)
'
' uses the functions:
' > ComputePeriodicPaymentAmountWInterestConstPaymConstRateLoan

'
Public Function ComputeIRateConstPaymLoan(StartingPrincipal, PeriodicPaymentAmountPrincipalAndInterests, NrOfYearlyPayments, NrOfTotalPayments)

Dim IMin, IMax, AComputed, LoopLevel, LoopCount, LoopIIncrement, LoopICurrent, LoopFloorI, LoopFloorA, DebugLoopCounter

    IMin = (0.00001) / 100
    IMax = (99.99999) / 100

    ' Compute the Annuity (the Periodic Payment Amount) with the minimum rate
    AComputed = ComputePeriodicPaymentAmountWInterestConstPaymConstRateLoan(IMin, NrOfYearlyPayments, NrOfTotalPayments, StartingPrincipal)

    ' if the given "Periodic Payment Amount" is equal to the computed Annuity returns the interest rate used for the computation
    If PeriodicPaymentAmountPrincipalAndInterests = AComputed Then
        ' returns IMin
        ComputeIRateConstPaymLoan = IMin
        ' exit from the function
        Exit Function
    End If

    ' Returns Error Value (Null) if the given "Periodic Payment Amount" is less than the computed Annuity
    If PeriodicPaymentAmountPrincipalAndInterests < AComputed Then
        ComputeIRateConstPaymLoan = Null
        Exit Function
    End If



    ' Compute the Annuity (the Periodic Payment Amount) with the maximum rate
    AComputed = ComputePeriodicPaymentAmountWInterestConstPaymConstRateLoan(IMax, NrOfYearlyPayments, NrOfTotalPayments, StartingPrincipal)

    ' if the given "Periodic Payment Amount" is equal to the computed Annuity returns the interest rate used for the computation
    If PeriodicPaymentAmountPrincipalAndInterests = AComputed Then
        ' returns IMax
        ComputeIRateConstPaymLoan = IMax
        ' exit from the function
        Exit Function
    End If

    ' Returns Error Value (Null) if the given "Periodic Payment Amount" is greater than the computed Annuity
    If PeriodicPaymentAmountPrincipalAndInterests > AComputed Then
        ComputeIRateConstPaymLoan = Null
        Exit Function
    End If

    ' loop
    '
    ' loop steps:
    ' 1> from IMin, increments of 0,05
    ' 2> from the floor rate computed in the previous step, increments of 0,02
    ' 3> from the floor rate computed in the previous step, increments of 0,005
    ' 4> from the floor rate computed in the previous step, increments of 0,0005
    ' 5> from the floor rate computed in the previous step, increments of 0,00005
    '
    ' set the counter to the first level
    LoopLevel = 1


    ' reset the loop counter to count loop cycles needed to find the rate (deactivate this in production)
    DebugLoopCounter = 0

    ' loop to find the rate
    Do

        ' test the loop level
        Select Case LoopLevel
            ' Level 1> from IMin, increments of 0,05
            Case 1

                ' set loop parameters
                LoopIIncrement = 0.05
                LoopICurrent = IMin

            ' Level 2> from the floor rate computed in the previous step, increments of 0,02
            Case 2

                ' set loop parameters
                LoopIIncrement = 0.02
                LoopICurrent = LoopFloorI

            ' Level 3> from the floor rate computed in the previous step, increments of 0,005
            Case 3

                ' set loop parameters
                LoopIIncrement = 0.005
                LoopICurrent = LoopFloorI

            ' Level 4> from the floor rate computed in the previous step, increments of 0,0005
            Case 4

                ' set loop parameters
                LoopIIncrement = 0.0005
                LoopICurrent = LoopFloorI

            ' Level 5> from the floor rate computed in the previous step, increments of 0,00005
            Case 5

                ' set loop parameters
                LoopIIncrement = 0.00005
                LoopICurrent = LoopFloorI

            ' exit from the loop and return the last rate
            Case 6

                ' returns LoopICurrent truncated to 2 decimals
                ComputeIRateConstPaymLoan = Fix(LoopICurrent * 10000) / 10000
                ' exit from the function
                Exit Function

        End Select

        ' loop until the computed Annuity is not greater than the given "Periodic Payment Amount"
        Do

            ' increments the debug counter
            DebugLoopCounter = DebugLoopCounter + 1

            ' increments the current interest rate
            LoopICurrent = LoopICurrent + LoopIIncrement

            ' compute the Annuity with the current rate
            AComputed = ComputePeriodicPaymentAmountWInterestConstPaymConstRateLoan(LoopICurrent, NrOfYearlyPayments, NrOfTotalPayments, StartingPrincipal)

            ' if the given "Periodic Payment Amount" is equal to the computed Annuity returns the interest rate used for the computation
            If PeriodicPaymentAmountPrincipalAndInterests = AComputed Then
                ' returns LoopICurrent
                ComputeIRateConstPaymLoan = LoopICurrent
                ' exit from the function
                Exit Function
            End If

            ' if the computed Annuity is grater than the given "Periodic Payment Amount" exit loop
            If AComputed > PeriodicPaymentAmountPrincipalAndInterests Then

                ' exit loop
                Exit Do

            ' if the computed Annuity is less than the given "Periodic Payment Amount" save Floor rate and exit loop
            Else

                ' save Floor rate
                LoopFloorA = AComputed
                LoopFloorI = LoopICurrent

            End If

        ' END - loop until the computed Annuity is not greater than the given "Periodic Payment Amount"
        Loop

        ' increments the loop level counter
        LoopLevel = LoopLevel + 1

    ' END - loop to find the rate
    Loop

End Function



' compute the Periodic Payment Amount (Principal And Interests)(la rata periodica) of a french amortization schedule (a series of equal payments at regular intervals) given:
' > the Annual Interest Rate
' > the Yearly Nr Of payments
' > the Total Nr Of payments
' > the Starting Principal
'
' returns the Periodic Payment Amount, made of Principal And Interests
'
' examples:
' > the Annual Interest Rate = 0.07   (7%)
' > the Nr Of payments = 120
' > the Starting Principal = 10000
' >>> returns 1423,77502727365
'
' used by:
' > ComputeIRateConstPaymLoan

'
Public Function ComputePeriodicPaymentAmountWInterestConstPaymConstRateLoan(AnnualInterestRate, NrOfYearlyPayments, NrOfTotalPayments, StartingPrincipal)
' Starting principal 10.000, Annual interest rate 0.07 (7%), 120 total payments, 1 payments for year, Periodic Payment Amount 1.423,78
Dim i, nt, ny, p, A

    i = AnnualInterestRate
    nt = NrOfTotalPayments
    ny = NrOfYearlyPayments
    p = StartingPrincipal

    ' calcola la rata iniziale (la formula funziona con qualunque durata e periodicità)
    A = p * (i / ny) / (1 - (1 / (1 + (i / ny)) ^ nt))

    ComputePeriodicPaymentAmountWInterestConstPaymConstRateLoan = A

End Function



Sub test()
Dim Result

    'Result = ComputeIRateConstPaymLoan(10000, 1423.78, 1, 10)    ' result 7%
    Result = ComputeIRateConstPaymLoan(10000, 116.1, 12, 120)      ' result 7%
    'Result = ComputeIRateConstPaymLoan(10000, 9700, 1, 10)   ' result 96,89%
    'Result = ComputeIRateConstPaymLoan(10000, 900, 1, 10)   ' error, annuity less than the minimum allowed
    'Result = ComputeIRateConstPaymLoan(10000, 100000000, 1, 10)   ' error, annuity greater than the maximum allowed
    '
    'Result = ComputePeriodicPaymentAmountWInterestConstPaymConstRateLoan(0.07, 1, 10, 10000)   ' result 1423,77502727365
    'Result = ComputePeriodicPaymentAmountWInterestConstPaymConstRateLoan(0.07, 12, 120, 10000)   ' result 116,108479218624

    If IsNull(Result) Then
        MsgBox "Errore"
    Else
        Debug.Print Result
        Debug.Print Result * 1000
    End If

End Sub
'计算法国摊销计划(一系列定期等额付款)的年利率,给出:
“>开始的主体
“>定期付款金额(本金和利息)
“>每年的付款数量
“>支付的总金额
'
'返回截断为2位小数的利息(对于7525%,返回00752)
'如果利率不可计算,则返回Null
'
例如:
“>起始主体=10000
“>定期付款金额(本金和利息)=1423.78
“>每年的付款数量=1
“>总付款数量=10
'>>>返回0,07(即7%)
'
'使用以下功能:
'>ComputePeriodicPaymentAmountWinterest ConstantPaymConstrateloan
'
公共功能计算ConstPaymLoan(开始本金、定期付款金额本金利息、NROF年付款、NROF总付款)
Dim IMin、IMax、A计算、LoopLevel、LoopCount、LoopIIIncrement、LoopICurrent、LoopFloorI、LoopFloorA、DebugLoopCounter
亚胺=(0.00001)/100
IMax=(99.99999)/100
'以最低利率计算年金(定期支付金额)
AComputed=ComputePeriodicPaymentAmountWinterest ConstantPaymConstrateloan(IMin、NrOfYearlyPayments、NrOfTotalPayments、StartingPrincipal)
'如果给定的“定期付款金额”等于计算的年金,则返回用于计算的利率
如果PeriodicPaymentAmountPrincipalandInInterests=已计算,则
”伊敏答道
ComputeRateConstPaymLoan=IMin
'退出函数
退出功能
如果结束
'如果给定的“定期付款金额”小于计算的年金,则返回错误值(Null)
如果定期付款金额主要利息A已计算,则
ComputeRateConstPaymLoan=Null
退出功能
如果结束
'循环
'
'循环步骤:
'1>来自IMin,增量为0,05
'2>从上一步中计算的最低速率,增量为0,02
'3>从上一步中计算的最低费率,增量为0005
'4>根据上一步计算的最低费率,增量为00005
'5>根据上一步计算的最低费率,增量为000005
'
'将计数器设置为第一级
LoopLevel=1
'重置循环计数器以计数查找速率所需的循环周期(在生产中停用此计数器)
DebugLoopCounter=0
'循环以查找速率
做
'测试循环级别
选择案例级别
'级别1>来自IMin,增量为0,05
案例1
'设置循环参数
环增量=0.05
回路电流=IMin
'级别2>从上一步中计算的最低费率开始,增量为0,02
案例2
'设置循环参数
环增量=0.02
LoopicCurrent=LoopFloorI
'级别3>根据上一步中计算的最低费率,增量为0005
案例3
'设置循环参数
回路增量=0.005
LoopicCurrent=LoopFloorI
'级别4>根据上一步计算的最低费率,增量为00005
案例4
'设置循环参数
回路增量=0.0005
LoopicCurrent=LoopFloorI
'级别5>根据上一步计算的最低费率,增量为000005
案例5
'设置循环参数
回路增量=0.00005
LoopicCurrent=LoopFloorI
'退出循环并返回最后一个速率
案例6
'返回被截断为2位小数的LoopicCurrent
ComputeRateConstPaymLoan=修复(循环电流*10000)/10000
'退出函数
退出功能
结束选择
'循环,直到计算的年金不大于给定的“定期付款金额”
做
"增加,