27#include <boost/algorithm/string.hpp>
29using QuantLib::DefaultProbabilityTermStructure;
30using QuantLib::Handle;
31using QuantLib::YieldTermStructure;
41Handle<YieldTermStructure>
xccyYieldCurve(
const QuantLib::ext::shared_ptr<Market>& market,
const string& ccyCode,
42 const string& configuration) {
47Handle<YieldTermStructure>
xccyYieldCurve(
const QuantLib::ext::shared_ptr<Market>& market,
const string& ccyCode,
48 bool& outXccyExists,
const string& configuration) {
50 Handle<YieldTermStructure> curve;
54 curve = market->yieldCurve(xccyCurve, configuration);
55 }
catch (
const Error&) {
56 DLOG(
"Could not link " << ccyCode <<
" termstructure to cross currency yield curve " << xccyCurve
57 <<
" so just using " << ccyCode <<
" discount curve.");
58 curve = market->discountCurve(ccyCode, configuration);
59 outXccyExists =
false;
65Handle<YieldTermStructure>
indexOrYieldCurve(
const QuantLib::ext::shared_ptr<Market>& market,
const std::string&
name,
66 const std::string& configuration) {
68 return market->iborIndex(
name, configuration)->forwardingTermStructure();
72 return market->yieldCurve(
name, configuration);
75 QL_FAIL(
"Could not find index or yield curve with name '" <<
name <<
"' under configuration '" << configuration
76 <<
"' or default configuration.");
80 auto tmp =
"__SECCRCRV_" + securityId +
"_&_" + creditCurveId +
"_&_";
85 if (boost::starts_with(
name,
"__SECCRCRV_")) {
86 std::size_t pos =
name.find(
"_&_", 11);
87 if (pos != std::string::npos) {
88 std::size_t pos2 =
name.find(
"_&_", pos + 3);
89 if (pos2 != std::string::npos) {
90 auto tmp =
name.substr(pos + 3, pos2 - (pos + 3));
99 const std::string& securityId,
100 const std::string& creditCurveId,
101 const std::string& configuration) {
102 Handle<QuantExt::CreditCurve> curve;
105 curve = market->defaultCurve(
name, configuration);
106 }
catch (
const std::exception&) {
107 DLOG(
"Could not link " << securityId <<
" to security specific credit curve " <<
name <<
" so just using "
108 << creditCurveId <<
" default curve.");
109 curve = market->defaultCurve(creditCurveId, configuration);
119 std::size_t pos2 =
name.find(
"__SECCRCRV_", pos);
120 if (pos2 != std::string::npos) {
121 std::size_t pos3 =
name.find(
"_&_", pos2);
122 if (pos3 != std::string::npos) {
123 std::size_t pos4 =
name.find(
"_&_", pos3 + 3);
124 if (pos4 != std::string::npos) {
125 name.replace(pos2, pos4 + 3 - pos2,
126 name.substr(pos2 + 11, pos3 - (pos2 + 11)) +
"(" +
127 name.substr(pos3 + 3, pos4 - (pos3 + 3)) +
")");
128 pos = pos + (pos4 - pos2 - 12);
137QuantLib::ext::shared_ptr<QuantExt::FxIndex>
buildFxIndex(
const string& fxIndex,
const string& domestic,
const string& foreign,
138 const QuantLib::ext::shared_ptr<Market>& market,
const string& configuration,
143 string source = fxInd->sourceCurrency().code();
144 string target = fxInd->targetCurrency().code();
145 string family = fxInd->familyName();
147 fxInd = *market->fxIndex(
"FX-" + family +
"-" + foreign +
"-" + domestic);
149 QL_REQUIRE((domestic == target && foreign == source) || (domestic == source && foreign == target),
150 "buildFxIndex(): index '" << fxIndex <<
"' does not match given currencies " << domestic <<
", "
156 return fxInd->clone(Handle<Quote>(),
xccyYieldCurve(market, foreign, configuration),
166 ccy1 = ind->sourceCurrency().code();
167 ccy2 = ind->targetCurrency().code();
168 fixingSource = ind->familyName();
170 QL_REQUIRE(index.size() == 6,
"getFxIndexConventions: index must be an FXIndex of form FX-ECB-EUR-USD, "
171 <<
"or a currency pair e.g. EURUSD, got '" + index +
"'");
172 ccy1 = index.substr(0, 3);
173 ccy2 = index.substr(3);
174 fixingSource =
"GENERIC";
178 return std::make_tuple(0, NullCalendar(), Unadjusted);
180 const QuantLib::ext::shared_ptr<Conventions>& conventions = InstrumentConventions::instance().conventions();
181 QuantLib::ext::shared_ptr<Convention> con;
184 con = conventions->get(
"FX-" + fixingSource +
"-" + ccy1 +
"-" + ccy2);
187 if (con ==
nullptr) {
189 con = conventions->get(
"FX-" + fixingSource +
"-" + ccy2 +
"-" + ccy1);
194 if (con ==
nullptr) {
196 con = conventions->getFxConvention(ccy1, ccy2);
200 if (
auto fxCon = QuantLib::ext::dynamic_pointer_cast<FXConvention>(con)) {
201 TLOG(
"getFxIndexConvention(" << index <<
"): " << fxCon->spotDays() <<
" / " << fxCon->advanceCalendar().name()
202 <<
" from convention.");
203 return std::make_tuple(fxCon->spotDays(), fxCon->advanceCalendar(), fxCon->convention());
204 }
else if (
auto comCon = QuantLib::ext::dynamic_pointer_cast<CommodityForwardConvention>(con); comCon !=
nullptr
206 TLOG(
"getFxIndexConvention(" << index <<
"): " << fxCon->spotDays() <<
" / " << fxCon->advanceCalendar().name()
207 <<
" from convention.");
208 return std::make_tuple(0, comCon->advanceCalendar(), comCon->bdc());
219 TLOG(
"getFxIndexConvention(" << index <<
"): 2 (default) / " << cal.name()
220 <<
" (from ccys), no convention found.");
221 return std::make_tuple(2, cal, Following);
222 }
catch (
const std::exception& e) {
223 ALOG(
"could not get fx index convention for '" << index <<
"': " << e.what() <<
", continue with 'USD'");
225 TLOG(
"getFxIndexConvention(" << index
226 <<
"): 2 (default) / USD (default), no convention found, could not parse calendar '"
227 << (ccy1 +
"," + ccy2) <<
"'");
232 Size pos = creditCurveId.rfind(
"_");
233 if (pos != std::string::npos) {
235 string termString = creditCurveId.substr(pos + 1, creditCurveId.length());
236 if (tryParse<Period>(termString, term,
parsePeriod)) {
237 return make_pair(creditCurveId.substr(0, pos), term);
240 return make_pair(creditCurveId, 0 * Days);
244 const std::string& creditCurveId,
245 const std::string& config) {
247 return market->defaultCurve(creditCurveId, config);
249 DLOG(
"indexCdsDefaultCurve: could not get '" << creditCurveId <<
"', fall back on curve id without tenor.");
253 return market->defaultCurve(p.first, config);
Currency and instrument specific conventions/defaults.
currency parser singleton class
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
QuantLib::ext::shared_ptr< FxIndex > parseFxIndex(const string &s, const Handle< Quote > &fxSpot, const Handle< YieldTermStructure > &sourceYts, const Handle< YieldTermStructure > &targetYts, const bool useConventions)
Convert std::string to QuantExt::FxIndex.
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
bool isPseudoCurrency(const string &code)
check for pseudo currency = precious metal or crypto currency */
Map text representations to QuantLib/QuantExt types.
Classes and functions for log message handling.
#define DLOG(text)
Logging Macro (Level = Debug)
#define ALOG(text)
Logging Macro (Level = Alert)
#define TLOG(text)
Logging Macro (Level = Data)
market data related utilties
Handle< YieldTermStructure > xccyYieldCurve(const QuantLib::ext::shared_ptr< Market > &market, const string &ccyCode, const string &configuration)
bool isFxIndex(const std::string &indexName)
std::string creditCurveNameFromSecuritySpecificCreditCurveName(const std::string &name)
QuantLib::Handle< QuantExt::CreditCurve > indexCdsDefaultCurve(const QuantLib::ext::shared_ptr< Market > &market, const std::string &creditCurveId, const std::string &config)
std::string prettyPrintInternalCurveName(std::string name)
Handle< YieldTermStructure > indexOrYieldCurve(const QuantLib::ext::shared_ptr< Market > &market, const std::string &name, const std::string &configuration)
string xccyCurveName(const string &ccyCode)
std::tuple< Natural, Calendar, BusinessDayConvention > getFxIndexConventions(const string &index)
std::pair< std::string, QuantLib::Period > splitCurveIdWithTenor(const std::string &creditCurveId)
QuantLib::Handle< QuantExt::CreditCurve > securitySpecificCreditCurve(const QuantLib::ext::shared_ptr< Market > &market, const std::string &securityId, const std::string &creditCurveId, const std::string &configuration)
QuantLib::ext::shared_ptr< QuantExt::FxIndex > buildFxIndex(const string &fxIndex, const string &domestic, const string &foreign, const QuantLib::ext::shared_ptr< Market > &market, const string &configuration, bool useXbsCurves)
const string xccyCurveNamePrefix
std::string securitySpecificCreditCurveName(const std::string &securityId, const std::string &creditCurveId)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.
string conversion utilities