Showing posts with label OIS-adjusted Libor forward rates. Show all posts
Showing posts with label OIS-adjusted Libor forward rates. Show all posts

Tuesday, June 5, 2018

QuantLib : Dual-Curve Bootstrapping and Swap Valuation

Implementing OIS curve bootstrapping in QuantLib was presented in my previous post. Story will continue. This post will present, how to implement dual-curve bootstrapping scheme and corresponding valuation for a simple single-currency vanilla swap transaction (collateralization is in transaction currency). Detailed information on how to implement this scheme has been acquired mainly from two sources: StackExchange post on this topic and QuantLib Python Cookbook, which is worth of checking out. The great story does not lose its value, even told in different languages.

The program


In the beginning of this program, two relinkable handles for containing yield term structures are created, one for discounting curve and another for projection curve. The real beauty of these creatures comes from the fact, that we can use these handles as curve "placeholders" within our program and later link (or re-link) these handles with any yield term structure implementation.

After this, Eonia OIS curve will be bootstrapped and discounting curve handle is linked to bootstrapped Eonia curve. Similar bootstrapping procedure will be performed for creating Euribor curve, but with a special twist. In order to implement dual-curve bootstrapping algorithm in QuantLib, discounting curve handle must be delivered as one argument for all swap rate helpers, along with dummy quote handle and dummy zero period. Then, projection curve handle is linked to bootstrapped Euribor curve. After this, our projection curve should return "OIS-adjusted" Euribor forward rates for creating floating leg cash flows.

Finally, seasoned vanilla swap transaction will be created and valued. Effectively, cash flow discounting will be performed by using discounting curve handle (in pricing engine), whereas cash flow projection will be performed by using projection curve handle (in index object, in swap transaction). Just for a final note, I carefully checked all constructors for deposit and FRA rate helpers, but did not find any possibility to deliver discount curve handle for these rate helpers.

Thanks for reading this blog.
-Mike

#include <iostream>
#include <ql\quantlib.hpp>
#include <map>
using namespace QuantLib;

int main() {

 try {

  // create common data 
  Date today(7, Jul, 2017);
  DayCounter dayCounter = Actual360();
  Calendar calendar = TARGET();
  Date settlementDate = calendar.advance(today, Period(2, Days));
  Natural settlementDays = settlementDate - today;
  Settings::instance().evaluationDate() = today;

  // create re-linkable handles for discounting and projection curves
  RelinkableHandle<YieldTermStructure> discountCurve;
  RelinkableHandle<YieldTermStructure> projectionCurve;
  // create container for all rate helpers
  std::vector<boost::shared_ptr<RateHelper>> rateHelpers;

  // create required indices
  auto eoniaIndex = boost::make_shared<Eonia>();
  // forward euribor fixings are requested from dual-curve-bootstrapped projection curve
  auto euriborIndex = boost::make_shared<Euribor6M>(projectionCurve);


  // eonia curve
  // create first cash instrument for eonia curve using deposit rate helper
  rateHelpers.push_back(boost::make_shared<DepositRateHelper>
   (Handle<Quote>(boost::make_shared<SimpleQuote>(-0.0036)), 
   Period(1, Days), eoniaIndex->fixingDays(),
   eoniaIndex->fixingCalendar(), eoniaIndex->businessDayConvention(), 
   eoniaIndex->endOfMonth(), eoniaIndex->dayCounter()));

  // create source data for eonia swaps (period, rate)
  std::map<Period, Real> eoniaSwapData;
  eoniaSwapData.insert(std::make_pair(Period(6, Months), -0.00353));
  eoniaSwapData.insert(std::make_pair(Period(1, Years), -0.00331));
  eoniaSwapData.insert(std::make_pair(Period(2, Years), -0.00248));
  eoniaSwapData.insert(std::make_pair(Period(3, Years), -0.00138));
  eoniaSwapData.insert(std::make_pair(Period(4, Years), -0.0001245));
  eoniaSwapData.insert(std::make_pair(Period(5, Years), 0.0011945));
  eoniaSwapData.insert(std::make_pair(Period(7, Years), 0.00387));
  eoniaSwapData.insert(std::make_pair(Period(10, Years), 0.007634));

  // create other instruments for eonia curve using ois rate helper
  std::for_each(eoniaSwapData.begin(), eoniaSwapData.end(),
   [settlementDays, &rateHelpers, &eoniaIndex](std::pair<Period, Real> p) -> void 
   { rateHelpers.push_back(boost::make_shared<OISRateHelper>(settlementDays,
   p.first, Handle<Quote>(boost::make_shared<SimpleQuote>(p.second)), eoniaIndex)); });
  
  // create eonia curve
  auto eoniaCurve = boost::make_shared<PiecewiseYieldCurve<Discount, LogLinear>>
   (0, eoniaIndex->fixingCalendar(), rateHelpers, eoniaIndex->dayCounter());  
  eoniaCurve->enableExtrapolation(true);
  // link discount curve to eonia curve
  discountCurve.linkTo(eoniaCurve);

  // clear rate helpers container
  rateHelpers.clear();


  // euribor curve
  // cash part
  rateHelpers.push_back(boost::make_shared<DepositRateHelper>(Handle<Quote>
   (boost::make_shared<SimpleQuote>(-0.00273)), Period(6, Months),
   settlementDays, calendar, euriborIndex->businessDayConvention(), 
   euriborIndex->endOfMonth(), euriborIndex->dayCounter()));

  // fra part
  rateHelpers.push_back(boost::make_shared<FraRateHelper>(Handle<Quote>
   (boost::make_shared<SimpleQuote>(-0.00194)), Period(6, Months), euriborIndex));

  // swap part
  rateHelpers.push_back(boost::make_shared<SwapRateHelper>(Handle<Quote>
   (boost::make_shared<SimpleQuote>(-0.00119)), Period(2, Years),
   calendar, Annual, ModifiedFollowing, Actual360(), euriborIndex,
   // in order to use dual-curve bootstrapping, discount curve handle must
   // be given as one argument for swap rate helper (along with dummy handle
   // for quote and dummy zero period for technical reasons)
   Handle<Quote>(), Period(0, Days), discountCurve));

  rateHelpers.push_back(boost::make_shared<SwapRateHelper>(Handle<Quote>
   (boost::make_shared<SimpleQuote>(0.00019)), Period(3, Years),
   calendar, Annual, ModifiedFollowing, Actual360(), euriborIndex,
   Handle<Quote>(), Period(0, Days), discountCurve));

  rateHelpers.push_back(boost::make_shared<SwapRateHelper>(Handle<Quote>
   (boost::make_shared<SimpleQuote>(0.00167)), Period(4, Years),
   calendar, Annual, ModifiedFollowing, Actual360(), euriborIndex,
   Handle<Quote>(), Period(0, Days), discountCurve));

  rateHelpers.push_back(boost::make_shared<SwapRateHelper>(Handle<Quote>
   (boost::make_shared<SimpleQuote>(0.00317)), Period(5, Years),
   calendar, Annual, ModifiedFollowing, Actual360(), euriborIndex,
   Handle<Quote>(), Period(0, Days), discountCurve));

  rateHelpers.push_back(boost::make_shared<SwapRateHelper>(Handle<Quote>
   (boost::make_shared<SimpleQuote>(0.00598)), Period(7, Years),
   calendar, Annual, ModifiedFollowing, Actual360(), euriborIndex,
   Handle<Quote>(), Period(0, Days), discountCurve));

  rateHelpers.push_back(boost::make_shared<SwapRateHelper>(Handle<Quote>
   (boost::make_shared<SimpleQuote>(0.00966)), Period(10, Years),
   calendar, Annual, ModifiedFollowing, Actual360(), euriborIndex,
   Handle<Quote>(), Period(0, Days), discountCurve));
  
  // create euribor curve
  auto euriborCurve = boost::make_shared<PiecewiseYieldCurve<Discount, LogLinear>>
   (0, euriborIndex->fixingCalendar(), rateHelpers, euriborIndex->dayCounter());
  euriborCurve->enableExtrapolation();
  // link projection curve to euribor curve
  projectionCurve.linkTo(euriborCurve);


  // create seasoned vanilla swap
  Date pastSettlementDate(5, Jun, 2015);

  Schedule fixedSchedule(pastSettlementDate, pastSettlementDate + Period(5, Years),
   Period(Annual), calendar, Unadjusted, Unadjusted,
   DateGeneration::Backward, false);

  Schedule floatSchedule(pastSettlementDate, pastSettlementDate + Period(5, Years),
   Period(Semiannual), calendar, Unadjusted, Unadjusted,
   DateGeneration::Backward, false);

  VanillaSwap swap(VanillaSwap::Payer, 10000000.0, fixedSchedule, 0.0285, 
   dayCounter, floatSchedule, euriborIndex, 0.0, dayCounter);

  // add required 6M euribor index fixing for floating leg valuation
  euriborIndex->addFixing(Date(1, Jun, 2017), -0.0025);

  // create pricing engine, request swap pv
  auto pricer = boost::make_shared<DiscountingSwapEngine>(discountCurve);
  swap.setPricingEngine(pricer);
  std::cout << swap.NPV() << std::endl;

 }
 catch (std::exception& e) {
  std::cout << e.what() << std::endl;
 }
 return 0;
}


Saturday, June 4, 2016

Excel/VBA : Optimizing smooth OIS-adjusted Libor forward curve using Solver

Optimization for Libor forward curve has been presented in this blog post. This time, we will adjust the presented optimization procedure in such way, that OIS-adjusted Libor forward rates are going to be solved for a given fixed set of swap rates and OIS discount factors.

In a nutshell, justification for OIS-adjusted forward rates is the following :

  • In the "old world", we first bootstrapped Libor zero-coupon curve, from which we calculated Libor discount factors and Libor forward rates (for constructing floating leg coupons) at the same time. Only one curve was ever needed to accomplish this procedure. 
  • In the "new world", since all swap cash flows are discounted using OIS discount factors and par swap rates are still used for constructing swap fixed leg cash flows, forward rates (OIS-adjusted Libor forward rates) have to be slightly adjusted, in order to equate present value of all swap cash flows back to be zero.
All the relevant issues have been clearly explained in this research paper by Barclays.

Screenshots below are showing required Excel worksheet setups along with optimized OIS-adjusted Libor forward curve and required additions to existing VBA program needed to perform this optimization task. In order to validate the optimized OIS-adjusted Libor forward curve, a 10-year vanilla swap has been re-priced using optimized OIS-adjusted Libor forward rates and given set of fixed OIS discount factors.

When setting up VBA program, first implement the program presented in this blog post. After this, add two new functions (IRSwapOISPV, linearInterpolation) presented below into module XLSFunctions. Finally, remember to include references to Solver and Microsoft Scripting Runtime libraries.

Thanks for reading this blog.
-Mike





Public Function IRSwapOISPV(ByRef forwards As Excel.Range, ByRef OISDF As Excel.Range, ByVal swapRate As Double, _
ByVal yearsToMaturity As Integer, ByVal floatingLegPaymentFrequency As Integer, ByVal notional As Double) As Double
    '
    ' PV (fixed payer swap) = -swapRate * Q_fixed + Q_float
    ' where Q_fixed is sumproduct of fixed leg OIS discount factors and corresponding time accrual factors
    ' and Q_float is sumproduct of adjusted libor forward rates, OIS discount factors and corresponding time accrual factors
    ' assumption : fixed leg is always paid annually
    ' since fixed leg is paid annually and maturity is always integer, time accrual factor will always be exactly 1.0
    Dim f As Variant: f = forwards.Value2
    Dim DF As Variant: DF = OISDF.Value2
    Dim Q_fixed As Double
    Dim Q_float As Double
    Dim floatingLegTenor As Double: floatingLegTenor = 1 / CDbl(floatingLegPaymentFrequency)
    Dim nextFixedLegCouponDate As Integer: nextFixedLegCouponDate = 1
    Dim currentTimePoint As Double: currentTimePoint = CDbl(0)
    '
    Dim i As Integer
    For i = 1 To (yearsToMaturity * floatingLegPaymentFrequency)
        currentTimePoint = currentTimePoint + floatingLegTenor
        '
        ' update floating leg Q
        Q_float = Q_float + f(i, 1) * linearInterpolation(currentTimePoint, OISDF.Value2) * floatingLegTenor
        '
        ' update fixed leg Q, if current time point is coupon payment date
        If ((currentTimePoint - CDbl(nextFixedLegCouponDate)) = 0) Then
            Q_fixed = Q_fixed + linearInterpolation(currentTimePoint, OISDF.Value2)
            nextFixedLegCouponDate = nextFixedLegCouponDate + 1
        End If
    Next i
    IRSwapOISPV = (-swapRate * Q_fixed + Q_float) * notional
End Function
'
Public Function linearInterpolation(ByVal maturity As Double, ByRef curve As Variant) As Double
    '
    ' read range into Nx2 array
    Dim r As Variant: r = curve
    '
    Dim n As Integer: n = UBound(r, 1)
    '
    ' boundary checkings
    If ((r(LBound(r, 1), 1)) > maturity) Then linearInterpolation = r(LBound(r, 1), 2): Exit Function
    If ((r(UBound(r, 1), 1)) < maturity) Then linearInterpolation = r(UBound(r, 1), 2): Exit Function
    '
    Dim i As Long
    For i = 1 To n
        If ((r(i, 1) <= maturity) And (r(i + 1, 1) >= maturity)) Then
            '
            Dim y0 As Double: y0 = r(i, 2)
            Dim y1 As Double: y1 = r(i + 1, 2)
            Dim x0 As Double: x0 = r(i, 1)
            Dim x1 As Double: x1 = r(i + 1, 1)
            '
            linearInterpolation = y0 + (y1 - y0) * ((maturity - x0) / (x1 - x0))
            Exit For
        End If
    Next i
End Function
'