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