Skip to content

Commit 269845d

Browse files
damienbarkerjenkins
authored andcommitted
Merge remote-tracking branch 'origin/master' into QPR-12235
2 parents 48dc48f + 57cf5aa commit 269845d

11 files changed

Lines changed: 280 additions & 23 deletions

Examples/Input/currencies.xml

Lines changed: 189 additions & 0 deletions
Large diffs are not rendered by default.

OREData/ored/utilities/currencyconfig.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,24 @@ void CurrencyConfig::fromXML(XMLNode* baseNode) {
4949
// the digit where we switch form roundng down to rounding up, typically 5 and used across all
5050
// Integer digit = parseInteger(XMLUtils::getChildValue(node, "Digit", false));
5151
string format = XMLUtils::getChildValue(node, "Format", false);
52+
QuantExt::ConfigurableCurrency::Type currencyType = parseCurrencyType(XMLUtils::getChildValue(node, "CurrencyType", false, "Major"));
5253
Rounding rounding(precision, roundingType);
53-
5454
QuantExt::ConfigurableCurrency c(name, isoCode, numericCode, symbol, fractionSymbol, fractionsPerUnit,
55-
rounding, format, minorUnitCodes);
56-
currencies_.push_back(c);
57-
55+
rounding, format, minorUnitCodes, currencyType);
5856
DLOG("loading configuration for currency code " << isoCode);
5957

60-
CurrencyParser::instance().addCurrency(c.code(), c);
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);
6170

6271
} catch (std::exception&) {
6372
ALOG("error loading currency config for name " << name << " iso code " << isoCode);
@@ -81,6 +90,7 @@ XMLNode* CurrencyConfig::toXML(XMLDocument& doc) {
8190
XMLUtils::addChild(doc, ccyNode, "RoundingPrecision", to_string(ccy.rounding().precision()));
8291
// XMLUtils::addChild(doc, ccyNode, "Digit", to_string(ccy.rounding().roundingDigit()));
8392
XMLUtils::addChild(doc, ccyNode, "Format", ccy.format());
93+
XMLUtils::addChild(doc, ccyNode, "CurrencyType", to_string(ccy.currencyType()));
8494
}
8595
return node;
8696
}

OREData/ored/utilities/currencyconfig.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <map>
2727
#include <ored/utilities/xmlutils.hpp>
28+
#include <qle/currencies/configurablecurrency.hpp>
2829
#include <ql/patterns/singleton.hpp>
2930
#include <ql/currency.hpp>
3031
#include <ql/utilities/dataparsers.hpp>
@@ -34,6 +35,7 @@
3435

3536
namespace ore {
3637
namespace data {
38+
using QuantExt::ConfigurableCurrency;
3739
using QuantLib::Currency;
3840
using std::map;
3941
using std::set;
@@ -50,13 +52,13 @@ class CurrencyConfig : public XMLSerializable {
5052
CurrencyConfig();
5153

5254
//! check out any configured currencies
53-
const vector<Currency>& getCurrencies() const { return currencies_; }
55+
const vector<ConfigurableCurrency>& getCurrencies() const { return currencies_; }
5456

5557
void fromXML(XMLNode* node) override;
5658
XMLNode* toXML(XMLDocument& doc) override;
5759

5860
private:
59-
vector<Currency> currencies_;
61+
vector<ConfigurableCurrency> currencies_;
6062
};
6163

6264
} // namespace data

OREData/ored/utilities/currencyhedgedequityindexdecomposition.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,13 @@ loadCurrencyHedgedIndexDecomposition(const std::string& name, const boost::share
2020
std::map<std::string, std::pair<double, std::string>> currencyWeightsAndFxIndexNames;
2121

2222
if (refDataMgr) {
23-
try {
23+
if (refDataMgr->hasData("CurrencyHedgedEquityIndex", name))
2424
indexRefData = boost::dynamic_pointer_cast<CurrencyHedgedEquityIndexReferenceDatum>(
2525
refDataMgr->getData("CurrencyHedgedEquityIndex", name));
26-
} catch (...) {
27-
// Index not a CurrencyHedgedEquityIndex or referencedata is missing, don't throw here
28-
}
2926

30-
if (indexRefData != nullptr) {
31-
try {
32-
underlyingRefData = boost::dynamic_pointer_cast<EquityIndexReferenceDatum>(
33-
refDataMgr->getData("EquityIndex", indexRefData->underlyingIndexName()));
34-
} catch (...) {
35-
// referencedata is missing, but don't throw here, return null ptr
36-
}
37-
}
27+
if (indexRefData != nullptr && refDataMgr->hasData("EquityIndex", indexRefData->underlyingIndexName()))
28+
underlyingRefData = boost::dynamic_pointer_cast<EquityIndexReferenceDatum>(
29+
refDataMgr->getData("EquityIndex", indexRefData->underlyingIndexName()));
3830
}
3931

4032
if (indexRefData == nullptr || underlyingRefData == nullptr) {

OREData/ored/utilities/currencyparser.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@ void CurrencyParser::addCurrency(const std::string& newName, const QuantLib::Cur
181181
addMinorCurrencyCodes(currency);
182182
}
183183

184+
void 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+
192+
void 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+
184200
void CurrencyParser::addMinorCurrencyCodes(const QuantLib::Currency& currency) {
185201
for (auto const& c : currency.minorUnitCodes()) {
186202
minorCurrencies_[c] = currency;

OREData/ored/utilities/currencyparser.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class CurrencyParser : public QuantLib::Singleton<CurrencyParser, std::integral_
4444
const std::string& delimiters) const;
4545

4646
void addCurrency(const std::string& newName, const QuantLib::Currency& currency);
47+
void addMetal(const std::string& newName, const QuantLib::Currency& currency);
48+
void addCrypto(const std::string& newName, const QuantLib::Currency& currency);
4749

4850
bool isValidCurrency(const std::string& name) const;
4951
bool isMinorCurrency(const std::string& name) const;

OREData/ored/utilities/parsers.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,22 @@ DayCounter parseDayCounter(const string& s) {
284284

285285
Currency parseCurrency(const string& s) { return CurrencyParser::instance().parseCurrency(s); }
286286

287+
QuantExt::ConfigurableCurrency::Type parseCurrencyType(const string& s) {
288+
static map<string, QuantExt::ConfigurableCurrency::Type> m = {
289+
{"Major", QuantExt::ConfigurableCurrency::Type::Major}, {"Fiat Currency", QuantExt::ConfigurableCurrency::Type::Major},
290+
{"Fiat", QuantExt::ConfigurableCurrency::Type::Major}, {"Metal", QuantExt::ConfigurableCurrency::Type::Metal},
291+
{"Precious Metal", QuantExt::ConfigurableCurrency::Type::Metal}, {"Crypto", QuantExt::ConfigurableCurrency::Type::Crypto},
292+
{"Cryptocurrency", QuantExt::ConfigurableCurrency::Type::Crypto}};
293+
294+
auto it = m.find(s);
295+
if (it != m.end()) {
296+
return it->second;
297+
} else {
298+
QL_FAIL("Currency type \"" << s << "\" not recognised");
299+
}
300+
}
301+
302+
287303
Currency parseMinorCurrency(const string& s) { return CurrencyParser::instance().parseMinorCurrency(s); }
288304

289305
Currency parseCurrencyWithMinors(const string& s) { return CurrencyParser::instance().parseCurrencyWithMinors(s); }

OREData/ored/utilities/parsers.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ QuantLib::DayCounter parseDayCounter(const string& s);
142142
*/
143143
QuantLib::Currency parseCurrency(const string& s);
144144

145+
//! Convert text to QuantExt::ConfigurableCurrency::Type (Major, Minor, Metal, Crypto)
146+
/*!
147+
\ingroup utilities
148+
*/
149+
QuantExt::ConfigurableCurrency::Type parseCurrencyType(const string& s);
150+
145151
//! Convert text to QuantLib::Currency for minor currencies e.g GBp -> GBPCurrency()
146152
/*!
147153
\ingroup utilities

QuantExt/qle/currencies/configurablecurrency.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,26 @@ using namespace QuantLib;
2626
ConfigurableCurrency::ConfigurableCurrency(const std::string& name, const std::string& code, Integer numericCode,
2727
const std::string& symbol, const std::string& fractionSymbol,
2828
Integer fractionsPerUnit, const Rounding& rounding,
29-
const std::string& formatString,
30-
const std::set<std::string>& minorUnitCodes) {
29+
const std::string& formatString, const std::set<std::string>& minorUnitCodes,
30+
ConfigurableCurrency::Type currencyType)
31+
: Currency(name, code, numericCode, symbol, fractionSymbol, fractionsPerUnit, rounding, formatString, Currency(),
32+
minorUnitCodes),
33+
currencyType_(currencyType) {
3134
data_ = boost::make_shared<Currency::Data>(name, code, numericCode, symbol, fractionSymbol, fractionsPerUnit,
3235
rounding, formatString, Currency(), minorUnitCodes);
3336
}
3437

38+
std::ostream& operator<<(std::ostream& os, ConfigurableCurrency::Type ccytype) {
39+
switch (ccytype) {
40+
case ConfigurableCurrency::Type::Major:
41+
return os << "Major";
42+
case ConfigurableCurrency::Type::Metal:
43+
return os <<"Metal";
44+
case ConfigurableCurrency::Type::Crypto:
45+
return os << "Crypto";
46+
default:
47+
QL_FAIL("Unknown AssetClass");
48+
}
49+
}
50+
3551
} // namespace QuantExt

QuantExt/qle/currencies/configurablecurrency.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ using namespace QuantLib;
3535
*/
3636
class ConfigurableCurrency : public Currency {
3737
public:
38+
enum class Type { Major, Metal, Crypto };
3839
ConfigurableCurrency(const std::string& name, const std::string& code, Integer numericCode,
3940
const std::string& symbol, const std::string& fractionSymbol, Integer fractionsPerUnit,
40-
const Rounding& rounding, const std::string& formatString,
41-
const std::set<std::string>& minorUnitCodes);
41+
const Rounding& rounding, const std::string& formatString,
42+
const std::set<std::string>& minorUnitCodes, Type currencyType = Type::Major);
43+
Type currencyType() const { return currencyType_; }
44+
45+
private:
46+
ConfigurableCurrency::Type currencyType_;
4247
};
4348

49+
std::ostream& operator<<(std::ostream& os, QuantExt::ConfigurableCurrency::Type ccytype);
50+
4451
} // namespace QuantExt
4552
#endif

0 commit comments

Comments
 (0)