34#include <ql/termstructures/yield/zerospreadedtermstructure.hpp>
36#include <boost/make_shared.hpp>
49 const string&, const string&> {
54 virtual string keyImpl(
const Currency& ccy,
const string& creditCurveId,
const bool hasCreditRisk,
55 const string& securityId,
const string& referenceCurveId)
override {
56 return ccy.code() +
"_" + creditCurveId +
"_" + securityId +
"_" + referenceCurveId;
70 virtual QuantLib::ext::shared_ptr<PricingEngine>
engineImpl(
const Currency& ccy,
const string& creditCurveId,
71 const bool hasCreditRisk,
const string& securityId,
72 const string& referenceCurveId)
override {
77 Handle<DefaultProbabilityTermStructure> dpts;
79 if (!creditCurveId.empty())
83 Handle<Quote> recovery;
89 WLOG(
"security specific recovery rate not found for security ID "
90 << securityId <<
", falling back on the recovery rate for credit curve Id " << creditCurveId);
91 if (!creditCurveId.empty())
101 if (!hasCreditRisk) {
102 dpts = Handle<DefaultProbabilityTermStructure>();
105 return QuantLib::ext::make_shared<QuantExt::DiscountingRiskyBondEngine>(yts, dpts, recovery, spread, tsperiod);
117 :
BondEngineBuilder(
"DiscountedCashflows",
"DiscountingRiskyBondEngineMultiState") {}
120 virtual QuantLib::ext::shared_ptr<PricingEngine>
engineImpl(
const Currency& ccy,
const string& creditCurveId,
121 const bool hasCreditRisk,
const string& securityId,
122 const string& referenceCurveId)
override {
126 Handle<Quote> spread;
132 std::vector<Handle<DefaultProbabilityTermStructure>> dpts;
133 std::vector<Handle<Quote>> recovery;
134 Size mainResultState = Null<Size>();
137 std::ostringstream rule_s;
138 rule_s <<
"Rule_" << i;
141 std::vector<std::string> tokens;
143 std::string stateCreditCurveId;
148 stateCreditCurveId = creditCurveId;
149 DLOG(
"Rule " << rule_s.str() <<
" is empty, use initial curve " << stateCreditCurveId
150 <<
" for this state.");
152 boost::split(tokens, rule, boost::is_any_of(
","));
153 QL_REQUIRE(tokens.size() == 2,
"invalid rule: " << rule);
154 stateCreditCurveId = regex_replace(creditCurveId, std::regex(tokens[0]), tokens[1]);
155 DLOG(
"Apply " << rule_s.str() <<
" => " << tokens[0] <<
" in " << creditCurveId <<
" yields state #"
156 << i <<
" creditCurve id " << stateCreditCurveId);
158 if (stateCreditCurveId == creditCurveId) {
160 DLOG(
"State #" << i <<
" is the main result state (overwriting previous choice)");
162 Handle<DefaultProbabilityTermStructure> this_dpts;
163 Handle<Quote> this_recovery;
164 if (!stateCreditCurveId.empty() && hasCreditRisk) {
168 dpts.push_back(this_dpts);
169 recovery.push_back(this_recovery);
173 Handle<DefaultProbabilityTermStructure> this_dpts;
174 Handle<Quote> this_recovery;
175 if (!creditCurveId.empty() && hasCreditRisk) {
179 dpts.push_back(this_dpts);
180 recovery.push_back(this_recovery);
182 DLOG(
"No rules given, only states are " << creditCurveId <<
" and default");
185 QL_REQUIRE(mainResultState != Null<Size>(),
186 "BondMultiStateEngineBuilder: No main state found for " << securityId <<
" / " << creditCurveId);
188 return QuantLib::ext::make_shared<QuantExt::DiscountingRiskyBondEngineMultiState>(yts, dpts, recovery, mainResultState,
Abstract template engine builder class.
Discounting Engine Builder class for Bonds.
BondDiscountingEngineBuilder()
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const Currency &ccy, const string &creditCurveId, const bool hasCreditRisk, const string &securityId, const string &referenceCurveId) override
Engine Builder base class for Bonds.
BondEngineBuilder(const std::string &model, const std::string &engine)
virtual string keyImpl(const Currency &ccy, const string &creditCurveId, const bool hasCreditRisk, const string &securityId, const string &referenceCurveId) override
Multi State Engine Builder class for Bonds.
BondMultiStateDiscountingEngineBuilder()
virtual QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const Currency &ccy, const string &creditCurveId, const bool hasCreditRisk, const string &securityId, const string &referenceCurveId) override
Abstract template EngineBuilder class that can cache engines and coupon pricers.
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.
std::string engineParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
const string & configuration(const MarketContext &key)
Return a configuration (or the default one if key not found)
Period parsePeriod(const string &s)
Convert text to QuantLib::Period.
Classes and functions for log message handling.
#define DLOG(text)
Logging Macro (Level = Debug)
#define WLOG(text)
Logging Macro (Level = Warning)
market data related utilties
QuantLib::Handle< QuantExt::CreditCurve > securitySpecificCreditCurve(const QuantLib::ext::shared_ptr< Market > &market, const std::string &securityId, const std::string &creditCurveId, const std::string &configuration)
Serializable Credit Default Swap.