Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
currencyparser.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 Quaternion Risk Management Ltd
3
4 This file is part of ORE, a free-software/open-source library
5 for transparent pricing and risk analysis - http://opensourcerisk.org
6
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 The license is also available online at <http://opensourcerisk.org>
11
12 This program is distributed on the basis that it will form a useful
13 contribution to risk analytics and model standardisation, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
16*/
17
18/*! \file ored/utilities/calendarparser.hpp
19 \brief calendar parser singleton class
20 \ingroup utilities
21*/
22
24
31
32#include <ql/currencies/all.hpp>
33#include <ql/errors.hpp>
34
35#include <boost/algorithm/string.hpp>
36
37namespace ore {
38namespace data {
39
40using namespace QuantLib;
41using namespace QuantExt;
42
44
45QuantLib::Currency CurrencyParser::parseCurrency(const std::string& name) const {
46 boost::shared_lock<boost::shared_mutex> lock(mutex_);
47 {
48 auto it = currencies_.find(name);
49 if (it != currencies_.end()) {
50 return it->second;
51 }
52 }
53 {
54 auto it = preciousMetals_.find(name);
55 if (it != preciousMetals_.end()) {
56 return it->second;
57 }
58 }
59 {
60 auto it = cryptoCurrencies_.find(name);
61 if (it != cryptoCurrencies_.end()) {
62 return it->second;
63 }
64 }
65 QL_FAIL("Currency \"" << name << "\" not recognized");
66}
67
68QuantLib::Currency CurrencyParser::parseMinorCurrency(const std::string& name) const {
69 boost::shared_lock<boost::shared_mutex> lock(mutex_);
70 auto it = minorCurrencies_.find(name);
71 if (it != minorCurrencies_.end()) {
72 return it->second;
73 }
74 QL_FAIL("Currency \"" << name << "\" not recognized");
75}
76
77QuantLib::Currency CurrencyParser::parseCurrencyWithMinors(const std::string& name) const {
78 try {
79 return parseCurrency(name);
80 } catch (...) {
81 }
83}
84
85std::pair<QuantLib::Currency, QuantLib::Currency>
86CurrencyParser::parseCurrencyPair(const std::string& name, const std::string& delimiters) const {
87 std::vector<std::string> tokens;
88 tokens = boost::split(tokens, name, boost::is_any_of(delimiters));
89 if (tokens.size() == 1) {
90 if (tokens[0].size() > 6) {
91 QL_FAIL("Failed to parse currency pair (" << tokens[0] << ")");
92 }
93
94 QuantLib::Currency ccy1 = parseCurrency(tokens[0].substr(0, 3));
95 QuantLib::Currency ccy2 = parseCurrency(tokens[0].substr(3));
96 return std::make_pair(ccy1, ccy2);
97 } else if (tokens.size() == 2) {
98 try {
99 QuantLib::Currency ccy1 = parseCurrency(tokens[0]);
100 QuantLib::Currency ccy2 = parseCurrency(tokens[1]);
101 return std::make_pair(ccy1, ccy2);
102 } catch (const std::exception& e) {
103 QL_FAIL("Failed to parse currency pair (" << name << "): " << e.what());
104 }
105 } else {
106 QL_FAIL("Failed to parse currency pair (" << name << ")");
107 }
108}
109
110bool CurrencyParser::isValidCurrency(const std::string& name) const {
111 try {
113 return true;
114 } catch (...) {
115 }
116 return false;
117}
118
119bool CurrencyParser::isMinorCurrency(const std::string& name) const {
120 boost::shared_lock<boost::shared_mutex> lock(mutex_);
121 return minorCurrencies_.find(name) != minorCurrencies_.end();
122}
123
124bool CurrencyParser::isPseudoCurrency(const std::string& name) const {
126}
127
128bool CurrencyParser::isPreciousMetal(const std::string& name) const {
129 boost::shared_lock<boost::shared_mutex> lock(mutex_);
130 return preciousMetals_.find(name) != preciousMetals_.end();
131}
132
133bool CurrencyParser::isCryptoCurrency(const std::string& name) const {
134 boost::shared_lock<boost::shared_mutex> lock(mutex_);
135 return cryptoCurrencies_.find(name) != cryptoCurrencies_.end();
136}
137
138bool CurrencyParser::hasMinorCurrency(const std::string& name) const {
139 boost::shared_lock<boost::shared_mutex> lock(mutex_);
140 for (auto const& c : minorCurrencies_) {
141 if (c.second.code() == name)
142 return true;
143 }
144 return false;
145}
146
147std::string CurrencyParser::getMinorCurrency(const std::string& name) const {
148 boost::shared_lock<boost::shared_mutex> lock(mutex_);
149 for (auto const& c : minorCurrencies_) {
150 if (c.second.code() == name)
151 return c.first;
152 }
153 QL_FAIL("no minor currency found for '" << name << "'");
154}
155
156std::set<std::string> CurrencyParser::pseudoCurrencyCodes() const {
157 boost::shared_lock<boost::shared_mutex> lock(mutex_);
158 std::set<std::string> tmp;
159 for (auto const& c : preciousMetals_)
160 tmp.insert(c.first);
161 for (auto const& c : cryptoCurrencies_)
162 tmp.insert(c.first);
163 return tmp;
164}
165
166QuantLib::Real CurrencyParser::convertMinorToMajorCurrency(const std::string& s, QuantLib::Real value) {
167 if (isMinorCurrency(s)) {
168 QuantLib::Currency ccy = parseMinorCurrency(s);
169 return value / ccy.fractionsPerUnit();
170 } else {
171 return value;
172 }
173}
174
175void CurrencyParser::addCurrency(const std::string& newName, const QuantLib::Currency& currency) {
176 boost::unique_lock<boost::shared_mutex> lock(mutex_);
177 if (currencies_.find(newName) != currencies_.end() || preciousMetals_.find(newName) != preciousMetals_.end() ||
178 cryptoCurrencies_.find(newName) != cryptoCurrencies_.end())
179 return;
180 currencies_[newName] = currency;
181 addMinorCurrencyCodes(currency);
182}
183
184void CurrencyParser::addMetal(const std::string& newName, const QuantLib::Currency& currency) {
185 boost::unique_lock<boost::shared_mutex> lock(mutex_);
186 if (currencies_.find(newName) != currencies_.end() || preciousMetals_.find(newName) != preciousMetals_.end() ||
187 cryptoCurrencies_.find(newName) != cryptoCurrencies_.end())
188 return;
189 preciousMetals_[newName] = currency;
190}
191
192void CurrencyParser::addCrypto(const std::string& newName, const QuantLib::Currency& currency) {
193 boost::unique_lock<boost::shared_mutex> lock(mutex_);
194 if (currencies_.find(newName) != currencies_.end() || preciousMetals_.find(newName) != preciousMetals_.end() ||
195 cryptoCurrencies_.find(newName) != cryptoCurrencies_.end())
196 return;
197 cryptoCurrencies_[newName] = currency;
198}
199
200void CurrencyParser::addMinorCurrencyCodes(const QuantLib::Currency& currency) {
201 for (auto const& c : currency.minorUnitCodes()) {
202 minorCurrencies_[c] = currency;
203 }
204}
205
207 boost::unique_lock<boost::shared_mutex> lock(mutex_);
208
209 currencies_ = {{"AED", AEDCurrency()}, {"AOA", AOACurrency()}, {"ARS", ARSCurrency()}, {"ATS", ATSCurrency()},
210 {"AUD", AUDCurrency()}, {"BEF", BEFCurrency()}, {"BGN", BGNCurrency()}, {"BHD", BHDCurrency()},
211 {"BRL", BRLCurrency()}, {"BWP", BWPCurrency()}, {"CAD", CADCurrency()}, {"CHF", CHFCurrency()},
212 {"CLF", CLFCurrency()}, {"CLP", CLPCurrency()}, {"CNH", CNHCurrency()}, {"CNY", CNYCurrency()},
213 {"COP", COPCurrency()}, {"COU", COUCurrency()}, {"CZK", CZKCurrency()}, {"DEM", DEMCurrency()},
214 {"DKK", DKKCurrency()}, {"EGP", EGPCurrency()}, {"ESP", ESPCurrency()}, {"ETB", ETBCurrency()},
215 {"EUR", EURCurrency()}, {"FIM", FIMCurrency()}, {"FRF", FRFCurrency()}, {"GBP", GBPCurrency()},
216 {"GEL", GELCurrency()}, {"GHS", GHSCurrency()}, {"GRD", GRDCurrency()}, {"HKD", HKDCurrency()},
217 {"HRK", HRKCurrency()}, {"HUF", HUFCurrency()}, {"IDR", IDRCurrency()}, {"IEP", IEPCurrency()},
218 {"ILS", ILSCurrency()}, {"INR", INRCurrency()}, {"ISK", ISKCurrency()}, {"ITL", ITLCurrency()},
219 {"JOD", JODCurrency()}, {"JPY", JPYCurrency()}, {"KES", KESCurrency()}, {"KRW", KRWCurrency()},
220 {"KWD", KWDCurrency()}, {"KZT", KZTCurrency()}, {"LKR", LKRCurrency()}, {"LUF", LUFCurrency()},
221 {"MAD", MADCurrency()}, {"MUR", MURCurrency()}, {"MXN", MXNCurrency()}, {"MXV", MXVCurrency()},
222 {"MYR", MYRCurrency()}, {"NGN", NGNCurrency()}, {"NLG", NLGCurrency()}, {"NOK", NOKCurrency()},
223 {"NZD", NZDCurrency()}, {"OMR", OMRCurrency()}, {"PEN", PENCurrency()}, {"PHP", PHPCurrency()},
224 {"PKR", PKRCurrency()}, {"PLN", PLNCurrency()}, {"PTE", PTECurrency()}, {"QAR", QARCurrency()},
225 {"RON", RONCurrency()}, {"RSD", RSDCurrency()}, {"RUB", RUBCurrency()}, {"SAR", SARCurrency()},
226 {"SEK", SEKCurrency()}, {"SGD", SGDCurrency()}, {"THB", THBCurrency()}, {"TND", TNDCurrency()},
227 {"TRY", TRYCurrency()}, {"TWD", TWDCurrency()}, {"UAH", UAHCurrency()}, {"UGX", UGXCurrency()},
228 {"USD", USDCurrency()}, {"UYU", UYUCurrency()}, {"VND", VNDCurrency()}, {"XOF", XOFCurrency()},
229 {"ZAR", ZARCurrency()}, {"ZMW", ZMWCurrency()}};
230
231 minorCurrencies_ = {{"GBp", GBPCurrency()}, {"GBX", GBPCurrency()}, {"ILa", ILSCurrency()}, {"ILX", ILSCurrency()},
232 {"ILs", ILSCurrency()}, {"KWf", KWDCurrency()}, {"ILA", ILSCurrency()}, {"ZAc", ZARCurrency()},
233 {"ZAC", ZARCurrency()}, {"ZAX", ZARCurrency()}};
234
235 preciousMetals_ = {{"XAG", XAGCurrency()}, {"XAU", XAUCurrency()}, {"XPT", XPTCurrency()}, {"XPD", XPDCurrency()}};
236
237 cryptoCurrencies_ = {{"XBT", BTCCurrency()}, {"BTC", BTCCurrency()}, {"ETH", ETHCurrency()}, {"ETC", ETCCurrency()},
238 {"BCH", BCHCurrency()}, {"XRP", XRPCurrency()}, {"LTC", LTCCurrency()}};
239
240 for (auto const& c : currencies_) {
241 addMinorCurrencyCodes(c.second);
242 }
243}
244
245} // namespace data
246} // namespace ore
std::set< std::string > pseudoCurrencyCodes() const
void addCrypto(const std::string &newName, const QuantLib::Currency &currency)
void addMinorCurrencyCodes(const QuantLib::Currency &currency)
std::map< std::string, QuantLib::Currency > minorCurrencies_
bool isCryptoCurrency(const std::string &name) const
std::string getMinorCurrency(const std::string &name) const
bool isPreciousMetal(const std::string &name) const
QuantLib::Real convertMinorToMajorCurrency(const std::string &s, QuantLib::Real value)
std::map< std::string, QuantLib::Currency > cryptoCurrencies_
std::map< std::string, QuantLib::Currency > currencies_
bool isPseudoCurrency(const std::string &name) const
bool isMinorCurrency(const std::string &name) const
std::pair< QuantLib::Currency, QuantLib::Currency > parseCurrencyPair(const std::string &name, const std::string &delimiters) const
QuantLib::Currency parseCurrency(const std::string &name) const
QuantLib::Currency parseCurrencyWithMinors(const std::string &name) const
bool isValidCurrency(const std::string &name) const
QuantLib::Currency parseMinorCurrency(const std::string &name) const
boost::shared_mutex mutex_
std::map< std::string, QuantLib::Currency > preciousMetals_
void addCurrency(const std::string &newName, const QuantLib::Currency &currency)
void addMetal(const std::string &newName, const QuantLib::Currency &currency)
bool hasMinorCurrency(const std::string &name) const
SafeStack< ValueType > value
currency parser singleton class
@ data
Definition: log.hpp:77
Size size(const ValueType &v)
Definition: value.cpp:145
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string name