Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/pricingengines/bond/bondfunctions.hpp
Line
Count
Source (jump to first uncovered line)
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
30
#include <ql/cashflows/cashflows.hpp>
31
#include <ql/cashflows/duration.hpp>
32
#include <ql/cashflow.hpp>
33
#include <ql/interestrate.hpp>
34
#include <ql/instruments/bond.hpp>
35
#include <ql/shared_ptr.hpp>
36
37
namespace 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
    */
54
    struct BondFunctions {
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
0
                          Rate guess = 0.05) {
172
0
            if (settlementDate == Date())
173
0
                settlementDate = bond.settlementDate();
174
175
0
            QL_REQUIRE(BondFunctions::isTradable(bond, settlementDate),
176
0
                       "non tradable at " << settlementDate <<
177
0
                       " (maturity being " << bond.maturityDate() << ")");
178
179
0
            Real amount = price.amount();
180
181
0
            if (price.type() == Bond::Price::Clean)
182
0
                amount += bond.accruedAmount(settlementDate);
183
184
0
            amount /= 100.0 / bond.notional(settlementDate);
185
186
0
            return CashFlows::yield<Solver>(solver, bond.cashflows(), amount, dayCounter,
187
0
                                            compounding,
188
0
                                            frequency, false, settlementDate,
189
0
                                            settlementDate, accuracy, guess);
190
0
        }
191
        static Time duration(const Bond& bond,
192
                             const InterestRate& yield,
193
                             Duration::Type type = Duration::Modified,
194
                             Date settlementDate = Date() );
195
        static Time duration(const Bond& bond,
196
                             Rate yield,
197
                             const DayCounter& dayCounter,
198
                             Compounding compounding,
199
                             Frequency frequency,
200
                             Duration::Type type = Duration::Modified,
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,
235
                               Spread zSpread,
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,
242
                               Spread zSpread,
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