19#include <boost/algorithm/string.hpp>
24#include <ql/errors.hpp>
34 const QuantLib::ext::shared_ptr<VolatilityConfig>& volatilityConfig,
35 const string& dayCounter,
const string&
calendar,
36 const string& strikeType,
const string& quoteName, Real strikeFactor,
37 const std::vector<QuantLib::Period>& terms,
38 const std::vector<std::string>& termCurves)
39 : CurveConfig(curveId, curveDescription), volatilityConfig_(volatilityConfig), dayCounter_(dayCounter),
40 calendar_(
calendar), strikeType_(strikeType), quoteName_(quoteName), strikeFactor_(strikeFactor), terms_(terms),
41 termCurves_(termCurves) {
43 QL_REQUIRE(terms_.size() == termCurves_.size(),
44 "CDSVolatilityCurveConfig: " << curveId
45 <<
" specifies different number of terms / curves (built via constructor)");
47 populateRequiredCurveIds();
50const QuantLib::ext::shared_ptr<VolatilityConfig>& CDSVolatilityCurveConfig::volatilityConfig()
const {
51 return volatilityConfig_;
54const string& CDSVolatilityCurveConfig::dayCounter()
const {
return dayCounter_; }
56const string& CDSVolatilityCurveConfig::calendar()
const {
return calendar_; }
58const string& CDSVolatilityCurveConfig::strikeType()
const {
return strikeType_; }
60const string& CDSVolatilityCurveConfig::quoteName()
const {
return quoteName_; }
62Real CDSVolatilityCurveConfig::strikeFactor()
const {
return strikeFactor_; }
64const std::vector<QuantLib::Period>& CDSVolatilityCurveConfig::terms()
const {
return terms_; }
66const std::vector<std::string>& CDSVolatilityCurveConfig::termCurves()
const {
return termCurves_; }
68void CDSVolatilityCurveConfig::fromXML(
XMLNode* node) {
93 WLOG(
"Using an Expiries node only in CDSVolatilityCurveConfig is deprecated. "
94 <<
"A volatility configuration node should be used instead.");
98 QL_REQUIRE(expiries.size() > 0,
"Need at least one expiry in the Expiries node.");
101 std::vector<std::string> quotes;
102 string stem = quoteStem();
103 for (
const string&
exp : expiries) {
104 for (
auto const& p : terms_) {
110 for (
const string&
exp : expiries) {
111 quotes.push_back(stem +
exp);
115 if (quotes.size() == 1) {
116 volatilityConfig_ = QuantLib::ext::make_shared<ConstantVolatilityConfig>(quotes[0]);
118 volatilityConfig_ = QuantLib::ext::make_shared<VolatilityCurveConfig>(quotes,
"Linear",
"Flat");
124 volatilityConfig_ = QuantLib::ext::make_shared<ConstantVolatilityConfig>();
126 volatilityConfig_ = QuantLib::ext::make_shared<VolatilityCurveConfig>();
128 volatilityConfig_ = QuantLib::ext::make_shared<VolatilityStrikeSurfaceConfig>();
130 QL_FAIL(
"CDSVolatilityCurveConfig does not yet support a DeltaSurface.");
132 QL_FAIL(
"CDSVolatilityCurveConfig does not yet support a MoneynessSurface.");
134 volatilityConfig_ = QuantLib::ext::make_shared<CDSProxyVolatilityConfig>();
136 QL_FAIL(
"CDSVolatility node expects one child node with name in list: Constant,"
137 <<
" Curve, StrikeSurface, ProxySurface.");
139 volatilityConfig_->fromXML(n);
142 dayCounter_ =
"A365";
146 calendar_ =
"NullCalendar";
159 populateRequiredCurveIds();
169 if (!terms_.empty()) {
170 QL_REQUIRE(terms_.size() == termCurves_.size(),
171 "CDSVolatilityCurveConfig::toXML(): internal error, terms size ("
172 << terms_.size() <<
") != termCurves size (" << termCurves_.size() <<
"), curveId = curveID_");
174 for (Size i = 0; i < terms_.size(); ++i) {
181 XMLNode* n = volatilityConfig_->toXML(doc);
186 if (!strikeType_.empty())
188 if (!quoteName_.empty())
195void CDSVolatilityCurveConfig::populateQuotes() {
198 if (
auto vc = QuantLib::ext::dynamic_pointer_cast<ConstantVolatilityConfig>(volatilityConfig_)) {
199 quotes_ = {vc->quote()};
200 }
else if (
auto vc = QuantLib::ext::dynamic_pointer_cast<VolatilityCurveConfig>(volatilityConfig_)) {
201 quotes_ = vc->quotes();
202 }
else if (
auto vc = QuantLib::ext::dynamic_pointer_cast<VolatilitySurfaceConfig>(volatilityConfig_)) {
207 string stem = quoteStem();
208 for (
const pair<string, string>& p : vc->quotes()) {
210 for (
auto const& t : terms_) {
214 if (terms_.size() <= 1) {
215 quotes_.push_back(stem + p.first +
"/" + p.second);
219 }
else if (
auto vc = QuantLib::ext::dynamic_pointer_cast<CDSProxyVolatilityConfig>(volatilityConfig_)) {
222 QL_FAIL(
"CDSVolatilityCurveConfig expected a constant, curve or surface");
226void CDSVolatilityCurveConfig::populateRequiredCurveIds() {
227 if (
auto vc = QuantLib::ext::dynamic_pointer_cast<CDSProxyVolatilityConfig>(volatilityConfig_)) {
228 requiredCurveIds_[CurveSpec::CurveType::CDSVolatility].insert(vc->cdsVolatilityCurve());
230 for (
auto const& c : termCurves_) {
232 requiredCurveIds_[CurveSpec::CurveType::Default].insert(spec->curveConfigID());
236string CDSVolatilityCurveConfig::quoteStem()
const {
238 string stem{
"INDEX_CDS_OPTION/RATE_LNVOL/"};
240 if (!quoteName_.empty()) {
CDS and index CDS volatility configuration.
CDSVolatilityCurveConfig()
Default constructor.
Small XML Document wrapper class.
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
static void checkNode(XMLNode *n, const string &expectedName)
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
static XMLNode * getChildNode(XMLNode *n, const string &name="")
static string getNodeValue(XMLNode *node)
Get a node's value.
static Period getChildValueAsPeriod(XMLNode *node, const string &name, bool mandatory=false, const QuantLib::Period &defaultValue=0 *QuantLib::Days)
static vector< string > getChildrenValuesAsStrings(XMLNode *node, const string &name, bool mandatory=false)
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
static void appendNode(XMLNode *parent, XMLNode *child)
QuantLib::ext::shared_ptr< CurveSpec > parseCurveSpec(const string &s)
function to convert a string into a curve spec
Real parseReal(const string &s)
Convert text to Real.
#define WLOG(text)
Logging Macro (Level = Warning)
RandomVariable exp(RandomVariable x)
std::string to_string(const LocationInfo &l)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.
string conversion utilities