QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
iborcoupon.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Giorgio Facchinetti
5 Copyright (C) 2007 Cristina Duminuco
6 Copyright (C) 2010, 2011 Ferdinando Ametrano
7 Copyright (C) 2017 Joseph Jeisman
8 Copyright (C) 2017 Fabrice Lecuyer
9
10 This file is part of QuantLib, a free-software/open-source library
11 for financial quantitative analysts and developers - http://quantlib.org/
12
13 QuantLib is free software: you can redistribute it and/or modify it
14 under the terms of the QuantLib license. You should have received a
15 copy of the license along with this program; if not, please email
16 <quantlib-dev@lists.sf.net>. The license is also available online at
17 <http://quantlib.org/license.shtml>.
18
19 This program is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 FOR A PARTICULAR PURPOSE. See the license for more details.
22*/
23
30#include <ql/optional.hpp>
31#include <utility>
32
33namespace QuantLib {
34
35 IborCoupon::IborCoupon(const Date& paymentDate,
36 Real nominal,
37 const Date& startDate,
38 const Date& endDate,
39 Natural fixingDays,
40 const ext::shared_ptr<IborIndex>& iborIndex,
41 Real gearing,
42 Spread spread,
43 const Date& refPeriodStart,
44 const Date& refPeriodEnd,
45 const DayCounter& dayCounter,
46 bool isInArrears,
47 const Date& exCouponDate)
48 : FloatingRateCoupon(paymentDate, nominal, startDate, endDate,
49 fixingDays, iborIndex, gearing, spread,
50 refPeriodStart, refPeriodEnd,
51 dayCounter, isInArrears, exCouponDate),
52 iborIndex_(iborIndex) {
54 }
55
57 auto p = ext::dynamic_pointer_cast<IborCouponPricer>(pricer_);
58 QL_REQUIRE(p, "IborCoupon: pricer not set or not derived from IborCouponPricer");
59 p->initializeCachedData(*this);
60 }
61
64 return fixingValueDate_;
65 }
66
69 return fixingEndDate_;
70 }
71
75 }
76
79 return spanningTime_;
80 }
81
85 }
86
88 return fixingDate_;
89 }
90
91 bool IborCoupon::hasFixed() const {
92 Date today = QuantLib::Settings::instance().evaluationDate();
93
94 if (fixingDate_ > today) {
95 return false;
96 } else if (fixingDate_ < today) {
97 return true;
98 } else {
99 // fixingDate_ == today
100 if (QuantLib::Settings::instance().enforcesTodaysHistoricFixings()) {
101 return true;
102 } else {
103 return index_->hasHistoricalFixing(fixingDate_);
104 }
105 }
106 }
107
110
111 /* instead of just returning index_->fixing(fixingValueDate_)
112 its logic is duplicated here using a specialized iborIndex
113 forecastFixing overload which
114 1) allows to save date/time recalculations, and
115 2) takes into account par coupon needs
116 */
117
118 if (hasFixed()) {
119 Rate result = index_->pastFixing(fixingDate_);
120 QL_REQUIRE(result != Null<Real>(),
121 "Missing " << index_->name() << " fixing for " << fixingDate_);
122 return result;
123 } else {
125 }
126 }
127
128 void IborCoupon::setPricer(const ext::shared_ptr<FloatingRateCouponPricer>& pricer) {
131 }
132
134 auto* v1 = dynamic_cast<Visitor<IborCoupon>*>(&v);
135 if (v1 != nullptr)
136 v1->visit(*this);
137 else
139 }
140
141
143 usingAtParCoupons_ = true;
144 }
145
147 usingAtParCoupons_ = false;
148 }
149
151 return usingAtParCoupons_;
152 }
153
154 IborLeg::IborLeg(Schedule schedule, ext::shared_ptr<IborIndex> index)
155 : schedule_(std::move(schedule)), index_(std::move(index)) {
156 QL_REQUIRE(index_, "no index provided");
157 }
158
160 notionals_ = std::vector<Real>(1,notional);
161 return *this;
162 }
163
164 IborLeg& IborLeg::withNotionals(const std::vector<Real>& notionals) {
165 notionals_ = notionals;
166 return *this;
167 }
168
170 paymentDayCounter_ = dayCounter;
171 return *this;
172 }
173
175 paymentAdjustment_ = convention;
176 return *this;
177 }
178
180 paymentLag_ = lag;
181 return *this;
182 }
183
185 paymentCalendar_ = cal;
186 return *this;
187 }
188
190 fixingDays_ = std::vector<Natural>(1,fixingDays);
191 return *this;
192 }
193
194 IborLeg& IborLeg::withFixingDays(const std::vector<Natural>& fixingDays) {
195 fixingDays_ = fixingDays;
196 return *this;
197 }
198
200 gearings_ = std::vector<Real>(1,gearing);
201 return *this;
202 }
203
204 IborLeg& IborLeg::withGearings(const std::vector<Real>& gearings) {
205 gearings_ = gearings;
206 return *this;
207 }
208
210 spreads_ = std::vector<Spread>(1,spread);
211 return *this;
212 }
213
214 IborLeg& IborLeg::withSpreads(const std::vector<Spread>& spreads) {
215 spreads_ = spreads;
216 return *this;
217 }
218
220 caps_ = std::vector<Rate>(1,cap);
221 return *this;
222 }
223
224 IborLeg& IborLeg::withCaps(const std::vector<Rate>& caps) {
225 caps_ = caps;
226 return *this;
227 }
228
230 floors_ = std::vector<Rate>(1,floor);
231 return *this;
232 }
233
234 IborLeg& IborLeg::withFloors(const std::vector<Rate>& floors) {
235 floors_ = floors;
236 return *this;
237 }
238
240 inArrears_ = flag;
241 return *this;
242 }
243
245 zeroPayments_ = flag;
246 return *this;
247 }
248
250 const Calendar& cal,
251 BusinessDayConvention convention,
252 bool endOfMonth) {
253 exCouponPeriod_ = period;
254 exCouponCalendar_ = cal;
255 exCouponAdjustment_ = convention;
256 exCouponEndOfMonth_ = endOfMonth;
257 return *this;
258 }
259
260 IborLeg& IborLeg::withIndexedCoupons(ext::optional<bool> b) {
262 return *this;
263 }
264
267 return *this;
268 }
269
270 IborLeg::operator Leg() const {
271
272 Leg leg = FloatingLeg<IborIndex, IborCoupon, CappedFlooredIborCoupon>(
273 schedule_, notionals_, index_, paymentDayCounter_,
274 paymentAdjustment_, fixingDays_, gearings_, spreads_,
275 caps_, floors_, inArrears_, zeroPayments_, paymentLag_, paymentCalendar_,
276 exCouponPeriod_, exCouponCalendar_, exCouponAdjustment_, exCouponEndOfMonth_);
277
278 if (caps_.empty() && floors_.empty() && !inArrears_) {
279 ext::shared_ptr<IborCouponPricer> pricer = ext::make_shared<BlackIborCouponPricer>(
282 Handle<Quote>(ext::make_shared<SimpleQuote>(1.0)), useIndexedCoupons_);
283 setCouponPricer(leg, pricer);
284 }
285
286 return leg;
287 }
288
289}
Floating rate coupon with additional cap/floor.
Cash flow vector builders.
degenerate base class for the Acyclic Visitor pattern
Definition: visitor.hpp:33
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
base floating-rate coupon class
virtual Date fixingDate() const
fixing date
ext::shared_ptr< InterestRateIndex > index_
virtual void setPricer(const ext::shared_ptr< FloatingRateCouponPricer > &)
ext::shared_ptr< FloatingRateCouponPricer > pricer_
void accept(AcyclicVisitor &) override
const ext::shared_ptr< InterestRateIndex > & index() const
floating index
ext::shared_ptr< FloatingRateCouponPricer > pricer() const
Shared handle to an observable.
Definition: handle.hpp:41
void createIndexedCoupons()
When called, IborCoupons are created as par coupons instead of indexed coupons.
Definition: iborcoupon.cpp:146
void createAtParCoupons()
When called, IborCoupons are created as indexed coupons instead of par coupons.
Definition: iborcoupon.cpp:142
void initializeCachedData() const
Definition: iborcoupon.cpp:56
bool hasFixed() const
Definition: iborcoupon.cpp:91
Time spanningTimeIndexMaturity() const
Period underlying the index fixing, as a year fraction.
Definition: iborcoupon.cpp:82
Rate indexFixing() const override
fixing of the underlying index
Definition: iborcoupon.cpp:108
ext::shared_ptr< IborIndex > iborIndex_
Definition: iborcoupon.hpp:94
void accept(AcyclicVisitor &) override
Definition: iborcoupon.cpp:133
const Date & fixingValueDate() const
Start of the deposit period underlying the index fixing.
Definition: iborcoupon.cpp:62
void setPricer(const ext::shared_ptr< FloatingRateCouponPricer > &) override
Definition: iborcoupon.cpp:128
IborCoupon(const Date &paymentDate, Real nominal, const Date &startDate, const Date &endDate, Natural fixingDays, const ext::shared_ptr< IborIndex > &index, 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())
Definition: iborcoupon.cpp:35
const Date & fixingEndDate() const
End of the deposit period underlying the coupon fixing.
Definition: iborcoupon.cpp:67
const Date & fixingMaturityDate() const
End of the deposit period underlying the index fixing.
Definition: iborcoupon.cpp:72
Date fixingDate() const override
fixing date
Definition: iborcoupon.cpp:87
Time spanningTime() const
Period underlying the coupon fixing, as a year fraction.
Definition: iborcoupon.cpp:77
helper class building a sequence of capped/floored ibor-rate coupons
Definition: iborcoupon.hpp:135
IborLeg & withSpreads(Spread spread)
Definition: iborcoupon.cpp:209
BusinessDayConvention paymentAdjustment_
Definition: iborcoupon.hpp:169
std::vector< Rate > caps_
Definition: iborcoupon.hpp:175
IborLeg & withPaymentCalendar(const Calendar &)
Definition: iborcoupon.cpp:184
BusinessDayConvention exCouponAdjustment_
Definition: iborcoupon.hpp:179
IborLeg & withPaymentAdjustment(BusinessDayConvention)
Definition: iborcoupon.cpp:174
Calendar paymentCalendar_
Definition: iborcoupon.hpp:171
IborLeg(Schedule schedule, ext::shared_ptr< IborIndex > index)
Definition: iborcoupon.cpp:154
ext::optional< bool > useIndexedCoupons_
Definition: iborcoupon.hpp:181
std::vector< Real > notionals_
Definition: iborcoupon.hpp:167
std::vector< Spread > spreads_
Definition: iborcoupon.hpp:174
IborLeg & withPaymentDayCounter(const DayCounter &)
Definition: iborcoupon.cpp:169
IborLeg & inArrears(bool flag=true)
Definition: iborcoupon.cpp:239
IborLeg & withFloors(Rate floor)
Definition: iborcoupon.cpp:229
IborLeg & withNotionals(Real notional)
Definition: iborcoupon.cpp:159
std::vector< Natural > fixingDays_
Definition: iborcoupon.hpp:172
IborLeg & withGearings(Real gearing)
Definition: iborcoupon.cpp:199
IborLeg & withPaymentLag(Integer lag)
Definition: iborcoupon.cpp:179
Calendar exCouponCalendar_
Definition: iborcoupon.hpp:178
std::vector< Rate > floors_
Definition: iborcoupon.hpp:175
IborLeg & withZeroPayments(bool flag=true)
Definition: iborcoupon.cpp:244
IborLeg & withExCouponPeriod(const Period &, const Calendar &, BusinessDayConvention, bool endOfMonth=false)
Definition: iborcoupon.cpp:249
IborLeg & withCaps(Rate cap)
Definition: iborcoupon.cpp:219
IborLeg & withIndexedCoupons(ext::optional< bool > b=true)
Definition: iborcoupon.cpp:260
IborLeg & withFixingDays(Natural fixingDays)
Definition: iborcoupon.cpp:189
IborLeg & withAtParCoupons(bool b=true)
Definition: iborcoupon.cpp:265
std::vector< Real > gearings_
Definition: iborcoupon.hpp:173
DayCounter paymentDayCounter_
Definition: iborcoupon.hpp:168
ext::shared_ptr< IborIndex > index_
Definition: iborcoupon.hpp:166
template class providing a null value for a given type.
Definition: null.hpp:59
Payment schedule.
Definition: schedule.hpp:40
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Visitor for a specific class
Definition: visitor.hpp:40
virtual void visit(T &)=0
Coupon pricers.
Calendar paymentCalendar_
Integer paymentLag_
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
std::function< Real(Real)> b
BusinessDayConvention
Business Day conventions.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
Coupon paying a Libor-type index.
base class for interest rate indexes
Definition: any.hpp:37
void setCouponPricer(const Leg &leg, const ext::shared_ptr< FloatingRateCouponPricer > &pricer)
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78
STL namespace.
Maps optional to either the boost or std implementation.
ext::shared_ptr< BlackVolTermStructure > v
Interest-rate term structure.