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/experimental/coupons/cmsspreadcoupon.hpp>
20 #include <ql/cashflows/cashflowvectors.hpp>
21 #include <ql/cashflows/capflooredcoupon.hpp>
22 
23 namespace QuantLib {
24 
CmsSpreadCoupon(const Date & paymentDate,Real nominal,const Date & startDate,const Date & endDate,Natural fixingDays,const ext::shared_ptr<SwapSpreadIndex> & index,Real gearing,Spread spread,const Date & refPeriodStart,const Date & refPeriodEnd,const DayCounter & dayCounter,bool isInArrears,const Date & exCouponDate)25     CmsSpreadCoupon::CmsSpreadCoupon(
26         const Date &paymentDate, Real nominal, const Date &startDate,
27         const Date &endDate, Natural fixingDays,
28         const ext::shared_ptr<SwapSpreadIndex> &index, Real gearing,
29         Spread spread, const Date &refPeriodStart,
30         const Date &refPeriodEnd,
31         const DayCounter &dayCounter, bool isInArrears, const Date &exCouponDate)
32         : FloatingRateCoupon(paymentDate, nominal, startDate, endDate,
33                              fixingDays, index, gearing, spread,
34                              refPeriodStart, refPeriodEnd, dayCounter,
35                              isInArrears, exCouponDate),
36           index_(index) {}
37 
accept(AcyclicVisitor & v)38     void CmsSpreadCoupon::accept(AcyclicVisitor &v) {
39         Visitor<CmsSpreadCoupon> *v1 = dynamic_cast<Visitor<CmsSpreadCoupon> *>(&v);
40         if (v1 != 0)
41             v1->visit(*this);
42         else
43             FloatingRateCoupon::accept(v);
44     }
45 
CmsSpreadLeg(const Schedule & schedule,const ext::shared_ptr<SwapSpreadIndex> & index)46     CmsSpreadLeg::CmsSpreadLeg(const Schedule &schedule,
47                                const ext::shared_ptr<SwapSpreadIndex> &index)
48         : schedule_(schedule), swapSpreadIndex_(index),
49           paymentAdjustment_(Following), inArrears_(false),
50           zeroPayments_(false) {}
51 
withNotionals(Real notional)52     CmsSpreadLeg &CmsSpreadLeg::withNotionals(Real notional) {
53         notionals_ = std::vector<Real>(1, notional);
54         return *this;
55     }
56 
57     CmsSpreadLeg &
withNotionals(const std::vector<Real> & notionals)58     CmsSpreadLeg::withNotionals(const std::vector<Real> &notionals) {
59         notionals_ = notionals;
60         return *this;
61     }
62 
63     CmsSpreadLeg &
withPaymentDayCounter(const DayCounter & dayCounter)64     CmsSpreadLeg::withPaymentDayCounter(const DayCounter &dayCounter) {
65         paymentDayCounter_ = dayCounter;
66         return *this;
67     }
68 
69     CmsSpreadLeg &
withPaymentAdjustment(BusinessDayConvention convention)70     CmsSpreadLeg::withPaymentAdjustment(BusinessDayConvention convention) {
71         paymentAdjustment_ = convention;
72         return *this;
73     }
74 
withFixingDays(Natural fixingDays)75     CmsSpreadLeg &CmsSpreadLeg::withFixingDays(Natural fixingDays) {
76         fixingDays_ = std::vector<Natural>(1, fixingDays);
77         return *this;
78     }
79 
80     CmsSpreadLeg &
withFixingDays(const std::vector<Natural> & fixingDays)81     CmsSpreadLeg::withFixingDays(const std::vector<Natural> &fixingDays) {
82         fixingDays_ = fixingDays;
83         return *this;
84     }
85 
withGearings(Real gearing)86     CmsSpreadLeg &CmsSpreadLeg::withGearings(Real gearing) {
87         gearings_ = std::vector<Real>(1, gearing);
88         return *this;
89     }
90 
91     CmsSpreadLeg &
withGearings(const std::vector<Real> & gearings)92     CmsSpreadLeg::withGearings(const std::vector<Real> &gearings) {
93         gearings_ = gearings;
94         return *this;
95     }
96 
withSpreads(Spread spread)97     CmsSpreadLeg &CmsSpreadLeg::withSpreads(Spread spread) {
98         spreads_ = std::vector<Spread>(1, spread);
99         return *this;
100     }
101 
102     CmsSpreadLeg &
withSpreads(const std::vector<Spread> & spreads)103     CmsSpreadLeg::withSpreads(const std::vector<Spread> &spreads) {
104         spreads_ = spreads;
105         return *this;
106     }
107 
withCaps(Rate cap)108     CmsSpreadLeg &CmsSpreadLeg::withCaps(Rate cap) {
109         caps_ = std::vector<Rate>(1, cap);
110         return *this;
111     }
112 
withCaps(const std::vector<Rate> & caps)113     CmsSpreadLeg &CmsSpreadLeg::withCaps(const std::vector<Rate> &caps) {
114         caps_ = caps;
115         return *this;
116     }
117 
withFloors(Rate floor)118     CmsSpreadLeg &CmsSpreadLeg::withFloors(Rate floor) {
119         floors_ = std::vector<Rate>(1, floor);
120         return *this;
121     }
122 
withFloors(const std::vector<Rate> & floors)123     CmsSpreadLeg &CmsSpreadLeg::withFloors(const std::vector<Rate> &floors) {
124         floors_ = floors;
125         return *this;
126     }
127 
inArrears(bool flag)128     CmsSpreadLeg &CmsSpreadLeg::inArrears(bool flag) {
129         inArrears_ = flag;
130         return *this;
131     }
132 
withZeroPayments(bool flag)133     CmsSpreadLeg &CmsSpreadLeg::withZeroPayments(bool flag) {
134         zeroPayments_ = flag;
135         return *this;
136     }
137 
operator Leg() const138     CmsSpreadLeg::operator Leg() const {
139         return FloatingLeg<SwapSpreadIndex, CmsSpreadCoupon,
140                            CappedFlooredCmsSpreadCoupon>(
141             schedule_, notionals_, swapSpreadIndex_, paymentDayCounter_,
142             paymentAdjustment_, fixingDays_, gearings_, spreads_, caps_,
143             floors_, inArrears_, zeroPayments_);
144     }
145 }
146