QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
bond.hpp
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) 2004 Jeff Yu
5 Copyright (C) 2004 M-Dimension Consulting Inc.
6 Copyright (C) 2005, 2006, 2007, 2008 StatPro Italia srl
7 Copyright (C) 2007, 2008, 2009 Ferdinando Ametrano
8 Copyright (C) 2007 Chiara Fornarola
9 Copyright (C) 2008 Simon Ibbotson
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/*! \file bond.hpp
26 \brief concrete bond class
27*/
28
29#ifndef quantlib_bond_hpp
30#define quantlib_bond_hpp
31
32#include <ql/instrument.hpp>
33
34#include <ql/time/calendar.hpp>
35#include <ql/cashflow.hpp>
36#include <ql/compounding.hpp>
37
38#include <vector>
39
40namespace QuantLib {
41
42 class DayCounter;
43
44 //! Base bond class
45 /*! Derived classes must fill the uninitialized data members.
46
47 \warning Most methods assume that the cash flows are stored
48 sorted by date, the redemption(s) being after any
49 cash flow at the same date. In particular, if there's
50 one single redemption, it must be the last cash flow,
51
52 \ingroup instruments
53
54 \test
55 - price/yield calculations are cross-checked for consistency.
56 - price/yield calculations are checked against known good
57 values.
58 */
59 class Bond : public Instrument {
60 public:
61 //! Bond price information
62 class Price {
63 public:
64 enum Type { Dirty, Clean };
67 Real amount() const {
68 QL_REQUIRE(amount_ != Null<Real>(), "no amount given");
69 return amount_;
70 }
71 Type type() const { return type_; }
72 bool isValid() const { return amount_ != Null<Real>(); }
73 private:
76 };
77
78 //! constructor for amortizing or non-amortizing bonds.
79 /*! Redemptions and maturity are calculated from the coupon
80 data, if available. Therefore, redemptions must not be
81 included in the passed cash flows.
82 */
85 const Date& issueDate = Date(),
86 const Leg& coupons = Leg());
87
88 //! old constructor for non amortizing bonds.
89 /*! \warning The last passed cash flow must be the bond
90 redemption. No other cash flow can have a date
91 later than the redemption date.
92 */
95 Real faceAmount,
96 const Date& maturityDate,
97 const Date& issueDate = Date(),
98 const Leg& cashflows = Leg());
99
100 class arguments;
101 class results;
102 class engine;
103
104 //! \name Instrument interface
105 //@{
106 bool isExpired() const override;
107 //@}
108 //! \name Observable interface
109 //@{
110 void deepUpdate() override;
111 //@}
112 //! \name Inspectors
113 //@{
114 Natural settlementDays() const;
115 const Calendar& calendar() const;
116
117 const std::vector<Real>& notionals() const;
118 virtual Real notional(Date d = Date()) const;
119
120 /*! \note returns all the cashflows, including the redemptions. */
121 const Leg& cashflows() const;
122 /*! returns just the redemption flows (not interest payments) */
123 const Leg& redemptions() const;
124 /*! returns the redemption, if only one is defined */
125 const ext::shared_ptr<CashFlow>& redemption() const;
126
127 Date startDate() const;
128 Date maturityDate() const;
129 Date issueDate() const;
130
131 bool isTradable(Date d = Date()) const;
132 Date settlementDate(Date d = Date()) const;
133 //@}
134
135 //! \name Calculations
136 //@{
137
138 //! theoretical clean price
139 /*! The default bond settlement is used for calculation.
140
141 \warning the theoretical price calculated from a flat term
142 structure might differ slightly from the price
143 calculated from the corresponding yield by means
144 of the other overload of this function. If the
145 price from a constant yield is desired, it is
146 advisable to use such other overload.
147 */
148 Real cleanPrice() const;
149
150 //! theoretical dirty price
151 /*! The default bond settlement is used for calculation.
152
153 \warning the theoretical price calculated from a flat term
154 structure might differ slightly from the price
155 calculated from the corresponding yield by means
156 of the other overload of this function. If the
157 price from a constant yield is desired, it is
158 advisable to use such other overload.
159 */
160 Real dirtyPrice() const;
161
162 //! theoretical settlement value
163 /*! The default bond settlement date is used for calculation. */
164 Real settlementValue() const;
165
166 //! theoretical bond yield
167 /*! The default bond settlement and theoretical price are used
168 for calculation.
169 */
170 Rate yield(const DayCounter& dc,
171 Compounding comp,
172 Frequency freq,
173 Real accuracy = 1.0e-8,
174 Size maxEvaluations = 100,
175 Real guess = 0.05,
176 Bond::Price::Type priceType = Bond::Price::Clean) const;
177
178 //! clean price given a yield and settlement date
179 /*! The default bond settlement is used if no date is given. */
181 const DayCounter& dc,
182 Compounding comp,
183 Frequency freq,
184 Date settlementDate = Date()) const;
185
186 //! dirty price given a yield and settlement date
187 /*! The default bond settlement is used if no date is given. */
189 const DayCounter& dc,
190 Compounding comp,
191 Frequency freq,
192 Date settlementDate = Date()) const;
193
194 //! settlement value as a function of the clean price
195 /*! The default bond settlement date is used for calculation. */
197
198 /*! \deprecated Use the overload taking a Bond::Price argument instead.
199 Deprecated in version 1.34.
200 */
201 [[deprecated("Use the overload taking a Bond::Price argument instead")]]
202 Rate yield(Real price,
203 const DayCounter& dc,
204 Compounding comp,
205 Frequency freq,
207 Real accuracy = 1.0e-8,
208 Size maxEvaluations = 100,
209 Real guess = 0.05,
210 Bond::Price::Type priceType = Bond::Price::Clean) const;
211
212 //! yield given a price and settlement date
213 /*! The default bond settlement is used if no date is given. */
214 Rate yield(Bond::Price price,
215 const DayCounter& dc,
216 Compounding comp,
217 Frequency freq,
219 Real accuracy = 1.0e-8,
220 Size maxEvaluations = 100,
221 Real guess = 0.05) const;
222
223 //! accrued amount at a given date
224 /*! The default bond settlement is used if no date is given. */
225 virtual Real accruedAmount(Date d = Date()) const;
226 //@}
227
228 /*! Expected next coupon: depending on (the bond and) the given date
229 the coupon can be historic, deterministic or expected in a
230 stochastic sense. When the bond settlement date is used the coupon
231 is the already-fixed not-yet-paid one.
232
233 The current bond settlement is used if no date is given.
234 */
235 virtual Rate nextCouponRate(Date d = Date()) const;
236
237 //! Previous coupon already paid at a given date
238 /*! Expected previous coupon: depending on (the bond and) the given
239 date the coupon can be historic, deterministic or expected in a
240 stochastic sense. When the bond settlement date is used the coupon
241 is the last paid one.
242
243 The current bond settlement is used if no date is given.
244 */
245 Rate previousCouponRate(Date d = Date()) const;
246
247 Date nextCashFlowDate(Date d = Date()) const;
249
250 protected:
251 void setupExpired() const override;
252 void setupArguments(PricingEngine::arguments*) const override;
253 void fetchResults(const PricingEngine::results*) const override;
254
255 /*! This method can be called by derived classes in order to
256 build redemption payments from the existing cash flows.
257 It must be called after setting up the cashflows_ vector
258 and will fill the notionalSchedule_, notionals_, and
259 redemptions_ data members.
260
261 If given, the elements of the redemptions vector will
262 multiply the amount of the redemption cash flow. The
263 elements will be taken in base 100, i.e., a redemption
264 equal to 100 does not modify the amount.
265
266 \pre The cashflows_ vector must contain at least one
267 coupon and must be sorted by date.
268 */
269 void addRedemptionsToCashflows(const std::vector<Real>& redemptions
270 = std::vector<Real>());
271
272 /*! This method can be called by derived classes in order to
273 build a bond with a single redemption payment. It will
274 fill the notionalSchedule_, notionals_, and redemptions_
275 data members.
276 */
279 const Date& date);
280
281 /*! This method can be called by derived classes in order to
282 build a bond with a single redemption payment. It will
283 fill the notionalSchedule_, notionals_, and redemptions_
284 data members.
285 */
287 const ext::shared_ptr<CashFlow>& redemption);
288
289 /*! used internally to collect notional information from the
290 coupons. It should not be called by derived classes,
291 unless they already provide redemption cash flows (in
292 which case they must set up the redemptions_ data member
293 independently). It will fill the notionalSchedule_ and
294 notionals_ data members.
295 */
297
300 std::vector<Date> notionalSchedule_;
301 std::vector<Real> notionals_;
302 Leg cashflows_; // all cashflows
303 Leg redemptions_; // the redemptions
304
307 };
308
310 public:
314 void validate() const override;
315 };
316
318 public:
320 void reset() override {
323 }
324 };
325
326 class Bond::engine : public GenericEngine<Bond::arguments,
327 Bond::results> {};
328
329
330 // inline definitions
331
333 return settlementDays_;
334 }
335
336 inline const Calendar& Bond::calendar() const {
337 return calendar_;
338 }
339
340 inline const std::vector<Real>& Bond::notionals() const {
341 return notionals_;
342 }
343
344 inline const Leg& Bond::cashflows() const {
345 return cashflows_;
346 }
347
348 inline const Leg& Bond::redemptions() const {
349 return redemptions_;
350 }
351
352 inline Date Bond::issueDate() const {
353 return issueDate_;
354 }
355
356}
357
358#endif
calendar class
Base class for cash flows.
Bond price information.
Definition: bond.hpp:62
bool isValid() const
Definition: bond.hpp:72
Price(Real amount, Type type)
Definition: bond.hpp:66
Type type() const
Definition: bond.hpp:71
Real amount() const
Definition: bond.hpp:67
void validate() const override
Definition: bond.cpp:410
void reset() override
Definition: bond.hpp:320
Base bond class.
Definition: bond.hpp:59
Calendar calendar_
Definition: bond.hpp:299
Real cleanPrice() const
theoretical clean price
Definition: bond.cpp:174
Leg redemptions_
Definition: bond.hpp:303
Real settlementValue_
Definition: bond.hpp:306
void setupArguments(PricingEngine::arguments *) const override
Definition: bond.cpp:296
const std::vector< Real > & notionals() const
Definition: bond.hpp:340
Rate previousCouponRate(Date d=Date()) const
Previous coupon already paid at a given date.
Definition: bond.cpp:279
Natural settlementDays() const
Definition: bond.hpp:332
bool isExpired() const override
returns whether the instrument might have value greater than zero.
Definition: bond.cpp:103
void addRedemptionsToCashflows(const std::vector< Real > &redemptions=std::vector< Real >())
Definition: bond.cpp:315
Rate yield(const DayCounter &dc, Compounding comp, Frequency freq, Real accuracy=1.0e-8, Size maxEvaluations=100, Real guess=0.05, Bond::Price::Type priceType=Bond::Price::Clean) const
theoretical bond yield
Definition: bond.cpp:198
virtual Real accruedAmount(Date d=Date()) const
accrued amount at a given date
Definition: bond.cpp:267
void deepUpdate() override
Definition: bond.cpp:367
const Calendar & calendar() const
Definition: bond.hpp:336
Date startDate() const
Definition: bond.cpp:146
const Leg & cashflows() const
Definition: bond.hpp:344
Date nextCashFlowDate(Date d=Date()) const
Definition: bond.cpp:283
Real settlementValue() const
theoretical settlement value
Definition: bond.cpp:186
std::vector< Real > notionals_
Definition: bond.hpp:301
Date issueDate() const
Definition: bond.hpp:352
Real dirtyPrice() const
theoretical dirty price
Definition: bond.cpp:178
Natural settlementDays_
Definition: bond.hpp:298
Leg cashflows_
Definition: bond.hpp:302
const ext::shared_ptr< CashFlow > & redemption() const
Definition: bond.cpp:140
void setSingleRedemption(Real notional, Real redemption, const Date &date)
Definition: bond.cpp:342
Date previousCashFlowDate(Date d=Date()) const
Definition: bond.cpp:287
Date issueDate_
Definition: bond.hpp:305
Date maturityDate() const
Definition: bond.cpp:150
bool isTradable(Date d=Date()) const
Definition: bond.cpp:157
const Leg & redemptions() const
Definition: bond.hpp:348
void setupExpired() const override
Definition: bond.cpp:291
void fetchResults(const PricingEngine::results *) const override
Definition: bond.cpp:305
std::vector< Date > notionalSchedule_
Definition: bond.hpp:300
Date maturityDate_
Definition: bond.hpp:305
void calculateNotionalsFromCashflows()
Definition: bond.cpp:374
virtual Real notional(Date d=Date()) const
Definition: bond.cpp:112
Date settlementDate(Date d=Date()) const
Definition: bond.cpp:161
virtual Rate nextCouponRate(Date d=Date()) const
Definition: bond.cpp:275
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
template base class for option pricing engines
Abstract instrument class.
Definition: instrument.hpp:44
template class providing a null value for a given type.
Definition: null.hpp:76
Compounding enumeration.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
Frequency
Frequency of events.
Definition: frequency.hpp:37
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
Abstract instrument class.
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