Build a volatility curve from a 1-D curve of volatility quotes.
117 {
118
119 LOG(
"CDSVolCurve: start building 1-D volatility curve");
120
121
122 QL_REQUIRE(vcc.quotes().size() > 0, "No quotes specified in config " << vc.curveID());
123
124
125
127
128
129 std::map<std::tuple<QuantLib::Date, QuantLib::Period, QuantLib::Real>, QuantLib::Handle<QuantLib::Quote>> quotes;
130
131
132 if (wildcard) {
133
134 DLOG(
"Have single quote with pattern " << wildcard->pattern());
135
136
137 for (const auto& md : loader.get(*wildcard, asof)) {
138
139 QL_REQUIRE(md->asofDate() == asof, "MarketDatum asofDate '" << md->asofDate() << "' <> asof '" << asof << "'");
140
141 auto q = QuantLib::ext::dynamic_pointer_cast<IndexCDSOptionQuote>(md);
142 QL_REQUIRE(q, "Internal error: could not downcast MarketDatum '" << md->name() << "' to IndexCDSOptionQuote");
143
144 TLOG(
"The quote " << q->name() <<
" matched the pattern");
145
146
147
148
149
150
151 QuantLib::Period quoteTerm;
152 if (q->indexTerm().empty()) {
153 if (vc.terms().size() > 1)
154 continue;
155 quoteTerm = vc.terms().empty() ? 5 * Years : vc.terms().front();
156 } else {
158 if (std::find(vc.terms().begin(), vc.terms().end(), quoteTerm) == vc.terms().end() &&
159 !vc.terms().empty())
160 continue;
161 }
162
163 Date expiryDate =
getExpiry(asof, q->expiry());
164 if (expiryDate > asof) {
165 quotes[std::make_tuple(expiryDate, quoteTerm,
166 strikeType_ == CreditVolCurve::Type::Price ? 1.0 : 0.0)] = q->quote();
167 TLOG(
"Added quote " << q->name() <<
": (" << io::iso_date(expiryDate) <<
"," << fixed
168 << setprecision(9) << q->quote()->value() << ")");
169 }
170 }
171
172
173 QL_REQUIRE(quotes.size() > 0, "No quotes found matching regular expression " << vcc.quotes()[0]);
174
175 } else {
176
177 DLOG(
"Have " << vcc.quotes().size() <<
" explicit quotes");
178
179
180 std::ostringstream ss;
182 Wildcard w(ss.str());
183 for (const auto& md : loader.get(w, asof)) {
184
185 QL_REQUIRE(md->asofDate() == asof, "MarketDatum asofDate '" << md->asofDate() << "' <> asof '" << asof << "'");
186
187 auto q = QuantLib::ext::dynamic_pointer_cast<IndexCDSOptionQuote>(md);
188 QL_REQUIRE(q, "Internal error: could not downcast MarketDatum '" << md->name() << "' to IndexCDSOptionQuote");
189
190
191 auto it = find(vcc.quotes().begin(), vcc.quotes().end(), q->name());
192 if (it != vcc.quotes().end()) {
193 TLOG(
"Found the configured quote " << q->name());
194
195 Date expiryDate =
getExpiry(asof, q->expiry());
196 QL_REQUIRE(expiryDate > asof, "CDS volatility quote '" << q->name() << "' has expiry in the past ("
197 << io::iso_date(expiryDate) << ")");
198
199 QuantLib::Period quoteTerm;
200 if (q->indexTerm().empty()) {
201 quoteTerm = vc.terms().size() == 1 ? vc.terms().front() : 5 * Years;
202 } else {
204 }
205 quotes[std::make_tuple(expiryDate, quoteTerm,
206 strikeType_ == CreditVolCurve::Type::Price ? 1.0 : 0.0)] = q->quote();
207 TLOG(
"Added quote " << q->name() <<
": (" << io::iso_date(expiryDate) <<
"," << fixed
208 << setprecision(9) << q->quote()->value() << ")");
209 }
210 }
211
212
213 QL_REQUIRE(quotes.size() == vcc.quotes().size(), "Found " << quotes.size() << " quotes, but "
214 << vcc.quotes().size()
215 << " quotes were given in config.");
216 }
217
218 DLOG(
"Creating InterpolatingCreditVolCurve object.");
219 vol_ = QuantLib::ext::make_shared<QuantExt::InterpolatingCreditVolCurve>(
221 std::vector<Handle<QuantExt::CreditCurve>>{}, quotes,
strikeType_);
222
223 LOG(
"CDSVolCurve: finished building 1-D volatility curve");
224}
QuantLib::Date getExpiry(const QuantLib::Date &asof, const QuantLib::ext::shared_ptr< Expiry > &expiry) const
Get an explicit expiry date from a CDS option quote's Expiry.
QuantExt::CreditVolCurve::Type strikeType_
QuantLib::Calendar calendar_
QuantLib::DayCounter dayCounter_
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
#define LOG(text)
Logging Macro (Level = Notice)
#define DLOG(text)
Logging Macro (Level = Debug)
#define TLOG(text)
Logging Macro (Level = Data)
boost::optional< Wildcard > getUniqueWildcard(const C &c)
checks if at most one element in C has a wild card and returns it in this case