Logo

SQL Server SWAPCURVE Function

Updated 2023-10-13 13:59:13.957000

Description

Use the table-valued function SWAPCURVE to calculate discount factors, zero-coupon rates, and continuously compounded zero-coupon rates from a series of cash rates, futures prices or swaps rates.

Syntax

SELECT * FROM [westclintech].[wct].[SWAPCURVE](
  <@InputData_RangeQuery, nvarchar(max),>
 ,<@StartDate, datetime,>
 ,<@Frequency, float,>
 ,<@SpotDate, datetime,>
 ,<@CashBasis, nvarchar(4000),>
 ,<@FuturesBasis, nvarchar(4000),>
 ,<@SwapsBasis, nvarchar(4000),>
 ,<@Interpolation, nvarchar(4000),>
 ,<@DateRoll, nvarchar(4000),>
 ,<@Holidays, nvarchar(max),>)

Arguments

@InputData_RangeQuery

a T-SQL statement, as a string, the specifies the cash rates, futures prices, and swap rates to be used in the SWAPCURVE calculations.

@StartDate

the starting date associated with the cash rates. @StartDate must be of the type datetime or of a type that implicitly converts to datetime.

@Frequency

the compounding frequency for the swaps rates. Permissible values are 1 (annually), 2 (semi-annually), 4 (quarterly) and 12 (monthly).

@SpotDate

The normal settlement date associated with the @StartDate. @SpotDate must be of the type datetime or of a type that implicitly converts to datetime.

@CashBasis

The interest basis code associated with the cash rates. Valid values are 0 (30/360), 1 (Actual/Actual), 2 (Actual/360), 3 Actual/365) and 4 (E30/360).

@FuturesBasis

The interest basis code associated with the futures prices. Valid values are 0 (30/360), 1 (Actual/Actual), 2 (Actual/360), 3 Actual/365) and 4 (E30/360).

@SwapsBasis

The interest basis code associated with the swaps rates. Valid values are 0 (30/360), 1 (Actual/Actual), 2 (Actual/360), 3 (Actual/365) and 4 (E30/360).

@Interpolation

The interpolation method to be used with the swaps rates. Valid values are L (linear) and S (Spline).

@DateRoll

The rule to be used when a calculated date lands on a non-business day. The @DateRollRule values are:

Aactual day is returned with no adjustment.
Fnext business day is returned.
Mnext business day is returned unless it is in a different month in which case the previous business day is returned.
Ppreceding business day is returned.
MPpreceding business day is returned unless it is in a different month in which in case the next business day is returned.

@Holidays

a comma separated string containing the holiday (non-business) dates to be used in the calculation of the number of business days. You can use the aggregate function NBD to create an appropriately formatted string.

Return Type

table

colNamecolDatatypecolDesc
mat_datedatetimeMaturity date. The date of the discount factor.
dffloatDiscount factor.
rsourcenvarchar(4000)Rate source. 'C' for cash, 'F' for futures, 'S' for swaps, 'I' for interpolated.
cczerofloatContinuously compounded zero coupon rate calculated from the discount factor

Remarks

To interpolate the zero coupon or continuously compounded zero coupon rate from the discount factors use the DFINTERP aggregate function.

Use the TENOR2DATE scalar function to convert abbreviations like 1M and 1Y to dates based on the spot date.

Use the ED_FUT2DATE scalar function to convert futures contract codes to the correct settlement date based on the start date.

Use the ED_FUT_CONV_ADJ_HL scalar function to convert the futures price to a convexity-adjusted interest rate.

Examples

In this example we will take a series of cash and swaps rates and convert them into zero coupon rates for the same date using linear interpolation. Note that we use the XLeratorDB TENOR2DATE function to calculate the actual maturity dates from the input.

SET NOCOUNT ON;
SELECT wct.TENOR2DATE(mDate, '2013-03-07', '2013-03-11', '') as mDate,
       cRate,
       iType
into #curves
FROM
(
    SELECT '1M',
           .0023,
           'C'
    UNION ALL
    SELECT '3M',
           .0028,
           'C'
    UNION ALL
    SELECT '6M',
           .0044,
           'C'
    UNION ALL
    SELECT '1Y',
           .0031,
           'S'
    UNION ALL
    SELECT '2Y',
           .0039,
           'S'
    UNION ALL
    SELECT '3Y',
           .0054,
           'S'
    UNION ALL
    SELECT '4Y',
           .0074,
           'S'
    UNION ALL
    SELECT '5Y',
           .0100,
           'S'
    UNION ALL
    SELECT '7Y',
           .0150,
           'S'
    UNION ALL
    SELECT '10Y',
           .0207,
           'S'
    UNION ALL
    SELECT '30Y',
           .0304,
           'S'
) n(mDate, cRate, iType);
SELECT *
FROM wct.SWAPCURVE(   'SELECT * FROM #curves', --@InputData_RangeQuery
                      '2013-03-07',            --@StartDate
                      2,                       --@Frequency
                      '2013-03-11',            --@SpotDate
                      2,                       --@CashBasis
                      2,                       --@FuturesBasis
                      0,                       --@SwapsBasis
                      'L',                     --@InterpMethod
                      'M',                     --@DateRoll
                      NULL                     --@Holidays
                  );
DROP TABLE #curves;

This produces the following result.

mat_datedfrsourcezero_cpncczero
2013-03-110.999974445097514C0.0023319444444390.002331914647881
2013-04-110.999776433820482C0.0023319972278220.002331736530539
2013-06-110.999259419468472C0.0028178357301950.002816792055380
2013-09-110.997730659702805C0.0044159212731810.004410906864564
2014-03-110.996882833702945S0.0030930173258550.003088191584351
2014-09-110.994738254820303I0.0034913125135870.003482111162617
2015-03-110.992210547586236S0.0039039108688450.003888666571725
2015-09-110.988409313511400I0.0046625393833650.004635413360614
2016-03-110.983899336463167S0.0054299181866130.005385969035826
2016-09-120.977748016665409I0.0064644490834080.006391986181362
2017-03-130.970696482511936S0.0075110222646810.007399881598589
2017-09-110.961417467684632I0.0088828213759670.008709213133916
2018-03-120.950875411471596S0.0102986388496710.010041433654505
2018-09-110.939464625035334I0.0116778314709720.011317014170550
2019-03-110.927008124079818I0.0130933062178580.012603383421096
2019-09-110.913366689366424I0.0145525186534780.013903116612769
2020-03-110.898776137247601S0.0160514643629510.015210179748591
2020-09-110.885263536866259I0.0172337344778480.016204922489765
2021-03-110.871142848233315I0.0184517556449850.017208304310179
2021-09-130.856043392396973I0.0197237323485180.018230555079216
2022-03-110.840737223990592I0.0210096582032700.019239983416046
2022-09-120.824404497953078I0.0223658872453510.020275978277086
2023-03-130.807594871522675S0.0237723576078310.021322739511739
2023-09-110.797094925159550I0.0241960517796930.021556054435498
2024-03-110.786423651997823I0.0246460535476640.021803770890632
2024-09-110.775649881397395I0.0251006038287520.022047010508417
2025-03-110.764846851110325I0.0255800592273440.022304325469763
2025-09-110.753934741602770I0.0260614267370710.022553938848428
2026-03-110.743005676454337I0.0265673251847660.022816462952316
2026-09-110.731973829835692I0.0270769261290740.023072090517917
2027-03-110.720937156464926I0.0276109947527640.023339692613036
2027-09-130.709681477174981I0.0281567492074720.023604138630319
2028-03-130.698537864872117I0.0287183213533620.023874120495007
2028-09-110.687444256357346I0.0292839494249480.024138469216980
2029-03-120.676184715394252I0.0298843103362690.024417931665849
2029-09-110.664964823619493I0.0304876020517190.024689607897286
2030-03-110.653713425482731I0.0311200123670530.024972874130968
2030-09-110.642384168861800I0.0317642364626480.025252086531706
2031-03-110.631096460866758I0.0324351620703140.025540930947872
2031-09-110.619736959862560I0.0331203117994660.025826375994832
2032-03-110.608409473761774I0.0338313525330240.026119111275627
2032-09-130.596911871263372I0.0345695022584740.026414420122145
2033-03-110.585726776588969I0.0353204873887310.026712159684831
2033-09-120.574294803864427I0.0361038270436960.027012747919353
2034-03-130.562936528893093I0.0369184091029980.027322142261104
2034-09-110.551710553821896I0.0377422739707600.027624978515574
2035-03-120.540381814616948I0.0386129776926820.027941536528259
2035-09-110.529128442680286I0.0395006211521430.028253835210513
2036-03-110.517883036524153I0.0404274057688770.028574912440061
2036-09-110.506617814851251I0.0413859823728570.028897357949475
2037-03-110.495455358394768I0.0423826726269020.029228218758796
2037-09-110.484254989344199I0.0434146901362020.029559687383324
2038-03-110.473166430097940I0.0444880969224670.029899557030417
2038-09-130.461923703698719I0.0456146102472480.030244584695020
2039-03-110.451044182765596I0.0467614064259770.030590457051664
2039-09-120.439955265915978I0.0479741664075780.030944244950757
2040-03-120.429029768995448I0.0492304464823250.031303696601327
2040-09-110.418166530457326I0.0505331339852540.031665131100932
2041-03-110.407380249456956I0.0518980370022620.032037241069873
2041-09-110.396575302977135I0.0533250174847500.032413308487447
2042-03-110.385914704914699I0.0548135948518670.032798291861593
2042-09-110.375239743644282I0.0563739761556060.033188256162288
2043-03-110.364716317935350S0.0580036324845380.033587440482903

In this example we include the Eurodollar's futures strip, starting with the June 2013 contract and we eliminate the 1-year swaps contract. Note that we use the XLeratorDB ED_FUT2DATE function to convert the futures contract code to the appropriate settlement date. We also exclude the interpolated values from the resultant table.

SELECT CASE iType
           WHEN 'F' THEN
               wct.ED_FUT2DATE(mDate, '2013-03-07')
           ELSE
               wct.TENOR2DATE(mDate, '2013-03-07', '2013-03-11', '')
       END as mDate,
       Case iType
           WHEN 'F' THEN
       (100 - cRate) / 100
           ELSE
               cRate
       END as cRate,
       iType
into #curves
FROM
(
    SELECT '1M',
           .0023,
           'C'
    UNION ALL
    SELECT '3M',
           .0028,
           'C'
    UNION ALL
    SELECT '6M',
           .0044,
           'C'
    UNION ALL
    SELECT '2Y',
           .0039,
           'S'
    UNION ALL
    SELECT '3Y',
           .0054,
           'S'
    UNION ALL
    SELECT '4Y',
           .0074,
           'S'
    UNION ALL
    SELECT '5Y',
           .0100,
           'S'
    UNION ALL
    SELECT '7Y',
           .0150,
           'S'
    UNION ALL
    SELECT '10Y',
           .0207,
           'S'
    UNION ALL
    SELECT '30Y',
           .0304,
           'S'
    UNION ALL
    SELECT 'M3',
           99.7050,
           'F'
    UNION ALL
    SELECT 'U3',
           99.6850,
           'F'
    UNION ALL
    SELECT 'Z3',
           99.6450,
           'F'
    UNION ALL
    SELECT 'H4',
           99.6100,
           'F'
    UNION ALL
    SELECT 'M4',
           99.5600,
           'F'
    UNION ALL
    SELECT 'U4',
           99.4950,
           'F'
    UNION ALL
    SELECT 'Z4',
           99.4050,
           'F'
) n(mDate, cRate, iType);
SELECT *
FROM wct.SWAPCURVE(   'SELECT * FROM #curves', --@InputData_RangeQuery
                      '2013-03-07',            --@StartDate
                      2,                       --@Frequency
                      '2013-03-11',            --@SpotDate
                      2,                       --@CashBasis
                      2,                       --@FuturesBasis
                      0,                       --@SwapsBasis
                      'L',                     --@InterpMethod
                      'M',                     --@DateRoll
                      NULL                     --@Holidays
                  )
WHERE rsource <> 'I'; --Exclude interpolation
DROP TABLE #curves;

This produces the following result.

mat_datedfrsourcezero_cpncczero
2013-03-110.999974445097514C0.0023319444444390.002331914647881
2013-04-110.999776433820482C0.0023319972278220.002331736530539
2013-06-110.999259419468472C0.0028178357301950.002816792055380
2013-09-180.998413753235007F0.0029738458134170.002971485938654
2013-12-180.997619398788721F0.0030454298214630.003041801964532
2014-03-190.996724976556286F0.0031811971609460.003175982217172
2014-06-180.995743339580683F0.0033340231141640.003326917122285
2014-09-170.994637082114909F0.0035206070737950.003511149789111
2014-12-170.993369018968611F0.0037484064992510.003735951132099
2015-03-110.992209307176538S0.0039045374175280.003889288239548
2016-03-110.983897627574036S0.0054305039382170.005386545355940
2017-03-130.970694156874036S0.0075116363645090.007400477702418
2018-03-120.950872297832894S0.0102993253281470.010042086408939
2020-03-110.898771587255192S0.0160522671365660.015210901260534
2023-03-130.807588927565738S0.0237732669784310.021323473912069
2043-03-110.364711076002967S0.0580049447749450.033587919093189