Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
bond.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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 portfolio/bond.hpp
20 \brief Bond trade data model and serialization
21 \ingroup tradedata
22 */
23#pragma once
24
29
30#include <utility>
31
32namespace ore {
33namespace data {
34
35/*! Serializable BondData
36 FIXME zero bonds are only supported via the third constructor, but not in fromXML()
37*/
38class BondData : public XMLSerializable {
39public:
40 //! Default Contructor
42 faceAmount_(0.0), zeroBond_(false), bondNotional_(1.0), isPayer_(false), isInflationLinked_(false) {}
43
44 //! Constructor to set up a bond from reference data
45 BondData(string securityId, Real bondNotional, bool hasCreditRisk = true)
48
49 //! Constructor for coupon bonds
51 string calendar, string issueDate, LegData& coupons, bool hasCreditRisk = true)
55 zeroBond_(false), bondNotional_(1.0) {
56 initialise();
57 }
58
59 //! Constructor for coupon bonds with multiple phases (represented as legs)
61 string calendar, string issueDate, const std::vector<LegData>& coupons, bool hasCreditRisk = true)
65 bondNotional_(1.0) {
66 initialise();
67 }
68
69 //! Constructor for zero bonds, FIXME these can only be set up via this ctor, not via fromXML()
71 string calendar, Real faceAmount, string maturityDate, string currency, string issueDate,
72 bool hasCreditRisk = true)
77 initialise();
78 }
79
80 //! Inspectors
81 const string& issuerId() const { return issuerId_; }
82 const string& creditCurveId() const { return creditCurveId_; }
83 const string& creditGroup() const { return creditGroup_; }
84 const string& securityId() const { return securityId_; }
85 const string& referenceCurveId() const { return referenceCurveId_; }
86 const string& incomeCurveId() const { return incomeCurveId_; }
87 const string& volatilityCurveId() const { return volatilityCurveId_; }
88 const string& settlementDays() const { return settlementDays_; }
89 const string& calendar() const { return calendar_; }
90 const string& issueDate() const { return issueDate_; }
92 Real priceQuoteBaseValue() const;
93 const std::vector<LegData>& coupons() const { return coupons_; }
94 const string& currency() const { return currency_; }
95 Real bondNotional() const { return bondNotional_; }
96 bool hasCreditRisk() const { return hasCreditRisk_; }
97 bool isPayer() const { return isPayer_; }
98 bool zeroBond() const { return zeroBond_; }
99 bool isInflationLinked() const { return isInflationLinked_; }
100 // only used for zero bonds
101 Real faceAmount() const { return faceAmount_; }
102 const string& maturityDate() const { return maturityDate_; }
103 const string& subType() const { return subType_; }
104
105 //! XMLSerializable interface
106 virtual void fromXML(XMLNode* node) override;
107 virtual XMLNode* toXML(XMLDocument& doc) const override;
108
109 //! populate data from reference datum and check data for completeness
110 void populateFromBondReferenceData(const QuantLib::ext::shared_ptr<BondReferenceDatum>& referenceDatum,
111 const std::string& startDate = "", const std::string& endDate = "");
112
113 //! look up reference datum in ref data manager and populate, check data for completeness
114 void populateFromBondReferenceData(const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData,
115 const std::string& startDate = "", const std::string& endDate = "");
116
117 //! check data for completeness
118 void checkData() const;
119
120 //! return isda sub type "Single Name", "Index" or throw if sub type can not be mapped
121 std::string isdaBaseProduct() const;
122
123private:
124 void initialise();
125 string issuerId_;
130 string incomeCurveId_; // only used for bond derivatives
131 string volatilityCurveId_; // only used for bond derivatives
133 string calendar_;
137 std::vector<LegData> coupons_;
139 Real faceAmount_; // only used for zero bonds
140 string maturityDate_; // only used for for zero bonds
141 string currency_;
146 string subType_;
147};
148
149//! Serializable Bond
150/*!
151\ingroup tradedata
152*/
153class Bond : public Trade {
154public:
155 //! Default Constructor
156 explicit Bond() : Trade("Bond") {}
157
158 //! Constructor taking an envelope and bond data
159 Bond(Envelope env, const BondData& bondData)
160 : Trade("Bond", env), originalBondData_(bondData), bondData_(bondData) {}
161
162 //! Trade interface
163 virtual void build(const QuantLib::ext::shared_ptr<EngineFactory>&) override;
164
165 //! inspectors
166 const BondData& bondData() const { return bondData_; }
167
168 //! Add underlying Bond names
169 std::map<AssetClass, std::set<std::string>>
170 underlyingIndices(const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceDataManager = nullptr) const override;
171
172 //! XMLSerializable interface
173 virtual void fromXML(XMLNode* node) override;
174 virtual XMLNode* toXML(XMLDocument& doc) const override;
175
176private:
178};
179
180//! Bond Factory that builds bonds from reference data
181
183 struct Result {
184 std::string builderLabel;
185 QuantLib::ext::shared_ptr<QuantLib::Bond> bond;
186 QuantLib::ext::shared_ptr<QuantExt::ModelBuilder> modelBuilder; // might be nullptr
187 bool isInflationLinked = false;
188 bool hasCreditRisk = true;
189 std::string currency;
190 std::string creditCurveId;
191 std::string securityId;
192 std::string creditGroup;
193 QuantExt::BondIndex::PriceQuoteMethod priceQuoteMethod = QuantExt::BondIndex::PriceQuoteMethod::PercentageOfPar;
195
196 double inflationFactor() const;
197 };
198 virtual ~BondBuilder() {}
199 virtual Result build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory,
200 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData,
201 const std::string& securityId) const = 0;
202};
203
204class BondFactory : public QuantLib::Singleton<BondFactory, std::integral_constant<bool, true>> {
205 map<std::string, QuantLib::ext::shared_ptr<BondBuilder>> builders_;
206 mutable boost::shared_mutex mutex_;
207
208public:
209 BondBuilder::Result build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory,
210 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData,
211 const std::string& securityId) const;
212 void addBuilder(const std::string& referenceDataType, const QuantLib::ext::shared_ptr<BondBuilder>& builder,
213 const bool allowOverwrite = false);
214};
215
217 virtual Result build(const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory,
218 const QuantLib::ext::shared_ptr<ReferenceDataManager>& referenceData,
219 const std::string& securityId) const override;
220};
221
222} // namespace data
223} // namespace ore
const string & securityId() const
Definition: bond.hpp:84
std::vector< LegData > coupons_
Definition: bond.hpp:137
std::string isdaBaseProduct() const
return isda sub type "Single Name", "Index" or throw if sub type can not be mapped
Definition: bond.cpp:213
bool hasCreditRisk() const
Definition: bond.hpp:96
bool zeroBond() const
Definition: bond.hpp:98
const string & currency() const
Definition: bond.hpp:94
Real faceAmount() const
Definition: bond.hpp:101
const string & subType() const
Definition: bond.hpp:103
string volatilityCurveId_
Definition: bond.hpp:131
void initialise()
Definition: bond.cpp:130
string priceQuoteBaseValue_
Definition: bond.hpp:136
BondData(string issuerId, string creditCurveId, string securityId, string referenceCurveId, string settlementDays, string calendar, string issueDate, LegData &coupons, bool hasCreditRisk=true)
Constructor for coupon bonds.
Definition: bond.hpp:50
BondData(string securityId, Real bondNotional, bool hasCreditRisk=true)
Constructor to set up a bond from reference data.
Definition: bond.hpp:45
Real priceQuoteBaseValue() const
Definition: bond.cpp:50
const string & volatilityCurveId() const
Definition: bond.hpp:87
const std::vector< LegData > & coupons() const
Definition: bond.hpp:93
void populateFromBondReferenceData(const QuantLib::ext::shared_ptr< BondReferenceDatum > &referenceDatum, const std::string &startDate="", const std::string &endDate="")
populate data from reference datum and check data for completeness
Definition: bond.cpp:175
const string & issuerId() const
Inspectors.
Definition: bond.hpp:81
bool isPayer() const
Definition: bond.hpp:97
const string & creditGroup() const
Definition: bond.hpp:83
string creditCurveId_
Definition: bond.hpp:126
const string & referenceCurveId() const
Definition: bond.hpp:85
const string & issueDate() const
Definition: bond.hpp:90
string issuerId_
Definition: bond.hpp:125
virtual void fromXML(XMLNode *node) override
XMLSerializable interface.
Definition: bond.cpp:59
string issueDate_
Definition: bond.hpp:134
string settlementDays_
Definition: bond.hpp:132
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: bond.cpp:95
const string & settlementDays() const
Definition: bond.hpp:88
string calendar_
Definition: bond.hpp:133
BondData(string issuerId, string creditCurveId, string securityId, string referenceCurveId, string settlementDays, string calendar, string issueDate, const std::vector< LegData > &coupons, bool hasCreditRisk=true)
Constructor for coupon bonds with multiple phases (represented as legs)
Definition: bond.hpp:60
string maturityDate_
Definition: bond.hpp:140
string referenceCurveId_
Definition: bond.hpp:129
const string & calendar() const
Definition: bond.hpp:89
string creditGroup_
Definition: bond.hpp:127
BondData()
Default Contructor.
Definition: bond.hpp:41
string priceQuoteMethod_
Definition: bond.hpp:135
bool isInflationLinked() const
Definition: bond.hpp:99
void checkData() const
check data for completeness
Definition: bond.cpp:201
const string & maturityDate() const
Definition: bond.hpp:102
string currency_
Definition: bond.hpp:141
const string & incomeCurveId() const
Definition: bond.hpp:86
string securityId_
Definition: bond.hpp:128
QuantExt::BondIndex::PriceQuoteMethod priceQuoteMethod() const
Definition: bond.cpp:45
BondData(string issuerId, string creditCurveId, string securityId, string referenceCurveId, string settlementDays, string calendar, Real faceAmount, string maturityDate, string currency, string issueDate, bool hasCreditRisk=true)
Constructor for zero bonds, FIXME these can only be set up via this ctor, not via fromXML()
Definition: bond.hpp:70
string incomeCurveId_
Definition: bond.hpp:130
const string & creditCurveId() const
Definition: bond.hpp:82
bool isInflationLinked_
Definition: bond.hpp:145
Real bondNotional() const
Definition: bond.hpp:95
void addBuilder(const std::string &referenceDataType, const QuantLib::ext::shared_ptr< BondBuilder > &builder, const bool allowOverwrite=false)
Definition: bond.cpp:349
map< std::string, QuantLib::ext::shared_ptr< BondBuilder > > builders_
Definition: bond.hpp:205
boost::shared_mutex mutex_
Definition: bond.hpp:206
BondBuilder::Result build(const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory, const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceData, const std::string &securityId) const
Definition: bond.cpp:331
Serializable Bond.
Definition: bond.hpp:153
BondData bondData_
Definition: bond.hpp:177
BondData originalBondData_
Definition: bond.hpp:177
Bond(Envelope env, const BondData &bondData)
Constructor taking an envelope and bond data.
Definition: bond.hpp:159
Bond()
Default Constructor.
Definition: bond.hpp:156
const BondData & bondData() const
inspectors
Definition: bond.hpp:166
Serializable object holding generic trade data, reporting dimensions.
Definition: envelope.hpp:51
Serializable object holding leg data.
Definition: legdata.hpp:844
Trade base class.
Definition: trade.hpp:55
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
Base class for all serializable classes.
Definition: xmlutils.hpp:101
leg data model and serialization
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Reference data model and serialization.
QuantLib::ext::shared_ptr< QuantExt::ModelBuilder > modelBuilder
Definition: bond.hpp:186
QuantLib::ext::shared_ptr< QuantLib::Bond > bond
Definition: bond.hpp:185
QuantExt::BondIndex::PriceQuoteMethod priceQuoteMethod
Definition: bond.hpp:193
double inflationFactor() const
Definition: bond.cpp:315
Bond Factory that builds bonds from reference data.
Definition: bond.hpp:182
virtual ~BondBuilder()
Definition: bond.hpp:198
virtual Result build(const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory, const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceData, const std::string &securityId) const =0
virtual Result build(const QuantLib::ext::shared_ptr< EngineFactory > &engineFactory, const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceData, const std::string &securityId) const override
Definition: bond.cpp:356
base trade data model and serialization
Trade Factory.