1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2021 Marcin Rybacki
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
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.
*/
#include "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/instruments/zerocouponswap.hpp>
#include <ql/cashflows/multipleresetscoupon.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/time/calendars/target.hpp>
using namespace QuantLib;
using namespace boost::unit_test_framework;
BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(ZeroCouponSwapTests)
struct CommonVars {
Date today, settlement;
Calendar calendar;
Natural settlementDays, paymentDelay;
DayCounter dayCount;
BusinessDayConvention businessConvention;
Real baseNominal, finalPayment;
ext::shared_ptr<IborIndex> euribor;
RelinkableHandle<YieldTermStructure> euriborHandle;
ext::shared_ptr<PricingEngine> discountEngine;
// utilities
CommonVars() {
settlementDays = 2;
paymentDelay = 1;
calendar = TARGET();
dayCount = Actual365Fixed();
businessConvention = ModifiedFollowing;
baseNominal = 1.0e6;
finalPayment = 1.2e6;
euribor = ext::shared_ptr<IborIndex>(new Euribor6M(euriborHandle));
euribor->addFixing(Date(10, February, 2021), 0.0085);
today = calendar.adjust(Date(15, March, 2021));
Settings::instance().evaluationDate() = today;
settlement = calendar.advance(today, settlementDays, Days);
euriborHandle.linkTo(flatRate(settlement, 0.007, dayCount));
discountEngine =
ext::shared_ptr<PricingEngine>(new DiscountingSwapEngine(euriborHandle));
}
ext::shared_ptr<CashFlow> createMultipleResetsCoupon(const Date& start, const Date& end) const {
Date paymentDate = calendar.advance(end, paymentDelay * Days, businessConvention);
Schedule schedule = MakeSchedule()
.from(start).to(end)
.withTenor(euribor->tenor())
.withCalendar(euribor->fixingCalendar());
auto cpn = ext::make_shared<MultipleResetsCoupon>(
paymentDate, baseNominal, schedule, settlementDays, euribor);
cpn->setPricer(ext::make_shared<CompoundingMultipleResetsPricer>());
return cpn;
}
ext::shared_ptr<ZeroCouponSwap> createZCSwap(Swap::Type type,
const Date& start,
const Date& end,
Real baseNominal,
Real finalPayment) {
auto swap = ext::make_shared<ZeroCouponSwap>(type, baseNominal, start, end, finalPayment,
euribor, calendar, businessConvention,
paymentDelay);
swap->setPricingEngine(discountEngine);
return swap;
}
ext::shared_ptr<ZeroCouponSwap> createZCSwap(Swap::Type type,
const Date& start,
const Date& end,
Real finalPayment) {
return createZCSwap(type, start, end, baseNominal, finalPayment);
}
ext::shared_ptr<ZeroCouponSwap> createZCSwap(Swap::Type type,
const Date& start,
const Date& end) {
return createZCSwap(type, start, end, finalPayment);
}
ext::shared_ptr<ZeroCouponSwap> createZCSwap(const Date& start,
const Date& end,
Rate fixedRate) {
auto swap = ext::make_shared<ZeroCouponSwap>(Swap::Receiver, baseNominal,
start, end, fixedRate, dayCount, euribor,
calendar, businessConvention, paymentDelay);
swap->setPricingEngine(discountEngine);
return swap;
}
};
void checkReplicationOfZeroCouponSwapNPV(const Date& start,
const Date& end,
Swap::Type type = Swap::Receiver) {
CommonVars vars;
const Real tolerance = 1.0e-8;
auto zcSwap = vars.createZCSwap(type, start, end);
Real actualNPV = zcSwap->NPV();
Real actualFixedLegNPV = zcSwap->fixedLegNPV();
Real actualFloatLegNPV = zcSwap->floatingLegNPV();
Date paymentDate =
vars.calendar.advance(end, vars.paymentDelay * Days, vars.businessConvention);
Real discountAtPayment =
paymentDate < vars.settlement ? 0.0 : vars.euriborHandle->discount(paymentDate);
Real expectedFixedLegNPV = -type * discountAtPayment * vars.finalPayment;
auto subPeriodCpn = vars.createMultipleResetsCoupon(start, end);
Real expectedFloatLegNPV =
paymentDate < vars.settlement ? 0.0 : Real(Integer(type) * discountAtPayment * subPeriodCpn->amount());
Real expectedNPV = expectedFloatLegNPV + expectedFixedLegNPV;
if ((std::fabs(actualNPV - expectedNPV) > tolerance) ||
(std::fabs(actualFixedLegNPV - expectedFixedLegNPV) > tolerance) ||
(std::fabs(actualFloatLegNPV - expectedFloatLegNPV) > tolerance))
BOOST_ERROR("unable to replicate NPVs of zero coupon swap and its legs\n"
<< " actual NPV: " << actualNPV << "\n"
<< " expected NPV: " << expectedNPV << "\n"
<< " actual fixed leg NPV: " << actualFixedLegNPV << "\n"
<< " expected fixed leg NPV: " << expectedFixedLegNPV << "\n"
<< " actual float leg NPV: " << actualFloatLegNPV << "\n"
<< " expected float leg NPV: " << expectedFloatLegNPV << "\n"
<< " start: " << start << "\n"
<< " end: " << end << "\n"
<< " type: " << type << "\n");
}
void checkFairFixedPayment(const Date& start,
const Date& end,
Swap::Type type) {
CommonVars vars;
const Real tolerance = 1.0e-8;
auto zcSwap = vars.createZCSwap(type, start, end);
Real fairFixedPayment = zcSwap->fairFixedPayment();
auto parZCSwap = vars.createZCSwap(type, start, end, fairFixedPayment);
Real parZCSwapNPV = parZCSwap->NPV();
if ((std::fabs(parZCSwapNPV) > tolerance))
BOOST_ERROR("unable to replicate fair fixed payment\n"
<< " actual NPV: " << parZCSwapNPV << "\n"
<< " expected NPV: 0.0\n"
<< " fair fixed payment: " << fairFixedPayment << "\n"
<< " start: " << start << "\n"
<< " end: " << end << "\n"
<< " type: " << type << "\n");
}
void checkFairFixedRate(const Date& start, const Date& end, Swap::Type type) {
CommonVars vars;
const Real tolerance = 1.0e-8;
auto zcSwap = vars.createZCSwap(type, start, end);
Rate fairFixedRate = zcSwap->fairFixedRate(vars.dayCount);
auto parZCSwap = vars.createZCSwap(start, end, fairFixedRate);
Real parZCSwapNPV = parZCSwap->NPV();
if ((std::fabs(parZCSwapNPV) > tolerance))
BOOST_ERROR("unable to replicate fair fixed rate\n"
<< " actual NPV: " << parZCSwapNPV << "\n"
<< " expected NPV: 0.0\n"
<< " fair fixed rate: " << fairFixedRate << "\n"
<< " start: " << start << "\n"
<< " end: " << end << "\n"
<< " type: " << type << "\n");
}
BOOST_AUTO_TEST_CASE(testInstrumentValuation) {
BOOST_TEST_MESSAGE("Testing zero coupon swap valuation...");
// Ongoing instrument
checkReplicationOfZeroCouponSwapNPV(Date(12, February, 2021), Date(12, February, 2041),
Swap::Receiver);
// Forward starting instrument
checkReplicationOfZeroCouponSwapNPV(Date(15, April, 2021), Date(12, February, 2041),
Swap::Payer);
// Expired instrument
checkReplicationOfZeroCouponSwapNPV(Date(12, February, 2000), Date(12, February, 2020));
}
BOOST_AUTO_TEST_CASE(testFairFixedPayment) {
BOOST_TEST_MESSAGE("Testing fair fixed payment...");
// Ongoing instrument
checkFairFixedPayment(Date(12, February, 2021), Date(12, February, 2041),
Swap::Receiver);
// Spot starting instrument
checkFairFixedPayment(Date(17, March, 2021), Date(12, February, 2041),
Swap::Payer);
}
BOOST_AUTO_TEST_CASE(testFairFixedRate) {
BOOST_TEST_MESSAGE("Testing fair fixed rate...");
// Ongoing instrument
checkFairFixedRate(Date(12, February, 2021), Date(12, February, 2041),
Swap::Receiver);
// Spot starting instrument
checkFairFixedRate(Date(17, March, 2021), Date(12, February, 2041), Swap::Payer);
}
BOOST_AUTO_TEST_CASE(testFixedPaymentFromRate) {
BOOST_TEST_MESSAGE("Testing fixed payment calculation from rate...");
CommonVars vars;
const Real tolerance = 1.0e-8;
const Rate fixedRate = 0.01;
Date start(12, February, 2021);
Date end(12, February, 2041);
auto zcSwap = vars.createZCSwap(start, end, fixedRate);
Real actualFxdPmt = zcSwap->fixedPayment();
Time T = vars.dayCount.yearFraction(start, end);
Real expectedFxdPmt = zcSwap->baseNominal() * (std::pow(1.0 + fixedRate, T) - 1.0);
if ((std::fabs(actualFxdPmt - expectedFxdPmt) > tolerance))
BOOST_ERROR("unable to replicate fixed payment from rate\n"
<< " actual fixed payment: " << actualFxdPmt << "\n"
<< " expected fixed payment: " << expectedFxdPmt << "\n"
<< " start: " << start << "\n"
<< " end: " << end << "\n");
}
BOOST_AUTO_TEST_CASE(testArgumentsValidation) {
BOOST_TEST_MESSAGE("Testing arguments validation...");
CommonVars vars;
Date start(12, February, 2021);
Date end(12, February, 2041);
// Negative base nominal
BOOST_CHECK_THROW(vars.createZCSwap(Swap::Payer, start, end, -1.0e6, 1.0e6),
Error);
// Start date after end date
BOOST_CHECK_THROW(vars.createZCSwap(end, start, 0.01), Error);
}
BOOST_AUTO_TEST_CASE(testExpectedCashFlowsInLegs) {
BOOST_TEST_MESSAGE("Testing expected cash flows in legs...");
CommonVars vars;
const Real tolerance = 1.0e-8;
Date start(12, February, 2021);
Date end(12, February, 2041);
auto zcSwap = vars.createZCSwap(start, end, 0.01);
auto fixedCashFlow = zcSwap->fixedLeg()[0];
auto floatingCashFlow = zcSwap->floatingLeg()[0];
Date paymentDate =
vars.calendar.advance(end, vars.paymentDelay * Days, vars.businessConvention);
auto subPeriodCpn = vars.createMultipleResetsCoupon(start, end);
if ((std::fabs(fixedCashFlow->amount() - zcSwap->fixedPayment()) > tolerance) ||
(fixedCashFlow->date() != paymentDate))
BOOST_ERROR("unable to replicate fixed leg\n"
<< " actual amount: " << fixedCashFlow->amount() << "\n"
<< " expected amount: " << zcSwap->fixedPayment() << "\n"
<< " actual payment date: " << fixedCashFlow->date() << "\n"
<< " expected payment date: " << paymentDate << "\n");
if ((std::fabs(floatingCashFlow->amount() - subPeriodCpn->amount()) > tolerance) ||
(floatingCashFlow->date() != paymentDate))
BOOST_ERROR("unable to replicate floating leg\n"
<< " actual amount: " << floatingCashFlow->amount() << "\n"
<< " expected amount: " << subPeriodCpn->amount() << "\n"
<< " actual payment date: " << floatingCashFlow->date() << "\n"
<< " expected payment date: " << paymentDate << "\n");
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
|