QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
creditdefaultswap.hpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008, 2009 Jose Aparicio
5 Copyright (C) 2008 Roland Lichters
6 Copyright (C) 2008 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file creditdefaultswap.hpp
23 \brief Credit default swap
24*/
25
26#ifndef quantlib_credit_default_swap_hpp
27#define quantlib_credit_default_swap_hpp
28
29#include <ql/instrument.hpp>
31#include <ql/default.hpp>
33#include <ql/time/schedule.hpp>
34#include <ql/optional.hpp>
35
36namespace QuantLib {
37
38 class YieldTermStructure;
39 class Claim;
40
41 //! Credit default swap
42 /*! \note This instrument currently assumes that the issuer did
43 not default until today's date.
44
45 \warning if <tt>Settings::includeReferenceDateCashFlows()</tt>
46 is set to <tt>true</tt>, payments occurring at the
47 settlement date of the swap might be included in the
48 NPV and therefore affect the fair-spread
49 calculation. This might not be what you want.
50
51 \warning conventionalSpread (and impliedHazardRate) by default
52 use the mid-point engine, which is not ISDA conform.
53
54 \ingroup instruments
55 */
57 public:
58 class arguments;
59 class results;
60 class engine;
63 ISDA
64 };
65 //! \name Constructors
66 //@{
67 //! CDS quoted as running-spread only
68 /*! @param side Whether the protection is bought or sold.
69 @param notional Notional value
70 @param spread Running spread in fractional units.
71 @param schedule Coupon schedule.
72 @param paymentConvention Business-day convention for
73 payment-date adjustment.
74 @param dayCounter Day-count convention for accrual.
75 @param settlesAccrual Whether or not the accrued coupon is
76 due in the event of a default.
77 @param paysAtDefaultTime If set to true, any payments
78 triggered by a default event are
79 due at default time. If set to
80 false, they are due at the end of
81 the accrual period.
82 @param protectionStart The first date where a default event will trigger the contract.
83 Before the CDS Big Bang 2009, this was typically trade date (T) + 1 calendar day.
84 After the CDS Big Bang 2009, protection is typically effective immediately i.e. on
85 trade date so this is what should be entered for protection start.
86 Notice that there is no default lookback period and protection start here.
87 In the way it determines the dirty amount it is more like the trade execution date.
88 @param lastPeriodDayCounter Day-count convention for accrual in last period
89 @param rebatesAccrual The protection seller pays the accrued
90 scheduled current coupon at the start
91 of the contract. The rebate date is not
92 provided but computed to be two days after
93 protection start.
94 @param tradeDate The contract's trade date. It will be used with the \p cashSettlementDays to determine
95 the date on which the cash settlement amount is paid. If not given, the trade date is
96 guessed from the protection start date and \p schedule date generation rule.
97 @param cashSettlementDays The number of business days from \p tradeDate to cash settlement date.
98 */
101 Rate spread,
102 const Schedule& schedule,
103 BusinessDayConvention paymentConvention,
104 const DayCounter& dayCounter,
105 bool settlesAccrual = true,
106 bool paysAtDefaultTime = true,
107 const Date& protectionStart = Date(),
108 ext::shared_ptr<Claim> = ext::shared_ptr<Claim>(),
109 const DayCounter& lastPeriodDayCounter = DayCounter(),
110 bool rebatesAccrual = true,
111 const Date& tradeDate = Date(),
113 //! CDS quoted as upfront and running spread
114 /*! @param side Whether the protection is bought or sold.
115 @param notional Notional value
116 @param upfront Upfront in fractional units.
117 @param spread Running spread in fractional units.
118 @param schedule Coupon schedule.
119 @param paymentConvention Business-day convention for
120 payment-date adjustment.
121 @param dayCounter Day-count convention for accrual.
122 @param settlesAccrual Whether or not the accrued coupon is
123 due in the event of a default.
124 @param paysAtDefaultTime If set to true, any payments
125 triggered by a default event are
126 due at default time. If set to
127 false, they are due at the end of
128 the accrual period.
129 @param protectionStart The first date where a default event will trigger the contract.
130 Before the CDS Big Bang 2009, this was typically trade date (T) + 1 calendar day.
131 After the CDS Big Bang 2009, protection is typically effective immediately i.e. on
132 trade date so this is what should be entered for protection start.
133 Notice that there is no default lookback period and protection start here.
134 In the way it determines the dirty amount it is more like the trade execution date.
135 @param upfrontDate Settlement date for the upfront and accrual
136 rebate (if any) payments.
137 Typically T+3, this is also the default
138 value.
139 @param lastPeriodDayCounter Day-count convention for accrual in last period
140 @param rebatesAccrual The protection seller pays the accrued
141 scheduled current coupon at the start
142 of the contract. The rebate date is not
143 provided but computed to be two days after
144 protection start.
145 @param tradeDate The contract's trade date. It will be used with the \p cashSettlementDays to determine
146 the date on which the cash settlement amount is paid if \p upfrontDate is empty. If not
147 given, the trade date is guessed from the protection start date and \p schedule date
148 generation rule.
149 @param cashSettlementDays The number of business days from \p tradeDate to cash settlement date.
150 */
154 Rate spread,
155 const Schedule& schedule,
156 BusinessDayConvention paymentConvention,
157 const DayCounter& dayCounter,
158 bool settlesAccrual = true,
159 bool paysAtDefaultTime = true,
160 const Date& protectionStart = Date(),
161 const Date& upfrontDate = Date(),
162 ext::shared_ptr<Claim> = ext::shared_ptr<Claim>(),
163 const DayCounter& lastPeriodDayCounter = DayCounter(),
164 bool rebatesAccrual = true,
165 const Date& tradeDate = Date(),
167 //@}
168 //! \name Instrument interface
169 //@{
170 bool isExpired() const override;
171 void setupArguments(PricingEngine::arguments*) const override;
172 void fetchResults(const PricingEngine::results*) const override;
173 //@}
174 //! \name Inspectors
175 //@{
176 Protection::Side side() const;
177 Real notional() const;
178 Rate runningSpread() const;
179 ext::optional<Rate> upfront() const;
180 bool settlesAccrual() const;
181 bool paysAtDefaultTime() const;
182 const Leg& coupons() const;
183 //! The first date for which defaults will trigger the contract
184 const Date& protectionStartDate() const;
185 //! The last date for which defaults will trigger the contract
186 const Date& protectionEndDate() const;
187 bool rebatesAccrual() const { return accrualRebate_ != nullptr; }
188 const ext::shared_ptr<SimpleCashFlow>& upfrontPayment() const;
189 const ext::shared_ptr<SimpleCashFlow>& accrualRebate() const;
190 const Date& tradeDate() const;
192 //@}
193 //! \name Results
194 //@{
195 /*! Returns the upfront spread that, given the running spread
196 and the quoted recovery rate, will make the instrument
197 have an NPV of 0.
198 */
199 Rate fairUpfront() const;
200 /*! Returns the running spread that, given the quoted recovery
201 rate, will make the running-only CDS have an NPV of 0.
202
203 \note This calculation does not take any upfront into
204 account, even if one was given.
205 */
206 Rate fairSpread() const;
207 /*! Returns the variation of the fixed-leg value given a
208 one-basis-point change in the running spread.
209 */
210 Real couponLegBPS() const;
211 Real upfrontBPS() const;
212 Real couponLegNPV() const;
213 Real defaultLegNPV() const;
214 Real upfrontNPV() const;
215 Real accrualRebateNPV() const;
216
217 //! Implied hazard rate calculation
218 /*! \note This method performs the calculation with the
219 instrument characteristics. It will coincide with
220 the ISDA calculation if your object has the standard
221 characteristics. Notably:
222 - The calendar should have no bank holidays, just
223 weekends.
224 - The yield curve should be LIBOR piecewise constant
225 in fwd rates, with a discount factor of 1 on the
226 calculation date, which coincides with the trade
227 date.
228 - Convention should be Following for yield curve and
229 contract cashflows.
230 - The CDS should pay accrued and mature on standard
231 IMM dates, settle on trade date +1 and upfront
232 settle on trade date +3.
233 */
234 Rate impliedHazardRate(Real targetNPV,
235 const Handle<YieldTermStructure>& discountCurve,
236 const DayCounter& dayCounter,
237 Real recoveryRate = 0.4,
238 Real accuracy = 1.0e-8,
239 PricingModel model = Midpoint) const;
240
241 //! Conventional/standard upfront-to-spread conversion
242 /*! Under a standard ISDA model and a set of standardised
243 instrument characteristics, it is the running only quoted
244 spread that will make a CDS contract have an NPV of 0 when
245 quoted for that running only spread. Refer to: "ISDA
246 Standard CDS converter specification." May 2009.
247
248 The conventional recovery rate to apply in the calculation
249 is as specified by ISDA, not necessarily equal to the
250 market-quoted one. It is typically 0.4 for SeniorSec and
251 0.2 for subordinate.
252
253 \note The conversion employs a flat hazard rate. As a result,
254 you will not recover the market quotes.
255
256 \note This method performs the calculation with the
257 instrument characteristics. It will coincide with
258 the ISDA calculation if your object has the standard
259 characteristics. Notably:
260 - The calendar should have no bank holidays, just
261 weekends.
262 - The yield curve should be LIBOR piecewise constant
263 in fwd rates, with a discount factor of 1 on the
264 calculation date, which coincides with the trade
265 date.
266 - Convention should be Following for yield curve and
267 contract cashflows.
268 - The CDS should pay accrued and mature on standard
269 IMM dates, settle on trade date +1 and upfront
270 settle on trade date +3.
271 */
272 Rate conventionalSpread(Real conventionalRecovery,
273 const Handle<YieldTermStructure>& discountCurve,
274 const DayCounter& dayCounter,
275 PricingModel model = Midpoint) const;
276 //@}
277 protected:
278 //! \name Instrument interface
279 //@{
280 void setupExpired() const override;
281 //@}
282 // data members
285 ext::optional<Rate> upfront_;
288 ext::shared_ptr<Claim> claim_;
290 ext::shared_ptr<SimpleCashFlow> upfrontPayment_;
291 ext::shared_ptr<SimpleCashFlow> accrualRebate_;
296 // results
303
304 private:
305 //! Shared initialisation.
306 void init(const Schedule& schedule, BusinessDayConvention paymentConvention, const DayCounter& dayCounter,
307 const DayCounter& lastPeriodDayCounter, bool rebatesAccrual, const Date& upfrontDate = Date());
308 };
309
310
312 : public virtual PricingEngine::arguments {
313 public:
314 arguments();
317 ext::optional<Rate> upfront;
320 // if not initialized by constructors means theres no flows.
321 ext::shared_ptr<SimpleCashFlow> upfrontPayment;
322 ext::shared_ptr<SimpleCashFlow> accrualRebate;
325 ext::shared_ptr<Claim> claim;
328 void validate() const override;
329 };
330
332 public:
341 void reset() override;
342 };
343
345 : public GenericEngine<CreditDefaultSwap::arguments,
346 CreditDefaultSwap::results> {};
347
348 /*! Return the CDS maturity date given the CDS trade date, \p tradeDate, the CDS \p tenor and a CDS \p rule.
349
350 A \c Null<Date>() is returned when a \p rule of \c CDS2015 and a \p tenor length of zero fail to yield a valid
351 CDS maturity date.
352
353 \warning An exception will be thrown if the \p rule is not \c CDS2015, \c CDS or \c OldCDS.
354
355 \warning An exception will be thrown if the \p rule is \c OldCDS and a \p tenor of 0 months is provided. This
356 restriction can be removed if 0M tenor was available before the CDS Big Bang 2009.
357
358 \warning An exception will be thrown if the \p tenor is not a multiple of 3 months. For the avoidance of
359 doubt, a \p tenor of 0 months is supported.
360 */
361 Date cdsMaturity(const Date& tradeDate, const Period& tenor, DateGeneration::Rule rule);
362
363}
364
365
366#endif
ext::shared_ptr< SimpleCashFlow > accrualRebate
ext::shared_ptr< SimpleCashFlow > upfrontPayment
const Date & protectionStartDate() const
The first date for which defaults will trigger the contract.
const ext::shared_ptr< SimpleCashFlow > & upfrontPayment() const
void init(const Schedule &schedule, BusinessDayConvention paymentConvention, const DayCounter &dayCounter, const DayCounter &lastPeriodDayCounter, bool rebatesAccrual, const Date &upfrontDate=Date())
Shared initialisation.
void setupArguments(PricingEngine::arguments *) const override
bool isExpired() const override
returns whether the instrument might have value greater than zero.
const Date & tradeDate() const
ext::shared_ptr< Claim > claim_
ext::shared_ptr< SimpleCashFlow > accrualRebate_
ext::optional< Rate > upfront() const
ext::optional< Rate > upfront_
Rate impliedHazardRate(Real targetNPV, const Handle< YieldTermStructure > &discountCurve, const DayCounter &dayCounter, Real recoveryRate=0.4, Real accuracy=1.0e-8, PricingModel model=Midpoint) const
Implied hazard rate calculation.
const ext::shared_ptr< SimpleCashFlow > & accrualRebate() const
void setupExpired() const override
const Date & protectionEndDate() const
The last date for which defaults will trigger the contract.
void fetchResults(const PricingEngine::results *) const override
Rate conventionalSpread(Real conventionalRecovery, const Handle< YieldTermStructure > &discountCurve, const DayCounter &dayCounter, PricingModel model=Midpoint) const
Conventional/standard upfront-to-spread conversion.
ext::shared_ptr< SimpleCashFlow > upfrontPayment_
Protection::Side side() const
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
template base class for option pricing engines
Shared handle to an observable.
Definition: handle.hpp:41
Abstract instrument class.
Definition: instrument.hpp:44
Payment schedule.
Definition: schedule.hpp:40
Classes for default-event handling.
default-probability term structure
BusinessDayConvention
Business Day conventions.
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Rate
interest rates
Definition: types.hpp:70
Abstract instrument class.
Definition: any.hpp:35
Date cdsMaturity(const Date &tradeDate, const Period &tenor, DateGeneration::Rule rule)
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
Maps optional to either the boost or std implementation.
date schedule
Predetermined cash flow.