QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
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 //! yield given a price and settlement date
199 /*! The default bond settlement is used if no date is given. */
200 Rate yield(Bond::Price price,
201 const DayCounter& dc,
202 Compounding comp,
203 Frequency freq,
205 Real accuracy = 1.0e-8,
206 Size maxEvaluations = 100,
207 Real guess = 0.05) const;
208
209 //! accrued amount at a given date
210 /*! The default bond settlement is used if no date is given. */
211 virtual Real accruedAmount(Date d = Date()) const;
212 //@}
213
214 /*! Expected next coupon: depending on (the bond and) the given date
215 the coupon can be historic, deterministic or expected in a
216 stochastic sense. When the bond settlement date is used the coupon
217 is the already-fixed not-yet-paid one.
218
219 The current bond settlement is used if no date is given.
220 */
221 virtual Rate nextCouponRate(Date d = Date()) const;
222
223 //! Previous coupon already paid at a given date
224 /*! Expected previous coupon: depending on (the bond and) the given
225 date the coupon can be historic, deterministic or expected in a
226 stochastic sense. When the bond settlement date is used the coupon
227 is the last paid one.
228
229 The current bond settlement is used if no date is given.
230 */
231 Rate previousCouponRate(Date d = Date()) const;
232
233 Date nextCashFlowDate(Date d = Date()) const;
235
236 protected:
237 void setupExpired() const override;
238 void setupArguments(PricingEngine::arguments*) const override;
239 void fetchResults(const PricingEngine::results*) const override;
240
241 /*! This method can be called by derived classes in order to
242 build redemption payments from the existing cash flows.
243 It must be called after setting up the cashflows_ vector
244 and will fill the notionalSchedule_, notionals_, and
245 redemptions_ data members.
246
247 If given, the elements of the redemptions vector will
248 multiply the amount of the redemption cash flow. The
249 elements will be taken in base 100, i.e., a redemption
250 equal to 100 does not modify the amount.
251
252 \pre The cashflows_ vector must contain at least one
253 coupon and must be sorted by date.
254 */
255 void addRedemptionsToCashflows(const std::vector<Real>& redemptions
256 = std::vector<Real>());
257
258 /*! This method can be called by derived classes in order to
259 build a bond with a single redemption payment. It will
260 fill the notionalSchedule_, notionals_, and redemptions_
261 data members.
262 */
265 const Date& date);
266
267 /*! This method can be called by derived classes in order to
268 build a bond with a single redemption payment. It will
269 fill the notionalSchedule_, notionals_, and redemptions_
270 data members.
271 */
273 const ext::shared_ptr<CashFlow>& redemption);
274
275 /*! used internally to collect notional information from the
276 coupons. It should not be called by derived classes,
277 unless they already provide redemption cash flows (in
278 which case they must set up the redemptions_ data member
279 independently). It will fill the notionalSchedule_ and
280 notionals_ data members.
281 */
283
286 std::vector<Date> notionalSchedule_;
287 std::vector<Real> notionals_;
288 Leg cashflows_; // all cashflows
289 Leg redemptions_; // the redemptions
290
293 };
294
296 public:
300 void validate() const override;
301 };
302
304 public:
306 void reset() override {
309 }
310 };
311
312 class Bond::engine : public GenericEngine<Bond::arguments,
313 Bond::results> {};
314
315
316 // inline definitions
317
319 return settlementDays_;
320 }
321
322 inline const Calendar& Bond::calendar() const {
323 return calendar_;
324 }
325
326 inline const std::vector<Real>& Bond::notionals() const {
327 return notionals_;
328 }
329
330 inline const Leg& Bond::cashflows() const {
331 return cashflows_;
332 }
333
334 inline const Leg& Bond::redemptions() const {
335 return redemptions_;
336 }
337
338 inline Date Bond::issueDate() const {
339 return issueDate_;
340 }
341
342}
343
344#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:399
void reset() override
Definition: bond.hpp:306
Base bond class.
Definition: bond.hpp:59
Calendar calendar_
Definition: bond.hpp:285
Real cleanPrice() const
theoretical clean price
Definition: bond.cpp:175
Leg redemptions_
Definition: bond.hpp:289
Real settlementValue_
Definition: bond.hpp:292
void setupArguments(PricingEngine::arguments *) const override
Definition: bond.cpp:285
const std::vector< Real > & notionals() const
Definition: bond.hpp:326
Rate previousCouponRate(Date d=Date()) const
Previous coupon already paid at a given date.
Definition: bond.cpp:268
Natural settlementDays() const
Definition: bond.hpp:318
bool isExpired() const override
returns whether the instrument might have value greater than zero.
Definition: bond.cpp:104
void addRedemptionsToCashflows(const std::vector< Real > &redemptions=std::vector< Real >())
Definition: bond.cpp:304
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:199
virtual Real accruedAmount(Date d=Date()) const
accrued amount at a given date
Definition: bond.cpp:256
void deepUpdate() override
Definition: bond.cpp:356
const Calendar & calendar() const
Definition: bond.hpp:322
Date startDate() const
Definition: bond.cpp:147
const Leg & cashflows() const
Definition: bond.hpp:330
Date nextCashFlowDate(Date d=Date()) const
Definition: bond.cpp:272
Real settlementValue() const
theoretical settlement value
Definition: bond.cpp:187
std::vector< Real > notionals_
Definition: bond.hpp:287
Date issueDate() const
Definition: bond.hpp:338
Real dirtyPrice() const
theoretical dirty price
Definition: bond.cpp:179
Natural settlementDays_
Definition: bond.hpp:284
Leg cashflows_
Definition: bond.hpp:288
const ext::shared_ptr< CashFlow > & redemption() const
Definition: bond.cpp:141
void setSingleRedemption(Real notional, Real redemption, const Date &date)
Definition: bond.cpp:331
Date previousCashFlowDate(Date d=Date()) const
Definition: bond.cpp:276
Date issueDate_
Definition: bond.hpp:291
Date maturityDate() const
Definition: bond.cpp:151
bool isTradable(Date d=Date()) const
Definition: bond.cpp:158
const Leg & redemptions() const
Definition: bond.hpp:334
void setupExpired() const override
Definition: bond.cpp:280
void fetchResults(const PricingEngine::results *) const override
Definition: bond.cpp:294
std::vector< Date > notionalSchedule_
Definition: bond.hpp:286
Date maturityDate_
Definition: bond.hpp:291
void calculateNotionalsFromCashflows()
Definition: bond.cpp:363
virtual Real notional(Date d=Date()) const
Definition: bond.cpp:113
Date settlementDate(Date d=Date()) const
Definition: bond.cpp:162
virtual Rate nextCouponRate(Date d=Date()) const
Definition: bond.cpp:264
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:59
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:37
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
std::vector< ext::shared_ptr< CashFlow > > Leg
Sequence of cash-flows.
Definition: cashflow.hpp:78