SQL Server amortization for floating-rate constant cash flow loans

ConstantCashFlowFR

Updated: 17 April 2015

Use the table-valued function ConstantCashFlowFR to return the cash flow schedule for a loan with a fixed maturity date and annuity-style payments using a table of forward rates to calculate each periodic payment. ConstantCashFlowFR computes the periodic interest and principal amount through to the maturity date.
The payment frequency is entered into ConstantCashFlowFR as the number of months between payments. For example, a loan with monthly payments would have a frequency of 1. A loan with quarterly payments would have a frequency of 3. A loan with annual payments would have a frequency of 12.
ConstantCashFlowFR supports both an initial grace period and an additional grace period during the life of the loan. All payments and payment dates are calculated with respect to the reference date supplied to the function (which should not be confused with the start date). If an initial grace period is entered in ConstantCashFlowFR and it is greater than the reference date then it becomes the first payment date and subsequent payments are calculated from that date forward.
If any payment would otherwise occur in the specified grace period, then that payment is moved to the end of the grace period and all remaining payments are calculated from the end of the grace period.
If no initial grace period is specified then that first payment date is calculated using the payment frequency. If the start date has been entered and the number of months between the start date and the reference date is less than the frequency, then the first payment date is calculated by adding the frequency (as a number of months) to the start date.
If no start date has been entered but a previous payment date has been entered and the number of months between the previous payment date and the reference date is less than the frequency then the first payment date is calculated by adding the frequency (as a number of months) to the previous payment date.
If there is no start date and no previous payment date or the number of months between those dates and the reference date is greater than the frequency the first payment date is calculated by adding the frequency (as a number of months) to the reference date.
All payments in the resultant table are moved to the end of the month and interest is calculated using these end-of-month dates.
The periodic interest payment is calculated as:

Where:

 Ip = Interest payment in period p Cp-1 = Capital Amount In Debt in period p-1 Rp = Interest rate in period p F = @Frequency Np = Number of Month in period p Np-1 = Number of Month in period p-1

If the period is irregular and is longer than the regular period then the interest amount is broken out into a regular interest amount and a 'grace' interest amount. The regular interest amount is simply the normal periodic interest and the grace interest is the difference between the amount calculated using the above-formula and the normal periodic interest.
The principal payment is calculated as:
PPAYp = -PPMT(Rp * F/12,1,N-(P-1),Cp-1,FV,0)
Where:

 PPAYp = Principal payment in period p PPMT = PPMT function Cp-1 = Capital Amount In Debt in period p-1 Rp = Interest rate in period p F = @Frequency P-1 = Period â€“ 1 FV = @LastPrinPayAmount

Syntax
SELECT * FROM [wct].[ConstantCashFlowFR](
<@OutstandingAmount, float,>
,<@LastPrinPayAmount, float,>
,<@PaymentFrequency, int,>
,<@MaturityDate, datetime,>
,<@ReferenceDate, datetime,>
,<@PrevPayDate, datetime,>
,<@StartDate, datetime,>
,<@FirstPayDate, datetime,>
,<@GracePeriodStartDate, datetime,>
,<@GracePeriodEndDate, datetime,>
,<@FutureRates, nvarchar(max),>)
Arguments
@OutstandingAmount
the principal amount of the loan. @OutstandingAmount is an expression of type float or of a type that can be implicitly converted to float.
@LastPrinPayAmount
the amount of principal to be paid off on the maturity date. @OutstandingAmount is an expression of type float or of a type that can be implicitly converted to float.
@PaymentFrequency
the number of months in a regular interest payment. @PaymentFrequency is an expression of type int or of a type that can be implicitly converted to int.
@MaturityDate
the last payment date of the loan. @MaturityDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@ReferenceDate
the starting date for the number of months with respect to all other dates. @ReferenceDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@PrevPayDate
the last interest payment date prior to the reference date. @PrevPayDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@StartDate
the start date of the loan. @StartDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@FirstPayDate
the first payment date of the loan if other than a regular periodic payment. @FirstPayDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@GracePeriodStartDate
the date on which the (interim) grace period commences. @GracePeriodStartDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@GracePeriodEndDate
the date on which the (interim) grace period concludes. @GracePeriodEndDate is an expression that returns a datetime or smalldatetime value, or a character string in date format.
@FutureRates
a SELECT statement, as a string, which identifies the forward dates and rates to be used in the calculation. The resultant table should consist of 2 columns: a date and a rate. The rate is decimal format such that 1% = .01.
Return Type
RETURNS TABLE (
[Period] [int] NULL,
[PrincipalPayment] [float] NULL,
[InterestPayment] [float] NULL,
[CashFlow] [float] NULL,
[OutstandingExposure] [float] NULL,
[CapitalAmountInDebt] [float] NULL,
[TotalExposure] [float] NULL,
[NumberOfMonth] [int] NULL,
[PaymentDate] [datetime] NULL,
[GraceInterest] [float] NULL,
[InterestRate] [float] NULL
)

 Column Description Period A reference number uniquely identifying a row in the resultant table. PrincipalPayment The amount of the principal payment. InterestPayment The amount of the regular interest payment. CashFlow PrincipalPayment + InterestPayment + GraceInterest. OutstandingExposure When Period = 0 then @OutstandingAmount. For Period > 0 then OutstandingExposure(Period-1) + InterestPayment. CapitalAmountInDebt When Period = 0, @OutstandingAmount. For Period > 0 then CapitalAmountInDebt(Period-1) â€“ PrincipalPayment TotalExposure When Period = 0, @OutstandingAmount. For Period > 0 then CapitalAmountInDebt(Period-1) + InterestPayment NumberOfMonth The number of months between the @ReferenceDate and the PaymentDate. PaymentDate The end-of-month date of the payment. GraceInterest The amount of the grace interest InterestRate The Interest Rate used in the calculation of the interest and principal payment

Remarks
Â·         The PaymentDate for all rows is generated as the last day of the month.
Â·         For Period = 0, PrincipalPayment, InterestPayment, CashFlow, NumberOfMonth, GraceInterest, and InterestRate are set to 0.
Â·         If @Frequency is NULL then @Frequency = 1.
Â·         If @ReferenceDate is NULL then @ReferenceDate = GETDATE()
Â·         GraceInterest is only calculated on @FirstPayDate and @GracePeriodEndDate.
Â·         GraceInterest is only calculated if NumberOfMonth â€“ NumberOfMonth(Period-1) > @PaymentFrequency.
Â·         GraceInterest is the difference between the interest for the period from the previous row to the current row minus the interest that would have been calculated for a period with length equal to @PaymentFrequency.
Â·         The final payment is adjusted for CapitalAmountInDebt(Period-1) and the length of the period if it is less than @PaymentFrequency.
Â·         The last row returned will always be for the maturity date and may be shorter than a regular period depending on the combination of dates and @PaymentFrequency.
Â·         The interest rate used in the calculation comes from the @FutureRates resultant table. The rate value for the maximum date less than or equal to the PaymentDate is used.
Â·         Available in XLeratorDB / financial 2008 only
Examples
Use the following forward rates, which have been inserted into a #fwdrates table, for all the examples.
SELECT
CAST(dt as datetime) as dt,
rate
INTO
#fwdrates
FROM (VALUES
('2014-12-31',0.025)
,('2015-01-31',0.025102)
,('2015-02-28',0.025205)
,('2015-03-31',0.025308)
,('2015-04-30',0.02541)
,('2015-05-31',0.025513)
,('2015-06-30',0.025615)
,('2015-07-31',0.025718)
,('2015-08-31',0.02582)
,('2015-09-30',0.025923)
,('2015-10-31',0.026025)
,('2015-11-30',0.026128)
,('2015-12-31',0.026231)
,('2016-01-31',0.026333)
,('2016-02-29',0.026436)
,('2016-03-31',0.026539)
,('2016-04-30',0.026641)
,('2016-05-31',0.026744)
,('2016-06-30',0.026847)
,('2016-07-31',0.026949)
,('2016-08-31',0.027052)
,('2016-09-30',0.027155)
,('2016-10-31',0.027257)
,('2016-11-30',0.02736)
,('2016-12-31',0.027463)
,('2017-01-31',0.027566)
,('2017-02-28',0.027668)
,('2017-03-31',0.027771)
,('2017-04-30',0.027874)
,('2017-05-31',0.027977)
,('2017-06-30',0.028079)
,('2017-07-31',0.028182)
,('2017-08-31',0.028285)
,('2017-09-30',0.028388)
,('2017-10-31',0.028491)
,('2017-11-30',0.028594)
,('2017-12-31',0.028696)
,('2018-01-31',0.028799)
,('2018-02-28',0.028902)
,('2018-03-31',0.029005)
,('2018-04-30',0.029108)
,('2018-05-31',0.029211)
,('2018-06-30',0.029314)
,('2018-07-31',0.029417)
,('2018-08-31',0.02952)
,('2018-09-30',0.029623)
,('2018-10-31',0.029726)
,('2018-11-30',0.029829)
,('2018-12-31',0.029932)
,('2019-01-31',0.030035)
,('2019-02-28',0.030138)
,('2019-03-31',0.030241)
,('2019-04-30',0.030344)
,('2019-05-31',0.030447)
,('2019-06-30',0.03055)
,('2019-07-31',0.030653)
,('2019-08-31',0.030756)
,('2019-09-30',0.030859)
,('2019-10-31',0.030962)
,('2019-11-30',0.031065)
,('2019-12-31',0.031168)
)n(dt,rate)

This is a simple 300,000 loan with quarterly payments with the interest rates coming from
#fwdrates.
SELECT
*
FROM
wct.ConstantCashFlowFR(
300000       --@OutstandingAmount
,0            --@LastPrinPayAmount
,3            --@PaymentFrequency
,'2019-12-15' --@MaturityDate
,'2014-12-15' --@ReferenceDate
,NULL         --@PrevPayDate
,NULL         --@StartDate
,NULL         --@FirstPayDate
,NULL         --@GracePeriodStartDate
,NULL         --@GracePeriodEndDate,
,'SELECT * FROM #fwdrates' --@FutureRates
)

This produces the following result.

In this example the SQL reflects a final principal payment of 172,000.
SELECT
*
FROM
wct.ConstantCashFlowFR(
300000       --@OutstandingAmount
,172000       --@LastPrinPayAmount
,3            --@PaymentFrequency
,'2019-12-15' --@MaturityDate
,'2014-12-15' --@ReferenceDate
,NULL         --@PrevPayDate
,NULL         --@StartDate
,NULL         --@FirstPayDate
,NULL         --@GracePeriodStartDate
,NULL         --@GracePeriodEndDate,
,'SELECT * FROM #fwdrates' --@FutureRates
)

This produces the following result.

In this example the last principal payment amount has been set to zero and a first payment date of 15-June-2015 has been added.
SELECT
*
FROM
wct.ConstantCashFlowFR(
300000       --@OutstandingAmount
,0            --@LastPrinPayAmount
,3            --@PaymentFrequency
,'2019-12-15' --@MaturityDate
,'2014-12-15' --@ReferenceDate
,NULL         --@PrevPayDate
,NULL         --@StartDate
,'2015-06-15' --@FirstPayDate
,NULL         --@GracePeriodStartDate
,NULL         --@GracePeriodEndDate,
,'SELECT * FROM #fwdrates' --@FutureRates
)

This produces the following result.

In this example there are no payments in 2018.
SELECT
*
FROM
wct.ConstantCashFlowFR(
300000       --@OutstandingAmount
,0            --@LastPrinPayAmount
,3            --@PaymentFrequency
,'2019-12-15' --@MaturityDate
,'2014-12-15' --@ReferenceDate
,NULL         --@PrevPayDate
,NULL         --@StartDate
,'2015-06-15' --@FirstPayDate
,'2018-01-01' --@GracePeriodStartDate
,'2019-01-01' --@GracePeriodEndDate,
,'SELECT * FROM #fwdrates' --@FutureRates
)

This produces the following results

In this example the first payment date is set to NULL and the previous payment date is set to '2014-11-15' so that the first payment date will be calculated using the previous payment date.
SELECT
*
FROM
wct.ConstantCashFlowFR(
300000       --@OutstandingAmount
,0            --@LastPrinPayAmount
,3            --@PaymentFrequency
,'2019-12-15' --@MaturityDate
,'2014-12-15' --@ReferenceDate
,'2014-11-15' --@PrevPayDate
,NULL         --@StartDate
,NULL         --@FirstPayDate
,'2018-01-01' --@GracePeriodStartDate
,'2019-01-01' --@GracePeriodEndDate,
,'SELECT * FROM #fwdrates' --@FutureRates
)

This produces the following result.

In this example the grace period and the previous payment date are eliminated and a start date has been added.
SELECT
*
FROM
wct.ConstantCashFlowFR(
300000       --@OutstandingAmount
,0            --@LastPrinPayAmount
,3            --@PaymentFrequency
,'2019-12-15' --@MaturityDate
,'2014-12-15' --@ReferenceDate
,'2014-11-15' --@PrevPayDate
,NULL         --@StartDate
,'2014-10-15' --@FirstPayDate
,NULL         --@GracePeriodStartDate
,NULL         --@GracePeriodEndDate,
,'SELECT * FROM #fwdrates' --@FutureRates
)

This produces the following result.