Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
underlying.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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
21
22using namespace QuantLib;
23
24namespace ore {
25namespace data {
26
27Underlying::Underlying(const std::string& type, const std::string& name, const QuantLib::Real weight) : Underlying() {
28 type_ = type;
29 name_ = name;
31};
32
34 type_ = XMLUtils::getChildValue(node, "Type", true);
35 name_ = XMLUtils::getChildValue(node, "Name", true);
36 if (auto n = XMLUtils::getChildNode(node, "Weight"))
38 else
39 weight_ = 1.0;
40}
41
43 XMLNode* node = doc.allocNode(nodeName_);
44 XMLUtils::addChild(doc, node, "Type", type_);
45 XMLUtils::addChild(doc, node, "Name", name_);
46 XMLUtils::addChild(doc, node, "Weight", weight_);
47 return node;
48}
49
53 isBasic_ = true;
54 } else {
55 QL_FAIL("Need a " << basicUnderlyingNodeName_ << " node for BasicUnderlying.");
56 }
57 setType("Basic");
58}
59
62 return node;
63}
64
66 if (equityName_.empty()) {
67 string name = name_;
68 if (!identifierType_.empty())
69 name = identifierType_ + ":" + name_;
70 if (!currency_.empty())
71 name = name + ":" + currency_;
72 if (!exchange_.empty()) {
73 if (currency_.empty())
74 name = name + ":";
75 name = name + ":" + exchange_;
76 }
78 }
79}
80
84 isBasic_ = true;
85 } else if (XMLUtils::getNodeName(node) == nodeName_) {
87 QL_REQUIRE(type_ == "Equity", "Underlying must be of type 'Equity'.");
88 identifierType_ = XMLUtils::getChildValue(node, "IdentifierType", false);
89 // if no identifier is provided, we just use name
90 if (!identifierType_.empty()) {
91 currency_ = XMLUtils::getChildValue(node, "Currency", false);
92 exchange_ = XMLUtils::getChildValue(node, "Exchange", false);
93 }
95 isBasic_ = false;
96 } else {
97 QL_FAIL("Need either a " << basicUnderlyingNodeName_ << " or " << nodeName_ << " for EquityUnderlying.");
98 }
99 setType("Equity");
100}
101
103 XMLNode* node;
104 if (isBasic_) {
106 } else {
107 node = Underlying::toXML(doc);
108 if (!identifierType_.empty())
109 XMLUtils::addChild(doc, node, "IdentifierType", identifierType_);
110 if (!currency_.empty())
111 XMLUtils::addChild(doc, node, "Currency", currency_);
112 if (!exchange_.empty())
113 XMLUtils::addChild(doc, node, "Exchange", exchange_);
114 }
115 return node;
116}
117
121 isBasic_ = true;
122 } else if (XMLUtils::getNodeName(node) == nodeName_) {
124 QL_REQUIRE(type_ == "Commodity", "Underlying must be of type 'Commodity'.");
125 priceType_ = XMLUtils::getChildValue(node, "PriceType", false);
126 if (auto n = XMLUtils::getChildNode(node, "FutureMonthOffset"))
128 else
129 futureMonthOffset_ = Null<Size>();
130 if (auto n = XMLUtils::getChildNode(node, "DeliveryRollDays"))
132 else
133 deliveryRollDays_ = Null<Size>();
134 deliveryRollCalendar_ = XMLUtils::getChildValue(node, "DeliveryRollCalendar", false);
135 isBasic_ = false;
136 futureExpiryDate_ = XMLUtils::getChildValue(node, "FutureExpiryDate", false);
137 futureContractMonth_ = XMLUtils::getChildValue(node, "FutureContractMonth", false);
138 QL_REQUIRE(futureExpiryDate_.empty() || futureContractMonth_.empty(),
139 "Only futureExpiryDate or futureContractMonth are allowed not both");
140 } else {
141 QL_FAIL("Need either a Name or Underlying node for CommodityUnderlying.");
142 }
143 setType("Commodity");
144}
145
147 XMLNode* node;
148 if (isBasic_) {
150 } else {
151 node = Underlying::toXML(doc);
152 if (!priceType_.empty())
153 XMLUtils::addChild(doc, node, "PriceType", priceType_);
154 if (futureMonthOffset_ != Null<Size>())
155 XMLUtils::addChild(doc, node, "FutureMonthOffset", (int)futureMonthOffset_);
156 if (deliveryRollDays_ != Null<Size>())
157 XMLUtils::addChild(doc, node, "DeliveryRollDays", (int)deliveryRollDays_);
158 if (!deliveryRollCalendar_.empty())
159 XMLUtils::addChild(doc, node, "DeliveryRollCalendar", deliveryRollCalendar_);
160 if (!futureExpiryDate_.empty())
161 XMLUtils::addChild(doc, node, "FutureExpiryDate", futureExpiryDate_);
162 if (!futureContractMonth_.empty())
163 XMLUtils::addChild(doc, node, "FutureContractMonth", futureContractMonth_);
164 }
165 return node;
166}
167
171 isBasic_ = true;
172 } else if (XMLUtils::getNodeName(node) == nodeName_) {
174 isBasic_ = false;
175 } else {
176 QL_FAIL("Need either a Name or Underlying node for FXUnderlying.");
177 }
178 setType("FX");
179}
180
182 XMLNode* node;
183 if (isBasic_) {
185 } else {
186 node = Underlying::toXML(doc);
187 }
188 return node;
189}
190
194 isBasic_ = true;
195 } else if (XMLUtils::getNodeName(node) == nodeName_) {
197 isBasic_ = false;
198 } else {
199 QL_FAIL("Need either a Name or Underlying node for InterestRateUnderlying.");
200 }
201 setType("InterestRate");
202}
203
205 XMLNode* node;
206 if (isBasic_) {
208 } else {
209 node = Underlying::toXML(doc);
210 }
211 return node;
212}
213
217 isBasic_ = true;
218 } else if (XMLUtils::getNodeName(node) == nodeName_) {
220 isBasic_ = false;
221 } else {
222 QL_FAIL("Need either a Name or Underlying node for CreditUnderlying.");
223 }
224 setType("Credit");
225}
226
228 XMLNode* node;
229 if (isBasic_) {
231 } else {
232 node = Underlying::toXML(doc);
233 }
234 return node;
235}
236
240 isBasic_ = true;
241 } else if (XMLUtils::getNodeName(node) == nodeName_) {
243 // optional
244 std::string interpolationString = XMLUtils::getChildValue(node, "Interpolation", false);
245 if (interpolationString != "")
246 interpolation_ = parseObservationInterpolation(interpolationString);
247 else
248 interpolation_ = QuantLib::CPI::InterpolationType::Flat;
249 isBasic_ = false;
250 } else {
251 QL_FAIL("Need either a Name or Underlying node for InflationUnderlying.");
252 }
253 setType("Inflation");
254}
255
257 XMLNode* node;
258 if (isBasic_) {
260 } else {
261 node = Underlying::toXML(doc);
262 XMLUtils::addChild(doc, node, "Interpolation", std::to_string(interpolation_));
263 }
264 return node;
265}
266
268 if (bondName_.empty()) {
269 if (!identifierType_.empty())
271 else
273 }
274}
275
279 isBasic_ = true;
280 } else if (XMLUtils::getNodeName(node) == nodeName_) {
282 QL_REQUIRE(type_ == "Bond", "Underlying must be of type 'Bond'.");
283 identifierType_ = XMLUtils::getChildValue(node, "IdentifierType", false);
284 setBondName();
285 isBasic_ = false;
286 } else {
287 QL_FAIL("Need either a " << basicUnderlyingNodeName_ << " or " << nodeName_ << " for BondUnderlying.");
288 }
289 bidAskAdjustment_ = XMLUtils::getChildValueAsDouble(node, "BidAskAdjustment", false, 0.0);
290 setType("Bond");
291}
292
294 XMLNode* node;
295 if (isBasic_) {
297 } else {
298 node = Underlying::toXML(doc);
299 if (!identifierType_.empty())
300 XMLUtils::addChild(doc, node, "IdentifierType", identifierType_);
301 }
302 return node;
303}
304
307 underlying_ = QuantLib::ext::make_shared<BasicUnderlying>();
308 } else if (XMLUtils::getNodeName(node) == nodeName_) {
309 string type = XMLUtils::getChildValue(node, "Type", true);
310 if (type == "Equity")
311 underlying_ = QuantLib::ext::make_shared<EquityUnderlying>();
312 else if (type == "Commodity")
313 underlying_ = QuantLib::ext::make_shared<CommodityUnderlying>();
314 else if (type == "FX")
315 underlying_ = QuantLib::ext::make_shared<FXUnderlying>();
316 else if (type == "InterestRate")
317 underlying_ = QuantLib::ext::make_shared<InterestRateUnderlying>();
318 else if (type == "Inflation")
319 underlying_ = QuantLib::ext::make_shared<InflationUnderlying>();
320 else if (type == "Credit")
321 underlying_ = QuantLib::ext::make_shared<CreditUnderlying>();
322 else if (type == "Bond")
323 underlying_ = QuantLib::ext::make_shared<BondUnderlying>();
324 else {
325 QL_FAIL("Unknown Underlying type " << type);
326 }
327 } else {
328 QL_FAIL("Need either a " << basicUnderlyingNodeName_ << " or " << nodeName_ << " node for Underlying.");
329 }
330 QL_REQUIRE(underlying_ != nullptr, "UnderlyingBuilder: underlying_ is null, this is unexpected");
331 underlying_->setNodeName(nodeName_);
332 underlying_->setBasicUnderlyingNodeName(basicUnderlyingNodeName_);
333 underlying_->fromXML(node);
334}
335
336XMLNode* UnderlyingBuilder::toXML(XMLDocument& doc) const { return NULL; }
337
338} // namespace data
339} // namespace ore
void fromXML(XMLNode *node) override
Definition: underlying.cpp:50
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:60
void setBondName()
set name of bond
Definition: underlying.cpp:267
void fromXML(XMLNode *node) override
Definition: underlying.cpp:276
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:293
void fromXML(XMLNode *node) override
Definition: underlying.cpp:118
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:146
void fromXML(XMLNode *node) override
Definition: underlying.cpp:214
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:227
void setEquityName()
set name of equity
Definition: underlying.cpp:65
void fromXML(XMLNode *node) override
Definition: underlying.cpp:81
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:102
const std::string & name() const override
Definition: underlying.hpp:109
void fromXML(XMLNode *node) override
Definition: underlying.cpp:168
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:181
QuantLib::CPI::InterpolationType interpolation_
Definition: underlying.hpp:209
void fromXML(XMLNode *node) override
Definition: underlying.cpp:237
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:256
void fromXML(XMLNode *node) override
Definition: underlying.cpp:191
XMLNode * toXML(XMLDocument &doc) const override
Definition: underlying.cpp:204
void fromXML(XMLNode *node) override
Definition: underlying.cpp:305
QuantLib::ext::shared_ptr< Underlying > underlying_
Definition: underlying.hpp:270
XMLNode * toXML(ore::data::XMLDocument &doc) const override
Definition: underlying.cpp:336
const std::string nodeName_
Definition: underlying.hpp:269
const std::string basicUnderlyingNodeName_
Definition: underlying.hpp:269
Class to hold Underlyings.
Definition: underlying.hpp:37
std::string basicUnderlyingNodeName_
Definition: underlying.hpp:71
const std::string & type() const
Definition: underlying.hpp:63
Real weight() const
Definition: underlying.hpp:65
std::string nodeName_
Definition: underlying.hpp:71
virtual void fromXML(ore::data::XMLNode *node) override
Definition: underlying.cpp:33
virtual const std::string & name() const
Definition: underlying.hpp:64
Underlying()
Default Constructor.
Definition: underlying.hpp:40
virtual ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
Definition: underlying.cpp:42
void setType(const string &type)
Definition: underlying.hpp:53
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
static string getNodeName(XMLNode *n)
Get and set a node's name.
Definition: xmlutils.cpp:473
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 * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
QuantLib::CPI::InterpolationType parseObservationInterpolation(const std::string &s)
Convert string to observation interpolation.
Definition: parsers.cpp:682
Real parseReal(const string &s)
Convert text to Real.
Definition: parsers.cpp:112
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
@ data
Definition: log.hpp:77
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string name
underlying data model