QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
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 static Rate atmRate(const Bond& bond,
119 const YieldTermStructure& discountCurve,
120 Date settlementDate = Date(),
121 Bond::Price price = {});
122 //@}
123
124 //! \name Yield (a.k.a. Internal Rate of Return, i.e. IRR) functions
125 //@{
126 static Real cleanPrice(const Bond& bond,
127 const InterestRate& yield,
128 Date settlementDate = Date());
129 static Real cleanPrice(const Bond& bond,
130 Rate yield,
131 const DayCounter& dayCounter,
132 Compounding compounding,
133 Frequency frequency,
134 Date settlementDate = Date());
135 static Real dirtyPrice(const Bond& bond,
136 const InterestRate& yield,
137 Date settlementDate = Date());
138 static Real dirtyPrice(const Bond& bond,
139 Rate yield,
140 const DayCounter& dayCounter,
141 Compounding compounding,
142 Frequency frequency,
143 Date settlementDate = Date());
144 static Real bps(const Bond& bond,
145 const InterestRate& yield,
146 Date settlementDate = Date());
147 static Real bps(const Bond& bond,
148 Rate yield,
149 const DayCounter& dayCounter,
150 Compounding compounding,
151 Frequency frequency,
152 Date settlementDate = Date());
153 static Rate yield(const Bond& bond,
154 Bond::Price price,
155 const DayCounter& dayCounter,
156 Compounding compounding,
157 Frequency frequency,
158 Date settlementDate = Date(),
159 Real accuracy = 1.0e-10,
160 Size maxIterations = 100,
161 Rate guess = 0.05);
162 template <typename Solver>
163 static Rate yield(const Solver& solver,
164 const Bond& bond,
165 Bond::Price price,
166 const DayCounter& dayCounter,
167 Compounding compounding,
168 Frequency frequency,
169 Date settlementDate = Date(),
170 Real accuracy = 1.0e-10,
171 Rate guess = 0.05) {
172 if (settlementDate == Date())
173 settlementDate = bond.settlementDate();
174
175 QL_REQUIRE(BondFunctions::isTradable(bond, settlementDate),
176 "non tradable at " << settlementDate <<
177 " (maturity being " << bond.maturityDate() << ")");
178
179 Real amount = price.amount();
180
181 if (price.type() == Bond::Price::Clean)
182 amount += bond.accruedAmount(settlementDate);
183
184 amount /= 100.0 / bond.notional(settlementDate);
185
186 return CashFlows::yield<Solver>(solver, bond.cashflows(), amount, dayCounter,
187 compounding,
188 frequency, false, settlementDate,
189 settlementDate, accuracy, guess);
190 }
191 static Time duration(const Bond& bond,
192 const InterestRate& yield,
194 Date settlementDate = Date() );
195 static Time duration(const Bond& bond,
196 Rate yield,
197 const DayCounter& dayCounter,
198 Compounding compounding,
199 Frequency frequency,
201 Date settlementDate = Date() );
202 static Real convexity(const Bond& bond,
203 const InterestRate& yield,
204 Date settlementDate = Date());
205 static Real convexity(const Bond& bond,
206 Rate yield,
207 const DayCounter& dayCounter,
208 Compounding compounding,
209 Frequency frequency,
210 Date settlementDate = Date());
211 static Real basisPointValue(const Bond& bond,
212 const InterestRate& yield,
213 Date settlementDate = Date());
214 static Real basisPointValue(const Bond& bond,
215 Rate yield,
216 const DayCounter& dayCounter,
217 Compounding compounding,
218 Frequency frequency,
219 Date settlementDate = Date());
220 static Real yieldValueBasisPoint(const Bond& bond,
221 const InterestRate& yield,
222 Date settlementDate = Date());
223 static Real yieldValueBasisPoint(const Bond& bond,
224 Rate yield,
225 const DayCounter& dayCounter,
226 Compounding compounding,
227 Frequency frequency,
228 Date settlementDate = Date());
229 //@}
230
231 //! \name Z-spread functions
232 //@{
233 static Real cleanPrice(const Bond& bond,
234 const ext::shared_ptr<YieldTermStructure>& discount,
236 const DayCounter& dayCounter,
237 Compounding compounding,
238 Frequency frequency,
239 Date settlementDate = Date());
240 static Real dirtyPrice(const Bond& bond,
241 const ext::shared_ptr<YieldTermStructure>& discount,
243 const DayCounter& dayCounter,
244 Compounding compounding,
245 Frequency frequency,
246 Date settlementDate = Date());
247 static Spread zSpread(const Bond& bond,
248 Bond::Price price,
249 const ext::shared_ptr<YieldTermStructure>&,
250 const DayCounter& dayCounter,
251 Compounding compounding,
252 Frequency frequency,
253 Date settlementDate = Date(),
254 Real accuracy = 1.0e-10,
255 Size maxIterations = 100,
256 Rate guess = 0.0);
257 //@}
258
259 };
260
261}
262
263#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:256
const Leg & cashflows() const
Definition: bond.hpp:330
Date maturityDate() const
Definition: bond.cpp:151
virtual Real notional(Date d=Date()) const
Definition: bond.cpp:113
Date settlementDate(Date d=Date()) const
Definition: bond.cpp:162
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:37
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 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 Leg::const_reverse_iterator previousCashFlow(const Bond &bond, Date refDate=Date())
static Rate atmRate(const Bond &bond, const YieldTermStructure &discountCurve, Date settlementDate=Date(), Bond::Price price={})
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 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 Spread zSpread(const Bond &bond, Bond::Price price, 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 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 Rate yield(const Bond &bond, Bond::Price price, const DayCounter &dayCounter, Compounding compounding, Frequency frequency, Date settlementDate=Date(), Real accuracy=1.0e-10, Size maxIterations=100, Rate guess=0.05)
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())