| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2014 Jose Aparicio |
| 5 | Copyright (C) 2014 Peter Caspers |
| 6 | |
| 7 | This file is part of QuantLib, a free-software/open-source library |
| 8 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 9 | |
| 10 | QuantLib is free software: you can redistribute it and/or modify it |
| 11 | under the terms of the QuantLib license. You should have received a |
| 12 | copy of the license along with this program; if not, please email |
| 13 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 14 | <http://quantlib.org/license.shtml>. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 19 | */ |
| 20 | |
| 21 | #include <ql/instruments/makecds.hpp> |
| 22 | #include <ql/time/daycounters/actual360.hpp> |
| 23 | #include <ql/time/calendars/weekendsonly.hpp> |
| 24 | |
| 25 | |
| 26 | namespace QuantLib { |
| 27 | |
| 28 | MakeCreditDefaultSwap::MakeCreditDefaultSwap(const Period &tenor, |
| 29 | const Real couponRate) |
| 30 | : side_(Protection::Buyer), nominal_(1.0), tenor_(tenor), |
| 31 | couponTenor_(3 * Months), couponRate_(couponRate), upfrontRate_(0.0), |
| 32 | dayCounter_(Actual360()), lastPeriodDayCounter_(Actual360(true)), |
| 33 | rule_(DateGeneration::CDS), cashSettlementDays_(3) {} |
| 34 | |
| 35 | MakeCreditDefaultSwap::MakeCreditDefaultSwap(const Date &termDate, |
| 36 | const Real couponRate) |
| 37 | : side_(Protection::Buyer), nominal_(1.0), termDate_(termDate), |
| 38 | couponTenor_(3 * Months), couponRate_(couponRate), upfrontRate_(0.0), |
| 39 | dayCounter_(Actual360()), lastPeriodDayCounter_(Actual360(true)), |
| 40 | rule_(DateGeneration::CDS), cashSettlementDays_(3) {} |
| 41 | |
| 42 | MakeCreditDefaultSwap::operator CreditDefaultSwap() const { |
| 43 | ext::shared_ptr<CreditDefaultSwap> swap = *this; |
| 44 | return *swap; |
| 45 | } |
| 46 | |
| 47 | MakeCreditDefaultSwap::operator ext::shared_ptr<CreditDefaultSwap>() const { |
| 48 | |
| 49 | Date tradeDate = (tradeDate_ != Null<Date>()) ? tradeDate_ : Settings::instance().evaluationDate(); |
| 50 | Date upfrontDate = WeekendsOnly().advance(tradeDate, n: cashSettlementDays_, unit: Days); |
| 51 | |
| 52 | Date protectionStart; |
| 53 | if (rule_ == DateGeneration::CDS2015 || rule_ == DateGeneration::CDS) { |
| 54 | protectionStart = tradeDate; |
| 55 | } else { |
| 56 | protectionStart = tradeDate + 1; |
| 57 | } |
| 58 | |
| 59 | Date end; |
| 60 | if (tenor_) { // NOLINT(readability-implicit-bool-conversion) |
| 61 | if (rule_ == DateGeneration::CDS2015 || rule_ == DateGeneration::CDS || rule_ == DateGeneration::OldCDS) { |
| 62 | end = cdsMaturity(tradeDate, tenor: *tenor_, rule: rule_); |
| 63 | } else { |
| 64 | end = tradeDate + *tenor_; |
| 65 | } |
| 66 | } else { |
| 67 | end = *termDate_; |
| 68 | } |
| 69 | |
| 70 | Schedule schedule(protectionStart, end, couponTenor_, WeekendsOnly(), Following, |
| 71 | Unadjusted, rule_, false); |
| 72 | |
| 73 | ext::shared_ptr<CreditDefaultSwap> cds = |
| 74 | ext::make_shared<CreditDefaultSwap>( |
| 75 | args: side_, args: nominal_, args: upfrontRate_, args: couponRate_, args&: schedule, args: Following, |
| 76 | args: dayCounter_, args: true, args: true, args&: protectionStart, args&: upfrontDate, |
| 77 | args: ext::shared_ptr<Claim>(), args: lastPeriodDayCounter_, args: true, args&: tradeDate, args: cashSettlementDays_); |
| 78 | |
| 79 | cds->setPricingEngine(engine_); |
| 80 | return cds; |
| 81 | |
| 82 | } |
| 83 | |
| 84 | MakeCreditDefaultSwap & |
| 85 | MakeCreditDefaultSwap::withUpfrontRate(Real upfrontRate) { |
| 86 | upfrontRate_ = upfrontRate; |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | MakeCreditDefaultSwap & |
| 91 | MakeCreditDefaultSwap::withSide(Protection::Side side) { |
| 92 | side_ = side; |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | MakeCreditDefaultSwap &MakeCreditDefaultSwap::withNominal(Real nominal) { |
| 97 | nominal_ = nominal; |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | MakeCreditDefaultSwap & |
| 102 | MakeCreditDefaultSwap::withCouponTenor(Period couponTenor) { |
| 103 | couponTenor_ = couponTenor; |
| 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | MakeCreditDefaultSwap & |
| 108 | MakeCreditDefaultSwap::withDayCounter(DayCounter &dayCounter) { |
| 109 | dayCounter_ = dayCounter; |
| 110 | return *this; |
| 111 | } |
| 112 | |
| 113 | MakeCreditDefaultSwap &MakeCreditDefaultSwap::withLastPeriodDayCounter( |
| 114 | DayCounter &lastPeriodDayCounter) { |
| 115 | lastPeriodDayCounter_ = lastPeriodDayCounter; |
| 116 | return *this; |
| 117 | } |
| 118 | |
| 119 | MakeCreditDefaultSwap& MakeCreditDefaultSwap::withDateGenerationRule(DateGeneration::Rule rule) { |
| 120 | rule_ = rule; |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | MakeCreditDefaultSwap& MakeCreditDefaultSwap::withCashSettlementDays(Natural cashSettlementDays) { |
| 125 | cashSettlementDays_ = cashSettlementDays; |
| 126 | return *this; |
| 127 | } |
| 128 | |
| 129 | MakeCreditDefaultSwap &MakeCreditDefaultSwap::withPricingEngine( |
| 130 | const ext::shared_ptr<PricingEngine> &engine) { |
| 131 | engine_ = engine; |
| 132 | return *this; |
| 133 | } |
| 134 | |
| 135 | MakeCreditDefaultSwap& MakeCreditDefaultSwap::withTradeDate(const Date& tradeDate) { |
| 136 | tradeDate_ = tradeDate; |
| 137 | return *this; |
| 138 | } |
| 139 | |
| 140 | } |
| 141 | |