This example shows how to set up a term structure and then price some simple bonds. The last part is dedicated to peripherical computations such as yield-to-price or price-to-yield.
#include <ql/qldefines.hpp>
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
# include <ql/auto_link.hpp>
#endif
#include <ql/instruments/bonds/zerocouponbond.hpp>
#include <ql/instruments/bonds/floatingratebond.hpp>
#include <ql/pricingengines/bond/discountingbondengine.hpp>
#include <ql/cashflows/couponpricer.hpp>
#include <ql/termstructures/yield/piecewiseyieldcurve.hpp>
#include <ql/termstructures/yield/bondhelpers.hpp>
#include <ql/termstructures/volatility/optionlet/constantoptionletvol.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/indexes/ibor/usdlibor.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/calendars/unitedstates.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <iostream>
#include <iomanip>
int main(int, char* []) {
try {
std::cout << std::endl;
Date settlementDate(18, September, 2008);
Integer settlementDays = 3;
Date todaysDate = calendar.
advance(settlementDate, -settlementDays, Days);
std::cout <<
"Today: " << todaysDate.
weekday()
<< ", " << todaysDate << std::endl;
std::cout << "Settlement date: " << settlementDate.weekday()
<< ", " << settlementDate << std::endl;
Real redemption = 100.0;
const Size numberOfBonds = 5;
Date(15, November, 2002),
};
};
Real couponRates[] = {
0.02375,
0.04625,
0.03125,
0.04000,
0.04500
};
Real marketQuotes[] = {
100.390625,
106.21875,
100.59375,
101.6875,
102.140625
};
std::vector<ext::shared_ptr<SimpleQuote>> quote;
for (Real marketQuote : marketQuotes) {
quote.push_back(ext::make_shared<SimpleQuote>(marketQuote));
}
for (Size i=0; i<numberOfBonds; i++) {
quoteHandle[i].
linkTo(quote[i]);
}
std::vector<ext::shared_ptr<RateHelper>> bondHelpers;
for (Size i=0; i<numberOfBonds; i++) {
auto bondHelper = ext::make_shared<FixedRateBondHelper>(
quoteHandle[i],
settlementDays,
100.0,
schedule,
std::vector<Rate>(1,couponRates[i]),
redemption,
issueDates[i]);
bondHelpers.push_back(bondHelper);
}
auto bondDiscountingTermStructure = ext::make_shared<PiecewiseYieldCurve<Discount,LogLinear>>(
todaysDate, bondHelpers, termStructureDayCounter);
auto d6mRate = ext::make_shared<SimpleQuote>(d6mQuote);
auto s2yRate = ext::make_shared<SimpleQuote>(s2yQuote);
auto s3yRate = ext::make_shared<SimpleQuote>(s3yQuote);
auto s5yRate = ext::make_shared<SimpleQuote>(s5yQuote);
auto s10yRate = ext::make_shared<SimpleQuote>(s10yQuote);
auto s15yRate = ext::make_shared<SimpleQuote>(s15yQuote);
Natural fixingDays = 2;
auto d6m = ext::make_shared<DepositRateHelper>(
6*Months, fixingDays,
true, depositDayCounter);
auto swFixedLegFrequency =
Annual;
auto swFixedLegDayCounter =
Thirty360(Thirty360::European);
auto swFloatingLegIndex = ext::make_shared<Euribor6M>();
auto s2y = ext::make_shared<SwapRateHelper>(
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex);
auto s3y = ext::make_shared<SwapRateHelper>(
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex);
auto s5y = ext::make_shared<SwapRateHelper>(
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex);
auto s10y = ext::make_shared<SwapRateHelper>(
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex);
auto s15y = ext::make_shared<SwapRateHelper>(
calendar, swFixedLegFrequency,
swFixedLegConvention, swFixedLegDayCounter,
swFloatingLegIndex);
std::vector<ext::shared_ptr<RateHelper>> depoSwapInstruments;
depoSwapInstruments.push_back(d6m);
depoSwapInstruments.push_back(s2y);
depoSwapInstruments.push_back(s3y);
depoSwapInstruments.push_back(s5y);
depoSwapInstruments.push_back(s10y);
depoSwapInstruments.push_back(s15y);
Date spotDate = calendar.
advance(todaysDate, fixingDays, Days);
auto depoSwapTermStructure = ext::make_shared<PiecewiseYieldCurve<Discount, LogLinear>>(
spotDate, depoSwapInstruments,
termStructureDayCounter);
Real faceAmount = 100;
auto bondEngine = ext::make_shared<DiscountingBondEngine>(discountingTermStructure);
settlementDays,
faceAmount,
Real(116.92),
settlementDays,
faceAmount,
fixedBondSchedule,
std::vector<Rate>(1, 0.045),
100.0,
Date(15, May, 2007));
const auto euribor6m = ext::make_shared<Euribor>(
Period(6, Months), forecastingTermStructure);
euribor6m->addFixing(
Date(18, October, 2007), 0.026);
euribor6m->addFixing(
Date(17, April, 2008), 0.028);
settlementDays,
faceAmount,
floatingBondSchedule,
euribor6m,
Natural(2),
std::vector<Real>(1, 1.0),
std::vector<Rate>(1, 0.001),
std::vector<Rate>(),
std::vector<Rate>(),
false,
Real(100.0),
Date(21, October, 2005));
auto pricer = ext::make_shared<BlackIborCouponPricer>();
ext::make_shared<ConstantOptionletVolatility>(
settlementDays,
calendar,
volatility,
pricer->setCapletVolatility(vol);
setCouponPricer(floatingRateBond.
cashflows(),pricer);
forecastingTermStructure.linkTo(depoSwapTermStructure);
discountingTermStructure.linkTo(bondDiscountingTermStructure);
std::cout << std::endl;
Size widths[] = { 18, 10, 10, 10 };
std::cout << std::setw(widths[0]) << " "
<< std::setw(widths[1]) << "ZC"
<< std::setw(widths[2]) << "Fixed"
<< std::setw(widths[3]) << "Floating"
<< std::endl;
Size width = widths[0] + widths[1] + widths[2] + widths[3];
std::string rule(width, '-');
std::cout << rule << std::endl;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << std::setw(widths[0]) << "Net present value"
<< std::setw(widths[1]) << zeroCouponBond.
NPV()
<< std::setw(widths[2]) << fixedRateBond.
NPV()
<< std::setw(widths[3]) << floatingRateBond.
NPV()
<< std::endl;
std::cout << std::setw(widths[0]) << "Clean price"
<< std::setw(widths[1]) << zeroCouponBond.
cleanPrice()
<< std::setw(widths[2]) << fixedRateBond.
cleanPrice()
<< std::setw(widths[3]) << floatingRateBond.
cleanPrice()
<< std::endl;
std::cout << std::setw(widths[0]) << "Dirty price"
<< std::setw(widths[1]) << zeroCouponBond.
dirtyPrice()
<< std::setw(widths[2]) << fixedRateBond.
dirtyPrice()
<< std::setw(widths[3]) << floatingRateBond.
dirtyPrice()
<< std::endl;
std::cout << std::setw(widths[0]) << "Accrued coupon"
<< std::endl;
std::cout << std::setw(widths[0]) << "Previous coupon"
<< std::setw(widths[1]) << "N/A"
<< std::endl;
std::cout << std::setw(widths[0]) << "Next coupon"
<< std::setw(widths[1]) << "N/A"
<< std::endl;
std::cout << std::setw(widths[0]) << "Yield"
<< std::setw(widths[1])
<< std::setw(widths[2])
<< std::setw(widths[3])
<< std::endl;
std::cout << std::endl;
std::cout << "Sample indirect computations (for the floating rate bond): " << std::endl;
std::cout << rule << std::endl;
std::cout << "Yield to Clean Price: "
std::cout << "Clean Price to Yield: "
<<
io::rate(floatingRateBond.
yield({floatingRateBond.cleanPrice(), Bond::Price::Clean},
<< std::endl
<< std::endl;
return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "unknown error" << std::endl;
return 1;
}
}