how to value interest rate swap with 2 curves with QuantLib C++ (quantlib swap example)

example of interest rate swap derivative valuation in c++ with quantlib.
for quantlib excel valuation see http://www.pricederivatives.com/en/amortizing-swap-valuation-excel-example/

maturity: 20 nov 2022
nominal: 1M
init date: 20 nov 2012
floating leg pays: Euribor 3m + 2% every 3 months
fixed leg pays:4% anually

calendar: TARGET
day counting convention fixed leg: 30/360
day counting convention floating leg: Actual/360

market data :

last fixing for Euribor 3m : 1%

for discounting we’ll use Eonia curve (could be obtained from reuters or bloomberg)
(here we’ll use faux market data)

[date] [discount factor]

31 Dec 2013 0.99
31 Dec 2024 0.80

for euribor forwards calculation we’ll use projecting curve (euribor 3m):

[date] [discount factor]

31 dec 2013 0.999
31 dec 2024 0.89

QuantLib swap example c++ code for valuation of this swap contract for 31/dec/2012 :

#include <ql/quantlib.hpp>

using namespace std;
using namespace QuantLib;
using namespace boost;

int main()
{
vector<Date> dates; 
vector<DiscountFactor> discountFactor; 

Date valuationDate(31,December,2012);
Settings::instance().evaluationDate()=valuationDate;

dates.push_back(valuationDate); discountFactor.push_back(1.0); 
dates.push_back(Date(31,December, 2013));  discountFactor.push_back(0.99); 
dates.push_back(Date(31,December, 2024));  discountFactor.push_back(0.80); 
shared_ptr<YieldTermStructure> forwardCurve(new InterpolatedDiscountCurve<LogLinear>(dates,discountFactor,Actual360())); 

discountFactor.pop_back();discountFactor.pop_back();

discountFactor.push_back(0.999);
discountFactor.push_back(0.89);

shared_ptr<YieldTermStructure> oisCurve(new InterpolatedDiscountCurve<LogLinear>(dates,discountFactor,Actual360())); 

Handle<YieldTermStructure> discountingTermStructure(oisCurve);
Handle<YieldTermStructure> forwardingTermStructure(forwardCurve);

Real nominal = 1000000.0;
Date previousResetDate(20,November,2012);
Date maturity(20,November,2022);
double spread = 0.02;
double fixedRate=0.04;

shared_ptr<IborIndex> euribor(new Euribor(3*Months,forwardingTermStructure));
euribor->addFixing(euribor->fixingDate(previousResetDate),0.01,true);
		
VanillaSwap::Type swapType = VanillaSwap::Payer;

Schedule fixedSchedule(previousResetDate, maturity,1*Years,
                               TARGET(), ModifiedFollowing,ModifiedFollowing,
                               DateGeneration::Forward, false);

Schedule floatSchedule(previousResetDate,maturity,3*Months,
                               TARGET(),ModifiedFollowing ,ModifiedFollowing,
                               DateGeneration::Forward, false);
        
VanillaSwap swap(VanillaSwap::Payer, nominal,fixedSchedule, fixedRate, Thirty360(),
            floatSchedule, euribor, spread,Actual360());
     
shared_ptr<PricingEngine> swapEngine(new DiscountingSwapEngine(discountingTermStructure));

swap.setPricingEngine(swapEngine);
       
double res=swap.NPV();

}


Posted in quantlib Tagged with: ,