Tuesday, March 18, 2014

USD Yield Curve building using Python and Quantlib

Yield Curve is fundamental building block in the pricing of Interest rate Derivative products.An Yield Curve constructed using US government issued Treasury securities is termed as Treasury Yield curve, if it is constructed using corporate bonds then it is called Corporate Bond Curve and so on. Here I will describe building LIBOR Yield Curve that is bedrock for Pricing an Interest Rate Derivative product like IR swaps. Yield curve construction starts with identifying instruments needed to build and then applying appropriate bootstrapping technique.

 LIBOR Yield curve is in general constructed blending Cash deposit products, Future contracts and Swap rates based on their liquidity in the market. This is because, each of these products are liquid at particular time to maturity in the market

Cash Deposit products: LIBOR cash deposit markets is very active in overnight, 1week, 1M, 3m and 6M maturities. These deposits are quoted in the market as cash rate earned for a particular maturity. In general LIBOR Rates are now fixed by Intercontinental Exchange after the infamous LIBOR Fixing scandal.These rates are quoted with ACT/360 basis convention. Futures products: Euro Dollar Futures are most liquid traded instruments on CME exchange. These futures imply LIBOR interest rate for that period. Typically they are traded on the market with March, June, September and December expiration dates along with serial months on the front end. These contracts are being traded as far as 10 years from now. A typical contract for instance, Jun 14 ( symbol: EDM4) will imply a 3m rate starting on June as of today. These contracts are very useful in risk management of trading book with interest rate swaps. Future contracts are traded on the exchange and are subject to initial and variation margin. Due to this when comparing futures rates with Over the counter traded markets, players incorporate Convexity adjustment (Here we will not cover that)
In our yield curve construction, we are going to use 8 future contracts for a period of 2 years. Different Banks have different requirements and hence this will vary from 8 to 16 future contracts.
Now let is move on to
Interest rate swaps Dollar Interest rate swaps are traded with in over the counter market. Recent Dodd Frank regulation has enjoined moving these products onto exchanges. Consequently, you are seeing movement of these products onto Swap Execution Facilities run by variety of players. Even to mitigate counterparty risk, these swaps are now moving on Clearing houses where they will become more like futures.
We will use these quoted swap rates from 3y onwards to 30y in our yield curve construction.
We utilize liquid, Deposits till 6M maturity, from there 8 future contracts and then from 3y onward till 30y swap rates are considered and blended together to construct discount curve and then Forward curve.
We will also use Piece wise flat curve interpolation methodology implemented by Quantlib in building our discount factors.Essentially this will serve the purpose of generating a smooth discount curve that can imply a smooth forward curve which is very important in pricing any kind of exotic derivatives.
# Copyright (C) 2014, Khandrika Capital Markets

# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the license for more details.

from QuantLib import *
import datetime
import numpy as np
import matplotlib.pyplot as plt


# global data
# Here enter the Yield Curve reference Data
calendar = TARGET()
todaysDate = Date(28,February,2014);
Settings.instance().evaluationDate = todaysDate
settlementDate = Date(4,March,2014);

# market quotes
# Update deposit Rates ( usual source will be LIBOR Fixings on the Curve Date
deposits = { (1,Weeks): 0.0023,
             (1,Months): 0.0023,
             (3,Months): 0.0023,
             (6,Months): 0.0023}
# Obtain Futures prices from CME traded Euro Dollar Futures
futures = { Date(19,3,2014): 99.765,
            Date(18,6,2014): 99.75,
            Date(17,9,2014): 99.73,
            Date(17,12,2014): 99.69,
            Date(18,3,2015): 99.605,
            Date(17,6,2015): 99.47,
            Date(16,9,2015): 99.3,
            Date(16,12,2015): 99.085 }
# Obtain Swap rates from Traded Swaps on the Curve data
swaps = { (3,Years): 0.0079,
          (4,Years): 0.012,
          (5,Years): 0.0157,
          (6,Years): 0.01865,
          (7,Years): 0.0216,
          (8,Years): 0.0235,
          (9,Years): 0.0254,
          (10,Years): 0.0273,
          (15,Years): 0.0297,
          (20,Years): 0.0316,
          (25,Years): 0.0335,
          (30,Years): 0.0354}

# convert them to Quote objects
for n,unit in deposits.keys():
    deposits[(n,unit)] = SimpleQuote(deposits[(n,unit)])
for d in futures.keys():
    futures[d] = SimpleQuote(futures[d])
for n,unit in swaps.keys():
    swaps[(n,unit)] = SimpleQuote(swaps[(n,unit)])

# build rate helpers

dayCounter = Actual360()
settlementDays = 2
depositHelpers = [ DepositRateHelper(QuoteHandle(deposits[(n,unit)]),
                                     Period(n,unit), settlementDays,
                                     calendar, ModifiedFollowing,
                                     False, dayCounter)
                   for n, unit in [(1,Weeks),(1,Months),(3,Months),
                                   (6,Months)] ]

dayCounter = Actual360()
months = 3
futuresHelpers = [ FuturesRateHelper(QuoteHandle(futures[d]),
                                     d, months,
                                     calendar, ModifiedFollowing,
                                     True, dayCounter,
                                     QuoteHandle(SimpleQuote(0.0)))
                   for d in futures.keys() ]

settlementDays = 2
fixedLegFrequency = Semiannual
fixedLegTenor = Period(6,Months)
fixedLegAdjustment = Unadjusted
fixedLegDayCounter = Thirty360()
floatingLegFrequency = Quarterly
floatingLegTenor = Period(3,Months)
floatingLegAdjustment = ModifiedFollowing
swapHelpers = [ SwapRateHelper(QuoteHandle(swaps[(n,unit)]),
                               Period(n,unit), calendar,
                               fixedLegFrequency, fixedLegAdjustment,
                               fixedLegDayCounter, Euribor3M())
                for n, unit in swaps.keys() ]

# term structure handles

discountTermStructure = RelinkableYieldTermStructureHandle()
forecastTermStructure = RelinkableYieldTermStructureHandle()

# term-structure construction

helpers = depositHelpers[:2] + futuresHelpers + swapHelpers[1:]
depoFuturesSwapCurve = PiecewiseFlatForward(settlementDate, helpers,
                                            Actual360())
print depoFuturesSwapCurve.dates()
for c in depoFuturesSwapCurve.dates():
    print depoFuturesSwapCurve.discount(c)
#output

1.0
0.999955279778
0.999801983663
0.999304040868
0.99867255034
0.997998353751
0.997225503985
0.996208950194
0.994872376004
0.993129765936
0.990838040119
0.976526437683
0.95283925956
0.923683284288
0.892554279843
0.824681470718
0.790220477256
0.753705873129
0.63046764213
0.517332972767
0.411723283158
0.315258111833

    


So far we have constructed the discount factors using Quantlib and Python. Have a great evening

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. thanks for your comments. You can build yield curve without using any external libraries. For this you have to build discount rates from market traded instruments like , Libor rates, futures prices and Swap rates.
      send me an email (chandsek@yahoo.com for further details) More than happy to discuss.

      Delete
  2. BTW YOUR BLOGS ARE SUPER HELPFUL!

    ReplyDelete
  3. This is a nice post explaining term structure in QuantLib. Have you every tried deriving spots from the treasury yields?

    ReplyDelete
  4. I have not tried that using Quantlib. But I think it would be trivial exercise as we need to solve for a Fixed rate bond.

    ReplyDelete