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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2013, 2016 Peter Caspers
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
<http://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 <ql/instruments/nonstandardswap.hpp>
#include <ql/cashflows/simplecashflow.hpp>
#include <ql/cashflows/iborcoupon.hpp>
#include <ql/cashflows/cmscoupon.hpp>
#include <ql/cashflows/capflooredcoupon.hpp>
#include <ql/cashflows/cashflowvectors.hpp>
#include <ql/cashflows/cashflows.hpp>
#include <ql/cashflows/couponpricer.hpp>
#include <ql/indexes/iborindex.hpp>
#include <ql/indexes/swapindex.hpp>
#include <ql/termstructures/yieldtermstructure.hpp>
namespace QuantLib {
NonstandardSwap::NonstandardSwap(const VanillaSwap &fromVanilla)
: Swap(2), type_((VanillaSwap::Type)fromVanilla.type()),
fixedNominal_(std::vector<Real>(fromVanilla.fixedLeg().size(),
fromVanilla.nominal())),
floatingNominal_(std::vector<Real>(fromVanilla.floatingLeg().size(),
fromVanilla.nominal())),
fixedSchedule_(fromVanilla.fixedSchedule()),
fixedRate_(std::vector<Real>(fromVanilla.fixedLeg().size(),
fromVanilla.fixedRate())),
fixedDayCount_(fromVanilla.fixedDayCount()),
floatingSchedule_(fromVanilla.floatingSchedule()),
iborIndex_(fromVanilla.iborIndex()),
spread_(std::vector<Real>(fromVanilla.floatingLeg().size(), fromVanilla.spread())),
gearing_(std::vector<Real>(fromVanilla.floatingLeg().size(), 1.0)),
singleSpreadAndGearing_(true),
floatingDayCount_(fromVanilla.floatingDayCount()),
paymentConvention_(fromVanilla.paymentConvention()),
intermediateCapitalExchange_(false), finalCapitalExchange_(false) {
init();
}
NonstandardSwap::NonstandardSwap(
const VanillaSwap::Type type, const std::vector<Real> &fixedNominal,
const std::vector<Real> &floatingNominal, const Schedule &fixedSchedule,
const std::vector<Real> &fixedRate, const DayCounter &fixedDayCount,
const Schedule &floatingSchedule,
const ext::shared_ptr<IborIndex> &iborIndex, const Real gearing,
const Spread spread, const DayCounter &floatingDayCount,
const bool intermediateCapitalExchange, const bool finalCapitalExchange,
boost::optional<BusinessDayConvention> paymentConvention)
: Swap(2), type_(type), fixedNominal_(fixedNominal),
floatingNominal_(floatingNominal), fixedSchedule_(fixedSchedule),
fixedRate_(fixedRate), fixedDayCount_(fixedDayCount),
floatingSchedule_(floatingSchedule), iborIndex_(iborIndex),
spread_(std::vector<Real>(floatingNominal.size(), spread)),
gearing_(std::vector<Real>(floatingNominal.size(), gearing)),
singleSpreadAndGearing_(true),
floatingDayCount_(floatingDayCount),
intermediateCapitalExchange_(intermediateCapitalExchange),
finalCapitalExchange_(finalCapitalExchange) {
if (paymentConvention) // NOLINT(readability-implicit-bool-conversion)
paymentConvention_ = *paymentConvention;
else
paymentConvention_ = floatingSchedule_.businessDayConvention();
init();
}
NonstandardSwap::NonstandardSwap(
const VanillaSwap::Type type, const std::vector<Real> &fixedNominal,
const std::vector<Real> &floatingNominal, const Schedule &fixedSchedule,
const std::vector<Real> &fixedRate, const DayCounter &fixedDayCount,
const Schedule &floatingSchedule,
const ext::shared_ptr<IborIndex> &iborIndex,
const std::vector<Real> &gearing, const std::vector<Spread> &spread,
const DayCounter &floatingDayCount,
const bool intermediateCapitalExchange, const bool finalCapitalExchange,
boost::optional<BusinessDayConvention> paymentConvention)
: Swap(2), type_(type), fixedNominal_(fixedNominal),
floatingNominal_(floatingNominal), fixedSchedule_(fixedSchedule),
fixedRate_(fixedRate), fixedDayCount_(fixedDayCount),
floatingSchedule_(floatingSchedule), iborIndex_(iborIndex),
spread_(spread), gearing_(gearing), singleSpreadAndGearing_(false),
floatingDayCount_(floatingDayCount),
intermediateCapitalExchange_(intermediateCapitalExchange),
finalCapitalExchange_(finalCapitalExchange) {
if (paymentConvention) // NOLINT(readability-implicit-bool-conversion)
paymentConvention_ = *paymentConvention;
else
paymentConvention_ = floatingSchedule_.businessDayConvention();
init();
}
void NonstandardSwap::init() {
QL_REQUIRE(fixedNominal_.size() == fixedRate_.size(),
"Fixed nominal size ("
<< fixedNominal_.size()
<< ") does not match fixed rate size ("
<< fixedRate_.size() << ")");
QL_REQUIRE(fixedNominal_.size() == fixedSchedule_.size() - 1,
"Fixed nominal size (" << fixedNominal_.size()
<< ") does not match schedule size ("
<< fixedSchedule_.size() << ") - 1");
QL_REQUIRE(floatingNominal_.size() == floatingSchedule_.size() - 1,
"Floating nominal size ("
<< floatingNominal_.size()
<< ") does not match schedule size ("
<< floatingSchedule_.size() << ") - 1");
QL_REQUIRE(floatingNominal_.size() == spread_.size(),
"Floating nominal size (" << floatingNominal_.size()
<< ") does not match spread size ("
<< spread_.size() << ")");
QL_REQUIRE(floatingNominal_.size() == gearing_.size(),
"Floating nominal size ("
<< floatingNominal_.size()
<< ") does not match gearing size (" << gearing_.size()
<< ")");
// if the gearing is zero then the ibor leg will be set up with fixed
// coupons which makes trouble here in this context. We therefore use
// a dirty trick and enforce the gearing to be non zero.
for (Size i = 0; i < gearing_.size(); ++i) {
if (close(gearing_[i], 0.0))
gearing_[i] = QL_EPSILON;
}
legs_[0] = FixedRateLeg(fixedSchedule_)
.withNotionals(fixedNominal_)
.withCouponRates(fixedRate_, fixedDayCount_)
.withPaymentAdjustment(paymentConvention_);
legs_[1] = IborLeg(floatingSchedule_, iborIndex_)
.withNotionals(floatingNominal_)
.withPaymentDayCounter(floatingDayCount_)
.withPaymentAdjustment(paymentConvention_)
.withSpreads(spread_)
.withGearings(gearing_);
if (intermediateCapitalExchange_) {
for (Size i = 0; i < legs_[0].size() - 1; i++) {
Real cap = fixedNominal_[i] - fixedNominal_[i + 1];
if (!close(cap, 0.0)) {
std::vector<ext::shared_ptr<CashFlow> >::iterator it1 =
legs_[0].begin();
std::advance(it1, i + 1);
legs_[0].insert(
it1, ext::shared_ptr<CashFlow>(
new Redemption(cap, legs_[0][i]->date())));
std::vector<Real>::iterator it2 = fixedNominal_.begin();
std::advance(it2, i + 1);
fixedNominal_.insert(it2, fixedNominal_[i]);
std::vector<Real>::iterator it3 = fixedRate_.begin();
std::advance(it3, i + 1);
fixedRate_.insert(it3, 0.0);
i++;
}
}
for (Size i = 0; i < legs_[1].size() - 1; i++) {
Real cap = floatingNominal_[i] - floatingNominal_[i + 1];
if (!close(cap, 0.0)) {
std::vector<ext::shared_ptr<CashFlow> >::iterator it1 =
legs_[1].begin();
std::advance(it1, i + 1);
legs_[1].insert(
it1, ext::shared_ptr<CashFlow>(
new Redemption(cap, legs_[1][i]->date())));
std::vector<Real>::iterator it2 = floatingNominal_.begin();
std::advance(it2, i + 1);
floatingNominal_.insert(it2, floatingNominal_[i]);
i++;
}
}
}
if (finalCapitalExchange_) {
legs_[0].push_back(ext::shared_ptr<CashFlow>(
new Redemption(fixedNominal_.back(), legs_[0].back()->date())));
fixedNominal_.push_back(fixedNominal_.back());
fixedRate_.push_back(0.0);
legs_[1].push_back(ext::shared_ptr<CashFlow>(new Redemption(
floatingNominal_.back(), legs_[1].back()->date())));
floatingNominal_.push_back(floatingNominal_.back());
}
for (Leg::const_iterator i = legs_[1].begin(); i < legs_[1].end(); ++i)
registerWith(*i);
switch (type_) {
case VanillaSwap::Payer:
payer_[0] = -1.0;
payer_[1] = +1.0;
break;
case VanillaSwap::Receiver:
payer_[0] = +1.0;
payer_[1] = -1.0;
break;
default:
QL_FAIL("Unknown nonstandard-swap type");
}
}
void NonstandardSwap::setupArguments(PricingEngine::arguments *args) const {
Swap::setupArguments(args);
NonstandardSwap::arguments *arguments =
dynamic_cast<NonstandardSwap::arguments *>(args);
if (arguments == 0)
return; // swap engine ...
arguments->type = type_;
arguments->fixedNominal = fixedNominal_;
arguments->floatingNominal = floatingNominal_;
arguments->fixedRate = fixedRate_;
const Leg &fixedCoupons = fixedLeg();
arguments->fixedResetDates = arguments->fixedPayDates =
std::vector<Date>(fixedCoupons.size());
arguments->fixedCoupons = std::vector<Real>(fixedCoupons.size());
arguments->fixedIsRedemptionFlow =
std::vector<bool>(fixedCoupons.size(), false);
for (Size i = 0; i < fixedCoupons.size(); ++i) {
ext::shared_ptr<FixedRateCoupon> coupon =
ext::dynamic_pointer_cast<FixedRateCoupon>(fixedCoupons[i]);
if (coupon != 0) {
arguments->fixedPayDates[i] = coupon->date();
arguments->fixedResetDates[i] = coupon->accrualStartDate();
arguments->fixedCoupons[i] = coupon->amount();
} else {
ext::shared_ptr<CashFlow> cashflow =
ext::dynamic_pointer_cast<CashFlow>(fixedCoupons[i]);
std::vector<Date>::const_iterator j =
std::find(arguments->fixedPayDates.begin(),
arguments->fixedPayDates.end(), cashflow->date());
QL_REQUIRE(j != arguments->fixedPayDates.end(),
"nominal redemption on "
<< cashflow->date()
<< "has no corresponding coupon");
Size jIdx = j - arguments->fixedPayDates.begin();
arguments->fixedIsRedemptionFlow[i] = true;
arguments->fixedCoupons[i] = cashflow->amount();
arguments->fixedResetDates[i] =
arguments->fixedResetDates[jIdx];
arguments->fixedPayDates[i] = cashflow->date();
}
}
const Leg &floatingCoupons = floatingLeg();
arguments->floatingResetDates = arguments->floatingPayDates =
arguments->floatingFixingDates =
std::vector<Date>(floatingCoupons.size());
arguments->floatingAccrualTimes =
std::vector<Time>(floatingCoupons.size());
arguments->floatingSpreads =
std::vector<Spread>(floatingCoupons.size());
arguments->floatingGearings = std::vector<Real>(floatingCoupons.size());
arguments->floatingCoupons = std::vector<Real>(floatingCoupons.size());
arguments->floatingIsRedemptionFlow =
std::vector<bool>(floatingCoupons.size(), false);
for (Size i = 0; i < floatingCoupons.size(); ++i) {
ext::shared_ptr<IborCoupon> coupon =
ext::dynamic_pointer_cast<IborCoupon>(floatingCoupons[i]);
if (coupon != 0) {
arguments->floatingResetDates[i] = coupon->accrualStartDate();
arguments->floatingPayDates[i] = coupon->date();
arguments->floatingFixingDates[i] = coupon->fixingDate();
arguments->floatingAccrualTimes[i] = coupon->accrualPeriod();
arguments->floatingSpreads[i] = coupon->spread();
arguments->floatingGearings[i] = coupon->gearing();
try {
arguments->floatingCoupons[i] = coupon->amount();
}
catch (Error &) {
arguments->floatingCoupons[i] = Null<Real>();
}
} else {
ext::shared_ptr<CashFlow> cashflow =
ext::dynamic_pointer_cast<CashFlow>(floatingCoupons[i]);
std::vector<Date>::const_iterator j = std::find(
arguments->floatingPayDates.begin(),
arguments->floatingPayDates.end(), cashflow->date());
QL_REQUIRE(j != arguments->floatingPayDates.end(),
"nominal redemption on "
<< cashflow->date()
<< "has no corresponding coupon");
Size jIdx = j - arguments->floatingPayDates.begin();
arguments->floatingIsRedemptionFlow[i] = true;
arguments->floatingCoupons[i] = cashflow->amount();
arguments->floatingResetDates[i] =
arguments->floatingResetDates[jIdx];
arguments->floatingFixingDates[i] =
arguments->floatingFixingDates[jIdx];
arguments->floatingAccrualTimes[i] = 0.0;
arguments->floatingSpreads[i] = 0.0;
arguments->floatingGearings[i] = 1.0;
arguments->floatingPayDates[i] = cashflow->date();
}
}
arguments->iborIndex = iborIndex();
}
void NonstandardSwap::setupExpired() const { Swap::setupExpired(); }
void NonstandardSwap::fetchResults(const PricingEngine::results *r) const {
Swap::fetchResults(r);
}
void NonstandardSwap::arguments::validate() const {
Swap::arguments::validate();
QL_REQUIRE(fixedNominal.size() == fixedPayDates.size(),
"number of fixed leg nominals plus redemption flows "
"different from number of payment dates");
QL_REQUIRE(fixedRate.size() == fixedPayDates.size(),
"number of fixed rates plus redemption flows different from "
"number of payment dates");
QL_REQUIRE(floatingNominal.size() == floatingPayDates.size(),
"number of float leg nominals different from number of "
"payment dates");
QL_REQUIRE(fixedResetDates.size() == fixedPayDates.size(),
"number of fixed start dates different from "
"number of fixed payment dates");
QL_REQUIRE(fixedPayDates.size() == fixedCoupons.size(),
"number of fixed payment dates different from "
"number of fixed coupon amounts");
QL_REQUIRE(floatingResetDates.size() == floatingPayDates.size(),
"number of floating start dates different from "
"number of floating payment dates");
QL_REQUIRE(floatingFixingDates.size() == floatingPayDates.size(),
"number of floating fixing dates different from "
"number of floating payment dates");
QL_REQUIRE(floatingAccrualTimes.size() == floatingPayDates.size(),
"number of floating accrual Times different from "
"number of floating payment dates");
QL_REQUIRE(floatingSpreads.size() == floatingPayDates.size(),
"number of floating spreads different from "
"number of floating payment dates");
QL_REQUIRE(floatingPayDates.size() == floatingCoupons.size(),
"number of floating payment dates different from "
"number of floating coupon amounts");
}
void NonstandardSwap::results::reset() { Swap::results::reset(); }
}
|