Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
durationadjustedcmscoupon.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <ql/cashflows/capflooredcoupon.hpp>
22#include <ql/cashflows/cashflowvectors.hpp>
23#include <ql/cashflows/cmscoupon.hpp>
24
25namespace QuantExt {
26
27DurationAdjustedCmsCoupon::DurationAdjustedCmsCoupon(const Date& paymentDate, Real nominal, const Date& startDate,
28 const Date& endDate, Natural fixingDays,
29 const QuantLib::ext::shared_ptr<SwapIndex>& swapIndex, Size duration,
30 Real gearing, Spread spread, const Date& refPeriodStart,
31 const Date& refPeriodEnd, const DayCounter& dayCounter,
32 bool isInArrears, const Date& exCouponDate)
33 : FloatingRateCoupon(paymentDate, nominal, startDate, endDate, fixingDays, swapIndex, gearing, spread,
34 refPeriodStart, refPeriodEnd, dayCounter, isInArrears, exCouponDate),
35 swapIndex_(swapIndex), duration_(duration) {}
36
39 if (duration_ == 0) {
40 return 1.0;
41 } else {
42 Real swapRate = swapIndex_->fixing(fixingDate());
43 Real tmp = 0.0;
44 for (Size i = 0; i < duration_; ++i) {
45 tmp += 1.0 / std::pow(1.0 + swapRate, static_cast<Real>(i + 1));
46 }
47 return tmp;
48 }
49}
50
51Rate DurationAdjustedCmsCoupon::indexFixing() const { return FloatingRateCoupon::indexFixing() * durationAdjustment(); }
52
53void DurationAdjustedCmsCoupon::accept(AcyclicVisitor& v) {
54 Visitor<DurationAdjustedCmsCoupon>* v1 = dynamic_cast<Visitor<DurationAdjustedCmsCoupon>*>(&v);
55 if (v1 != 0)
56 v1->visit(*this);
57 else
58 FloatingRateCoupon::accept(v);
59}
60
61DurationAdjustedCmsLeg::DurationAdjustedCmsLeg(const Schedule& schedule, const QuantLib::ext::shared_ptr<SwapIndex>& swapIndex,
62 const Size duration)
63 : schedule_(schedule), swapIndex_(swapIndex), paymentLag_(0), paymentCalendar_(Calendar()),
64 paymentAdjustment_(Following), inArrears_(false), zeroPayments_(false), exCouponPeriod_(Period()),
65 exCouponCalendar_(Calendar()), exCouponAdjustment_(Unadjusted), exCouponEndOfMonth_(false), duration_(duration) {}
66
68 notionals_ = std::vector<Real>(1, notional);
69 return *this;
70}
71
73 notionals_ = notionals;
74 return *this;
75}
76
78 paymentDayCounter_ = dayCounter;
79 return *this;
80}
81
83 paymentAdjustment_ = convention;
84 return *this;
85}
86
88 paymentLag_ = lag;
89 return *this;
90}
91
93 paymentCalendar_ = cal;
94 return *this;
95}
96
98 fixingDays_ = std::vector<Natural>(1, fixingDays);
99 return *this;
100}
101
102DurationAdjustedCmsLeg& DurationAdjustedCmsLeg::withFixingDays(const std::vector<Natural>& fixingDays) {
103 fixingDays_ = fixingDays;
104 return *this;
105}
106
108 gearings_ = std::vector<Real>(1, gearing);
109 return *this;
110}
111
113 gearings_ = gearings;
114 return *this;
115}
116
118 spreads_ = std::vector<Spread>(1, spread);
119 return *this;
120}
121
123 spreads_ = spreads;
124 return *this;
125}
126
128 caps_ = std::vector<Rate>(1, cap);
129 return *this;
130}
131
133 caps_ = caps;
134 return *this;
135}
136
138 floors_ = std::vector<Rate>(1, floor);
139 return *this;
140}
141
143 floors_ = floors;
144 return *this;
145}
146
148 inArrears_ = flag;
149 return *this;
150}
151
153 zeroPayments_ = flag;
154 return *this;
155}
156
158 duration_ = duration;
159 return *this;
160}
161
163 BusinessDayConvention convention, bool endOfMonth) {
164 exCouponPeriod_ = period;
165 exCouponCalendar_ = cal;
166 exCouponAdjustment_ = convention;
167 exCouponEndOfMonth_ = endOfMonth;
168 return *this;
169}
170
171DurationAdjustedCmsLeg::operator Leg() const {
172 Size n = schedule_.size() - 1;
173
174 QL_REQUIRE(!notionals_.empty(), "no notional given");
175 QL_REQUIRE(notionals_.size() <= n, "too many notionals (" << notionals_.size() << "), only " << n << " required");
176 QL_REQUIRE(gearings_.size() <= n, "too many gearings (" << gearings_.size() << "), only " << n << " required");
177 QL_REQUIRE(spreads_.size() <= n, "too many spreads (" << spreads_.size() << "), only " << n << " required");
178 QL_REQUIRE(caps_.size() <= n, "too many caps (" << caps_.size() << "), only " << n << " required");
179 QL_REQUIRE(floors_.size() <= n, "too many floors (" << floors_.size() << "), only " << n << " required");
180 QL_REQUIRE(!zeroPayments_ || !inArrears_, "in-arrears and zero features are not compatible");
181
182 Leg leg;
183 leg.reserve(n);
184
185 Calendar calendar = schedule_.calendar();
186 Calendar paymentCalendar = schedule_.calendar();
187
188 if (!paymentCalendar_.empty()) {
189 paymentCalendar = paymentCalendar_;
190 }
191
192 Date refStart, start, refEnd, end;
193 Date exCouponDate;
194 Date lastPaymentDate = paymentCalendar.advance(schedule_.date(n), paymentLag_, Days, paymentAdjustment_);
195
196 for (Size i = 0; i < n; ++i) {
197 refStart = start = schedule_.date(i);
198 refEnd = end = schedule_.date(i + 1);
199 Date paymentDate =
200 zeroPayments_ ? lastPaymentDate : paymentCalendar.advance(end, paymentLag_, Days, paymentAdjustment_);
201 if (i == 0 && (schedule_.hasIsRegular() && schedule_.hasTenor() && !schedule_.isRegular(i + 1))) {
202 BusinessDayConvention bdc = schedule_.businessDayConvention();
203 refStart = calendar.adjust(end - schedule_.tenor(), bdc);
204 }
205 if (i == n - 1 && (schedule_.hasIsRegular() && schedule_.hasTenor() && !schedule_.isRegular(i + 1))) {
206 BusinessDayConvention bdc = schedule_.businessDayConvention();
207 refEnd = calendar.adjust(start + schedule_.tenor(), bdc);
208 }
209 if (exCouponPeriod_ != Period()) {
210 Calendar exCouponCalendar = calendar;
211 if (!exCouponCalendar_.empty()) {
212 exCouponCalendar = exCouponCalendar_;
213 }
214 exCouponDate =
215 exCouponCalendar_.advance(paymentDate, -exCouponPeriod_, exCouponAdjustment_, exCouponEndOfMonth_);
216 }
217 auto cpn = QuantLib::ext::make_shared<DurationAdjustedCmsCoupon>(
218 paymentDate, detail::get(notionals_, i, 1.0), start, end,
219 detail::get(fixingDays_, i, swapIndex_->fixingDays()), swapIndex_, duration_,
220 detail::get(gearings_, i, 1.0), detail::get(spreads_, i, 0.0), refStart, refEnd, paymentDayCounter_,
221 inArrears_, exCouponDate);
222 if (detail::noOption(caps_, floors_, i)) {
223 leg.push_back(cpn);
224 } else {
225 leg.push_back(QuantLib::ext::make_shared<CappedFlooredCoupon>(cpn, detail::get(caps_, i, Null<Rate>()),
226 detail::get(floors_, i, Null<Rate>())));
227 }
228 }
229 return leg;
230}
231
232} // namespace QuantExt
void accept(AcyclicVisitor &) override
QuantLib::ext::shared_ptr< SwapIndex > swapIndex_
DurationAdjustedCmsCoupon(const Date &paymentDate, Real nominal, const Date &startDate, const Date &endDate, Natural fixingDays, const QuantLib::ext::shared_ptr< SwapIndex > &index, Size duration=0, Real gearing=1.0, Spread spread=0.0, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date(), const DayCounter &dayCounter=DayCounter(), bool isInArrears=false, const Date &exCouponDate=Date())
DurationAdjustedCmsLeg & withPaymentCalendar(const Calendar &)
DurationAdjustedCmsLeg & withPaymentAdjustment(BusinessDayConvention)
DurationAdjustedCmsLeg & withZeroPayments(bool flag=true)
DurationAdjustedCmsLeg & withSpreads(Spread spread)
DurationAdjustedCmsLeg & withFixingDays(Natural fixingDays)
DurationAdjustedCmsLeg & withDuration(Size duration)
DurationAdjustedCmsLeg & withPaymentLag(Natural lag)
DurationAdjustedCmsLeg & withFloors(Rate floor)
DurationAdjustedCmsLeg(const Schedule &schedule, const QuantLib::ext::shared_ptr< SwapIndex > &swapIndex, const Size duration)
DurationAdjustedCmsLeg & withNotionals(Real notional)
DurationAdjustedCmsLeg & withGearings(Real gearing)
DurationAdjustedCmsLeg & withExCouponPeriod(const Period &, const Calendar &, BusinessDayConvention, bool endOfMonth=false)
DurationAdjustedCmsLeg & withPaymentDayCounter(const DayCounter &)
DurationAdjustedCmsLeg & inArrears(bool flag=true)
DurationAdjustedCmsLeg & withCaps(Rate cap)
cms coupon scaled by a duration number