Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
bondindex.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file qle/indexes/bondindex.hpp
20 \brief bond index class representing historical and forward bond prices
21 \ingroup indexes
22*/
23
24#pragma once
25
26#include <ql/handle.hpp>
27#include <ql/index.hpp>
28#include <ql/indexes/interestrateindex.hpp>
29#include <ql/instruments/bond.hpp>
30#include <ql/termstructures/defaulttermstructure.hpp>
31#include <ql/termstructures/yieldtermstructure.hpp>
32#include <ql/time/calendar.hpp>
33#include <ql/time/calendars/nullcalendar.hpp>
34#include <ql/time/daycounters/simpledaycounter.hpp>
35#include <ql/cashflows/floatingratecoupon.hpp>
36#include <ql/cashflows/couponpricer.hpp>
37
38namespace QuantExt {
39
40using namespace QuantLib;
41
42class DiscountingRiskyBondEngine;
43
44//! Bond Index
45/*! \ingroup indexes */
46class BondIndex : public Index, public Observer {
47public:
49
50 /*! The values that this index return are of the form
51
52 - 1.02 meaning 102% price clean or dirty (depending on the flag dirty in the ctor) i.e.
53 the absolute bond clean or dirty NPV is divided by the current notional at the
54 fixing date
55 - 10020 meaning an absolute NPV in terms of the current notional of the underlying bond
56 at the fixing date, again clean or dirty depending on the flag dirty in the ctor,
57 here the notional would be 10000
58
59 The first form is returned if the flag relative in the ctor is set to true, the second
60 if this flag is set to false.
61
62 The fixing projection (fixingDate > today) assumes that the given bond is vanilla,
63 i.e. its present value can be calculated by discounting the cashflows retrieved
64 with Bond::cashflows().
65
66 If the bond has a pricing engine attached and today's fixing is projected, the
67 pricing engine's result will be used. Otherwise today's fixing will be calculated
68 as projected fixings for dates > today, i.e. by simply discounting the bond's
69 cashflows.
70
71 If no bond is given, only historical fixings are returned by the index and only the
72 clean price mode and relative price mode are supported respectively. Otherwise
73 an exception is thrown whenever a fixing is requested from the index.
74
75 To compute projected fixings for dates > today, a discountCurve is required. The
76 other quotes and curves are optional and default as follows:
77 - defaultCurve: defaults to zero hazard spread
78 - recoveryRate: defaults to zero
79 - securitySpread: defaults to zero
80 - incomCurve: defaults to the curve build as discountCurve + securitySpread
81
82 If conditionalOnSurvival is set to true, a projected fixing will be conditional
83 on survival until the associated bond settlement date, otherwise it will include
84 the default probability between today and the settlement date.
85
86 If priceQuoteMethod = CurrencyPerUnit, a fixing in the fixing history will be divided
87 by priceQuoteBaseValue before returning it.
88 */
89 BondIndex(const std::string& securityName, const bool dirty = false, const bool relative = true,
90 const Calendar& fixingCalendar = NullCalendar(), const QuantLib::ext::shared_ptr<QuantLib::Bond>& bond = nullptr,
91 const Handle<YieldTermStructure>& discountCurve = Handle<YieldTermStructure>(),
92 const Handle<DefaultProbabilityTermStructure>& defaultCurve = Handle<DefaultProbabilityTermStructure>(),
93 const Handle<Quote>& recoveryRate = Handle<Quote>(),
94 const Handle<Quote>& securitySpread = Handle<Quote>(),
95 const Handle<YieldTermStructure>& incomeCurve = Handle<YieldTermStructure>(),
96 const bool conditionalOnSurvival = true, const Date& issueDate = Date(),
98 const double priceQuoteBaseValue = 1.0, const bool isInflationLinked = false,
99 const double bidAskAdjustment = 0.0, const bool bondIssueDateFallback = false);
100
101 //! \name Index interface
102 //@{
103 std::string name() const override;
104 Calendar fixingCalendar() const override;
105 bool isValidFixingDate(const Date& fixingDate) const override;
106 Real fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override;
107 //@}
108
109 //! \name Observer interface
110 //@{
111 void update() override;
112 //@}
113
114 //! \name Fixing calculations
115 //@{
116 virtual Rate forecastFixing(const Date& fixingDate) const;
117 Rate pastFixing(const Date& fixingDate) const;
118 //@}
119
120 //! \name Inspectors
121 //@{
122 const std::string& securityName() const { return securityName_; }
123 bool dirty() const { return dirty_; }
124 bool relative() const { return relative_; }
125 QuantLib::ext::shared_ptr<QuantLib::Bond> bond() const { return bond_; }
126 Handle<YieldTermStructure> discountCurve() const { return discountCurve_; }
127 Handle<DefaultProbabilityTermStructure> defaultCurve() const { return defaultCurve_; }
128 Handle<Quote> recoveryRate() const { return recoveryRate_; }
129 Handle<Quote> securitySpread() const { return securitySpread_; }
130 Handle<YieldTermStructure> incomeCurve() const { return incomeCurve_; }
132 Date issueDate() const { return issueDate_; }
134 double priceQuoteBaseValue() const { return priceQuoteBaseValue_; }
135 //@}
136
137protected:
138 std::string securityName_;
141 QuantLib::ext::shared_ptr<QuantLib::Bond> bond_;
142 Handle<YieldTermStructure> discountCurve_;
143 Handle<DefaultProbabilityTermStructure> defaultCurve_;
144 Handle<Quote> recoveryRate_;
145 Handle<Quote> securitySpread_;
146 Handle<YieldTermStructure> incomeCurve_;
153 QuantLib::ext::shared_ptr<DiscountingRiskyBondEngine> vanillaBondEngine_;
155};
156
157//! Bond Futures Index
158/*! \ingroup indexes */
160public:
162 const QuantLib::Date& expiryDate, const std::string& securityName, const bool dirty = false,
163 const bool relative = true, const Calendar& fixingCalendar = NullCalendar(),
164 const QuantLib::ext::shared_ptr<QuantLib::Bond>& bond = nullptr,
165 const Handle<YieldTermStructure>& discountCurve = Handle<YieldTermStructure>(),
166 const Handle<DefaultProbabilityTermStructure>& defaultCurve = Handle<DefaultProbabilityTermStructure>(),
167 const Handle<Quote>& recoveryRate = Handle<Quote>(), const Handle<Quote>& securitySpread = Handle<Quote>(),
168 const Handle<YieldTermStructure>& incomeCurve = Handle<YieldTermStructure>(),
169 const bool conditionalOnSurvival = true, const Date& issueDate = Date(),
171 const double priceQuoteBaseValue = 1.0);
172
173 //! \name Index interface
174 //@{
175 std::string name() const override;
176 //@}
177
178 //! \name Fixing calculations
179 //@{
180 Rate forecastFixing(const Date& fixingDate) const override;
181 //@}
182
183 //! \name Inspectors
184 //@{
185 const QuantLib::Date& expiryDate() const { return expiryDate_; }
186 //@}
187
188private:
190 mutable std::string name_;
191};
192
193//! Constant Maturity Bond Index
194/*!
195 The purpose of this object is converting generic bond prices into yields
196 and to use the yields as fixings in the context of floating rate coupons
197 \ingroup indexes
198*/
199class ConstantMaturityBondIndex : public InterestRateIndex {
200public:
201 ConstantMaturityBondIndex(// index interface
202 const std::string& familyName,
203 const Period& tenor,
204 Natural settlementDays = 0,
205 Currency currency = Currency(),
206 Calendar fixingCalendar = NullCalendar(),
207 DayCounter dayCounter = SimpleDayCounter(),
208 // maturity data calculation
209 BusinessDayConvention convention = Following,
210 bool endOfMonth = false,
211 // underlying
212 ext::shared_ptr<Bond> bond = nullptr,
213 // price to yield conversion
214 Compounding compounding = Compounded,
215 Frequency frequency = Annual,
216 Real accuracy = 1.0e-8,
217 Size maxEvaluations = 100,
218 Real guess = 0.05,
219 QuantLib::Bond::Price::Type priceType = QuantLib::Bond::Price::Clean)
220 : InterestRateIndex(familyName, tenor, settlementDays, currency, fixingCalendar, dayCounter),
222 bond_(bond), compounding_(compounding), frequency_(frequency),
223 accuracy_(accuracy), maxEvaluations_(maxEvaluations), guess_(guess), priceType_(priceType) {
224 std::ostringstream o;
225 o << familyName_ << "-" << tenor_;
226 name_ = o.str();
227 if (bond_) {
228 registerWith(bond_);
229 bondStartDate_ = bond->startDate();
230 }
231 }
232
233 //! \name InterestRateIndex interface
234 //@{
235 Date maturityDate(const Date& valueDate) const override;
236 //@}
237
238 //! \name Fixing calculations
239 //@{
240 Rate forecastFixing(const Date& fixingDate) const override;
241 //@}
242
243 //! \name Inspectors
244 //@{
245 BusinessDayConvention convention() const { return convention_; }
246 bool endOfMonth() const { return endOfMonth_; }
247 const ext::shared_ptr<Bond>& bond() const { return bond_; }
248 //@}
249
250private:
251 BusinessDayConvention convention_;
253 ext::shared_ptr<Bond> bond_;
254 Compounding compounding_;
255 Frequency frequency_;
258 Real guess_;
259 Bond::Price::Type priceType_;
261 std::string securityId_;
262 std::string creditCurveId_;
263};
264
265
266} // namespace QuantExt
Bond Futures Index.
Definition: bondindex.hpp:159
const QuantLib::Date & expiryDate() const
Definition: bondindex.hpp:185
std::string name() const override
Definition: bondindex.cpp:170
Rate forecastFixing(const Date &fixingDate) const override
Definition: bondindex.cpp:182
bool dirty() const
Definition: bondindex.hpp:123
const std::string & securityName() const
Definition: bondindex.hpp:122
virtual Rate forecastFixing(const Date &fixingDate) const
Definition: bondindex.cpp:92
Handle< YieldTermStructure > discountCurve_
Definition: bondindex.hpp:142
Handle< Quote > recoveryRate() const
Definition: bondindex.hpp:128
double priceQuoteBaseValue() const
Definition: bondindex.hpp:134
double priceQuoteBaseValue_
Definition: bondindex.hpp:150
Handle< YieldTermStructure > incomeCurve_
Definition: bondindex.hpp:146
void update() override
Definition: bondindex.cpp:63
std::string securityName_
Definition: bondindex.hpp:138
Calendar fixingCalendar() const override
Definition: bondindex.cpp:59
Handle< YieldTermStructure > discountCurve() const
Definition: bondindex.hpp:126
bool conditionalOnSurvival() const
Definition: bondindex.hpp:131
Handle< DefaultProbabilityTermStructure > defaultCurve() const
Definition: bondindex.hpp:127
Handle< YieldTermStructure > incomeCurve() const
Definition: bondindex.hpp:130
Date issueDate() const
Definition: bondindex.hpp:132
PriceQuoteMethod priceQuoteMethod_
Definition: bondindex.hpp:149
Rate pastFixing(const Date &fixingDate) const
Definition: bondindex.cpp:134
std::string name() const override
Definition: bondindex.cpp:57
Handle< Quote > securitySpread_
Definition: bondindex.hpp:145
Handle< Quote > securitySpread() const
Definition: bondindex.hpp:129
bool relative() const
Definition: bondindex.hpp:124
QuantLib::ext::shared_ptr< QuantLib::Bond > bond() const
Definition: bondindex.hpp:125
Handle< DefaultProbabilityTermStructure > defaultCurve_
Definition: bondindex.hpp:143
bool isValidFixingDate(const Date &fixingDate) const override
Definition: bondindex.cpp:61
QuantLib::ext::shared_ptr< DiscountingRiskyBondEngine > vanillaBondEngine_
Definition: bondindex.hpp:153
PriceQuoteMethod priceQuoteMethod() const
Definition: bondindex.hpp:133
QuantLib::ext::shared_ptr< QuantLib::Bond > bond_
Definition: bondindex.hpp:141
Calendar fixingCalendar_
Definition: bondindex.hpp:140
Handle< Quote > recoveryRate_
Definition: bondindex.hpp:144
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
Definition: bondindex.cpp:65
Constant Maturity Bond Index.
Definition: bondindex.hpp:199
const ext::shared_ptr< Bond > & bond() const
Definition: bondindex.hpp:247
ConstantMaturityBondIndex(const std::string &familyName, const Period &tenor, Natural settlementDays=0, Currency currency=Currency(), Calendar fixingCalendar=NullCalendar(), DayCounter dayCounter=SimpleDayCounter(), BusinessDayConvention convention=Following, bool endOfMonth=false, ext::shared_ptr< Bond > bond=nullptr, Compounding compounding=Compounded, Frequency frequency=Annual, Real accuracy=1.0e-8, Size maxEvaluations=100, Real guess=0.05, QuantLib::Bond::Price::Type priceType=QuantLib::Bond::Price::Clean)
Definition: bondindex.hpp:201
Date maturityDate(const Date &valueDate) const override
Definition: bondindex.cpp:207
BusinessDayConvention convention_
Definition: bondindex.hpp:251
BusinessDayConvention convention() const
Definition: bondindex.hpp:245
ext::shared_ptr< Bond > bond_
Definition: bondindex.hpp:253
Rate forecastFixing(const Date &fixingDate) const override
Definition: bondindex.cpp:212