QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fixedratecoupon.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004, 2007 StatPro Italia srl
6 Copyright (C) 2007 Piter Dias
7 Copyright (C) 2010 Ferdinando Ametrano
8 Copyright (C) 2017 Joseph Jeisman
9 Copyright (C) 2017 Fabrice Lecuyer
10
11 This file is part of QuantLib, a free-software/open-source library
12 for financial quantitative analysts and developers - http://quantlib.org/
13
14 QuantLib is free software: you can redistribute it and/or modify it
15 under the terms of the QuantLib license. You should have received a
16 copy of the license along with this program; if not, please email
17 <quantlib-dev@lists.sf.net>. The license is also available online at
18 <http://quantlib.org/license.shtml>.
19
20 This program is distributed in the hope that it will be useful, but WITHOUT
21 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. See the license for more details.
23*/
24
25#include <ql/cashflows/fixedratecoupon.hpp>
26#include <utility>
27
28using std::vector;
29
30namespace QuantLib {
31
33 Real nominal,
34 Rate rate,
35 const DayCounter& dayCounter,
36 const Date& accrualStartDate,
37 const Date& accrualEndDate,
38 const Date& refPeriodStart,
39 const Date& refPeriodEnd,
40 const Date& exCouponDate)
41 : Coupon(paymentDate, nominal, accrualStartDate, accrualEndDate,
42 refPeriodStart, refPeriodEnd, exCouponDate),
43 rate_(InterestRate(rate, dayCounter, Simple, Annual)) {}
44
46 Real nominal,
47 InterestRate interestRate,
48 const Date& accrualStartDate,
49 const Date& accrualEndDate,
50 const Date& refPeriodStart,
51 const Date& refPeriodEnd,
52 const Date& exCouponDate)
53 : Coupon(paymentDate,
54 nominal,
55 accrualStartDate,
56 accrualEndDate,
57 refPeriodStart,
58 refPeriodEnd,
59 exCouponDate),
60 rate_(std::move(interestRate)) {}
61
63 calculate();
64 return amount_;
65 }
66
70 1.0);
71 }
72
74 if (d <= accrualStartDate_ || d > paymentDate_) {
75 // out of coupon range
76 return 0.0;
77 } else if (tradingExCoupon(d)) {
78 return -nominal()*(rate_.compoundFactor(d,
79 std::max(d, accrualEndDate_),
81 refPeriodEnd_) - 1.0);
82 } else {
83 // usual case
85 std::min(d,accrualEndDate_),
87 refPeriodEnd_) - 1.0);
88 }
89 }
90
91
93 : schedule_(schedule), paymentCalendar_(schedule.calendar()) {}
94
96 notionals_ = vector<Real>(1,notional);
97 return *this;
98 }
99
100 FixedRateLeg& FixedRateLeg::withNotionals(const vector<Real>& notionals) {
101 notionals_ = notionals;
102 return *this;
103 }
104
106 const DayCounter& dc,
107 Compounding comp,
108 Frequency freq) {
109 couponRates_.resize(1);
110 couponRates_[0] = InterestRate(rate, dc, comp, freq);
111 return *this;
112 }
113
115 couponRates_.resize(1);
116 couponRates_[0] = i;
117 return *this;
118 }
119
120 FixedRateLeg& FixedRateLeg::withCouponRates(const vector<Rate>& rates,
121 const DayCounter& dc,
122 Compounding comp,
123 Frequency freq) {
124 couponRates_.resize(rates.size());
125 for (Size i=0; i<rates.size(); ++i)
126 couponRates_[i] = InterestRate(rates[i], dc, comp, freq);
127 return *this;
128 }
129
131 const vector<InterestRate>& interestRates) {
132 couponRates_ = interestRates;
133 return *this;
134 }
135
137 BusinessDayConvention convention) {
138 paymentAdjustment_ = convention;
139 return *this;
140 }
141
143 const DayCounter& dayCounter) {
144 firstPeriodDC_ = dayCounter;
145 return *this;
146 }
147
149 const DayCounter& dayCounter) {
150 lastPeriodDC_ = dayCounter;
151 return *this;
152 }
153
155 paymentCalendar_ = cal;
156 return *this;
157 }
158
160 paymentLag_ = lag;
161 return *this;
162 }
163
165 const Period& period,
166 const Calendar& cal,
167 BusinessDayConvention convention,
168 bool endOfMonth) {
169 exCouponPeriod_ = period;
170 exCouponCalendar_ = cal;
171 exCouponAdjustment_ = convention;
172 exCouponEndOfMonth_ = endOfMonth;
173 return *this;
174 }
175
176 FixedRateLeg::operator Leg() const {
177
178 QL_REQUIRE(!couponRates_.empty(), "no coupon rates given");
179 QL_REQUIRE(!notionals_.empty(), "no notional given");
180
181 Leg leg;
182 leg.reserve(schedule_.size()-1);
183
184 // first period might be short or long
185 Date start = schedule_.date(0), end = schedule_.date(1);
186 Date paymentDate = paymentCalendar_.advance(end, paymentLag_, Days, paymentAdjustment_);
187 Date exCouponDate;
188 InterestRate rate = couponRates_[0];
189 Real nominal = notionals_[0];
190
191 if (exCouponPeriod_ != Period())
192 {
193 exCouponDate = exCouponCalendar_.advance(paymentDate,
194 -exCouponPeriod_,
195 exCouponAdjustment_,
196 exCouponEndOfMonth_);
197 }
198 Date ref = schedule_.hasTenor() &&
199 schedule_.hasIsRegular() && !schedule_.isRegular(1) ?
200 schedule_.calendar().advance(end,
201 -schedule_.tenor(),
202 schedule_.businessDayConvention(),
203 schedule_.endOfMonth())
204 : start;
205 InterestRate r(rate.rate(),
206 firstPeriodDC_.empty() ? rate.dayCounter()
207 : firstPeriodDC_,
208 rate.compounding(), rate.frequency());
209 leg.push_back(ext::shared_ptr<CashFlow>(new
210 FixedRateCoupon(paymentDate, nominal, r,
211 start, end, ref, end, exCouponDate)));
212 // regular periods
213 for (Size i=2; i<schedule_.size()-1; ++i) {
214 start = end; end = schedule_.date(i);
215 Date paymentDate = paymentCalendar_.advance(end, paymentLag_, Days, paymentAdjustment_);
216 if (exCouponPeriod_ != Period())
217 {
218 exCouponDate = exCouponCalendar_.advance(paymentDate,
219 -exCouponPeriod_,
220 exCouponAdjustment_,
221 exCouponEndOfMonth_);
222 }
223 if ((i-1) < couponRates_.size())
224 rate = couponRates_[i-1];
225 else
226 rate = couponRates_.back();
227 if ((i-1) < notionals_.size())
228 nominal = notionals_[i-1];
229 else
230 nominal = notionals_.back();
231 leg.push_back(ext::shared_ptr<CashFlow>(new
232 FixedRateCoupon(paymentDate, nominal, rate,
233 start, end, start, end, exCouponDate)));
234 }
235 if (schedule_.size() > 2) {
236 // last period might be short or long
237 Size N = schedule_.size();
238 start = end; end = schedule_.date(N-1);
239 Date paymentDate = paymentCalendar_.advance(end, paymentLag_, Days, paymentAdjustment_);
240 if (exCouponPeriod_ != Period())
241 {
242 exCouponDate = exCouponCalendar_.advance(paymentDate,
243 -exCouponPeriod_,
244 exCouponAdjustment_,
245 exCouponEndOfMonth_);
246 }
247 if ((N-2) < couponRates_.size())
248 rate = couponRates_[N-2];
249 else
250 rate = couponRates_.back();
251 if ((N-2) < notionals_.size())
252 nominal = notionals_[N-2];
253 else
254 nominal = notionals_.back();
255 InterestRate r( rate.rate(), lastPeriodDC_.empty() ?
256 rate.dayCounter() :
257 lastPeriodDC_ , rate.compounding(), rate.frequency() );
258 if ((schedule_.hasIsRegular() && schedule_.isRegular(N - 1)) ||
259 !schedule_.hasTenor()) {
260 leg.push_back(ext::shared_ptr<CashFlow>(new
261 FixedRateCoupon(paymentDate, nominal, r,
262 start, end, start, end, exCouponDate)));
263 } else {
264 Date ref = schedule_.calendar().advance(
265 start,
266 schedule_.tenor(),
267 schedule_.businessDayConvention(),
268 schedule_.endOfMonth());
269 leg.push_back(ext::shared_ptr<CashFlow>(new
270 FixedRateCoupon(paymentDate, nominal, r,
271 start, end, start, ref, exCouponDate)));
272 }
273 }
274 return leg;
275 }
276
277}
calendar class
Definition: calendar.hpp:61
bool tradingExCoupon(const Date &refDate=Date()) const
returns true if the cashflow is trading ex-coupon on the refDate
Definition: cashflow.cpp:51
coupon accruing over a fixed period
Definition: coupon.hpp:39
Date paymentDate_
Definition: coupon.hpp:90
Date refPeriodEnd_
Definition: coupon.hpp:92
Date accrualStartDate_
Definition: coupon.hpp:92
virtual Real nominal() const
Definition: coupon.hpp:100
Date refPeriodStart_
Definition: coupon.hpp:92
Date accrualEndDate_
Definition: coupon.hpp:92
Concrete date class.
Definition: date.hpp:125
static Date endOfMonth(const Date &d)
last day of the month to which the given date belongs
Definition: date.hpp:428
static Date advance(const Date &d, Integer units, TimeUnit)
Definition: date.cpp:139
day counter class
Definition: daycounter.hpp:44
Coupon paying a fixed interest rate
void performCalculations() const override
Real amount() const override
returns the amount of the cash flow
Real accruedAmount(const Date &) const override
accrued amount at the given date
FixedRateCoupon(const Date &paymentDate, Real nominal, Rate rate, const DayCounter &dayCounter, const Date &accrualStartDate, const Date &accrualEndDate, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date(), const Date &exCouponDate=Date())
helper class building a sequence of fixed rate coupons
BusinessDayConvention paymentAdjustment_
FixedRateLeg & withNotionals(Real)
BusinessDayConvention exCouponAdjustment_
std::vector< InterestRate > couponRates_
FixedRateLeg & withPaymentCalendar(const Calendar &)
FixedRateLeg(const Schedule &schedule)
std::vector< Real > notionals_
FixedRateLeg & withPaymentAdjustment(BusinessDayConvention)
FixedRateLeg & withCouponRates(Rate, const DayCounter &paymentDayCounter, Compounding comp=Simple, Frequency freq=Annual)
FixedRateLeg & withLastPeriodDayCounter(const DayCounter &)
FixedRateLeg & withFirstPeriodDayCounter(const DayCounter &)
FixedRateLeg & withPaymentLag(Natural lag)
FixedRateLeg & withExCouponPeriod(const Period &, const Calendar &, BusinessDayConvention, bool endOfMonth=false)
Concrete interest rate class.
Real compoundFactor(Time t) const
compound factor implied by the rate compounded at time t.
virtual void calculate() const
Definition: lazyobject.hpp:253
Payment schedule.
Definition: schedule.hpp:40
Frequency
Frequency of events.
Definition: frequency.hpp:37
BusinessDayConvention
Business Day conventions.
@ Annual
once a year
Definition: frequency.hpp:39
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.