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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2015 Jose Aparicio
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 <ql/pricingengines/swap/cvaswapengine.hpp>
#include <ql/cashflows/fixedratecoupon.hpp>
#include <ql/cashflows/floatingratecoupon.hpp>
#include <ql/indexes/iborindex.hpp>
#include <ql/instruments/makevanillaswap.hpp>
#include <ql/exercise.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/termstructures/credit/flathazardrate.hpp>
#include <ql/pricingengines/swaption/blackswaptionengine.hpp>
#include <ql/time/calendars/nullcalendar.hpp>
namespace QuantLib {
CounterpartyAdjSwapEngine::CounterpartyAdjSwapEngine(
const Handle<YieldTermStructure>& discountCurve,
const Handle<PricingEngine>& swaptionEngine,
const Handle<DefaultProbabilityTermStructure>& ctptyDTS,
Real ctptyRecoveryRate,
const Handle<DefaultProbabilityTermStructure>& invstDTS,
Real invstRecoveryRate)
: baseSwapEngine_(Handle<PricingEngine>(
ext::make_shared<DiscountingSwapEngine>(discountCurve))),
swaptionletEngine_(swaptionEngine),
discountCurve_(discountCurve),
defaultTS_(ctptyDTS),
ctptyRecoveryRate_(ctptyRecoveryRate),
invstDTS_(invstDTS.empty() ? Handle<DefaultProbabilityTermStructure>(
ext::make_shared<FlatHazardRate>(0, NullCalendar(), 1.e-12,
ctptyDTS->dayCounter()) ) : invstDTS ),
invstRecoveryRate_(invstRecoveryRate)
{
registerWith(discountCurve);
registerWith(ctptyDTS);
registerWith(invstDTS_);
registerWith(swaptionEngine);
}
CounterpartyAdjSwapEngine::CounterpartyAdjSwapEngine(
const Handle<YieldTermStructure>& discountCurve,
const Volatility blackVol,
const Handle<DefaultProbabilityTermStructure>& ctptyDTS,
Real ctptyRecoveryRate,
const Handle<DefaultProbabilityTermStructure>& invstDTS,
Real invstRecoveryRate)
: baseSwapEngine_(Handle<PricingEngine>(
ext::make_shared<DiscountingSwapEngine>(discountCurve))),
swaptionletEngine_(Handle<PricingEngine>(
ext::make_shared<BlackSwaptionEngine>(discountCurve,
blackVol))),
discountCurve_(discountCurve),
defaultTS_(ctptyDTS),
ctptyRecoveryRate_(ctptyRecoveryRate),
invstDTS_(invstDTS.empty() ? Handle<DefaultProbabilityTermStructure>(
ext::make_shared<FlatHazardRate>(0, NullCalendar(), 1.e-12,
ctptyDTS->dayCounter()) ) : invstDTS ),
invstRecoveryRate_(invstRecoveryRate)
{
registerWith(discountCurve);
registerWith(ctptyDTS);
registerWith(invstDTS_);
}
CounterpartyAdjSwapEngine::CounterpartyAdjSwapEngine(
const Handle<YieldTermStructure>& discountCurve,
const Handle<Quote>& blackVol,
const Handle<DefaultProbabilityTermStructure>& ctptyDTS,
Real ctptyRecoveryRate,
const Handle<DefaultProbabilityTermStructure>& invstDTS,
Real invstRecoveryRate)
: baseSwapEngine_(Handle<PricingEngine>(
ext::make_shared<DiscountingSwapEngine>(discountCurve))),
swaptionletEngine_(Handle<PricingEngine>(
ext::make_shared<BlackSwaptionEngine>(discountCurve,
blackVol))),
discountCurve_(discountCurve),
defaultTS_(ctptyDTS),
ctptyRecoveryRate_(ctptyRecoveryRate),
invstDTS_(invstDTS.empty() ? Handle<DefaultProbabilityTermStructure>(
ext::make_shared<FlatHazardRate>(0, NullCalendar(), 1.e-12,
ctptyDTS->dayCounter()) ) : invstDTS ),
invstRecoveryRate_(invstRecoveryRate)
{
registerWith(discountCurve);
registerWith(ctptyDTS);
registerWith(invstDTS_);
registerWith(blackVol);
}
void CounterpartyAdjSwapEngine::calculate() const {
/* both DTS, YTS ref dates and pricing date consistency
checks? settlement... */
QL_REQUIRE(!discountCurve_.empty(),
"no discount term structure set");
QL_REQUIRE(!defaultTS_.empty(),
"no ctpty default term structure set");
QL_REQUIRE(!swaptionletEngine_.empty(),
"no swap option engine set");
QL_REQUIRE(arguments_.nominal != Null<Real>(),
"non-constant nominals are not supported yet");
Date priceDate = defaultTS_->referenceDate();
Real cumOptVal = 0.,
cumPutVal = 0.;
// Vanilla swap so 0 leg is floater
auto nextFD =
arguments_.fixedPayDates.begin();
Date swapletStart = priceDate;
while (*nextFD < priceDate) ++nextFD;
// Compute fair spread for strike value:
// copy args into the non risky engine
auto* noCVAArgs = dynamic_cast<Swap::arguments*>(baseSwapEngine_->getArguments());
QL_REQUIRE(noCVAArgs != nullptr, "wrong argument type");
noCVAArgs->legs = this->arguments_.legs;
noCVAArgs->payer = this->arguments_.payer;
baseSwapEngine_->calculate();
ext::shared_ptr<FixedRateCoupon> coupon = ext::dynamic_pointer_cast<FixedRateCoupon>(arguments_.legs[0][0]);
QL_REQUIRE(coupon,"dynamic cast of fixed leg coupon failed.");
Rate baseSwapRate = coupon->rate();
const auto* vSResults = dynamic_cast<const Swap::results*>(baseSwapEngine_->getResults());
QL_REQUIRE(vSResults != nullptr, "wrong result type");
Rate baseSwapFairRate = -baseSwapRate * vSResults->legNPV[1] /
vSResults->legNPV[0];
Real baseSwapNPV = vSResults->value;
Swap::Type reversedType = arguments_.type == Swap::Payer ? Swap::Receiver : Swap::Payer;
// Swaplet options summatory:
while(nextFD != arguments_.fixedPayDates.end()) {
// iFD coupon not fixed, create swaptionlet:
ext::shared_ptr<FloatingRateCoupon> floatCoupon = ext::dynamic_pointer_cast<FloatingRateCoupon>(arguments_.legs[1][0]);
QL_REQUIRE(floatCoupon,"dynamic cast of floating leg coupon failed.");
ext::shared_ptr<IborIndex> swapIndex = ext::dynamic_pointer_cast<IborIndex>(floatCoupon->index());
QL_REQUIRE(swapIndex,"dynamic cast of floating leg index failed.");
// Alternatively one could cap this period to, say, 1M
// Period swapPeriod = ext::dynamic_pointer_cast<FloatingRateCoupon>(
// arguments_.legs[1][0])->index()->tenor();
Period baseSwapsTenor(arguments_.fixedPayDates.back().serialNumber()
- swapletStart.serialNumber(), Days);
ext::shared_ptr<VanillaSwap> swaplet = MakeVanillaSwap(
baseSwapsTenor,
swapIndex,
baseSwapFairRate // strike
)
.withType(arguments_.type)
.withNominal(arguments_.nominal)
//////// .withSettlementDays(2)
.withEffectiveDate(swapletStart)
.withTerminationDate(arguments_.fixedPayDates.back());
ext::shared_ptr<VanillaSwap> revSwaplet = MakeVanillaSwap(
baseSwapsTenor,
swapIndex,
baseSwapFairRate // strike
)
.withType(reversedType)
.withNominal(arguments_.nominal)
///////// .withSettlementDays(2)
.withEffectiveDate(swapletStart)
.withTerminationDate(arguments_.fixedPayDates.back());
Swaption swaptionlet(swaplet,
ext::make_shared<EuropeanExercise>(swapletStart));
Swaption putSwaplet(revSwaplet,
ext::make_shared<EuropeanExercise>(swapletStart));
swaptionlet.setPricingEngine(swaptionletEngine_.currentLink());
putSwaplet.setPricingEngine(swaptionletEngine_.currentLink());
// atm underlying swap means that the value of put = value
// call so this double pricing is not needed
cumOptVal += swaptionlet.NPV() * defaultTS_->defaultProbability(
swapletStart, *nextFD);
cumPutVal += putSwaplet.NPV() * invstDTS_->defaultProbability(
swapletStart, *nextFD);
swapletStart = *nextFD;
++nextFD;
}
results_.value = baseSwapNPV - (1.-ctptyRecoveryRate_) * cumOptVal
+ (1.-invstRecoveryRate_) * cumPutVal;
results_.fairRate = -baseSwapRate * (vSResults->legNPV[1]
- (1.-ctptyRecoveryRate_) * cumOptVal +
(1.-invstRecoveryRate_) * cumPutVal )
/ vSResults->legNPV[0];
}
}
|