QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
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
92
94
95 /* instead of just returning index_->fixing(fixingValueDate_)
96 its logic is duplicated here using a specialized iborIndex
97 forecastFixing overload which
98 1) allows to save date/time recalculations, and
99 2) takes into account par coupon needs
100 */
101 Date today = QuantLib::Settings::instance().evaluationDate();
102
103 if (fixingDate_>today)
104 return iborIndex_->forecastFixing(fixingValueDate_,
107
108 if (fixingDate_<today ||
109 QuantLib::Settings::instance().enforcesTodaysHistoricFixings()) {
110 // do not catch exceptions
111 Rate result = index_->pastFixing(fixingDate_);
112 QL_REQUIRE(result != Null<Real>(),
113 "Missing " << index_->name() << " fixing for " << fixingDate_);
114 return result;
115 }
116
117 try {
118 Rate result = index_->pastFixing(fixingDate_);
119 if (result!=Null<Real>())
120 return result;
121 else
122 ; // fall through and forecast
123 } catch (Error&) {
124 ; // fall through and forecast
125 }
126 return iborIndex_->forecastFixing(fixingValueDate_,
129 }
130
131 void IborCoupon::setPricer(const ext::shared_ptr<FloatingRateCouponPricer>& pricer) {
134 }
135
137 auto* v1 = dynamic_cast<Visitor<IborCoupon>*>(&v);
138 if (v1 != nullptr)
139 v1->visit(*this);
140 else
142 }
143
144
146 usingAtParCoupons_ = true;
147 }
148
150 usingAtParCoupons_ = false;
151 }
152
154 return usingAtParCoupons_;
155 }
156
157 IborLeg::IborLeg(Schedule schedule, ext::shared_ptr<IborIndex> index)
158 : schedule_(std::move(schedule)), index_(std::move(index)) {
159 QL_REQUIRE(index_, "no index provided");
160 }
161
163 notionals_ = std::vector<Real>(1,notional);
164 return *this;
165 }
166
167 IborLeg& IborLeg::withNotionals(const std::vector<Real>& notionals) {
168 notionals_ = notionals;
169 return *this;
170 }
171
173 paymentDayCounter_ = dayCounter;
174 return *this;
175 }
176
178 paymentAdjustment_ = convention;
179 return *this;
180 }
181
183 paymentLag_ = lag;
184 return *this;
185 }
186
188 paymentCalendar_ = cal;
189 return *this;
190 }
191
193 fixingDays_ = std::vector<Natural>(1,fixingDays);
194 return *this;
195 }
196
197 IborLeg& IborLeg::withFixingDays(const std::vector<Natural>& fixingDays) {
198 fixingDays_ = fixingDays;
199 return *this;
200 }
201
203 gearings_ = std::vector<Real>(1,gearing);
204 return *this;
205 }
206
207 IborLeg& IborLeg::withGearings(const std::vector<Real>& gearings) {
208 gearings_ = gearings;
209 return *this;
210 }
211
213 spreads_ = std::vector<Spread>(1,spread);
214 return *this;
215 }
216
217 IborLeg& IborLeg::withSpreads(const std::vector<Spread>& spreads) {
218 spreads_ = spreads;
219 return *this;
220 }
221
223 caps_ = std::vector<Rate>(1,cap);
224 return *this;
225 }
226
227 IborLeg& IborLeg::withCaps(const std::vector<Rate>& caps) {
228 caps_ = caps;
229 return *this;
230 }
231
233 floors_ = std::vector<Rate>(1,floor);
234 return *this;
235 }
236
237 IborLeg& IborLeg::withFloors(const std::vector<Rate>& floors) {
238 floors_ = floors;
239 return *this;
240 }
241
243 inArrears_ = flag;
244 return *this;
245 }
246
248 zeroPayments_ = flag;
249 return *this;
250 }
251
253 const Calendar& cal,
254 BusinessDayConvention convention,
255 bool endOfMonth) {
256 exCouponPeriod_ = period;
257 exCouponCalendar_ = cal;
258 exCouponAdjustment_ = convention;
259 exCouponEndOfMonth_ = endOfMonth;
260 return *this;
261 }
262
263 IborLeg& IborLeg::withIndexedCoupons(ext::optional<bool> b) {
265 return *this;
266 }
267
270 return *this;
271 }
272
273 IborLeg::operator Leg() const {
274
275 Leg leg = FloatingLeg<IborIndex, IborCoupon, CappedFlooredIborCoupon>(
276 schedule_, notionals_, index_, paymentDayCounter_,
277 paymentAdjustment_, fixingDays_, gearings_, spreads_,
278 caps_, floors_, inArrears_, zeroPayments_, paymentLag_, paymentCalendar_,
279 exCouponPeriod_, exCouponCalendar_, exCouponAdjustment_, exCouponEndOfMonth_);
280
281 if (caps_.empty() && floors_.empty() && !inArrears_) {
282 ext::shared_ptr<IborCouponPricer> pricer = ext::make_shared<BlackIborCouponPricer>(
285 Handle<Quote>(ext::make_shared<SimpleQuote>(1.0)), useIndexedCoupons_);
286 setCouponPricer(leg, pricer);
287 }
288
289 return leg;
290 }
291
292}
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 error class.
Definition: errors.hpp:39
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:149
void createAtParCoupons()
When called, IborCoupons are created as indexed coupons instead of par coupons.
Definition: iborcoupon.cpp:145
void initializeCachedData() const
Definition: iborcoupon.cpp:56
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:91
ext::shared_ptr< IborIndex > iborIndex_
Definition: iborcoupon.hpp:93
void accept(AcyclicVisitor &) override
Definition: iborcoupon.cpp:136
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:131
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:134
IborLeg & withSpreads(Spread spread)
Definition: iborcoupon.cpp:212
BusinessDayConvention paymentAdjustment_
Definition: iborcoupon.hpp:168
std::vector< Rate > caps_
Definition: iborcoupon.hpp:174
IborLeg & withPaymentCalendar(const Calendar &)
Definition: iborcoupon.cpp:187
BusinessDayConvention exCouponAdjustment_
Definition: iborcoupon.hpp:178
IborLeg & withPaymentAdjustment(BusinessDayConvention)
Definition: iborcoupon.cpp:177
Calendar paymentCalendar_
Definition: iborcoupon.hpp:170
IborLeg(Schedule schedule, ext::shared_ptr< IborIndex > index)
Definition: iborcoupon.cpp:157
ext::optional< bool > useIndexedCoupons_
Definition: iborcoupon.hpp:180
std::vector< Real > notionals_
Definition: iborcoupon.hpp:166
std::vector< Spread > spreads_
Definition: iborcoupon.hpp:173
IborLeg & withPaymentDayCounter(const DayCounter &)
Definition: iborcoupon.cpp:172
IborLeg & inArrears(bool flag=true)
Definition: iborcoupon.cpp:242
IborLeg & withFloors(Rate floor)
Definition: iborcoupon.cpp:232
IborLeg & withNotionals(Real notional)
Definition: iborcoupon.cpp:162
std::vector< Natural > fixingDays_
Definition: iborcoupon.hpp:171
IborLeg & withGearings(Real gearing)
Definition: iborcoupon.cpp:202
IborLeg & withPaymentLag(Integer lag)
Definition: iborcoupon.cpp:182
Calendar exCouponCalendar_
Definition: iborcoupon.hpp:177
std::vector< Rate > floors_
Definition: iborcoupon.hpp:174
IborLeg & withZeroPayments(bool flag=true)
Definition: iborcoupon.cpp:247
IborLeg & withExCouponPeriod(const Period &, const Calendar &, BusinessDayConvention, bool endOfMonth=false)
Definition: iborcoupon.cpp:252
IborLeg & withCaps(Rate cap)
Definition: iborcoupon.cpp:222
IborLeg & withIndexedCoupons(ext::optional< bool > b=true)
Definition: iborcoupon.cpp:263
IborLeg & withFixingDays(Natural fixingDays)
Definition: iborcoupon.cpp:192
IborLeg & withAtParCoupons(bool b=true)
Definition: iborcoupon.cpp:268
std::vector< Real > gearings_
Definition: iborcoupon.hpp:172
DayCounter paymentDayCounter_
Definition: iborcoupon.hpp:167
ext::shared_ptr< IborIndex > index_
Definition: iborcoupon.hpp:165
template class providing a null value for a given type.
Definition: null.hpp:76
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.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
ext::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:35
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.