• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 14910176578

08 May 2025 03:28PM UTC coverage: 73.315% (+0.02%) from 73.3%
14910176578

Pull #2195

github

web-flow
Merge 3a61f499c into 5d972fb7b
Pull Request #2195: Added `Handle<Quote>` for spread in `OISRateHelper`

32 of 33 new or added lines in 2 files covered. (96.97%)

277 existing lines in 25 files now uncovered.

56277 of 76761 relevant lines covered (73.31%)

8687029.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/ql/instruments/bond.hpp
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

40
namespace 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 };
65
            Price() : amount_(Null<Real>()), type_(Bond::Price::Clean) {}
66
            Price(Real amount, Type type) : amount_(amount), type_(type) {}
6,545✔
67
            Real amount() const {
12,390✔
68
                QL_REQUIRE(amount_ != Null<Real>(), "no amount given");
12,390✔
69
                return amount_;
12,390✔
70
            }
71
            Type type() const { return type_; }
12,386✔
72
            bool isValid() const { return amount_ != Null<Real>(); }
540✔
73
          private:
74
            Real amount_;
75
            Type type_;
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
        */
83
        Bond(Natural settlementDays,
84
             Calendar calendar,
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
        */
93
        Bond(Natural settlementDays,
94
             Calendar calendar,
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. */
180
        Real cleanPrice(Rate yield,
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. */
188
        Real dirtyPrice(Rate yield,
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. */
196
        Real settlementValue(Real cleanPrice) const;
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,
204
                   Date settlementDate = Date(),
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;
234
        Date previousCashFlowDate(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
        */
263
        void setSingleRedemption(Real notional,
264
                                 Real redemption,
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
        */
272
        void setSingleRedemption(Real notional,
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
        */
282
        void calculateNotionalsFromCashflows();
283

284
        Natural settlementDays_;
285
        Calendar calendar_;
286
        std::vector<Date> notionalSchedule_;
287
        std::vector<Real> notionals_;
288
        Leg cashflows_; // all cashflows
289
        Leg redemptions_; // the redemptions
290

291
        Date maturityDate_, issueDate_;
292
        mutable Real settlementValue_;
293
    };
294

295
    class Bond::arguments : public PricingEngine::arguments {
32✔
296
      public:
297
        Date settlementDate;
298
        Leg cashflows;
299
        Calendar calendar;
300
        void validate() const override;
301
    };
302

303
    class Bond::results : public Instrument::results {
304
      public:
305
        Real settlementValue;
UNCOV
306
        void reset() override {
×
307
            settlementValue = Null<Real>();
868,149✔
308
            Instrument::results::reset();
868,149✔
UNCOV
309
        }
×
310
    };
311

312
    class Bond::engine : public GenericEngine<Bond::arguments,
259✔
313
                                              Bond::results> {};
314

315

316
    // inline definitions
317

318
    inline Natural Bond::settlementDays() const {
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_;
904,216✔
332
    }
333

334
    inline const Leg& Bond::redemptions() const {
335
        return redemptions_;
336
    }
337

338
    inline Date Bond::issueDate() const {
339
        return issueDate_;
10✔
340
    }
341

342
}
343

344
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc