Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
currencyconfig.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
23#include <ql/currency.hpp>
25#include <string>
26
27namespace ore {
28namespace data {
29
31
33 currencies_.clear();
34 XMLUtils::checkNode(baseNode, "CurrencyConfig");
35
36 for (auto node : XMLUtils::getChildrenNodes(baseNode, "Currency")) {
37 string name = XMLUtils::getChildValue(node, "Name", false);
38 string isoCode = XMLUtils::getChildValue(node, "ISOCode", false);
39 auto tmp = parseListOfValues(XMLUtils::getChildValue(node, "MinorUnitCodes", false));
40 std::set<std::string> minorUnitCodes(tmp.begin(), tmp.end());
41 try {
42 DLOG("Loading external currency configuration for " << isoCode);
43 Integer numericCode = parseInteger(XMLUtils::getChildValue(node, "NumericCode", false));
44 string symbol = XMLUtils::getChildValue(node, "Symbol", false);
45 string fractionSymbol = XMLUtils::getChildValue(node, "Symbol", false);
46 Integer fractionsPerUnit = parseInteger(XMLUtils::getChildValue(node, "FractionsPerUnit", false));
47 Rounding::Type roundingType = parseRoundingType(XMLUtils::getChildValue(node, "RoundingType", false));
48 Integer precision = parseInteger(XMLUtils::getChildValue(node, "RoundingPrecision", false));
49 // the digit where we switch form roundng down to rounding up, typically 5 and used across all
50 // Integer digit = parseInteger(XMLUtils::getChildValue(node, "Digit", false));
51 string format = XMLUtils::getChildValue(node, "Format", false);
52 QuantExt::ConfigurableCurrency::Type currencyType = parseCurrencyType(XMLUtils::getChildValue(node, "CurrencyType", false, "Major"));
53 Rounding rounding(precision, roundingType);
54 QuantExt::ConfigurableCurrency c(name, isoCode, numericCode, symbol, fractionSymbol, fractionsPerUnit,
55 rounding, format, minorUnitCodes, currencyType);
56 DLOG("loading configuration for currency code " << isoCode);
57
58 switch (currencyType) {
59 case QuantExt::ConfigurableCurrency::Type::Crypto:
60 CurrencyParser::instance().addCrypto(c.code(), c);
61 break;
62 case QuantExt::ConfigurableCurrency::Type::Metal:
63 CurrencyParser::instance().addMetal(c.code(), c);
64 break;
65 default:
66 CurrencyParser::instance().addCurrency(c.code(), c);
67 break;
68 }
69 currencies_.push_back(c);
70
71 } catch (std::exception&) {
72 ALOG("error loading currency config for name " << name << " iso code " << isoCode);
73 }
74 }
75}
76
78 XMLNode* node = doc.allocNode("CurrencyConfig");
79 for (auto ccy : currencies_) {
80 XMLNode* ccyNode = XMLUtils::addChild(doc, node, "Currency");
81 XMLUtils::addChild(doc, ccyNode, "Name", ccy.name());
82 XMLUtils::addChild(doc, ccyNode, "ISOCode", ccy.code());
84 doc, ccyNode, "MinorUnitCodes",
85 std::vector<std::string>(ccy.minorUnitCodes().begin(), ccy.minorUnitCodes().end()));
86 XMLUtils::addChild(doc, ccyNode, "NumericCode", to_string(ccy.numericCode()));
87 XMLUtils::addChild(doc, ccyNode, "Symbol", ccy.symbol());
88 XMLUtils::addChild(doc, ccyNode, "FractionSymbol", ccy.fractionSymbol());
89 XMLUtils::addChild(doc, ccyNode, "RoundingType", to_string(ccy.rounding().type()));
90 XMLUtils::addChild(doc, ccyNode, "RoundingPrecision", to_string(ccy.rounding().precision()));
91 // XMLUtils::addChild(doc, ccyNode, "Digit", to_string(ccy.rounding().roundingDigit()));
92 XMLUtils::addChild(doc, ccyNode, "Format", ccy.format());
93 XMLUtils::addChild(doc, ccyNode, "CurrencyType", to_string(ccy.currencyType()));
94 }
95 return node;
96}
97
98} // namespace data
99} // namespace ore
void fromXML(XMLNode *node) override
XMLNode * toXML(XMLDocument &doc) const override
vector< ConfigurableCurrency > currencies_
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
static void addGenericChildAsList(XMLDocument &doc, XMLNode *n, const string &name, const vector< T > &values, const string &attrName="", const string &attr="")
Definition: xmlutils.hpp:144
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 * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
currency parser singleton class
QuantExt::ConfigurableCurrency::Type parseCurrencyType(const string &s)
Convert text to QuantExt::ConfigurableCurrency::Type (Major, Minor, Metal, Crypto)
Definition: parsers.cpp:292
QuantLib::Rounding::Type parseRoundingType(const std::string &s)
Convert text to QuantLib::Rounding.
Definition: parsers.cpp:1027
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544
std::vector< string > parseListOfValues(string s, const char escape, const char delim, const char quote)
Definition: parsers.cpp:639
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.
string conversion utilities
string name