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