Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
makecds.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*
20 Copyright (C) 2014 Jose Aparicio
21 Copyright (C) 2014 Peter Caspers
22
23 This file is part of QuantLib, a free-software/open-source library
24 for financial quantitative analysts and developers - http://quantlib.org/
25
26 QuantLib is free software: you can redistribute it and/or modify it
27 under the terms of the QuantLib license. You should have received a
28 copy of the license along with this program; if not, please email
29 <quantlib-dev@lists.sf.net>. The license is also available online at
30 <http://quantlib.org/license.shtml>.
31
32 This program is distributed in the hope that it will be useful, but WITHOUT
33 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34 FOR A PARTICULAR PURPOSE. See the license for more details.
35 */
36
37#include <ql/time/calendars/weekendsonly.hpp>
38#include <ql/time/daycounters/actual360.hpp>
40
41#include <boost/make_shared.hpp>
42
43namespace QuantExt {
44
45MakeCreditDefaultSwap::MakeCreditDefaultSwap(const Period& tenor, const Real couponRate)
46 : side_(Protection::Buyer), nominal_(1.0), tenor_(tenor), termDate_(boost::none), couponTenor_(3 * Months),
47 couponRate_(couponRate), upfrontRate_(0.0),
48 dayCounter_(Actual360()), lastPeriodDayCounter_(Actual360(true)),
49 rule_(DateGeneration::CDS2015), cashSettlementDays_(3), settlesAccrual_(true),
50 paysAtDefaultTime_(true), rebatesAccrual_(true) {}
51
52MakeCreditDefaultSwap::MakeCreditDefaultSwap(const Date& termDate, const Real couponRate)
53 : side_(Protection::Buyer), nominal_(1.0), tenor_(boost::none), termDate_(termDate), couponTenor_(3 * Months),
54 couponRate_(couponRate), upfrontRate_(0.0),
55 dayCounter_(Actual360()), lastPeriodDayCounter_(Actual360(true)),
56 rule_(DateGeneration::CDS2015), cashSettlementDays_(3), settlesAccrual_(true),
57 paysAtDefaultTime_(true), rebatesAccrual_(true) {}
58
59MakeCreditDefaultSwap::operator CreditDefaultSwap() const {
60 QuantLib::ext::shared_ptr<CreditDefaultSwap> swap = *this;
61 return *swap;
62}
63
64 MakeCreditDefaultSwap::operator QuantLib::ext::shared_ptr<QuantExt::CreditDefaultSwap>() const {
65
66 Date tradeDate = Settings::instance().evaluationDate();
67 Date upfrontDate = WeekendsOnly().advance(tradeDate, cashSettlementDays_, Days);
68
69 Date protectionStart;
70 if (rule_ == DateGeneration::CDS2015 || rule_ == DateGeneration::CDS) {
71 protectionStart = tradeDate;
72 } else {
73 protectionStart = tradeDate + 1;
74 }
75
76 Date end;
77 if (tenor_) {
78 if (rule_ == DateGeneration::CDS2015 || rule_ == DateGeneration::CDS || rule_ == DateGeneration::OldCDS) {
79 end = cdsMaturity(tradeDate, *tenor_, rule_);
80 } else {
81 end = tradeDate + *tenor_;
82 }
83 } else {
84 end = *termDate_;
85 }
86
87 Schedule schedule(protectionStart, end, couponTenor_, WeekendsOnly(), Following, Unadjusted, rule_, false);
88
89 CreditDefaultSwap::ProtectionPaymentTime timing = paysAtDefaultTime_ ?
90 CreditDefaultSwap::ProtectionPaymentTime::atDefault :
91 CreditDefaultSwap::ProtectionPaymentTime::atPeriodEnd;
92 QuantLib::ext::shared_ptr<CreditDefaultSwap> cds = QuantLib::ext::make_shared<CreditDefaultSwap>(
93 side_, nominal_, upfrontRate_, couponRate_, schedule, Following, dayCounter_, settlesAccrual_,
94 timing, protectionStart, upfrontDate, QuantLib::ext::shared_ptr<Claim>(),
95 lastPeriodDayCounter_, rebatesAccrual_, tradeDate, cashSettlementDays_);
96
97 cds->setPricingEngine(engine_);
98 return cds;
99}
100
102 upfrontRate_ = upfrontRate;
103 return *this;
104}
105
107 side_ = side;
108 return *this;
109}
110
112 nominal_ = nominal;
113 return *this;
114}
115
117 couponTenor_ = couponTenor;
118 return *this;
119}
120
122 dayCounter_ = dayCounter;
123 return *this;
124}
125
127 lastPeriodDayCounter_ = lastPeriodDayCounter;
128 return *this;
129}
130
132 rule_ = rule;
133 return *this;
134}
135
137 cashSettlementDays_ = cashSettlementDays;
138 return *this;
139}
140
141MakeCreditDefaultSwap& MakeCreditDefaultSwap::withPricingEngine(const QuantLib::ext::shared_ptr<PricingEngine>& engine) {
142 engine_ = engine;
143 return *this;
144}
145
147 settlesAccrual_ = settlesAccrual;
148 return *this;
149}
150
152 paysAtDefaultTime_ = paysAtDefaultTime;
153 return *this;
154}
155
157 rebatesAccrual_ = rebatesAccrual;
158 return *this;
159}
160} // namespace QuantExt
QuantLib::ext::shared_ptr< PricingEngine > engine_
Definition: cdsoption.cpp:78
MakeCreditDefaultSwap & withPaysAtDefaultTime(bool)
Definition: makecds.cpp:151
MakeCreditDefaultSwap & withCouponTenor(Period)
Definition: makecds.cpp:116
MakeCreditDefaultSwap & withLastPeriodDayCounter(const DayCounter &)
Definition: makecds.cpp:126
MakeCreditDefaultSwap & withNominal(Real)
Definition: makecds.cpp:111
MakeCreditDefaultSwap & withRebatesAccrual(bool)
Definition: makecds.cpp:156
MakeCreditDefaultSwap & withPricingEngine(const QuantLib::ext::shared_ptr< PricingEngine > &)
Definition: makecds.cpp:141
MakeCreditDefaultSwap & withDayCounter(const DayCounter &)
Definition: makecds.cpp:121
QuantLib::ext::shared_ptr< PricingEngine > engine_
Definition: makecds.hpp:93
MakeCreditDefaultSwap & withDateGenerationRule(DateGeneration::Rule rule)
Definition: makecds.cpp:131
DateGeneration::Rule rule_
Definition: makecds.hpp:88
MakeCreditDefaultSwap & withCashSettlementDays(Natural cashSettlementDays)
Definition: makecds.cpp:136
MakeCreditDefaultSwap(const Period &tenor, const Real couponRate)
Definition: makecds.cpp:45
MakeCreditDefaultSwap & withSide(Protection::Side)
Definition: makecds.cpp:106
MakeCreditDefaultSwap & withSettlesAccrual(bool)
Definition: makecds.cpp:146
MakeCreditDefaultSwap & withUpfrontRate(Real)
Definition: makecds.cpp:101
Helper class to instantiate standard market cds.