QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
bondfunctions.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) 2007, 2008, 2009, 2010 Ferdinando Ametrano
5 Copyright (C) 2007 Chiara Fornarola
6 Copyright (C) 2009 StatPro Italia srl
7 Copyright (C) 2009 Nathan Abbott
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23/*! \file bondfunctions.hpp
24 \brief bond functions
25*/
26
27#ifndef quantlib_bond_functions_hpp
28#define quantlib_bond_functions_hpp
29
32#include <ql/cashflow.hpp>
33#include <ql/interestrate.hpp>
35#include <ql/shared_ptr.hpp>
36
37namespace QuantLib {
38
39 // forward declarations
40 class Bond;
41 class DayCounter;
42 class YieldTermStructure;
43
44 //! Bond adapters of CashFlows functions
45 /*! See CashFlows for functions' documentation.
46
47 These adapters calls into CashFlows functions passing as input the
48 Bond cashflows, the dirty price (i.e. npv) calculated from clean
49 price, the bond settlement date (unless another date is given), zero
50 ex-dividend days, and excluding any cashflow on the settlement date.
51
52 Prices are always clean, as per market convention.
53 */
55 //! \name Date inspectors
56 //@{
57 static Date startDate(const Bond& bond);
58 static Date maturityDate(const Bond& bond);
59 static bool isTradable(const Bond& bond,
60 Date settlementDate = Date());
61 //@}
62
63 //! \name CashFlow inspectors
64 //@{
65 static Leg::const_reverse_iterator
66 previousCashFlow(const Bond& bond,
67 Date refDate = Date());
68 static Leg::const_iterator nextCashFlow(const Bond& bond,
69 Date refDate = Date());
70 static Date previousCashFlowDate(const Bond& bond,
71 Date refDate = Date());
72 static Date nextCashFlowDate(const Bond& bond,
73 Date refDate = Date());
74 static Real previousCashFlowAmount(const Bond& bond,
75 Date refDate = Date());
76 static Real nextCashFlowAmount(const Bond& bond,
77 Date refDate = Date());
78 //@}
79
80 //! \name Coupon inspectors
81 //@{
82 static Rate previousCouponRate(const Bond& bond,
83 Date settlementDate = Date());
84 static Rate nextCouponRate(const Bond& bond,
85 Date settlementDate = Date());
86 static Date accrualStartDate(const Bond& bond,
87 Date settlementDate = Date());
88 static Date accrualEndDate(const Bond& bond,
89 Date settlementDate = Date());
90 static Date referencePeriodStart(const Bond& bond,
91 Date settlementDate = Date());
92 static Date referencePeriodEnd(const Bond& bond,
93 Date settlementDate = Date());
94 static Time accrualPeriod(const Bond& bond,
95 Date settlementDate = Date());
96 static Date::serial_type accrualDays(const Bond& bond,
97 Date settlementDate = Date());
98 static Time accruedPeriod(const Bond& bond,
99 Date settlementDate = Date());
100 static Date::serial_type accruedDays(const Bond& bond,
101 Date settlementDate = Date());
102 static Real accruedAmount(const Bond& bond,
103 Date settlementDate = Date());
104 //@}
105
106 //! \name YieldTermStructure functions
107 //@{
108 static Real cleanPrice(const Bond& bond,
109 const YieldTermStructure& discountCurve,
110 Date settlementDate = Date());
111 static Real dirtyPrice(const Bond& bond,
112 const YieldTermStructure& discountCurve,
113 Date settlementDate = Date());
114 static Real bps(const Bond& bond,
115 const YieldTermStructure& discountCurve,
116 Date settlementDate = Date());
117
118 /*! \deprecated Use the overload taking a Bond::Price argument instead.
119 Deprecated in version 1.34.
120 */
121 [[deprecated("Use the overload taking a Bond::Price argument instead")]]
122 static Rate atmRate(const Bond& bond,
123 const YieldTermStructure& discountCurve,
124 Date settlementDate,
126 static Rate atmRate(const Bond& bond,
127 const YieldTermStructure& discountCurve,
128 Date settlementDate = Date(),
129 Bond::Price price = {});
130 //@}
131
132 //! \name Yield (a.k.a. Internal Rate of Return, i.e. IRR) functions
133 //@{
134 static Real cleanPrice(const Bond& bond,
135 const InterestRate& yield,
136 Date settlementDate = Date());
137 static Real cleanPrice(const Bond& bond,
138 Rate yield,
139 const DayCounter& dayCounter,
140 Compounding compounding,
141 Frequency frequency,
142 Date settlementDate = Date());
143 static Real dirtyPrice(const Bond& bond,
144 const InterestRate& yield,
145 Date settlementDate = Date());
146 static Real dirtyPrice(const Bond& bond,
147 Rate yield,
148 const DayCounter& dayCounter,
149 Compounding compounding,
150 Frequency frequency,
151 Date settlementDate = Date());
152 static Real bps(const Bond& bond,
153 const InterestRate& yield,
154 Date settlementDate = Date());
155 static Real bps(const Bond& bond,
156 Rate yield,
157 const DayCounter& dayCounter,
158 Compounding compounding,
159 Frequency frequency,
160 Date settlementDate = Date());
161 /*! \deprecated Use the overload taking a Bond::Price argument instead.
162 Deprecated in version 1.34.
163 */
164 [[deprecated("Use the overload taking a Bond::Price argument instead")]]
165 static Rate yield(const Bond& bond,
166 Real price,
167 const DayCounter& dayCounter,
168 Compounding compounding,
169 Frequency frequency,
170 Date settlementDate = Date(),
171 Real accuracy = 1.0e-10,
172 Size maxIterations = 100,
173 Rate guess = 0.05,
175 static Rate yield(const Bond& bond,
176 Bond::Price price,
177 const DayCounter& dayCounter,
178 Compounding compounding,
179 Frequency frequency,
180 Date settlementDate = Date(),
181 Real accuracy = 1.0e-10,
182 Size maxIterations = 100,
183 Rate guess = 0.05);
184 /*! \deprecated Use the overload taking a Bond::Price argument instead.
185 Deprecated in version 1.34.
186 */
187 template <typename Solver>
188 [[deprecated("Use the overload taking a Bond::Price argument instead")]]
189 static Rate yield(const Solver& solver,
190 const Bond& bond,
191 Real price,
192 const DayCounter& dayCounter,
193 Compounding compounding,
194 Frequency frequency,
195 Date settlementDate = Date(),
196 Real accuracy = 1.0e-10,
197 Rate guess = 0.05,
199 return yield(solver, bond, {price, priceType}, dayCounter, compounding, frequency,
200 settlementDate, accuracy, guess);
201 }
202 template <typename Solver>
203 static Rate yield(const Solver& solver,
204 const Bond& bond,
205 Bond::Price price,
206 const DayCounter& dayCounter,
207 Compounding compounding,
208 Frequency frequency,
209 Date settlementDate = Date(),
210 Real accuracy = 1.0e-10,
211 Rate guess = 0.05) {
212 if (settlementDate == Date())
213 settlementDate = bond.settlementDate();
214
215 QL_REQUIRE(BondFunctions::isTradable(bond, settlementDate),
216 "non tradable at " << settlementDate <<
217 " (maturity being " << bond.maturityDate() << ")");
218
219 Real amount = price.amount();
220
221 if (price.type() == Bond::Price::Clean)
222 amount += bond.accruedAmount(settlementDate);
223
224 amount /= 100.0 / bond.notional(settlementDate);
225
226 return CashFlows::yield<Solver>(solver, bond.cashflows(), amount, dayCounter,
227 compounding,
228 frequency, false, settlementDate,
229 settlementDate, accuracy, guess);
230 }
231 static Time duration(const Bond& bond,
232 const InterestRate& yield,
234 Date settlementDate = Date() );
235 static Time duration(const Bond& bond,
236 Rate yield,
237 const DayCounter& dayCounter,
238 Compounding compounding,
239 Frequency frequency,
241 Date settlementDate = Date() );
242 static Real convexity(const Bond& bond,
243 const InterestRate& yield,
244 Date settlementDate = Date());
245 static Real convexity(const Bond& bond,
246 Rate yield,
247 const DayCounter& dayCounter,
248 Compounding compounding,
249 Frequency frequency,
250 Date settlementDate = Date());
251 static Real basisPointValue(const Bond& bond,
252 const InterestRate& yield,
253 Date settlementDate = Date());
254 static Real basisPointValue(const Bond& bond,
255 Rate yield,
256 const DayCounter& dayCounter,
257 Compounding compounding,
258 Frequency frequency,
259 Date settlementDate = Date());
260 static Real yieldValueBasisPoint(const Bond& bond,
261 const InterestRate& yield,
262 Date settlementDate = Date());
263 static Real yieldValueBasisPoint(const Bond& bond,
264 Rate yield,
265 const DayCounter& dayCounter,
266 Compounding compounding,
267 Frequency frequency,
268 Date settlementDate = Date());
269 //@}
270
271 //! \name Z-spread functions
272 //@{
273 static Real cleanPrice(const Bond& bond,
274 const ext::shared_ptr<YieldTermStructure>& discount,
276 const DayCounter& dayCounter,
277 Compounding compounding,
278 Frequency frequency,
279 Date settlementDate = Date());
280 static Real dirtyPrice(const Bond& bond,
281 const ext::shared_ptr<YieldTermStructure>& discount,
283 const DayCounter& dayCounter,
284 Compounding compounding,
285 Frequency frequency,
286 Date settlementDate = Date());
287 /*! \deprecated Use the overload taking a Bond::Price argument instead.
288 Deprecated in version 1.34.
289 */
290 [[deprecated("Use the overload taking a Bond::Price argument instead")]]
291 static Spread zSpread(const Bond& bond,
293 const ext::shared_ptr<YieldTermStructure>&,
294 const DayCounter& dayCounter,
295 Compounding compounding,
296 Frequency frequency,
297 Date settlementDate = Date(),
298 Real accuracy = 1.0e-10,
299 Size maxIterations = 100,
300 Rate guess = 0.0);
301 static Spread zSpread(const Bond& bond,
302 Bond::Price price,
303 const ext::shared_ptr<YieldTermStructure>&,
304 const DayCounter& dayCounter,
305 Compounding compounding,
306 Frequency frequency,
307 Date settlementDate = Date(),
308 Real accuracy = 1.0e-10,
309 Size maxIterations = 100,
310 Rate guess = 0.0);
311 //@}
312
313 };
314
315}
316
317#endif
concrete bond class
Base class for cash flows.
Cash-flow analysis functions.
Bond price information.
Definition: bond.hpp:62
Type type() const
Definition: bond.hpp:71
Real amount() const
Definition: bond.hpp:67
Base bond class.
Definition: bond.hpp:59
virtual Real accruedAmount(Date d=Date()) const
accrued amount at a given date
Definition: bond.cpp:267
const Leg & cashflows() const
Definition: bond.hpp:344
Date maturityDate() const
Definition: bond.cpp:150
virtual Real notional(Date d=Date()) const
Definition: bond.cpp:112
Date settlementDate(Date d=Date()) const
Definition: bond.cpp:161
Concrete date class.
Definition: date.hpp:125
std::int_fast32_t serial_type
serial number type
Definition: date.hpp:128
day counter class
Definition: daycounter.hpp:44
Concrete interest rate class.
Interest-rate term structure.
Duration type enumeration.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Frequency
Frequency of events.
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Spread
spreads on interest rates
Definition: types.hpp:74
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Instrument rate class.
Definition: any.hpp:35
Compounding
Interest rate coumpounding rule.
Definition: compounding.hpp:32
Maps shared_ptr to either the boost or std implementation.
Bond adapters of CashFlows functions.
static Real yieldValueBasisPoint(const Bond &bond, const InterestRate &yield, Date settlementDate=Date())
static Rate yield(const Bond &bond, Real price, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.05, Bond::Price::Type priceType=Bond::Price::Clean)
static Leg::const_iterator nextCashFlow(const Bond &bond, Date refDate=Date())
static Rate yield(const Solver &solver, const Bond &bond, Bond::Price price, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Rate guess=0.05)
static Rate yield(const Solver &solver, const Bond &bond, Real price, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Rate guess=0.05, Bond::Price::Type priceType=Bond::Price::Clean)
static Spread zSpread(const Bond &bond, Real cleanPrice, const ext::shared_ptr< YieldTermStructure > &, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.0)
static Leg::const_reverse_iterator previousCashFlow(const Bond &bond, Date refDate=Date())
static Rate nextCouponRate(const Bond &bond, Date settlementDate=Date())
static Date startDate(const Bond &bond)
static Date referencePeriodStart(const Bond &bond, Date settlementDate=Date())
static bool isTradable(const Bond &bond, Date settlementDate=Date())
static Real bps(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date())
static Date maturityDate(const Bond &bond)
static Date::serial_type accruedDays(const Bond &bond, Date settlementDate=Date())
static Rate atmRate(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate, Real cleanPrice)
static Real dirtyPrice(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date())
static Date previousCashFlowDate(const Bond &bond, Date refDate=Date())
static Real nextCashFlowAmount(const Bond &bond, Date refDate=Date())
static Real convexity(const Bond &bond, const InterestRate &yield, Date settlementDate=Date())
static Real basisPointValue(const Bond &bond, const InterestRate &yield, Date settlementDate=Date())
static Date::serial_type accrualDays(const Bond &bond, Date settlementDate=Date())
static Date nextCashFlowDate(const Bond &bond, Date refDate=Date())
static Date referencePeriodEnd(const Bond &bond, Date settlementDate=Date())
static Real accruedAmount(const Bond &bond, Date settlementDate=Date())
static Date accrualStartDate(const Bond &bond, Date settlementDate=Date())
static Real cleanPrice(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date())
static Rate previousCouponRate(const Bond &bond, Date settlementDate=Date())
static Date accrualEndDate(const Bond &bond, Date settlementDate=Date())
static Time duration(const Bond &bond, const InterestRate &yield, Duration::Type type=Duration::Modified, Date settlementDate=Date())
static Time accruedPeriod(const Bond &bond, Date settlementDate=Date())
static Time accrualPeriod(const Bond &bond, Date settlementDate=Date())
static Real previousCashFlowAmount(const Bond &bond, Date refDate=Date())