Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
premiumdata.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2021 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
22
23using namespace QuantLib;
24
25namespace ore {
26namespace data {
27
28QuantLib::Date PremiumData::latestPremiumDate() const {
29 QuantLib::Date latestPaymentDate = Date::minDate();
30
31 for (const PremiumDatum& d : premiumData_)
32 latestPaymentDate = std::max(latestPaymentDate, d.payDate);
33
34 return latestPaymentDate;
35}
36
38
39 // support deprecated variant, where data is given in single fields under the root node
40
41 auto depr_amount_node = XMLUtils::getChildNode(node, "PremiumAmount");
42 auto depr_ccy_node = XMLUtils::getChildNode(node, "PremiumCurrency");
43 auto depr_date_node = XMLUtils::getChildNode(node, "PremiumPayDate");
44
45 if (depr_amount_node != nullptr) {
46 std::string amountStr = XMLUtils::getNodeValue(depr_amount_node);
47 if (!amountStr.empty()) {
48 double amount = parseReal(amountStr);
49 if (!close_enough(amount, 0.0)) {
50 QL_REQUIRE(depr_ccy_node, "PremiumAmount (" << amount << ") given, but no PremiumCurrency");
51 QL_REQUIRE(depr_date_node, "PremiumAmount (" << amount << ") given, but no PremiumPayDate");
52 std::string ccyStr = XMLUtils::getNodeValue(depr_ccy_node);
53 std::string dateStr = XMLUtils::getNodeValue(depr_date_node);
54 QL_REQUIRE(!ccyStr.empty(), "PremiumAmount (" << amount << ") given, but no PremiumCurrency");
55 QL_REQUIRE(!dateStr.empty(), "PremiumAmount (" << amount << ") given, but no PremiumPayDate");
56 premiumData_.emplace_back(amount, ccyStr, parseDate(dateStr));
57 }
58 }
59 }
60
61 // standard variant, data is given in Premium nodes under Premiums root node
62
63 if (auto p = XMLUtils::getChildNode(node, "Premiums")) {
64 QL_REQUIRE(premiumData_.empty(), "Single PremiumAmount and Premiums node are not allowed simultaneously. Move "
65 "the single premium to the Premiums node instead.");
66 for (auto n : XMLUtils::getChildrenNodes(p, "Premium")) {
68 d.amount = XMLUtils::getChildValueAsDouble(n, "Amount", true);
69 d.ccy = XMLUtils::getChildValue(n, "Currency", true);
70 d.payDate = parseDate(XMLUtils::getChildValue(n, "PayDate", true));
71 premiumData_.push_back(d);
72 }
73 }
74}
75
77 XMLNode* node = doc.allocNode("Premiums");
78 for (auto const& d : premiumData_) {
79 XMLNode* p = XMLUtils::addChild(doc, node, "Premium");
80 XMLUtils::addChild(doc, p, "Amount", d.amount);
81 XMLUtils::addChild(doc, p, "Currency", d.ccy);
82 XMLUtils::addChild(doc, p, "PayDate", ore::data::to_string(d.payDate));
83 }
84 return node;
85}
86
87} // namespace data
88} // namespace ore
std::vector< PremiumDatum > premiumData_
Definition: premiumdata.hpp:67
QuantLib::Date latestPremiumDate() const
Definition: premiumdata.cpp:28
virtual void fromXML(XMLNode *node) override
Definition: premiumdata.cpp:37
virtual XMLNode * toXML(XMLDocument &doc) const override
Definition: premiumdata.cpp:76
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
Definition: xmlutils.cpp:428
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
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
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
@ data
Definition: log.hpp:77
Filter close_enough(const RandomVariable &x, const RandomVariable &y)
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
premium data
string conversion utilities