Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
commoditylegdata.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5
6 This file is part of ORE, a free-software/open-source library
7 for transparent pricing and risk analysis - http://opensourcerisk.org
8
9 ORE is free software: you can redistribute it and/or modify it
10 under the terms of the Modified BSD License. You should have received a
11 copy of the license along with this program.
12 The license is also available online at <http://opensourcerisk.org>
13
14 This program is distributed on the basis that it will form a useful
15 contribution to risk analytics and model standardisation, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
26#include <ql/cashflows/simplecashflow.hpp>
28
29#include <boost/algorithm/string/predicate.hpp>
30#include <boost/make_shared.hpp>
31
32using boost::iequals;
42using QuantLib::Natural;
43using QuantLib::Null;
44using QuantLib::Real;
45using std::ostream;
46using std::string;
47using std::vector;
48
49namespace ore {
50namespace data {
51
53 if (iequals(s, "CalculationPeriodEndDate")) {
55 } else if (iequals(s, "CalculationPeriodStartDate")) {
57 } else if (iequals(s, "TerminationDate")) {
59 } else if (iequals(s, "FutureExpiryDate")) {
61 } else {
62 QL_FAIL("Could not parse " << s << " to CommodityPayRelativeTo");
63 }
64}
65
66ostream& operator<<(ostream& out, const CommodityPayRelativeTo& cprt) {
68 return out << "CalculationPeriodEndDate";
70 return out << "CalculationPeriodStartDate";
71 } else if (cprt == CommodityPayRelativeTo::TerminationDate) {
72 return out << "TerminationDate";
74 return out << "FutureExpiryDate";
75 } else {
76 QL_FAIL("Do not recognise CommodityPayRelativeTo " << static_cast<int>(cprt));
77 }
78}
79
81 if (iequals(s, "Spot")) {
83 } else if (iequals(s, "FutureSettlement")) {
85 } else {
86 QL_FAIL("Could not parse " << s << " to CommodityPriceType");
87 }
88}
89
90ostream& operator<<(ostream& out, const CommodityPriceType& cpt) {
91 if (cpt == CommodityPriceType::Spot) {
92 return out << "Spot";
93 } else if (cpt == CommodityPriceType::FutureSettlement) {
94 return out << "FutureSettlement";
95 } else {
96 QL_FAIL("Do not recognise CommodityPriceType " << static_cast<int>(cpt));
97 }
98}
99
101 if (iequals(s, "FutureExpiryDate")) {
103 } else if (iequals(s, "None")) {
105 } else {
106 QL_FAIL("Could not parse " << s << " to CommodityPricingDateRule");
107 }
108}
109
110ostream& operator<<(std::ostream& out, const CommodityPricingDateRule& cpdr) {
112 return out << "FutureExpiryDate";
113 } else if (cpdr == CommodityPricingDateRule::None) {
114 return out << "None";
115 } else {
116 QL_FAIL("Do not recognise CommodityPricingDateRule " << static_cast<int>(cpdr));
117 }
118}
119
121 : ore::data::LegAdditionalData("CommodityFixed"),
122 commodityPayRelativeTo_(CommodityPayRelativeTo::CalculationPeriodEndDate) {}
123
124CommodityFixedLegData::CommodityFixedLegData(const vector<Real>& quantities, const vector<string>& quantityDates,
125 const vector<Real>& prices, const vector<string>& priceDates,
126 CommodityPayRelativeTo commodityPayRelativeTo, const string& tag)
127 : LegAdditionalData("CommodityFixed"), quantities_(quantities), quantityDates_(quantityDates), prices_(prices),
128 priceDates_(priceDates), commodityPayRelativeTo_(commodityPayRelativeTo), tag_(tag) {}
129
130void CommodityFixedLegData::setQuantities(const vector<Real>& quantities) {
131 // Ensure that the quantityDates_ are cleared also.
133 quantityDates_.clear();
134}
135
137 XMLUtils::checkNode(node, "CommodityFixedLegData");
138
139 quantities_.clear();
140 if (XMLUtils::getChildNode(node, "Quantities")) {
141 quantities_ = XMLUtils::getChildrenValuesWithAttributes<Real>(node, "Quantities", "Quantity",
142 "startDate", quantityDates_, &parseReal, true);
143 }
144
145 prices_ = XMLUtils::getChildrenValuesWithAttributes<Real>(node, "Prices", "Price", "startDate", priceDates_,
146 &parseReal, true);
147
149 if (XMLNode* n = XMLUtils::getChildNode(node, "CommodityPayRelativeTo")) {
151 }
152 tag_ = XMLUtils::getChildValue(node, "Tag", false);
153}
154
156 XMLNode* node = doc.allocNode("CommodityFixedLegData");
157
158 if (!quantities_.empty()) {
159 XMLUtils::addChildrenWithOptionalAttributes(doc, node, "Quantities", "Quantity",
160 quantities_, "startDate", quantityDates_);
161 }
162
163 XMLUtils::addChildrenWithOptionalAttributes(doc, node, "Prices", "Price", prices_, "startDate", priceDates_);
164 XMLUtils::addChild(doc, node, "CommodityPayRelativeTo", to_string(commodityPayRelativeTo_));
165 if (!tag_.empty())
166 XMLUtils::addChild(doc, node, "Tag", tag_);
167
168 return node;
169}
170
172 : ore::data::LegAdditionalData("CommodityFloating"),
174 commodityQuantityFrequency_(CommodityQuantityFrequency::PerCalculationPeriod),
175 commodityPayRelativeTo_(CommodityPayRelativeTo::CalculationPeriodEndDate),
177 pricingLag_(0), isAveraged_(false), isInArrears_(true), futureMonthOffset_(0),
178 deliveryRollDays_(0), includePeriodEnd_(true), excludePeriodStart_(true),
179 hoursPerDay_(Null<Natural>()), useBusinessDays_(true), dailyExpiryOffset_(0),
180 unrealisedQuantity_(false), lastNDays_(Null<Natural>()), fxIndex_("") {}
181
183 const string& name, CommodityPriceType priceType, const vector<Real>& quantities,
184 const vector<string>& quantityDates, CommodityQuantityFrequency commodityQuantityFrequency,
185 CommodityPayRelativeTo commodityPayRelativeTo, const vector<Real>& spreads, const vector<string>& spreadDates,
186 const vector<Real>& gearings, const vector<string>& gearingDates, CommodityPricingDateRule pricingDateRule,
187 const string& pricingCalendar, Natural pricingLag, const vector<string>& pricingDates, bool isAveraged,
188 bool isInArrears, Natural futureMonthOffset, Natural deliveryRollDays, bool includePeriodEnd,
189 bool excludePeriodStart, Natural hoursPerDay, bool useBusinessDays, const string& tag,
190 Natural dailyExpiryOffset, bool unrealisedQuantity, QuantLib::Natural lastNDays, std::string fxIndex)
191 : LegAdditionalData("CommodityFloating"), name_(name), priceType_(priceType), quantities_(quantities),
192 quantityDates_(quantityDates), commodityQuantityFrequency_(commodityQuantityFrequency),
193 commodityPayRelativeTo_(commodityPayRelativeTo), spreads_(spreads), spreadDates_(spreadDates),
194 gearings_(gearings), gearingDates_(gearingDates), pricingDateRule_(pricingDateRule),
195 pricingCalendar_(pricingCalendar), pricingLag_(pricingLag), pricingDates_(pricingDates), isAveraged_(isAveraged),
196 isInArrears_(isInArrears), futureMonthOffset_(futureMonthOffset), deliveryRollDays_(deliveryRollDays),
197 includePeriodEnd_(includePeriodEnd), excludePeriodStart_(excludePeriodStart), hoursPerDay_(hoursPerDay),
198 useBusinessDays_(useBusinessDays), tag_(tag), dailyExpiryOffset_(dailyExpiryOffset),
199 unrealisedQuantity_(unrealisedQuantity), lastNDays_(lastNDays), fxIndex_(fxIndex) {
200 indices_.insert("COMM-" + name_);
201}
202
204
205 XMLUtils::checkNode(node, "CommodityFloatingLegData");
206
207 name_ = XMLUtils::getChildValue(node, "Name", true);
208 indices_.insert("COMM-" + name_);
210 quantities_ = XMLUtils::getChildrenValuesWithAttributes<Real>(node, "Quantities", "Quantity", "startDate",
211 quantityDates_, &parseReal, true);
212
213 commodityQuantityFrequency_ = CommodityQuantityFrequency::PerCalculationPeriod;
214 if (XMLNode* n = XMLUtils::getChildNode(node, "CommodityQuantityFrequency")) {
216 }
217
219 if (XMLNode* n = XMLUtils::getChildNode(node, "CommodityPayRelativeTo")) {
221 }
222
223 spreads_ = XMLUtils::getChildrenValuesWithAttributes<Real>(node, "Spreads", "Spread", "startDate", spreadDates_,
224 &parseReal);
225 gearings_ = XMLUtils::getChildrenValuesWithAttributes<Real>(node, "Gearings", "Gearing", "startDate", gearingDates_,
226 &parseReal);
227
229 if (XMLNode* n = XMLUtils::getChildNode(node, "PricingDateRule")) {
231 }
232
233 pricingCalendar_ = XMLUtils::getChildValue(node, "PricingCalendar", false);
234 pricingLag_ = XMLUtils::getChildValueAsInt(node, "PricingLag", false);
235 pricingDates_ = XMLUtils::getChildrenValues(node, "PricingDates", "PricingDate", false);
236
237 isAveraged_ = false;
238 if (XMLNode* n = XMLUtils::getChildNode(node, "IsAveraged")) {
240 }
241
242 isInArrears_ = true;
243 if (XMLNode* n = XMLUtils::getChildNode(node, "IsInArrears")) {
245 }
246
247 futureMonthOffset_ = XMLUtils::getChildValueAsInt(node, "FutureMonthOffset", false);
248 deliveryRollDays_ = XMLUtils::getChildValueAsInt(node, "DeliveryRollDays", false);
249
250 includePeriodEnd_ = true;
251 if (XMLNode* n = XMLUtils::getChildNode(node, "IncludePeriodEnd")) {
253 }
254
255 excludePeriodStart_ = true;
256 if (XMLNode* n = XMLUtils::getChildNode(node, "ExcludePeriodStart")) {
258 }
259
260 hoursPerDay_ = Null<Natural>();
261 if (XMLNode* n = XMLUtils::getChildNode(node, "HoursPerDay")) {
263 }
264
265 useBusinessDays_ = true;
266 if (XMLNode* n = XMLUtils::getChildNode(node, "UseBusinessDays")) {
268 }
269
270 tag_ = XMLUtils::getChildValue(node, "Tag", false);
271
272 dailyExpiryOffset_ = Null<Natural>();
273 if (XMLNode* n = XMLUtils::getChildNode(node, "DailyExpiryOffset")) {
275 }
276
277 unrealisedQuantity_ = false;
278 if (XMLNode* n = XMLUtils::getChildNode(node, "UnrealisedQuantity")) {
280 }
281
282 lastNDays_ = Null<Natural>();
283 if (XMLNode* n = XMLUtils::getChildNode(node, "LastNDays")) {
285 }
286
287 if (XMLNode* n = XMLUtils::getChildNode(node, "FXIndex")) {
289 }
290}
291
293
294 XMLNode* node = doc.allocNode("CommodityFloatingLegData");
295
296 XMLUtils::addChild(doc, node, "Name", name_);
297 XMLUtils::addChild(doc, node, "PriceType", to_string(priceType_));
298 XMLUtils::addChildrenWithOptionalAttributes(doc, node, "Quantities", "Quantity", quantities_, "startDate",
300 XMLUtils::addChild(doc, node, "CommodityQuantityFrequency", to_string(commodityQuantityFrequency_));
301 XMLUtils::addChild(doc, node, "CommodityPayRelativeTo", to_string(commodityPayRelativeTo_));
302
303 if (!spreads_.empty())
304 XMLUtils::addChildrenWithOptionalAttributes(doc, node, "Spreads", "Spread", spreads_, "startDate",
306
307 if (!gearings_.empty())
308 XMLUtils::addChildrenWithOptionalAttributes(doc, node, "Gearings", "Gearing", gearings_, "startDate",
310
311 XMLUtils::addChild(doc, node, "PricingDateRule", to_string(pricingDateRule_));
312
313 if (!pricingCalendar_.empty())
314 XMLUtils::addChild(doc, node, "PricingCalendar", pricingCalendar_);
315
316 XMLUtils::addChild(doc, node, "PricingLag", static_cast<int>(pricingLag_));
317
318 if (!pricingDates_.empty())
319 XMLUtils::addChildren(doc, node, "PricingDates", "PricingDate", pricingDates_);
320
321 XMLUtils::addChild(doc, node, "IsAveraged", isAveraged_);
322 XMLUtils::addChild(doc, node, "IsInArrears", isInArrears_);
323 XMLUtils::addChild(doc, node, "FutureMonthOffset", static_cast<int>(futureMonthOffset_));
324 XMLUtils::addChild(doc, node, "DeliveryRollDays", static_cast<int>(deliveryRollDays_));
325 XMLUtils::addChild(doc, node, "IncludePeriodEnd", includePeriodEnd_);
326 XMLUtils::addChild(doc, node, "ExcludePeriodStart", excludePeriodStart_);
327 if (hoursPerDay_ != Null<Natural>())
328 XMLUtils::addChild(doc, node, "HoursPerDay", static_cast<int>(hoursPerDay_));
329 XMLUtils::addChild(doc, node, "UseBusinessDays", useBusinessDays_);
330 if (!tag_.empty())
331 XMLUtils::addChild(doc, node, "Tag", tag_);
332 if (dailyExpiryOffset_ != Null<Natural>())
333 XMLUtils::addChild(doc, node, "DailyExpiryOffset", static_cast<int>(dailyExpiryOffset_));
335 XMLUtils::addChild(doc, node, "UnrealisedQuantity", unrealisedQuantity_);
336 if (lastNDays_ != Null<Natural>())
337 XMLUtils::addChild(doc, node, "LastNDays", static_cast<int>(lastNDays_));
338 if (!fxIndex_.empty())
339 XMLUtils::addChild(doc, node, "FXIndex", fxIndex_);
340
341 return node;
342}
343
344} // namespace data
345} // namespace ore
CommodityFixedLegData()
Default constructor.
void setQuantities(const std::vector< QuantLib::Real > &quantities)
Set the fixed leg data quantities.
std::vector< std::string > priceDates_
void fromXML(ore::data::XMLNode *node) override
std::vector< std::string > quantityDates_
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
std::vector< QuantLib::Real > prices_
std::vector< QuantLib::Real > quantities_
CommodityPayRelativeTo commodityPayRelativeTo_
const std::vector< QuantLib::Real > & quantities() const
CommodityFloatingLegData()
Default constructor.
QuantExt::CommodityQuantityFrequency commodityQuantityFrequency_
void fromXML(ore::data::XMLNode *node) override
std::vector< std::string > pricingDates_
CommodityPricingDateRule pricingDateRule_
std::vector< std::string > gearingDates_
std::vector< std::string > quantityDates_
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
std::vector< QuantLib::Real > quantities_
std::vector< std::string > spreadDates_
CommodityPayRelativeTo commodityPayRelativeTo_
std::vector< QuantLib::Real > spreads_
std::vector< QuantLib::Real > gearings_
Serializable Additional Leg Data.
Definition: legdata.hpp:63
std::set< std::string > indices_
Definition: legdata.hpp:77
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
XML Utilities Class.
Definition: xmlutils.hpp:119
static void addChildren(XMLDocument &doc, XMLNode *n, const string &names, const string &name, const vector< T > &values)
Definition: xmlutils.cpp:502
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 void addChildrenWithOptionalAttributes(XMLDocument &doc, XMLNode *n, const string &names, const string &name, const vector< T > &values, const string &attrName, const vector< string > &attrs)
Definition: xmlutils.cpp:542
static string getNodeValue(XMLNode *node)
Get a node's value.
Definition: xmlutils.cpp:489
static int getChildValueAsInt(XMLNode *node, const string &name, bool mandatory=false, int defaultValue=0)
Definition: xmlutils.cpp:291
static vector< string > getChildrenValues(XMLNode *node, const string &names, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:306
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
leg data for commodity leg types
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
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
leg data model and serialization
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
CommodityQuantityFrequency
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
set< Date > pricingDates(const Date &s, const Date &e, const Calendar &pricingCalendar, bool excludeStart, bool includeEnd, bool useBusinessDays)
CommodityPricingDateRule parseCommodityPricingDateRule(const string &s)
CommodityPriceType parseCommodityPriceType(const string &s)
CommodityPayRelativeTo parseCommodityPayRelativeTo(const string &s)
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
CommodityQuantityFrequency parseCommodityQuantityFrequency(const string &s)
Convert text to QuantExt::CommodityQuantityFrequency.
Definition: parsers.cpp:1192
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
string conversion utilities
string name