26#include <ql/time/daycounters/actual360.hpp>
35 DLOG(
"CreditDefaultSwap::build() called for trade " <<
id());
43 QuantLib::ext::shared_ptr<ReferenceDataManager> refData = engineFactory->referenceData();
44 if (refData && refData->hasData(
"Credit", entity)) {
45 auto refDatum = refData->getData(
"Credit", entity);
46 QuantLib::ext::shared_ptr<CreditReferenceDatum> creditRefDatum =
47 QuantLib::ext::dynamic_pointer_cast<CreditReferenceDatum>(refDatum);
48 additionalData_[
"isdaSubProduct"] = creditRefDatum->creditData().entityType;
49 if (creditRefDatum->creditData().entityType ==
"") {
50 ALOG(
"EntityType is blank in credit reference data for entity " << entity);
53 ALOG(
"Credit reference data missing for entity " << entity <<
", isdaSubProduct left blank");
58 const QuantLib::ext::shared_ptr<Market> market = engineFactory->market();
59 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder(
"CreditDefaultSwap");
67 QL_REQUIRE(legData.legType() ==
"Fixed",
"CreditDefaultSwap requires Fixed leg");
69 QL_REQUIRE(schedule.size() > 1,
"CreditDefaultSwap requires at least two dates in the schedule");
71 BusinessDayConvention payConvention = Following;
72 if (!legData.paymentConvention().empty()) {
76 Protection::Side prot = legData.isPayer() ? Protection::Side::Buyer : Protection::Side::Seller;
79 DayCounter dc = Actual360();
80 if (!legData.dayCounter().empty()) {
86 DayCounter lastPeriodDayCounter;
87 auto strLpdc = legData.lastPeriodDayCounter();
88 if (!strLpdc.empty()) {
91 Actual360 standardDayCounter;
92 if (dc == standardDayCounter) {
93 lastPeriodDayCounter = dc == standardDayCounter ? Actual360(
true) : dc;
94 legData.lastPeriodDayCounter() =
"A360 (Incl Last)";
96 lastPeriodDayCounter = dc;
101 auto legBuilder = engineFactory->legBuilder(legData.legType());
103 legBuilder->buildLeg(legData, engineFactory,
requiredFixings_, builder->configuration(MarketContext::pricing));
109 QL_REQUIRE(legData.indexing().empty() ||
swap_.
upfrontFee() == Null<Real>(),
110 "CreditDefaultSwap with indexed coupon leg does not allow for an upfront fee");
114 Real fixedRate = 0.0;
115 if (
auto fixedData = QuantLib::ext::dynamic_pointer_cast<FixedLegData>(legData.concreteLegData())) {
116 if (fixedData->rates().size() == 1)
117 fixedRate = fixedData->rates().front();
120 QuantLib::ext::shared_ptr<QuantLib::CreditDefaultSwap> cds;
123 cds = QuantLib::ext::make_shared<QuantLib::CreditDefaultSwap>(
128 cds = QuantLib::ext::make_shared<QuantLib::CreditDefaultSwap>(
135 maturity_ = cds->coupons().back()->date();
137 QuantLib::ext::shared_ptr<CreditDefaultSwapEngineBuilder> cdsBuilder =
138 QuantLib::ext::dynamic_pointer_cast<CreditDefaultSwapEngineBuilder>(builder);
140 QL_REQUIRE(cdsBuilder,
"No Builder found for CreditDefaultSwap: " <<
id());
146 legs_ = {cds->coupons()};
162 instrument_->qlInstrument()->result<Real>(
"upfrontPremiumNPV") +
163 instrument_->qlInstrument()->result<Real>(
"accrualRebateNPV");
167 additionalData_[
"legType[1]"] = std::string(
"Protection");
168 additionalData_[
"currentNotional[1]"] = additionalData_[
"currentNotional[2]"];
169 additionalData_[
"originalNotional[1]"] = additionalData_[
"originalNotional[2]"];
172 return additionalData_;
176 Date asof = Settings::instance().evaluationDate();
178 if (!
legs_.empty()) {
179 for (Size i = 0; i <
legs_[0].size(); ++i) {
180 QuantLib::ext::shared_ptr<Coupon> coupon = boost::dynamic_pointer_cast<Coupon>(
legs_[0][i]);
181 if (coupon->date() > asof)
182 return coupon->nominal();
190 Trade::fromXML(node);
191 XMLNode* cdsNode = XMLUtils::getChildNode(node,
"CreditDefaultSwapData");
192 QL_REQUIRE(cdsNode,
"No CreditDefaultSwapData Node");
197 XMLNode* node = Trade::toXML(doc);
Builder that returns an engine to price a credit default swap.
const Date & upfrontDate() const
const string & issuerId() const
QuantLib::Real recoveryRate() const
const boost::optional< CdsReferenceInformation > & referenceInformation() const
QuantLib::Natural cashSettlementDays() const
const LegData & leg() const
void fromXML(XMLNode *node) override
XMLNode * toXML(XMLDocument &doc) const override
bool settlesAccrual() const
const QuantLib::Date & tradeDate() const
PPT protectionPaymentTime() const
const string & creditCurveId() const
bool rebatesAccrual() const
const Date & protectionStart() const
CreditDefaultSwapData swap_
QuantLib::Real notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
virtual void fromXML(XMLNode *node) override
virtual XMLNode * toXML(XMLDocument &doc) const override
virtual void build(const QuantLib::ext::shared_ptr< EngineFactory > &) override
const std::map< std::string, boost::any > & additionalData() const override
returns all additional data returned by the trade once built
const string & legType() const
const vector< double > & notionals() const
std::vector< bool > legPayers_
std::vector< string > legCurrencies_
std::vector< QuantLib::Leg > legs_
void setSensitivityTemplate(const EngineBuilder &builder)
RequiredFixings requiredFixings_
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
void setLegBasedAdditionalData(const Size legNo, Size resultLegId=Null< Size >()) const
std::map< std::string, boost::any > additionalData_
Vanilla Instrument Wrapper.
Small XML Document wrapper class.
Ibor cap, floor or collar trade data model and serialization.
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
BusinessDayConvention parseBusinessDayConvention(const string &s)
Convert text to QuantLib::BusinessDayConvention.
DayCounter parseDayCounter(const string &s)
Convert text to QuantLib::DayCounter.
leg data model and serialization
Classes and functions for log message handling.
#define DLOG(text)
Logging Macro (Level = Debug)
#define ALOG(text)
Logging Macro (Level = Alert)
std::string to_string(const LocationInfo &l)
Schedule makeSchedule(const ScheduleDates &data)
Serializable Credit Default Swap.
Map text representations to QuantLib/QuantExt types.
Reference data model and serialization.
string conversion utilities