Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
collateralbalance.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 ORE is free software: you can redistribute it and/or modify it
8 under the terms of the Modified BSD License. You should have received a
9 copy of the license along with this program.
10
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
23#include <ql/errors.hpp>
24
29
30namespace ore {
31namespace data {
32
33void CollateralBalances::add(const QuantLib::ext::shared_ptr<CollateralBalance>& cb) {
34 std::pair<NettingSetDetails, QuantLib::ext::shared_ptr<CollateralBalance>> newCollateralBalance(cb->nettingSetDetails(),
35 cb);
36 collateralBalances_.insert(newCollateralBalance);
37}
38
39bool CollateralBalances::has(const NettingSetDetails& nettingSetDetails) const {
40 return collateralBalances_.find(nettingSetDetails) != collateralBalances_.end();
41}
42
43bool CollateralBalances::has(const std::string& nettingSetId) const {
44 return has(NettingSetDetails(nettingSetId));
45}
46
48 collateralBalances_.clear();
49}
50
52 return collateralBalances_.empty();
53}
54
56 fromXML(node);
57}
58
59const QuantLib::ext::shared_ptr<CollateralBalance>& CollateralBalances::get(const NettingSetDetails& nettingSetDetails) const {
60 if (has(nettingSetDetails))
61 return collateralBalances_.find(nettingSetDetails)->second;
62 else
63 QL_FAIL("CollateralBalance not found in manager: " << nettingSetDetails);
64}
65
66const QuantLib::ext::shared_ptr<CollateralBalance>& CollateralBalances::get(const std::string& nettingSetId) const {
67 return get(NettingSetDetails(nettingSetId));
68}
69
70
71void CollateralBalances::currentIM(const std::string& baseCurrency,
72 std::map<std::string, QuantLib::Real>& currentIM) {
73 for (auto& cb : collateralBalances_) {
74 if (cb.second->currency() == baseCurrency) {
75 currentIM[cb.first.nettingSetId()] = cb.second->initialMargin();
76 }
77 }
78}
79
81 XMLUtils::checkNode(node, "CollateralBalance");
82
83 XMLNode* nettingSetDetailsNode = XMLUtils::getChildNode(node, "NettingSetDetails");
84 if (nettingSetDetailsNode) {
85 nettingSetDetails_.fromXML(nettingSetDetailsNode);
86 } else {
87 nettingSetId_ = XMLUtils::getChildValue(node, "NettingSetId", false);
89 }
90
91 currency_ = XMLUtils::getChildValue(node, "Currency", true);
92
93 XMLNode* initialMarginNode = XMLUtils::getChildNode(node, "InitialMargin");
94 if (!initialMarginNode || (initialMarginNode && XMLUtils::getNodeValue(initialMarginNode).empty()))
95 im_ = QuantLib::Null<QuantLib::Real>();
96 else
97 im_ = parseReal(XMLUtils::getNodeValue(initialMarginNode));
98
99 XMLNode* variationMarginNode = XMLUtils::getChildNode(node, "VariationMargin");
100 if (!variationMarginNode || (variationMarginNode && XMLUtils::getNodeValue(variationMarginNode).empty()))
101 vm_ = QuantLib::Null<QuantLib::Real>();
102 else
103 vm_ = parseReal(XMLUtils::getNodeValue(variationMarginNode));
104
105 DLOG("Loaded collateral balances for netting set " << nettingSetId());
106 DLOG("Currency: " << currency());
107 DLOG("Variation Margin: " << variationMargin());
108 DLOG("Initial Margin: " << initialMargin());
109}
110
112 XMLNode* node = doc.allocNode("CollateralBalance");
113 XMLUtils::addChild(doc, node, "Currency", currency_);
115 XMLUtils::addChild(doc, node, "NettingSetId", nettingSetId_);
116 } else {
118 }
119 if (im_ != QuantLib::Null<QuantLib::Real>())
120 XMLUtils::addChild(doc, node, "InitialMargin", std::round(im_));
121 if (vm_ != QuantLib::Null<QuantLib::Real>())
122 XMLUtils::addChild(doc, node, "VariationMargin", std::round(vm_));
123 return node;
124}
125
127 XMLUtils::checkNode(node, "CollateralBalances");
128 std::vector<XMLNode*> nettingSetNodes = XMLUtils::getChildrenNodes(node, "CollateralBalance");
129 for (unsigned i = 0; i < nettingSetNodes.size(); i++) {
130 try {
131 QuantLib::ext::shared_ptr<CollateralBalance> cb(new CollateralBalance(nettingSetNodes[i]));
132 add(cb);
133 } catch (const std::exception& ex) {
134 ore::data::StructuredConfigurationErrorMessage("Collateral balances", "",
135 "Collateral balance node failed to parse", ex.what())
136 .log();
137 }
138 }
139}
140
142 XMLNode* node = doc.allocNode("CollateralBalances");
143 for (auto it = collateralBalances_.begin(); it != collateralBalances_.end(); ++it) {
144 XMLUtils::appendNode(node, it->second->toXML(doc));
145 }
146 return node;
147}
148
149const std::map<NettingSetDetails, QuantLib::ext::shared_ptr<CollateralBalance>>& CollateralBalances::collateralBalances() {
150 return collateralBalances_;
151}
152
153} // namespace data
154} // namespace ore
const std::string & currency() const
const std::string & nettingSetId() const
void fromXML(ore::data::XMLNode *node) override
NettingSetDetails nettingSetDetails_
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
const QuantLib::Real & variationMargin() const
const QuantLib::Real & initialMargin() const
void add(const QuantLib::ext::shared_ptr< CollateralBalance > &cb)
bool has(const std::string &nettingSetId) const
void fromXML(ore::data::XMLNode *node) override
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override
const std::map< NettingSetDetails, QuantLib::ext::shared_ptr< CollateralBalance > > & collateralBalances()
void currentIM(const std::string &baseCurrency, std::map< std::string, QuantLib::Real > &currentIM)
std::map< NettingSetDetails, QuantLib::ext::shared_ptr< CollateralBalance > > collateralBalances_
const QuantLib::ext::shared_ptr< CollateralBalance > & get(const std::string &nettingSetId) const
void log() const
generate Boost log record to pass to corresponding sinks
Definition: log.cpp:491
Serializable object holding netting set identification data.
bool empty() const
Check if the netting set details have been populated.
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
Utility classes for Structured configuration errors, contains the configuration type and ID (NettingS...
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 checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
Definition: xmlutils.cpp:428
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
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
Holder class for collateral balances.
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
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
Serializable Credit Default Swap.
Definition: namespaces.docs:23
Map text representations to QuantLib/QuantExt types.
Class for structured configuration errors.