@@ -48,15 +48,15 @@ namespace QuantLib {
4848 bool rebatesAccrual,
4949 const Date& tradeDate,
5050 Natural cashSettlementDays)
51- : side_(side), notional_(notional), upfront_(boost::none),
52- runningSpread_ (spread ), settlesAccrual_(settlesAccrual),
51+ : side_(side), notional_(notional), upfront_(boost::none), runningSpread_(spread),
52+ schedule_ (schedule), paymentConvention_(convention ), settlesAccrual_(settlesAccrual),
5353 paysAtDefaultTime_(paysAtDefaultTime),
54- protectionPaymentTime_(settlesAccrual ? atDefault : atPeriodEnd),
55- claim_(claim),
54+ protectionPaymentTime_(settlesAccrual ? atDefault : atPeriodEnd), claim_(claim),
5655 protectionStart_(protectionStart == Null<Date>() ? schedule[0] : protectionStart),
57- tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays) {
56+ tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays),
57+ rebatesAccrual_(rebatesAccrual) {
5858
59- init (schedule, convention, dayCounter, lastPeriodDayCounter, rebatesAccrual );
59+ init (dayCounter, lastPeriodDayCounter);
6060 }
6161
6262 CreditDefaultSwap::CreditDefaultSwap (Protection::Side side,
@@ -75,15 +75,15 @@ namespace QuantLib {
7575 bool rebatesAccrual,
7676 const Date& tradeDate,
7777 Natural cashSettlementDays)
78- : side_(side), notional_(notional), upfront_(upfront),
79- runningSpread_(runningSpread ), settlesAccrual_(settlesAccrual),
78+ : side_(side), notional_(notional), upfront_(upfront), runningSpread_(runningSpread),
79+ schedule_(schedule), paymentConvention_(convention ), settlesAccrual_(settlesAccrual),
8080 paysAtDefaultTime_(paysAtDefaultTime),
81- protectionPaymentTime_(settlesAccrual ? atDefault : atPeriodEnd),
82- claim_(claim),
81+ protectionPaymentTime_(settlesAccrual ? atDefault : atPeriodEnd), claim_(claim),
8382 protectionStart_(protectionStart == Null<Date>() ? schedule[0] : protectionStart),
84- tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays) {
83+ tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays),
84+ rebatesAccrual_(rebatesAccrual) {
8585
86- init (schedule, convention, dayCounter, lastPeriodDayCounter, rebatesAccrual , upfrontDate);
86+ init (dayCounter, lastPeriodDayCounter, upfrontDate);
8787 }
8888
8989 CreditDefaultSwap::CreditDefaultSwap (Protection::Side side,
@@ -100,13 +100,15 @@ namespace QuantLib {
100100 bool rebatesAccrual,
101101 const Date& tradeDate,
102102 Natural cashSettlementDays)
103- : side_(side), notional_(notional), upfront_(boost::none), runningSpread_(spread), settlesAccrual_(settlesAccrual),
103+ : side_(side), notional_(notional), upfront_(boost::none), runningSpread_(spread),
104+ schedule_(schedule), paymentConvention_(convention), settlesAccrual_(settlesAccrual),
104105 paysAtDefaultTime_(protectionPaymentTime == atDefault ? true : false ),
105106 protectionPaymentTime_(protectionPaymentTime), claim_(claim),
106- protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart), tradeDate_(tradeDate),
107- cashSettlementDays_(cashSettlementDays) {
107+ protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart),
108+ tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays),
109+ rebatesAccrual_(rebatesAccrual) {
108110
109- init (schedule, convention, dayCounter, lastPeriodDayCounter, rebatesAccrual );
111+ init (dayCounter, lastPeriodDayCounter);
110112 }
111113
112114 CreditDefaultSwap::CreditDefaultSwap (Protection::Side side,
@@ -126,13 +128,14 @@ namespace QuantLib {
126128 const Date& tradeDate,
127129 Natural cashSettlementDays)
128130 : side_(side), notional_(notional), upfront_(upfront), runningSpread_(runningSpread),
129- settlesAccrual_(settlesAccrual),
131+ schedule_(schedule), paymentConvention_(convention), settlesAccrual_(settlesAccrual),
130132 paysAtDefaultTime_(protectionPaymentTime == atDefault ? true : false ),
131133 protectionPaymentTime_(protectionPaymentTime), claim_(claim),
132- protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart), tradeDate_(tradeDate),
133- cashSettlementDays_(cashSettlementDays) {
134+ protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart),
135+ tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays),
136+ rebatesAccrual_(rebatesAccrual) {
134137
135- init (schedule, convention, dayCounter, lastPeriodDayCounter, rebatesAccrual , upfrontDate);
138+ init (dayCounter, lastPeriodDayCounter, upfrontDate);
136139 }
137140
138141 CreditDefaultSwap::CreditDefaultSwap (Protection::Side side,
@@ -150,13 +153,15 @@ namespace QuantLib {
150153 bool rebatesAccrual,
151154 const Date& tradeDate,
152155 Natural cashSettlementDays)
153- : side_(side), notional_(notional), upfront_(boost::none), runningSpread_(spread), settlesAccrual_(settlesAccrual),
156+ : side_(side), notional_(notional), upfront_(boost::none), runningSpread_(spread),
157+ schedule_(schedule), paymentConvention_(convention), settlesAccrual_(settlesAccrual),
154158 paysAtDefaultTime_(protectionPaymentTime == atDefault ? true : false ),
155159 protectionPaymentTime_(protectionPaymentTime), claim_(claim), leg_(amortized_leg),
156- protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart), tradeDate_(tradeDate),
157- cashSettlementDays_(cashSettlementDays) {
160+ protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart),
161+ tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays),
162+ rebatesAccrual_(rebatesAccrual) {
158163
159- init (schedule, convention, dayCounter, lastPeriodDayCounter, rebatesAccrual );
164+ init (dayCounter, lastPeriodDayCounter);
160165 }
161166
162167 CreditDefaultSwap::CreditDefaultSwap (Protection::Side side,
@@ -177,82 +182,89 @@ namespace QuantLib {
177182 const Date& tradeDate,
178183 Natural cashSettlementDays)
179184 : side_(side), notional_(notional), upfront_(upfront), runningSpread_(runningSpread),
180- settlesAccrual_(settlesAccrual),
185+ schedule_(schedule), paymentConvention_(convention), settlesAccrual_(settlesAccrual),
181186 paysAtDefaultTime_(protectionPaymentTime == atDefault ? true : false ),
182- protectionPaymentTime_(protectionPaymentTime), claim_(claim),
183- leg_(amortized_leg), protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart),
184- tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays) {
187+ protectionPaymentTime_(protectionPaymentTime), claim_(claim), leg_(amortized_leg),
188+ protectionStart_(protectionStart == Date() ? schedule[0] : protectionStart),
189+ tradeDate_(tradeDate), cashSettlementDays_(cashSettlementDays),
190+ rebatesAccrual_(rebatesAccrual) {
185191
186- init (schedule, convention, dayCounter, lastPeriodDayCounter, rebatesAccrual , upfrontDate);
192+ init (dayCounter, lastPeriodDayCounter, upfrontDate);
187193 }
188194
189195
190- void CreditDefaultSwap::init (const Schedule& schedule, BusinessDayConvention paymentConvention ,
191- const DayCounter& dayCounter, const DayCounter& lastPeriodDayCounter,
192- bool rebatesAccrual, const Date& upfrontDate) {
196+ void CreditDefaultSwap::init (const DayCounter& dayCounter ,
197+ const DayCounter& lastPeriodDayCounter,
198+ const Date& upfrontDate) {
193199
194- QL_REQUIRE (!schedule .empty (), " CreditDefaultSwap needs a non-empty schedule." );
200+ QL_REQUIRE (!schedule_ .empty (), " CreditDefaultSwap needs a non-empty schedule." );
195201
196- bool postBigBang = false ;
197- if (schedule .hasRule ()) {
198- DateGeneration::Rule rule = schedule .rule ();
199- postBigBang = rule == DateGeneration::CDS || rule == DateGeneration::CDS2015;
202+ postBigBang_ = false ;
203+ if (schedule_ .hasRule ()) {
204+ DateGeneration::Rule rule = schedule_ .rule ();
205+ postBigBang_ = rule == DateGeneration::CDS || rule == DateGeneration::CDS2015;
200206 }
201207
202- if (!postBigBang ) {
203- QL_REQUIRE (protectionStart_ <= schedule [0 ], " CreditDefaultSwap: protection can not start after accrual" );
208+ if (!postBigBang_ ) {
209+ QL_REQUIRE (protectionStart_ <= schedule_ [0 ], " CreditDefaultSwap: protection can not start after accrual" );
204210 }
205211
206212 // If the leg_ has not already been populated via amortised leg ctor, populate it.
207213 if (leg_.empty ()) {
208- leg_ = FixedRateLeg (schedule )
214+ leg_ = FixedRateLeg (schedule_ )
209215 .withNotionals (notional_)
210216 .withCouponRates (runningSpread_, dayCounter)
211- .withPaymentAdjustment (paymentConvention )
217+ .withPaymentAdjustment (paymentConvention_ )
212218 .withLastPeriodDayCounter (lastPeriodDayCounter);
213219 }
214220
215221 // Deduce the trade date if not given.
216222 if (tradeDate_ == Date ()) {
217- if (postBigBang ) {
223+ if (postBigBang_ ) {
218224 tradeDate_ = protectionStart_;
219225 } else {
220226 tradeDate_ = protectionStart_ - 1 ;
221227 }
222228 }
223229
224230 // Deduce the cash settlement date if not given.
225- Date effectiveUpfrontDate = upfrontDate;
226- if (effectiveUpfrontDate == Date ()) {
227- effectiveUpfrontDate = schedule.calendar ().advance (tradeDate_, cashSettlementDays_, Days, paymentConvention);
231+ effectiveUpfrontDate_ = upfrontDate;
232+ if (effectiveUpfrontDate_ == Date ()) {
233+ effectiveUpfrontDate_ = schedule_.calendar ().advance (tradeDate_, cashSettlementDays_,
234+ Days, paymentConvention_);
228235 }
229- QL_REQUIRE (effectiveUpfrontDate >= protectionStart_,
236+ QL_REQUIRE (effectiveUpfrontDate_ >= protectionStart_,
230237 " The cash settlement date must not be before the protection start date." );
231238
232239 // Create the upfront payment, if one is provided.
233240 Real upfrontAmount = 0.0 ;
234241 if (upfront_)
235242 upfrontAmount = *upfront_ * notional_;
236- upfrontPayment_ = boost::make_shared<SimpleCashFlow>(upfrontAmount, effectiveUpfrontDate );
237-
243+ upfrontPayment_ = boost::make_shared<SimpleCashFlow>(upfrontAmount, effectiveUpfrontDate_ );
244+
238245 // Set the maturity date.
239- maturity_ = schedule .dates ().back ();
246+ maturity_ = schedule_ .dates ().back ();
240247
248+ if (!claim_)
249+ claim_ = boost::make_shared<FaceValueClaim>();
250+ registerWith (claim_);
251+ }
252+
253+ void CreditDefaultSwap::performCalculations () const {
241254 // Deal with the accrual rebate. We use the standard conventions for accrual calculation introduced with the
242255 // CDS Big Bang in 2009
243- if (rebatesAccrual && postBigBang ) {
256+ if (rebatesAccrual_ && postBigBang_ ) {
244257 accrualRebate_ = boost::make_shared<SimpleCashFlow>(
245- CashFlows::accruedAmount (leg_, leg_.back ()->date () == tradeDate_ + 1 , tradeDate_ + 1 ),
246- effectiveUpfrontDate);
258+ CashFlows::accruedAmount (leg_, leg_.back ()->date () == tradeDate_ + 1 ,
259+ tradeDate_ + 1 ),
260+ effectiveUpfrontDate_);
247261 Date current = std::max ((Date)Settings::instance ().evaluationDate (), tradeDate_);
248262 accrualRebateCurrent_ = boost::make_shared<SimpleCashFlow>(
249- CashFlows::accruedAmount (leg_, false , current + 1 ),
250- schedule.calendar ().advance (current, cashSettlementDays_, Days, paymentConvention));
263+ CashFlows::accruedAmount (leg_, false , current + 1 ),
264+ schedule_.calendar ().advance (current, cashSettlementDays_, Days,
265+ paymentConvention_));
251266 }
252-
253- if (!claim_)
254- claim_ = boost::make_shared<FaceValueClaim>();
255- registerWith (claim_);
267+ Instrument::performCalculations ();
256268 }
257269
258270 Protection::Side CreditDefaultSwap::side () const {
@@ -471,6 +483,7 @@ namespace QuantLib {
471483 Real accuracy,
472484 PricingModel model) const {
473485
486+ calculate ();
474487 ext::shared_ptr<SimpleQuote> flatRate = ext::make_shared<SimpleQuote>(0.0 );
475488
476489 Handle<DefaultProbabilityTermStructure> probability =
@@ -498,6 +511,7 @@ namespace QuantLib {
498511 const DayCounter& dayCounter,
499512 PricingModel model) const {
500513
514+ calculate ();
501515 ext::shared_ptr<SimpleQuote> flatRate = ext::make_shared<SimpleQuote>(0.0 );
502516
503517 Handle<DefaultProbabilityTermStructure> probability =
@@ -552,10 +566,12 @@ namespace QuantLib {
552566 }
553567
554568 const ext::shared_ptr<SimpleCashFlow>& CreditDefaultSwap::accrualRebate () const {
569+ calculate ();
555570 return accrualRebate_;
556571 }
557572
558573 const ext::shared_ptr<SimpleCashFlow>& CreditDefaultSwap::accrualRebateCurrent () const {
574+ calculate ();
559575 return accrualRebateCurrent_;
560576 }
561577
0 commit comments