23#include <ql/utilities/dataparsers.hpp>
30 static map<string, CSA::Type> t = {
37 QL_FAIL(
"Cannot convert \"" << s <<
"\" to CSA::Type");
44 return out <<
"Bilateral";
46 return out <<
"CallOnly";
48 return out <<
"PostOnly";
50 QL_FAIL(
"csa type not covered");
69 QL_REQUIRE(
csaCurrency_.size() == 3,
"NettingSetDefinition build error;"
70 <<
" CSA currency should be a three-letter ISO code");
72 QL_REQUIRE(
thresholdPay_ >= 0,
"NettingSetDefinition build error; negative thresholdPay");
73 QL_REQUIRE(
thresholdRcv_ >= 0,
"NettingSetDefinition build error; negative thresholdRcv");
74 QL_REQUIRE(
mtaPay_ >= 0,
"NettingSetDefinition build error; negative mtaPay");
75 QL_REQUIRE(
mtaRcv_ >= 0,
"NettingSetDefinition build error; negative mtaRcv");
76 QL_REQUIRE(
iaType_ ==
"FIXED",
"NettingSetDefinition build error;"
77 <<
" unsupported independent amount type; " <<
iaType_);
80 "NettingSetDefinition build error;"
81 <<
" non-positive margining frequency");
82 QL_REQUIRE(
mpr_ >= Period(0, Days),
"NettingSetDefinition build error;"
83 <<
" negative margin period of risk");
85 LOG(
"NettingSetDefinition has CSA margining frequency ("
91 "NettingSetDefinition build error;"
92 <<
"EligibleCollaterals currency should be a three-letter ISO code");
111 DLOG(nettingSetDetails_ <<
": NettingSetDefinition built from XML... ");
115 : nettingSetDetails_(nettingSetDetails), activeCsaFlag_(false) {
117 DLOG(nettingSetDetails_ <<
": uncollateralised NettingSetDefinition built.");
121 const string& csaCurrency,
const string& index,
const Real& thresholdPay,
122 const Real& thresholdRcv,
const Real& mtaPay,
const Real& mtaRcv,
123 const Real& iaHeld,
const string& iaType,
const string& marginCallFreq,
124 const string& marginPostFreq,
const string& mpr,
const Real& collatSpreadPay,
125 const Real& collatSpreadRcv,
const vector<string>& eligCollatCcys,
126 bool applyInitialMargin,
const string& initialMarginType,
127 const bool calculateIMAmount,
const bool calculateVMAmount,
128 const string& nonExemptIMRegulations)
129 : nettingSetDetails_(nettingSetDetails), activeCsaFlag_(true) {
131 csa_ = QuantLib::ext::make_shared<CSA>(
132 parseCsaType(bilateral), csaCurrency, index, thresholdPay, thresholdRcv, mtaPay, mtaRcv, iaHeld, iaType,
134 eligCollatCcys, applyInitialMargin,
parseCsaType(initialMarginType), calculateIMAmount, calculateVMAmount,
135 nonExemptIMRegulations);
138 DLOG(nettingSetDetails_ <<
": collateralised NettingSetDefinition built. ");
146 if (nettingSetDetailsNode) {
147 nettingSetDetails_.fromXML(nettingSetDetailsNode);
156 if (activeCsaFlag_) {
161 if (csaTypeStr.empty())
162 csaTypeStr =
"Bilateral";
172 Real collatSpreadRcv =
176 string marginCallFreqStr, marginPostFreqStr;
181 if (marginCallFreqStr.empty())
182 marginCallFreqStr =
"1D";
183 if (marginPostFreqStr.empty())
184 marginPostFreqStr =
"1D";
195 vector<string> eligCollatCcys;
203 if (initialMarginType.empty())
204 initialMarginType =
"Bilateral";
211 csa_ = QuantLib::ext::make_shared<CSA>(
parseCsaType(csaTypeStr), csaCurrency, index, thresholdPay, thresholdRcv, mtaPay,
212 mtaRcv, iaHeld, iaType,
parsePeriod(marginCallFreqStr),
214 collatSpreadRcv, eligCollatCcys, applyInitialMargin,
215 parseCsaType(initialMarginType), calculateIMAmount, calculateVMAmount,
216 nonExemptIMRegulations);
227 if (nettingSetDetails_.emptyOptionalFields()) {
237 if (activeCsaFlag_) {
238 QL_REQUIRE(csa_,
"CSA details not defined");
247 XMLUtils::addChild(doc, csaSubNode,
"CollateralCompoundingSpreadPay", csa_->collatSpreadPay());
248 XMLUtils::addChild(doc, csaSubNode,
"CollateralCompoundingSpreadReceive", csa_->collatSpreadRcv());
257 XMLUtils::addChild(doc, iaSubNode,
"IndependentAmountHeld", csa_->independentAmountHeld());
258 XMLUtils::addChild(doc, iaSubNode,
"IndependentAmountType", csa_->independentAmountType());
264 XMLUtils::addChild(doc, csaSubNode,
"ApplyInitialMargin", csa_->applyInitialMargin());
268 XMLUtils::addChild(doc, csaSubNode,
"NonExemptIMRegulations", csa_->nonExemptIMRegulations());
275 string nettingSetLog = nettingSetDetails_.empty() ? nettingSetId_ :
ore::data::to_string(nettingSetDetails_);
276 LOG(nettingSetLog <<
": Validating netting set definition");
277 QL_REQUIRE(nettingSetId_.size() > 0 || !nettingSetDetails_.empty(),
278 "NettingSetDefinition build error; no netting set ID or netting set details");
280 if (activeCsaFlag_) {
281 QL_REQUIRE(csa_,
"CSA not defined yet");
282 string nettingSetLog = nettingSetDetails_.empty() ? nettingSetId_ :
ore::data::to_string(nettingSetDetails_);
283 LOG(nettingSetLog <<
": Validating netting set definition's CSA details");
vector< string > eligCollatCcys_
void fromXML(XMLNode *node) override
XMLNode * toXML(XMLDocument &doc) const override
NettingSetDefinition(XMLNode *node)
Serializable object holding netting set identification data.
Small XML Document wrapper class.
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
static void addChildren(XMLDocument &doc, XMLNode *n, const string &names, const string &name, const vector< T > &values)
static void checkNode(XMLNode *n, const string &expectedName)
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
static bool getChildValueAsBool(XMLNode *node, const string &name, bool mandatory=false, bool defaultValue=true)
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static vector< string > getChildrenValues(XMLNode *node, const string &names, const string &name, bool mandatory=false)
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
static void appendNode(XMLNode *parent, XMLNode *child)
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
Classes and functions for log message handling.
#define LOG(text)
Logging Macro (Level = Notice)
#define DLOG(text)
Logging Macro (Level = Debug)
std::ostream & operator<<(std::ostream &out, EquityReturnType t)
CSA::Type parseCsaType(const string &s)
Size size(const ValueType &v)
std::string to_string(const LocationInfo &l)
Serializable Credit Default Swap.
Netting Set Definition - including CSA information where available.
Map text representations to QuantLib/QuantExt types.
string conversion utilities