Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
basketdata.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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
20
23#include <ql/errors.hpp>
24#include <set>
25#include <string>
26#include <vector>
27
28using namespace QuantLib;
29using std::set;
30using std::string;
31using std::vector;
32
33namespace ore {
34namespace data {
35
37 : notional_(Null<Real>()), priorNotional_(Null<Real>()), weight_(Null<Real>()), priorWeight_(Null<Real>()),
38 recovery_(Null<Real>()), weightInsteadOfNotional_(false) {}
39
40BasketConstituent::BasketConstituent(const string& issuerName, const string& creditCurveId, Real notional,
41 const string& currency, const string& qualifier, Real priorNotional, Real recovery,
42 const Date& auctionDate, const Date& auctionSettlementDate,
43 const Date& defaultDate, const Date& eventDeterminationDate)
44 : issuerName_(issuerName), creditCurveId_(creditCurveId), notional_(notional), currency_(currency),
45 qualifier_(qualifier), priorNotional_(priorNotional), weight_(Null<Real>()), priorWeight_(Null<Real>()),
46 recovery_(recovery), auctionDate_(auctionDate), auctionSettlementDate_(auctionSettlementDate),
47 defaultDate_(defaultDate), eventDeterminationDate_(eventDeterminationDate), weightInsteadOfNotional_(false) {}
48
49BasketConstituent::BasketConstituent(const string& issuerName, const string& creditCurveId, Real weight,
50 const string& qualifier, Real priorWeight, Real recovery, const Date& auctionDate,
51 const Date& auctionSettlementDate, const Date& defaultDate,
52 const Date& eventDeterminationDate)
53 : issuerName_(issuerName), creditCurveId_(creditCurveId), notional_(Null<Real>()), currency_(""),
54 qualifier_(qualifier), priorNotional_(), weight_(weight), priorWeight_(priorWeight), recovery_(recovery),
55 auctionDate_(auctionDate), auctionSettlementDate_(auctionSettlementDate), defaultDate_(defaultDate),
56 eventDeterminationDate_(eventDeterminationDate), weightInsteadOfNotional_(true) {}
57
58//! Constructor taking CDS reference information, \p cdsReferenceInfo.
59BasketConstituent::BasketConstituent(const string& issuerName, const CdsReferenceInformation& cdsReferenceInfo,
60 Real notional, const string& currency, const string& qualifier, Real priorNotional,
61 Real recovery, const Date& auctionDate, const Date& auctionSettlementDate,
62 const Date& defaultDate, const Date& eventDeterminationDate)
63 : issuerName_(issuerName), cdsReferenceInfo_(cdsReferenceInfo), creditCurveId_(cdsReferenceInfo_->id()),
64 notional_(notional), currency_(currency), qualifier_(qualifier), priorNotional_(priorNotional),
65 weight_(Null<Real>()), priorWeight_(Null<Real>()), recovery_(recovery), auctionDate_(auctionDate),
66 auctionSettlementDate_(auctionSettlementDate), defaultDate_(defaultDate),
67 eventDeterminationDate_(eventDeterminationDate), weightInsteadOfNotional_(false) {}
68
70
71 XMLUtils::checkNode(node, "Name");
72
73 issuerName_ = XMLUtils::getChildValue(node, "IssuerId", true);
74 qualifier_ = XMLUtils::getChildValue(node, "Qualifier", false);
75
76 // May get an explicit CreditCurveId node. If so, we use it. Otherwise, we must have ReferenceInformation node.
77 if (XMLNode* tmp = XMLUtils::getChildNode(node, "CreditCurveId")) {
79 } else {
80 tmp = XMLUtils::getChildNode(node, "ReferenceInformation");
81 QL_REQUIRE(tmp, "Need either a CreditCurveId or ReferenceInformation node in each BasketConstituent.");
83 cdsReferenceInfo_->fromXML(tmp);
85 }
86
87 bool zeroWeightOrZeroNotional = false;
88 if (auto notionalNode = XMLUtils::getChildNode(node, "Notional")) {
91 currency_ = XMLUtils::getChildValue(node, "Currency", true);
92 if (close(notional_, 0.0))
93 zeroWeightOrZeroNotional = true;
94 } else {
95 auto weightNode = XMLUtils::getChildNode(node, "Weight");
96 QL_REQUIRE(weightNode, "a 'Notional' or 'Weight' node is mandatory.");
99 if (close(weight_, 0.0))
100 zeroWeightOrZeroNotional = true;
101 currency_ = "";
102 }
103 if (zeroWeightOrZeroNotional) {
104 priorNotional_ = Null<Real>();
105 priorWeight_ = Null<Real>();
107 if (auto n = XMLUtils::getChildNode(node, "PriorNotional"))
109 } else {
110 if (auto n = XMLUtils::getChildNode(node, "PriorWeight"))
112 }
113
114 recovery_ = Null<Real>();
115 if (auto n = XMLUtils::getChildNode(node, "RecoveryRate"))
117
118 auctionDate_ = Date();
119 if (auto n = XMLUtils::getChildNode(node, "AuctionDate"))
121
122 auctionSettlementDate_ = Date();
123 if (auto n = XMLUtils::getChildNode(node, "AuctionSettlementDate"))
125
126 defaultDate_ = Date();
127 if (auto n = XMLUtils::getChildNode(node, "DefaultDate"))
129
131 if (auto n = XMLUtils::getChildNode(node, "EventDeterminationDate"))
133 }
134
135}
136
138
139 XMLNode* node = doc.allocNode("Name");
140
141 XMLUtils::addChild(doc, node, "IssuerId", issuerName_);
142 if (!qualifier_.empty())
143 XMLUtils::addChild(doc, node, "Qualifier", qualifier_);
144
145 // We either have reference information or an explicit credit curve ID
146 if (cdsReferenceInfo_) {
147 XMLUtils::appendNode(node, cdsReferenceInfo_->toXML(doc));
148 } else {
149 XMLUtils::addChild(doc, node, "CreditCurveId", creditCurveId_);
150 }
151
153 XMLUtils::addChild(doc, node, "Notional", notional_);
154 XMLUtils::addChild(doc, node, "Currency", currency_);
155 } else {
156 XMLUtils::addChild(doc, node, "Weight", weight_);
157 }
158
159 if (close(notional_, 0.0) && !weightInsteadOfNotional_) {
160
161 if (priorNotional_ != Null<Real>())
162 XMLUtils::addChild(doc, node, "PriorNotional", priorNotional_);
163
164 if (recovery_ != Null<Real>())
165 XMLUtils::addChild(doc, node, "RecoveryRate", recovery_);
166
167 if (auctionDate_ != Date())
168 XMLUtils::addChild(doc, node, "AuctionDate", to_string(auctionDate_));
169
170 if (auctionSettlementDate_ != Date())
171 XMLUtils::addChild(doc, node, "AuctionSettlementDate", to_string(auctionSettlementDate_));
172
173 if (defaultDate_ != Date())
174 XMLUtils::addChild(doc, node, "DefaultDate", to_string(defaultDate_));
175
176 if (eventDeterminationDate_ != Date())
177 XMLUtils::addChild(doc, node, "EventDeterminationDate", to_string(eventDeterminationDate_));
178 } else if (close(weight_, 0.0) && weightInsteadOfNotional_) {
179
180 if (priorWeight_ != Null<Real>())
181 XMLUtils::addChild(doc, node, "PriorWeight", priorWeight_);
182
183 if (recovery_ != Null<Real>())
184 XMLUtils::addChild(doc, node, "RecoveryRate", recovery_);
185
186 if (auctionDate_ != Date())
187 XMLUtils::addChild(doc, node, "AuctionDate", to_string(auctionDate_));
188
189 if (auctionSettlementDate_ != Date())
190 XMLUtils::addChild(doc, node, "AuctionSettlementDate", to_string(auctionSettlementDate_));
191
192 if (defaultDate_ != Date())
193 XMLUtils::addChild(doc, node, "DefaultDate", to_string(defaultDate_));
194
195 if (eventDeterminationDate_ != Date())
196 XMLUtils::addChild(doc, node, "EventDeterminationDate", to_string(eventDeterminationDate_));
197 }
198
199 return node;
200}
201
202const string& BasketConstituent::issuerName() const { return issuerName_; }
203
204const string& BasketConstituent::creditCurveId() const { return creditCurveId_; }
205
206const boost::optional<CdsReferenceInformation>& BasketConstituent::cdsReferenceInfo() const {
207 return cdsReferenceInfo_;
208}
209
210QuantLib::Real BasketConstituent::notional() const {
211 QL_REQUIRE(!weightInsteadOfNotional_, "Try to access notional from basket constituent "
212 << issuerName_ << ", but weight (w=" << weight_
213 << ") was given.");
214 return notional_;
215}
216const string& BasketConstituent::currency() const {
217 QL_REQUIRE(!weightInsteadOfNotional_, "Try to access currceny from basket constituent "
218 << issuerName_ << ", but weight instead of notional given");
219 return currency_;
220}
221
222QuantLib::Real BasketConstituent::priorNotional() const {
223 QL_REQUIRE(!weightInsteadOfNotional_, "Try to access priorNotional from basket constituent "
224 << issuerName_ << ", but priorWeight (w=" << priorWeight_
225 << ") was given.");
226 return priorNotional_;
227}
228
230
231QuantLib::Real BasketConstituent::weight() const {
232 QL_REQUIRE(weightInsteadOfNotional_, "Try to access weight from basket constituent "
233 << issuerName_ << ", but notional (N=" << notional_ << " "
234 << currency_ << ") was given.");
235 return weight_;
236}
237
238QuantLib::Real BasketConstituent::priorWeight() const {
239 QL_REQUIRE(weightInsteadOfNotional_, "Try to access priorWeight from basket constituent "
240 << issuerName_ << ", but priorNotional (N=" << priorNotional_ << " "
241 << currency_ << ") was given.");
242 return priorWeight_;
243}
244
245const Date& BasketConstituent::auctionDate() const { return auctionDate_; }
246
248
249const Date& BasketConstituent::defaultDate() const { return defaultDate_; }
250
252
254
255bool operator<(const BasketConstituent& lhs, const BasketConstituent& rhs) {
256 return lhs.creditCurveId() < rhs.creditCurveId();
257}
258
260
261BasketData::BasketData(const vector<BasketConstituent>& constituents) : constituents_(constituents) {}
262
263const vector<BasketConstituent>& BasketData::constituents() const { return constituents_; }
264
266
267 XMLUtils::checkNode(node, "BasketData");
268
269 constituents_.clear();
270 for (XMLNode* child = XMLUtils::getChildNode(node, "Name"); child; child = XMLUtils::getNextSibling(child)) {
271 BasketConstituent constituent;
272 constituent.fromXML(child);
273 constituents_.push_back(constituent);
274 }
275}
276
278
279 XMLNode* node = doc.allocNode("BasketData");
280
281 for (auto c : constituents_) {
282 auto cNode = c.toXML(doc);
283 XMLUtils::appendNode(node, cNode);
284 }
285
286 return node;
287}
288} // namespace data
289} // namespace ore
credit basket data model and serialization
const std::string & currency() const
Definition: basketdata.cpp:216
QuantLib::Date eventDeterminationDate_
Definition: basketdata.hpp:112
QuantLib::Real weight() const
Definition: basketdata.cpp:231
bool weightInsteadOfNotional() const
Definition: basketdata.cpp:253
const QuantLib::Date & defaultDate() const
Definition: basketdata.cpp:249
boost::optional< ore::data::CdsReferenceInformation > cdsReferenceInfo_
Definition: basketdata.hpp:100
BasketConstituent()
Default constructor.
Definition: basketdata.cpp:36
void fromXML(ore::data::XMLNode *node) override
Definition: basketdata.cpp:69
QuantLib::Real notional() const
Definition: basketdata.cpp:210
QuantLib::Real priorNotional() const
Definition: basketdata.cpp:222
const QuantLib::Date & eventDeterminationDate() const
Definition: basketdata.cpp:251
QuantLib::Real recovery() const
Definition: basketdata.cpp:229
const QuantLib::Date & auctionDate() const
Definition: basketdata.cpp:245
QuantLib::Real priorWeight() const
Definition: basketdata.cpp:238
const boost::optional< ore::data::CdsReferenceInformation > & cdsReferenceInfo() const
Definition: basketdata.cpp:206
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
Definition: basketdata.cpp:137
const std::string & issuerName() const
Definition: basketdata.cpp:202
const QuantLib::Date & auctionSettlementDate() const
Definition: basketdata.cpp:247
QuantLib::Date auctionSettlementDate_
Definition: basketdata.hpp:110
const std::string & creditCurveId() const
Definition: basketdata.cpp:204
void fromXML(ore::data::XMLNode *node) override
Definition: basketdata.cpp:265
const std::vector< BasketConstituent > & constituents() const
Definition: basketdata.cpp:263
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
Definition: basketdata.cpp:277
BasketData()
Default constructor.
Definition: basketdata.cpp:259
std::vector< BasketConstituent > constituents_
Definition: basketdata.hpp:148
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static string getNodeValue(XMLNode *node)
Get a node's value.
Definition: xmlutils.cpp:489
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
Get a node's next sibling node.
Definition: xmlutils.cpp:484
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
bool operator<(const Dividend &d1, const Dividend &d2)
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string conversion utilities