32#include <ql/pricingengines/credit/midpointcdsengine.hpp>
34#include <boost/make_shared.hpp>
57 QuantLib::Real
recoveryRate = QuantLib::Null<QuantLib::Real>())
86 if (lhs.
recoveryRate() == QuantLib::Null<QuantLib::Real>() &&
87 rhs.
recoveryRate() == QuantLib::Null<QuantLib::Real>()) {
90 }
else if ((lhs.
recoveryRate() != QuantLib::Null<QuantLib::Real>() &&
91 rhs.
recoveryRate() == QuantLib::Null<QuantLib::Real>()) ||
92 (lhs.
recoveryRate() == QuantLib::Null<QuantLib::Real>() &&
93 rhs.
recoveryRate() != QuantLib::Null<QuantLib::Real>())) {
135 QuantLib::Real recoveryRate = QuantLib::Null<QuantLib::Real>())
override {
149 QuantLib::ext::shared_ptr<PricingEngine>
150 engineImpl(QuantLib::Currency ccy, std::string creditCurveId,
151 QuantLib::Real recoveryRate = QuantLib::Null<QuantLib::Real>())
override {
154 auto yts =
market_->discountCurve(ccy.code(), cfg);
155 auto dpts =
market_->defaultCurve(creditCurveId, cfg);
157 if (recoveryRate == QuantLib::Null<QuantLib::Real>()) {
159 recoveryRate =
market_->recoveryRate(creditCurveId, cfg)->value();
162 return QuantLib::ext::make_shared<MidPointCdsEngine>(dpts->curve(), recoveryRate, yts);
177 virtual QuantLib::ext::shared_ptr<PricingEngine>
178 engineImpl(QuantLib::Currency ccy, std::string creditCurveId,
179 QuantLib::Real recoveryRate = QuantLib::Null<QuantLib::Real>())
override {
183 std::vector<Handle<DefaultProbabilityTermStructure>> dpts;
184 std::vector<Handle<Quote>> recovery;
185 Size mainResultState = Null<Size>();
188 std::ostringstream rule_s;
189 rule_s <<
"Rule_" << i;
192 std::vector<std::string> tokens;
194 std::string stateCreditCurveId;
199 stateCreditCurveId = creditCurveId;
200 DLOG(
"Rule " << rule_s.str() <<
" is empty, use initial curve " << stateCreditCurveId
201 <<
" for this state.");
203 boost::split(tokens, rule, boost::is_any_of(
","));
204 QL_REQUIRE(tokens.size() == 2,
"invalid rule: " << rule);
205 stateCreditCurveId = regex_replace(creditCurveId, std::regex(tokens[0]), tokens[1]);
206 DLOG(
"Apply " << rule_s.str() <<
" => " << tokens[0] <<
" in " << creditCurveId <<
" yields state #"
207 << i <<
" creditCurve id " << stateCreditCurveId);
209 if (stateCreditCurveId == creditCurveId) {
211 DLOG(
"State #" << i <<
" is the main result state (overwriting previous choice)");
214 recovery.push_back(recoveryRate == Null<Real>()
216 : Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(recoveryRate)));
221 recovery.push_back(recoveryRate == Null<Real>()
223 : Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(recoveryRate)));
225 DLOG(
"No rules given, only states are " << creditCurveId <<
" and default");
228 QL_REQUIRE(mainResultState != Null<Size>(),
229 "CreditDefaultSwapMultiStateEngineBuilder: No main state found for " << creditCurveId);
231 return QuantLib::ext::make_shared<QuantExt::MidPointCdsEngineMultiState>(dpts, recovery, yts, mainResultState);
Abstract template engine builder class.
QuantLib::Real recoveryRate() const
Return the recovery rate if it is set, otherwise Null<Real>()
std::string creditCurveId_
const std::string & creditCurveId() const
Return the credit curve Id.
QuantLib::Real recoveryRate_
const QuantLib::Currency & currency() const
Return the currency.
CDSEngineKey(const std::string &creditCurveId, const QuantLib::Currency &ccy, QuantLib::Real recoveryRate=QuantLib::Null< QuantLib::Real >())
Abstract template EngineBuilder class that can cache engines and coupon pricers.
Engine builder base class for credit default swaps.
CDSEngineKey keyImpl(QuantLib::Currency ccy, std::string creditCurveId, QuantLib::Real recoveryRate=QuantLib::Null< QuantLib::Real >()) override
CreditDefaultSwapEngineBuilder(const std::string &model, const std::string &engine)
QuantLib::ext::shared_ptr< Market > market_
const string & engine() const
Return the engine name.
map< string, string > engineParameters_
const string & model() const
Return the model name.
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
Midpoint engine builder class for credit default swaps.
MidPointCdsEngineBuilder()
QuantLib::ext::shared_ptr< PricingEngine > engineImpl(QuantLib::Currency ccy, std::string creditCurveId, QuantLib::Real recoveryRate=QuantLib::Null< QuantLib::Real >()) override
Multi State Engine Builder class for CDS.
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(QuantLib::Currency ccy, std::string creditCurveId, QuantLib::Real recoveryRate=QuantLib::Null< QuantLib::Real >()) override
MidPointCdsMultiStateEngineBuilder()
Classes and functions for log message handling.
#define DLOG(text)
Logging Macro (Level = Debug)
bool operator<(const Dividend &d1, const Dividend &d2)
bool operator!=(const Filter &a, const Filter &b)
Filter operator>=(const RandomVariable &x, const RandomVariable &y)
bool operator==(const Dividend &d1, const Dividend &d)
bool operator>(const Distributionpair &p1, const Distributionpair &p2)
Filter operator<=(const RandomVariable &x, const RandomVariable &y)
Serializable Credit Default Swap.