19#include <boost/algorithm/string.hpp>
20#include <boost/make_shared.hpp>
44template <
class A,
class B,
class C>
45A lookup(
const B& map,
const C& key,
const string& configuration,
const string& type,
bool continueOnError =
false) {
46 auto it = map.find(make_pair(configuration, key));
47 if (it == map.end()) {
50 if (it == map.end()) {
52 QL_FAIL(
"did not find object '" << key <<
"' of type " << type
53 <<
" under configuration '" << configuration <<
"' or 'default'");
61template <
class A,
class B,
class C>
62A lookup(
const B& map,
const C& key,
const YieldCurveType y,
const string& configuration,
const string& type) {
63 auto it = map.find(make_tuple(configuration, y, key));
64 if (it == map.end()) {
67 QL_REQUIRE(it != map.end(),
"did not find object " << key <<
" of type " << type <<
" under configuration '"
68 << configuration <<
"' or 'default' in YieldCurves");
75 const string& configuration)
const {
78 QuantLib::ext::shared_ptr<IborIndex> notUsed;
80 return iborIndex(key, configuration)->forwardingTermStructure();
83 DLOG(
"no ibor index found under '" << key <<
"' - look for a genuine yield curve");
91 QL_FAIL(
"yield curve type not handled");
93 return lookup<Handle<YieldTermStructure>>(
yieldCurves_, key, type, configuration,
"yield curve / ibor index");
109 return lookup<Handle<IborIndex>>(
iborIndices_, key, configuration,
"ibor index");
114 return lookup<Handle<SwapIndex>>(
swapIndices_, key, configuration,
"swap index");
118 const string& configuration)
const {
131 QuantLib::ext::shared_ptr<IborIndex> index;
133 QL_FAIL(
"did not find swaption curve for key '" << key <<
"'");
135 auto ccy = index->currency().code();
148 QL_FAIL(
"did not find swaption curve for key '" << key <<
"'");
164 QuantLib::ext::shared_ptr<IborIndex> index;
166 QL_FAIL(
"did not find swaption index bases for key '" << key <<
"'");
168 auto ccy = index->currency().code();
181 QL_FAIL(
"did not find swaption index bases for key '" << key <<
"'");
193 const string& configuration)
const {
195 return lookup<Handle<QuantLib::SwaptionVolatilityStructure>>(
yieldVolCurves_, key, configuration,
196 "yield volatility curve");
200 QL_REQUIRE(
fx_ !=
nullptr,
201 "MarketImpl::fxIndex(" <<
fxIndex <<
"): fx_ is null. This is an internal error. Contact dev.");
202 return fx_->getIndex(
fxIndex,
this, configuration);
207 if (ccypair.substr(0,3) == ccypair.substr(3))
208 return Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(1.0));
209 return fxIndex(ccypair, configuration)->fxQuote();
213 if (ccypair.substr(0, 3) == ccypair.substr(3))
214 return Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(1.0));
215 return fxIndex(ccypair, configuration)->fxQuote(
true);
220 auto it =
fxVols_.find(make_pair(configuration, ccypair));
225 QL_REQUIRE(ccypair.length() == 6,
"invalid ccy pair length");
226 std::string ccypairInverted = ccypair.substr(3, 3) + ccypair.substr(0, 3);
228 it =
fxVols_.find(make_pair(configuration, ccypairInverted));
230 Handle<BlackVolTermStructure> h(QuantLib::ext::make_shared<QuantExt::BlackInvertedVolTermStructure>(it->second));
231 h->enableExtrapolation();
234 fxVols_[make_pair(configuration, ccypair)] = h;
238 QL_FAIL(
"did not find fx vol object " << ccypair);
247 return lookup<Handle<QuantExt::CreditCurve>>(
defaultCurves_, key, configuration,
"default curve");
254 return lookup<Handle<Quote>>(
recoveryRates_, key, configuration,
"recovery rate");
257Handle<QuantExt::CreditVolCurve>
MarketImpl::cdsVol(
const string& key,
const string& configuration)
const {
259 return lookup<Handle<QuantExt::CreditVolCurve>>(
cdsVols_, key, configuration,
"cds vol curve");
262Handle<QuantExt::BaseCorrelationTermStructure>
265 return lookup<Handle<QuantExt::BaseCorrelationTermStructure>>(
baseCorrelations_, key, configuration,
266 "base correlation curve");
282 QuantLib::ext::shared_ptr<IborIndex> index;
284 QL_FAIL(
"did not find capfloor curve for key '" << key <<
"'");
286 auto ccy = index->currency().code();
299 QL_FAIL(
"did not find capfloor curve for key '" << key <<
"'");
303 const string& configuration)
const {
316 QuantLib::ext::shared_ptr<IborIndex> index;
318 return std::make_pair(
string(),0*Days);
320 auto ccy = index->currency().code();
333 return std::make_pair(
string(), 0 * Days);
339 "yoy inflation capfloor curve");
344 return lookup<Handle<ZeroInflationIndex>>(
zeroInflationIndices_, indexName, configuration,
"zero inflation index");
349 return lookup<Handle<YoYInflationIndex>>(
yoyInflationIndices_, indexName, configuration,
"yoy inflation index");
353 const string& configuration)
const {
356 "cpi cap floor volatility surface");
361 return lookup<Handle<Quote>>(
equitySpots_, key, configuration,
"equity spot");
366 return lookup<Handle<QuantExt::EquityIndex2>>(
equityCurves_, key, configuration,
"equity curve");
372 "dividend yield curve");
377 return lookup<Handle<BlackVolTermStructure>>(
equityVols_, key, configuration,
"equity vol curve");
382 return equityCurve(eqName, configuration)->equityForecastCurve();
387 return lookup<Handle<Quote>>(
securitySpreads_, key, configuration,
"security spread");
390Handle<QuantExt::InflationIndexObserver>
MarketImpl::baseCpis(
const string& key,
const string& configuration)
const {
392 return lookup<Handle<QuantExt::InflationIndexObserver>>(
baseCpis_, key, configuration,
"base CPI");
396 const string& configuration)
const {
397 return commodityIndex(commodityName, configuration)->priceCurve();
402 return lookup<Handle<CommodityIndex>>(
commodityIndices_, commodityName, configuration,
"commodity indices");
406 const string& configuration)
const {
408 return lookup<Handle<BlackVolTermStructure>>(
commodityVols_, commodityName, configuration,
"commodity volatility");
412 const string& configuration)
const {
428 return Handle<QuantExt::CorrelationTermStructure>(
429 QuantLib::ext::make_shared<QuantExt::NegativeCorrelationTermStructure>(it->second));
433 return Handle<QuantExt::CorrelationTermStructure>(
434 QuantLib::ext::make_shared<QuantExt::NegativeCorrelationTermStructure>(it->second));
441 return Handle<QuantExt::CorrelationTermStructure>(
442 QuantLib::ext::make_shared<QuantExt::NegativeCorrelationTermStructure>(it->second));
446 return Handle<QuantExt::CorrelationTermStructure>(
447 QuantLib::ext::make_shared<QuantExt::NegativeCorrelationTermStructure>(it->second));
462 QL_FAIL(
"did not find object " << index1 <<
"/" << index2 <<
" in CorrelationCurves");
468Handle<Quote>
MarketImpl::cpr(
const string& securityID,
const string& configuration)
const {
470 return lookup<Handle<Quote>>(
cprs_, securityID, configuration,
"cpr");
477 std::vector<string> tokens;
478 split(tokens,
swapIndex, boost::is_any_of(
"-"));
479 QL_REQUIRE(tokens.size() == 3 || tokens.size() == 4,
480 "three or four tokens required in " <<
swapIndex <<
": CCY-CMS-TENOR or CCY-CMS-TAG-TENOR");
481 QL_REQUIRE(tokens[0].
size() == 3,
"invalid currency code in " <<
swapIndex);
482 QL_REQUIRE(tokens[1] ==
"CMS",
"expected CMS as second token in " <<
swapIndex);
484 Handle<YieldTermStructure> discounting, forwarding;
485 QuantLib::ext::shared_ptr<IborIndex> dummeyIndex;
487 discounting =
iborIndex(discountIndex, configuration)->forwardingTermStructure();
489 discounting =
yieldCurve(discountIndex, configuration);
491 const QuantLib::ext::shared_ptr<Conventions>& conventions = InstrumentConventions::instance().conventions();
492 auto swapCon = QuantLib::ext::dynamic_pointer_cast<data::SwapIndexConvention>(conventions->get(
swapIndex));
493 QL_REQUIRE(swapCon,
"expected SwapIndexConvention for " <<
swapIndex);
494 auto conIbor = QuantLib::ext::dynamic_pointer_cast<data::IRSwapConvention>(conventions->get(swapCon->conventions()));
495 auto conOisComp = QuantLib::ext::dynamic_pointer_cast<data::OisConvention>(conventions->get(swapCon->conventions()));
496 auto conOisAvg = QuantLib::ext::dynamic_pointer_cast<data::AverageOisConvention>(conventions->get(swapCon->conventions()));
497 QL_REQUIRE(conIbor || conOisComp || conOisAvg,
498 "expected IRSwapConvention, OisConvention, AverageOisConvention for " << swapCon->conventions());
500 string fi = conIbor ? conIbor->indexName() : (conOisComp ? conOisComp->indexName() : conOisAvg->indexName());
502 forwarding = discounting;
504 forwarding =
iborIndex(fi, configuration)->forwardingTermStructure();
508 }
catch (std::exception& e) {
509 QL_FAIL(
"Failure in MarketImpl::addSwapIndex() with index " <<
swapIndex <<
" : " << e.what());
517 it =
refreshTs_.insert(make_pair(configuration, std::set<QuantLib::ext::shared_ptr<TermStructure>>())).first;
520 if (it->second.empty()) {
523 it->second.insert(*x.second);
527 Handle<YieldTermStructure> y = x.second->forwardingTermStructure();
529 it->second.insert(*y);
534 Handle<YieldTermStructure> y = x.second->forwardingTermStructure();
536 it->second.insert(*y);
537 y = x.second->discountingTermStructure();
539 it->second.insert(*y);
544 it->second.insert(*x.second);
548 it->second.insert(*x.second);
552 it->second.insert(*x.second);
556 it->second.insert(*x.second);
560 it->second.insert(*x.second->curve());
564 it->second.insert(*x.second);
568 it->second.insert(*x.second);
572 it->second.insert(*x.second->zeroInflationTermStructure());
577 it->second.insert(*x.second->yoyInflationTermStructure());
582 it->second.insert(*x.second);
586 it->second.insert(*x.second);
590 it->second.insert(*x.second);
594 Handle<YieldTermStructure> y = x.second->equityForecastCurve();
596 it->second.insert(*y);
597 y = x.second->equityDividendCurve();
599 it->second.insert(*y);
604 const auto& pts = x.second->priceCurve();
606 it->second.insert(*pts);
611 it->second.insert(*x.second);
616 it->second.insert(*x.second);
622 for (
auto& x : it->second)
Handle< BlackVolTermStructure > fxVol(const string &ccypair, const string &configuration=Market::defaultConfiguration) const
QuantLib::Handle< QuantExt::FxIndex > fxIndex(const string &fxIndex, const string &configuration=Market::defaultConfiguration) const
static const string defaultConfiguration
Default configuration label.
QuantLib::Handle< QuantExt::CommodityIndex > commodityIndex(const std::string &commodityName, const std::string &configuration=Market::defaultConfiguration) const override
Commodity index.
map< pair< string, string >, Handle< OptionletVolatilityStructure > > capFloorCurves_
string shortSwapIndexBase(const string &key, const string &configuration=Market::defaultConfiguration) const override
string swapIndexBase(const string &key, const string &configuration=Market::defaultConfiguration) const override
Handle< Quote > securitySpread(const string &securityID, const string &configuration=Market::defaultConfiguration) const override
Bond Spreads.
Handle< QuantExt::BaseCorrelationTermStructure > baseCorrelation(const string &name, const string &configuration=Market::defaultConfiguration) const override
Base correlation structures.
Handle< QuantLib::SwaptionVolatilityStructure > swaptionVol(const string &key, const string &configuration=Market::defaultConfiguration) const override
Swaptions.
virtual Handle< ZeroInflationIndex > zeroInflationIndex(const string &indexName, const string &configuration=Market::defaultConfiguration) const override
Inflation Indexes.
Handle< QuantExt::EquityIndex2 > equityCurve(const string &eqName, const string &configuration=Market::defaultConfiguration) const override
map< tuple< string, string, string >, Handle< QuantExt::CorrelationTermStructure > > correlationCurves_
Handle< BlackVolTermStructure > equityVol(const string &eqName, const string &configuration=Market::defaultConfiguration) const override
Equity volatilities.
map< string, std::set< QuantLib::ext::shared_ptr< TermStructure > > > refreshTs_
Handle< QuantExt::InflationIndexObserver > baseCpis(const string &index, const string &configuration=Market::defaultConfiguration) const
Cpi Base Quotes.
Handle< YieldTermStructure > discountCurveImpl(const string &ccy, const string &configuration=Market::defaultConfiguration) const override
map< pair< string, string >, QuantLib::Handle< QuantExt::EquityIndex2 > > equityCurves_
Handle< Quote > fxSpotImpl(const string &ccypair, const string &configuration=Market::defaultConfiguration) const override
Handle< BlackVolTermStructure > fxVolImpl(const string &ccypair, const string &configuration=Market::defaultConfiguration) const override
map< pair< string, string >, Handle< CPIVolatilitySurface > > cpiInflationCapFloorVolatilitySurfaces_
map< pair< string, string >, Handle< QuantExt::InflationIndexObserver > > baseCpis_
virtual Handle< CPIVolatilitySurface > cpiInflationCapFloorVolatilitySurface(const string &indexName, const string &configuration=Market::defaultConfiguration) const override
Inflation Cap Floor Volatility Surfaces.
Handle< QuantExt::CreditVolCurve > cdsVol(const string &name, const string &configuration=Market::defaultConfiguration) const override
CDS volatilities.
map< pair< string, string >, Handle< BlackVolTermStructure > > fxVols_
QuantLib::ext::shared_ptr< FXTriangulation > fx_
map< tuple< string, YieldCurveType, string >, Handle< YieldTermStructure > > yieldCurves_
map< pair< string, string >, QuantLib::Handle< QuantExt::CommodityIndex > > commodityIndices_
map< pair< string, string >, Handle< QuantExt::CreditCurve > > defaultCurves_
Handle< YieldTermStructure > equityDividendCurve(const string &eqName, const string &configuration=Market::defaultConfiguration) const override
map< pair< string, string >, Handle< IborIndex > > iborIndices_
QuantLib::Handle< QuantExt::FxIndex > fxIndexImpl(const string &fxIndex, const string &configuration=Market::defaultConfiguration) const override
FX.
map< pair< string, string >, Handle< YoYOptionletVolatilitySurface > > yoyCapFloorVolSurfaces_
Handle< QuantExt::CreditCurve > defaultCurve(const string &, const string &configuration=Market::defaultConfiguration) const override
Default Curves and Recovery Rates.
map< pair< string, string >, Handle< YoYInflationIndex > > yoyInflationIndices_
map< pair< string, string >, Handle< QuantExt::CreditVolCurve > > cdsVols_
Handle< QuantExt::YoYOptionletVolatilitySurface > yoyCapFloorVol(const string &name, const string &configuration=Market::defaultConfiguration) const override
YoY Inflation CapFloor volatilities.
map< pair< string, string >, Handle< Quote > > equitySpots_
pair< string, string > swapIndexBases(const string &key, const string &configuration=Market::defaultConfiguration) const
map< pair< string, string >, Handle< QuantLib::SwaptionVolatilityStructure > > swaptionCurves_
map< pair< string, string >, Handle< QuantExt::BaseCorrelationTermStructure > > baseCorrelations_
QuantLib::Handle< Quote > cpr(const string &securityID, const string &configuration=Market::defaultConfiguration) const override
map< pair< string, string >, Handle< Quote > > cprs_
Handle< Quote > fxRateImpl(const string &ccypair, const string &configuration=Market::defaultConfiguration) const override
Handle< Quote > recoveryRate(const string &, const string &configuration=Market::defaultConfiguration) const override
map< pair< string, string >, Handle< Quote > > recoveryRates_
QuantLib::Handle< QuantExt::PriceTermStructure > commodityPriceCurve(const string &commodityName, const string &configuration=Market::defaultConfiguration) const override
Commodity curves.
Handle< Quote > equitySpot(const string &eqName, const string &configuration=Market::defaultConfiguration) const override
Equity curves.
void refresh(const string &configuration=Market::defaultConfiguration) override
Send an explicit update() call to all term structures.
map< pair< string, string >, Handle< ZeroInflationIndex > > zeroInflationIndices_
Handle< SwapIndex > swapIndex(const string &indexName, const string &configuration=Market::defaultConfiguration) const override
map< pair< string, string >, pair< string, string > > swaptionIndexBases_
std::pair< string, QuantLib::Period > capFloorVolIndexBase(const string &key, const string &configuration=Market::defaultConfiguration) const override
Handle< QuantExt::CorrelationTermStructure > correlationCurve(const string &index1, const string &index2, const string &configuration=Market::defaultConfiguration) const override
Correlation curves.
Handle< OptionletVolatilityStructure > capFloorVol(const string &key, const string &configuration=Market::defaultConfiguration) const override
CapFloor volatilities.
map< pair< string, string >, Handle< BlackVolTermStructure > > equityVols_
virtual Handle< YoYInflationIndex > yoyInflationIndex(const string &indexName, const string &configuration=Market::defaultConfiguration) const override
map< pair< string, string >, Handle< QuantLib::SwaptionVolatilityStructure > > yieldVolCurves_
map< pair< string, string >, Handle< SwapIndex > > swapIndices_
map< pair< string, string >, Handle< Quote > > securitySpreads_
Handle< YieldTermStructure > yieldCurve(const YieldCurveType &type, const string &ccy, const string &configuration=Market::defaultConfiguration) const override
Yield Curves.
Handle< IborIndex > iborIndex(const string &indexName, const string &configuration=Market::defaultConfiguration) const override
map< pair< string, string >, QuantLib::Handle< QuantLib::BlackVolTermStructure > > commodityVols_
virtual void require(const MarketObject o, const string &name, const string &configuration, const bool forceBuild=false) const
Handle< QuantLib::SwaptionVolatilityStructure > yieldVol(const string &securityID, const string &configuration=Market::defaultConfiguration) const override
Yield volatility.
QuantLib::Handle< QuantLib::BlackVolTermStructure > commodityVolatility(const string &commodityName, const string &configuration=Market::defaultConfiguration) const override
Commodity volatility.
void addSwapIndex(const string &swapindex, const string &discountIndex, const string &configuration=Market::defaultConfiguration) const
add a swap index to the market
Handle< YieldTermStructure > equityForecastCurve(const string &eqName, const string &configuration=Market::defaultConfiguration) const override
Equity forecasting curves.
map< pair< string, string >, std::pair< string, QuantLib::Period > > capFloorIndexBase_
Currency and instrument specific conventions/defaults.
QuantLib::ext::shared_ptr< SwapIndex > parseSwapIndex(const string &s, const Handle< YieldTermStructure > &f, const Handle< YieldTermStructure > &d)
Convert std::string to QuantLib::SwapIndex.
bool tryParseIborIndex(const string &s, QuantLib::ext::shared_ptr< IborIndex > &index)
Try to convert std::string to QuantLib::IborIndex.
bool isGenericIborIndex(const string &indexName)
Return true if the indexName is that of a generic ibor index, otherwise false.
Map text representations to QuantLib/QuantExt types.
#define DLOG(text)
Logging Macro (Level = Debug)
market data related utilties
An implementation of the Market class that stores the required objects in maps.
bool isFxIndex(const std::string &indexName)
@ YoYInflationCapFloorVol
@ ZeroInflationCapFloorVol
Size size(const ValueType &v)
std::string inverseFxIndex(const std::string &indexName)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.