| 1 | /* |
| 2 | Copyright (C) 2014 Peter Caspers |
| 3 | |
| 4 | This file is part of QuantLib, a free-software/open-source library |
| 5 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 6 | |
| 7 | QuantLib is free software: you can redistribute it and/or modify it |
| 8 | under the terms of the QuantLib license. You should have received a |
| 9 | copy of the license along with this program; if not, please email |
| 10 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 11 | <http://quantlib.org/license.shtml>. |
| 12 | |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, but |
| 15 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 | or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <ql/cashflows/capflooredcoupon.hpp> |
| 20 | #include <ql/cashflows/cashflowvectors.hpp> |
| 21 | #include <ql/experimental/coupons/cmsspreadcoupon.hpp> |
| 22 | #include <utility> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | CmsSpreadCoupon::CmsSpreadCoupon( |
| 27 | const Date &paymentDate, Real nominal, const Date &startDate, |
| 28 | const Date &endDate, Natural fixingDays, |
| 29 | const ext::shared_ptr<SwapSpreadIndex> &index, Real gearing, |
| 30 | Spread spread, const Date &refPeriodStart, |
| 31 | const Date &refPeriodEnd, |
| 32 | const DayCounter &dayCounter, bool isInArrears, const Date &exCouponDate) |
| 33 | : FloatingRateCoupon(paymentDate, nominal, startDate, endDate, |
| 34 | fixingDays, index, gearing, spread, |
| 35 | refPeriodStart, refPeriodEnd, dayCounter, |
| 36 | isInArrears, exCouponDate), |
| 37 | index_(index) {} |
| 38 | |
| 39 | void CmsSpreadCoupon::accept(AcyclicVisitor &v) { |
| 40 | auto* v1 = dynamic_cast<Visitor<CmsSpreadCoupon>*>(&v); |
| 41 | if (v1 != nullptr) |
| 42 | v1->visit(*this); |
| 43 | else |
| 44 | FloatingRateCoupon::accept(v); |
| 45 | } |
| 46 | |
| 47 | CmsSpreadLeg::CmsSpreadLeg(Schedule schedule, ext::shared_ptr<SwapSpreadIndex> index) |
| 48 | : schedule_(std::move(schedule)), swapSpreadIndex_(std::move(index)) { |
| 49 | QL_REQUIRE(swapSpreadIndex_, "no index provided" ); |
| 50 | } |
| 51 | |
| 52 | CmsSpreadLeg &CmsSpreadLeg::withNotionals(Real notional) { |
| 53 | notionals_ = std::vector<Real>(1, notional); |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | CmsSpreadLeg & |
| 58 | CmsSpreadLeg::withNotionals(const std::vector<Real> ¬ionals) { |
| 59 | notionals_ = notionals; |
| 60 | return *this; |
| 61 | } |
| 62 | |
| 63 | CmsSpreadLeg & |
| 64 | CmsSpreadLeg::withPaymentDayCounter(const DayCounter &dayCounter) { |
| 65 | paymentDayCounter_ = dayCounter; |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | CmsSpreadLeg & |
| 70 | CmsSpreadLeg::withPaymentAdjustment(BusinessDayConvention convention) { |
| 71 | paymentAdjustment_ = convention; |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | CmsSpreadLeg &CmsSpreadLeg::withFixingDays(Natural fixingDays) { |
| 76 | fixingDays_ = std::vector<Natural>(1, fixingDays); |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | CmsSpreadLeg & |
| 81 | CmsSpreadLeg::withFixingDays(const std::vector<Natural> &fixingDays) { |
| 82 | fixingDays_ = fixingDays; |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | CmsSpreadLeg &CmsSpreadLeg::withGearings(Real gearing) { |
| 87 | gearings_ = std::vector<Real>(1, gearing); |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | CmsSpreadLeg & |
| 92 | CmsSpreadLeg::withGearings(const std::vector<Real> &gearings) { |
| 93 | gearings_ = gearings; |
| 94 | return *this; |
| 95 | } |
| 96 | |
| 97 | CmsSpreadLeg &CmsSpreadLeg::withSpreads(Spread spread) { |
| 98 | spreads_ = std::vector<Spread>(1, spread); |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | CmsSpreadLeg & |
| 103 | CmsSpreadLeg::withSpreads(const std::vector<Spread> &spreads) { |
| 104 | spreads_ = spreads; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | CmsSpreadLeg &CmsSpreadLeg::withCaps(Rate cap) { |
| 109 | caps_ = std::vector<Rate>(1, cap); |
| 110 | return *this; |
| 111 | } |
| 112 | |
| 113 | CmsSpreadLeg &CmsSpreadLeg::withCaps(const std::vector<Rate> &caps) { |
| 114 | caps_ = caps; |
| 115 | return *this; |
| 116 | } |
| 117 | |
| 118 | CmsSpreadLeg &CmsSpreadLeg::withFloors(Rate floor) { |
| 119 | floors_ = std::vector<Rate>(1, floor); |
| 120 | return *this; |
| 121 | } |
| 122 | |
| 123 | CmsSpreadLeg &CmsSpreadLeg::withFloors(const std::vector<Rate> &floors) { |
| 124 | floors_ = floors; |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | CmsSpreadLeg &CmsSpreadLeg::inArrears(bool flag) { |
| 129 | inArrears_ = flag; |
| 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | CmsSpreadLeg &CmsSpreadLeg::withZeroPayments(bool flag) { |
| 134 | zeroPayments_ = flag; |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | CmsSpreadLeg::operator Leg() const { |
| 139 | return FloatingLeg<SwapSpreadIndex, CmsSpreadCoupon, |
| 140 | CappedFlooredCmsSpreadCoupon>( |
| 141 | schedule: schedule_, nominals: notionals_, index: swapSpreadIndex_, paymentDayCounter: paymentDayCounter_, |
| 142 | paymentAdj: paymentAdjustment_, fixingDays: fixingDays_, gearings: gearings_, spreads: spreads_, caps: caps_, |
| 143 | floors: floors_, isInArrears: inArrears_, isZero: zeroPayments_); |
| 144 | } |
| 145 | } |
| 146 | |