Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
IndexCreditDefaultSwap Class Reference

#include <ored/portfolio/indexcreditdefaultswap.hpp>

+ Inheritance diagram for IndexCreditDefaultSwap:
+ Collaboration diagram for IndexCreditDefaultSwap:

Public Member Functions

 IndexCreditDefaultSwap ()
 Default constructor. More...
 
 IndexCreditDefaultSwap (const Envelope &env, const IndexCreditDefaultSwapData &swap, const BasketData &basket)
 Constructor. More...
 
virtual void build (const QuantLib::ext::shared_ptr< EngineFactory > &) override
 
QuantLib::Real notional () const override
 Return the current notional in npvCurrency. See individual sub-classes for the precise definition. More...
 
virtual void fromXML (XMLNode *node) override
 
virtual XMLNodetoXML (ore::data::XMLDocument &doc) const override
 
const IndexCreditDefaultSwapDataswap () const
 
CreditPortfolioSensitivityDecomposition sensitivityDecomposition () const
 
const std::map< std::string, QuantLib::Real > & constituents () const
 
const std::map< std::string, boost::any > & additionalData () const override
 returns all additional data returned by the trade once built More...
 
- Public Member Functions inherited from Trade
 Trade ()
 Default constructor. More...
 
 Trade (const string &tradeType, const Envelope &env=Envelope(), const TradeActions &ta=TradeActions())
 Base class constructor. More...
 
virtual ~Trade ()
 Default destructor. More...
 
virtual void build (const QuantLib::ext::shared_ptr< EngineFactory > &)=0
 
virtual std::map< std::string, RequiredFixings::FixingDatesfixings (const QuantLib::Date &settlementDate=QuantLib::Date()) const
 
const RequiredFixingsrequiredFixings () const
 
virtual std::map< AssetClass, std::set< std::string > > underlyingIndices (const QuantLib::ext::shared_ptr< ReferenceDataManager > &referenceDataManager=nullptr) const
 
void reset ()
 Reset trade, clear all base class data. This does not reset accumulated timings for this trade. More...
 
void resetPricingStats (const std::size_t numberOfPricings=0, const boost::timer::nanosecond_type cumulativePricingTime=0)
 Reset accumulated timings to given values. More...
 
string & id ()
 Set the trade id. More...
 
void setEnvelope (const Envelope &envelope)
 Set the envelope with counterparty and portfolio info. More...
 
void setAdditionalData (const std::map< std::string, boost::any > &additionalData)
 
TradeActionstradeActions ()
 Set the trade actions. More...
 
const string & id () const
 
const string & tradeType () const
 
const Envelopeenvelope () const
 
const set< string > & portfolioIds () const
 
const TradeActionstradeActions () const
 
const QuantLib::ext::shared_ptr< InstrumentWrapper > & instrument () const
 
const std::vector< QuantLib::Leg > & legs () const
 
const std::vector< string > & legCurrencies () const
 
const std::vector< bool > & legPayers () const
 
const string & npvCurrency () const
 
virtual string notionalCurrency () const
 
const Date & maturity () const
 
virtual bool isExpired (const Date &d)
 
const string & issuer () const
 
template<typename T >
additionalDatum (const std::string &tag) const
 returns any additional datum. More...
 
const std::string & sensitivityTemplate () const
 
void validate () const
 Utility to validate that everything that needs to be set in this base class is actually set. More...
 
virtual bool hasCashflows () const
 
boost::timer::nanosecond_type getCumulativePricingTime () const
 Get cumulative timing spent on pricing. More...
 
std::size_t getNumberOfPricings () const
 Get number of pricings. More...
 
- Public Member Functions inherited from XMLSerializable
virtual ~XMLSerializable ()
 
virtual void fromXML (XMLNode *node)=0
 
virtual XMLNodetoXML (XMLDocument &doc) const =0
 
void fromFile (const std::string &filename)
 
void toFile (const std::string &filename) const
 
void fromXMLString (const std::string &xml)
 Parse from XML string. More...
 
std::string toXMLString () const
 Parse from XML string. More...
 

Private Attributes

IndexCreditDefaultSwapData swap_
 
BasketData basket_
 
std::map< std::string, QuantLib::Real > constituents_
 map of all the constituents to notionals More...
 
CreditPortfolioSensitivityDecomposition sensitivityDecomposition_
 

Additional Inherited Members

- Protected Member Functions inherited from Trade
Date addPremiums (std::vector< QuantLib::ext::shared_ptr< Instrument > > &instruments, std::vector< Real > &multipliers, const Real tradeMultiplier, const PremiumData &premiumData, const Real premiumMultiplier, const Currency &tradeCurrency, const QuantLib::ext::shared_ptr< EngineFactory > &factory, const string &configuration)
 
void setLegBasedAdditionalData (const Size legNo, Size resultLegId=Null< Size >()) const
 
void setSensitivityTemplate (const EngineBuilder &builder)
 
void setSensitivityTemplate (const std::string &id)
 
- Protected Attributes inherited from Trade
string tradeType_
 
QuantLib::ext::shared_ptr< InstrumentWrapperinstrument_
 
std::vector< QuantLib::Leg > legs_
 
std::vector< string > legCurrencies_
 
std::vector< boollegPayers_
 
string npvCurrency_
 
QuantLib::Real notional_
 
string notionalCurrency_
 
Date maturity_
 
string issuer_
 
string sensitivityTemplate_
 
bool sensitivityTemplateSet_ = false
 
std::size_t savedNumberOfPricings_ = 0
 
boost::timer::nanosecond_type savedCumulativePricingTime_ = 0
 
RequiredFixings requiredFixings_
 
std::map< std::string, boost::any > additionalData_
 

Detailed Description

Definition at line 30 of file indexcreditdefaultswap.hpp.

Constructor & Destructor Documentation

◆ IndexCreditDefaultSwap() [1/2]

Default constructor.

Definition at line 33 of file indexcreditdefaultswap.hpp.

33: Trade("IndexCreditDefaultSwap") {}
Trade()
Default constructor.
Definition: trade.hpp:59

◆ IndexCreditDefaultSwap() [2/2]

IndexCreditDefaultSwap ( const Envelope env,
const IndexCreditDefaultSwapData swap,
const BasketData basket 
)

Constructor.

Definition at line 36 of file indexcreditdefaultswap.hpp.

37 : Trade("IndexCreditDefaultSwap", env), swap_(swap), basket_(basket) {}

Member Function Documentation

◆ build()

void build ( const QuantLib::ext::shared_ptr< EngineFactory > &  )
overridevirtual

Build QuantLib/QuantExt instrument, link pricing engine. If build() is called multiple times, reset() should be called between these calls.

Implements Trade.

Definition at line 39 of file indexcreditdefaultswap.cpp.

39 {
40 DLOG("IndexCreditDefaultSwap::build() called for trade " << id());
41
42 // ISDA taxonomy
43 additionalData_["isdaAssetClass"] = string("Credit");
44 additionalData_["isdaBaseProduct"] = string("Index");
45 string entity = swap_.creditCurveId();
46 QuantLib::ext::shared_ptr<ReferenceDataManager> refData = engineFactory->referenceData();
47 if (refData && refData->hasData("CreditIndex", entity)) {
48 auto refDatum = refData->getData("CreditIndex", entity);
49 QuantLib::ext::shared_ptr<CreditIndexReferenceDatum> creditIndexRefDatum =
50 QuantLib::ext::dynamic_pointer_cast<CreditIndexReferenceDatum>(refDatum);
51 additionalData_["isdaSubProduct"] = creditIndexRefDatum->indexFamily();
52 if (creditIndexRefDatum->indexFamily() == "") {
53 ALOG("IndexFamily is blank in credit index reference data for entity " << entity);
54 }
55 } else {
56 ALOG("Credit index reference data missing for entity " << entity << ", isdaSubProduct left blank");
57 }
58 // skip the transaction level mapping for now
59 additionalData_["isdaTransaction"] = string("");
60
61 const QuantLib::ext::shared_ptr<Market> market = engineFactory->market();
62 QuantLib::ext::shared_ptr<EngineBuilder> builder = engineFactory->builder("IndexCreditDefaultSwap");
63
64 QL_REQUIRE(swap_.leg().legType() == "Fixed", "IndexCreditDefaultSwap requires Fixed leg");
65 QuantLib::ext::shared_ptr<FixedLegData> fixedLegData =
66 QuantLib::ext::dynamic_pointer_cast<FixedLegData>(swap_.leg().concreteLegData());
67
68 auto configuration = builder->configuration(MarketContext::pricing);
69 FixedLegBuilder flb;
70 legs_.resize(1);
71 legs_[0] = flb.buildLeg(swap_.leg(), engineFactory, requiredFixings_, configuration);
72
73 Schedule schedule = makeSchedule(swap_.leg().schedule());
74 BusinessDayConvention payConvention = parseBusinessDayConvention(swap_.leg().paymentConvention());
75 Protection::Side prot = swap_.leg().isPayer() ? Protection::Side::Buyer : Protection::Side::Seller;
76
78 DayCounter dc = parseDayCounter(swap_.leg().dayCounter());
79
80 // In general for CDS and CDS index trades, the standard day counter is Actual/360 and the final
81 // period coupon accrual includes the maturity date.
82 Actual360 standardDayCounter;
83 DayCounter lastPeriodDayCounter = dc == standardDayCounter ? Actual360(true) : dc;
84
85 QL_REQUIRE(fixedLegData->rates().size() == 1, "IndexCreditDefaultSwap requires single rate");
86
87 Real indexFactor = 1.0;
88
89 // From the basket data or reference data, we need a vector of notionals and a vector of creditCurves
90 vector<Real> basketNotionals;
91 vector<string> basketCreditCurves;
92
93 if (!swap_.basket().constituents().empty()) {
94
95 const auto& constituents = swap_.basket().constituents();
96 DLOG("Building constituents from basket data containing " << constituents.size() << " elements.");
97
98 Real totalNtl = 0.0;
99 for (const auto& c : constituents) {
100 Real ntl = Null<Real>();
101
102 const auto& creditCurve = c.creditCurveId();
103
104 if (c.weightInsteadOfNotional()) {
105 ntl = c.weight() * notional_;
106 } else {
107 ntl = c.notional();
108 }
109
110 if (!close(0.0, ntl) && ntl > 0.0) {
111 if (std::find(basketCreditCurves.begin(), basketCreditCurves.end(), creditCurve) ==
112 basketCreditCurves.end()) {
113 DLOG("Adding underlying " << creditCurve << " with notional " << ntl);
114 constituents_[creditCurve] = ntl;
115 basketCreditCurves.push_back(creditCurve);
116 basketNotionals.push_back(ntl);
117 totalNtl += ntl;
118 } else {
119 StructuredTradeErrorMessage(id(), "IndexCDS", "Error building trade",
120 ("Invalid Basket: found a duplicate credit curve " + creditCurve +
121 ", skip it. Check the basket data for possible errors.")
122 .c_str()).log();
123 }
124
125 } else {
126 DLOG("Skipped adding underlying, " << creditCurve << ", because its notional, " << ntl
127 << ", was non-positive.");
128 }
129 }
130 QL_REQUIRE(basketCreditCurves.size() == basketNotionals.size(),
131 "numbers of defaults curves (" << basketCreditCurves.size() << ") and notionals ("
132 << basketNotionals.size() << ") doesnt match");
133 DLOG("All underlyings added, total notional = " << totalNtl);
134
135 if (totalNtl > notional_ * (1.0 + 1.0E-4)) {
136 StructuredTradeErrorMessage(id(), "IndexCDS", "Error building trade",
137 ("Sum of basket notionals (" + std::to_string(totalNtl) +
138 ") is greater than trade notional (" + std::to_string(notional_) +
139 "). Check the basket data for possible errors.")
140 .c_str())
141 .log();
142 }
143
144 indexFactor = totalNtl / notional_;
145
146 DLOG("Finished building constituents using basket data.");
147
148 } else {
149 // get data from ReferenceDatum
151 DLOG("Getting CreditIndexReferenceDatum for id " << id);
152 QL_REQUIRE(engineFactory->referenceData(), "No BasketData or ReferenceDataManager");
153 QL_REQUIRE(engineFactory->referenceData()->hasData(CreditIndexReferenceDatum::TYPE, id),
154 "No CreditIndex reference data for " << id);
155 QuantLib::ext::shared_ptr<ReferenceDatum> refData =
156 engineFactory->referenceData()->getData(CreditIndexReferenceDatum::TYPE, id);
157 QuantLib::ext::shared_ptr<CreditIndexReferenceDatum> creditRefData =
158 QuantLib::ext::dynamic_pointer_cast<CreditIndexReferenceDatum>(refData);
159 DLOG("Got CreditIndexReferenceDatum for id " << id);
160
161 Real totalWeight = 0.0;
162 for (const auto& c : creditRefData->constituents()) {
163
164 const auto& name = c.name();
165 auto weight = c.weight();
166
167 if (!close(0.0, weight) && weight > 0.0) {
168 DLOG("Adding underlying " << name << " with weight " << weight);
169 constituents_[name] = notional_ * weight;
170 basketCreditCurves.push_back(name);
171 basketNotionals.push_back(notional_ * weight);
172 totalWeight += weight;
173 } else {
174 DLOG("Skipped adding underlying, " << name << ", because its weight, " << weight
175 << ", was non-positive.");
176 }
177 }
178
179 indexFactor = totalWeight;
180
181 DLOG("All underlyings added, total weight = " << totalWeight);
182
183 if (!close(1.0, totalWeight) && totalWeight > 1.0) {
184 ALOG("Total weight is greater than 1, possible error in CreditIndexReferenceDatum");
185 }
186 }
187
188 QuantLib::ext::shared_ptr<QuantExt::IndexCreditDefaultSwap> cds;
189 if (swap_.upfrontFee() == Null<Real>()) {
190 cds = QuantLib::ext::make_shared<QuantExt::IndexCreditDefaultSwap>(
191 prot, indexFactor * notional_, basketNotionals, fixedLegData->rates().front(), schedule, payConvention, dc,
192 swap_.settlesAccrual(), swap_.protectionPaymentTime(), swap_.protectionStart(), QuantLib::ext::shared_ptr<Claim>(),
193 lastPeriodDayCounter, true, swap_.tradeDate(), swap_.cashSettlementDays());
194 } else {
195 cds = QuantLib::ext::make_shared<QuantExt::IndexCreditDefaultSwap>(
196 prot, indexFactor * notional_, basketNotionals, swap_.upfrontFee(), fixedLegData->rates().front(), schedule,
198 swap_.upfrontDate(), QuantLib::ext::shared_ptr<Claim>(), lastPeriodDayCounter, true, swap_.tradeDate(),
200 }
201
202 QuantLib::ext::shared_ptr<IndexCreditDefaultSwapEngineBuilder> cdsBuilder =
203 QuantLib::ext::dynamic_pointer_cast<IndexCreditDefaultSwapEngineBuilder>(builder);
204
207
208 QL_REQUIRE(cdsBuilder, "No Builder found for IndexCreditDefaultSwap: " << id());
209 std::string curveIdWithTerm = swap_.creditCurveIdWithTerm();
210 // warn if the term can not be implied, except when a custom baskets is defined
211 if (swap_.basket().constituents().empty() && splitCurveIdWithTenor(curveIdWithTerm).second == 0 * Days) {
212 StructuredTradeWarningMessage(
213 id(), tradeType(), "Could not imply Index CDS term.",
214 "Index CDS term could not be derived from start, end date, are these dates correct (credit curve id is '" +
215 swap_.creditCurveId() + "')").log();
216 }
217
218 maturity_ = cds->coupons().back()->date();
219
220 cds->setPricingEngine(cdsBuilder->engine(parseCurrency(npvCurrency_), swap_.creditCurveIdWithTerm(),
221 basketCreditCurves, boost::none, swap_.recoveryRate(), false));
222 setSensitivityTemplate(*cdsBuilder);
223
224 instrument_.reset(new VanillaInstrument(cds));
225
226
227 legs_ = {cds->coupons()};
229 legPayers_ = {swap_.leg().isPayer()};
230
231 if (swap_.protectionStart() != Date())
233 else
234 additionalData_["startDate"] = to_string(schedule.dates().front());
235
236 sensitivityDecomposition_ = cdsBuilder->sensitivityDecomposition();
237}
const std::vector< BasketConstituent > & constituents() const
Definition: basketdata.cpp:263
QuantLib::Natural cashSettlementDays() const
const QuantLib::Date & tradeDate() const
const std::map< std::string, QuantLib::Real > & constituents() const
QuantLib::Real notional() const override
Return the current notional in npvCurrency. See individual sub-classes for the precise definition.
std::map< std::string, QuantLib::Real > constituents_
map of all the constituents to notionals
CreditPortfolioSensitivityDecomposition sensitivityDecomposition_
const string & paymentConvention() const
Definition: legdata.hpp:878
const string & currency() const
Definition: legdata.hpp:873
const ScheduleData & schedule() const
Definition: legdata.hpp:874
bool isPayer() const
Definition: legdata.hpp:872
const string & legType() const
Definition: legdata.hpp:890
const string & dayCounter() const
Definition: legdata.hpp:877
QuantLib::ext::shared_ptr< LegAdditionalData > concreteLegData() const
Definition: legdata.hpp:891
string npvCurrency_
Definition: trade.hpp:201
std::vector< bool > legPayers_
Definition: trade.hpp:200
std::vector< string > legCurrencies_
Definition: trade.hpp:199
std::vector< QuantLib::Leg > legs_
Definition: trade.hpp:198
QuantLib::Real notional_
Definition: trade.hpp:202
void setSensitivityTemplate(const EngineBuilder &builder)
Definition: trade.cpp:295
QuantLib::ext::shared_ptr< InstrumentWrapper > instrument_
Definition: trade.hpp:197
string notionalCurrency_
Definition: trade.hpp:203
const string & tradeType() const
Definition: trade.hpp:133
std::map< std::string, boost::any > additionalData_
Definition: trade.hpp:224
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
Definition: parsers.cpp:290
BusinessDayConvention parseBusinessDayConvention(const string &s)
Convert text to QuantLib::BusinessDayConvention.
Definition: parsers.cpp:173
DayCounter parseDayCounter(const string &s)
Convert text to QuantLib::DayCounter.
Definition: parsers.cpp:209
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define ALOG(text)
Logging Macro (Level = Alert)
Definition: log.hpp:544
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
std::pair< std::string, QuantLib::Period > splitCurveIdWithTenor(const std::string &creditCurveId)
Definition: marketdata.cpp:231
Schedule makeSchedule(const ScheduleDates &data)
Definition: schedule.cpp:263
string name
+ Here is the call graph for this function:

◆ notional()

QuantLib::Real notional ( ) const
overridevirtual

Return the current notional in npvCurrency. See individual sub-classes for the precise definition.

Reimplemented from Trade.

Definition at line 257 of file indexcreditdefaultswap.cpp.

257 {
258 Date asof = Settings::instance().evaluationDate();
259 // get the current notional from the premium leg
260 for (Size i = 0; i < legs_[0].size(); ++i) {
261 QuantLib::ext::shared_ptr<Coupon> coupon = QuantLib::ext::dynamic_pointer_cast<Coupon>(legs_[0][i]);
262 if (coupon->date() > asof)
263 return coupon->nominal();
264 }
265
266 // if not provided, return null
267 ALOG("Error retrieving current notional for index credit default swap " << id() << " as of " << io::iso_date(asof));
268 return Null<Real>();
269}

◆ fromXML()

void fromXML ( XMLNode node)
overridevirtual

Reimplemented from Trade.

Definition at line 271 of file indexcreditdefaultswap.cpp.

271 {
272 Trade::fromXML(node);
273 XMLNode* cdsNode = XMLUtils::getChildNode(node, "IndexCreditDefaultSwapData");
274 QL_REQUIRE(cdsNode, "No IndexCreditDefaultSwapData Node");
275 swap_.fromXML(cdsNode);
276}
void fromXML(ore::data::XMLNode *node) override
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60

◆ toXML()

XMLNode * toXML ( ore::data::XMLDocument doc) const
overridevirtual

Reimplemented from Trade.

Definition at line 278 of file indexcreditdefaultswap.cpp.

278 {
279 XMLNode* node = Trade::toXML(doc);
280 XMLUtils::appendNode(node, swap_.toXML(doc));
281 return node;
282}
ore::data::XMLNode * toXML(ore::data::XMLDocument &doc) const override

◆ swap()

const IndexCreditDefaultSwapData & swap ( ) const

Definition at line 45 of file indexcreditdefaultswap.hpp.

45{ return swap_; }

◆ sensitivityDecomposition()

CreditPortfolioSensitivityDecomposition sensitivityDecomposition ( ) const

Definition at line 47 of file indexcreditdefaultswap.hpp.

◆ constituents()

const std::map< std::string, QuantLib::Real > & constituents ( ) const

Definition at line 49 of file indexcreditdefaultswap.hpp.

49{ return constituents_; };

◆ additionalData()

const std::map< std::string, boost::any > & additionalData ( ) const
overridevirtual

returns all additional data returned by the trade once built

Reimplemented from Trade.

Definition at line 239 of file indexcreditdefaultswap.cpp.

239 {
241 additionalData_["legNPV[1]"] = instrument_->qlInstrument()->result<Real>("protectionLegNPV");
242 additionalData_["legNPV[2]"] = instrument_->qlInstrument()->result<Real>("premiumLegNPVDirty") +
243 instrument_->qlInstrument()->result<Real>("upfrontPremiumNPV") +
244 instrument_->qlInstrument()->result<Real>("accrualRebateNPV");
245 additionalData_["isPayer[1]"] = !swap_.leg().isPayer();
246 additionalData_["isPayer[2]"] = swap_.leg().isPayer();
247 additionalData_["legType[2]"] = swap_.leg().legType();
248 additionalData_["legType[1]"] = std::string("Protection");
249 additionalData_["currentNotional[1]"] = additionalData_["currentNotional[2]"];
250 additionalData_["originalNotional[1]"] = additionalData_["originalNotional[2]"];
251 additionalData_["notionalCurrency[1]"] = notionalCurrency_;
252 additionalData_["notionalCurrency[2]"] = notionalCurrency_;
253 return additionalData_;
254}
void setLegBasedAdditionalData(const Size legNo, Size resultLegId=Null< Size >()) const
Definition: trade.cpp:153

Member Data Documentation

◆ swap_

Definition at line 54 of file indexcreditdefaultswap.hpp.

◆ basket_

BasketData basket_
private

Definition at line 55 of file indexcreditdefaultswap.hpp.

◆ constituents_

std::map<std::string, QuantLib::Real> constituents_
private

map of all the constituents to notionals

Definition at line 57 of file indexcreditdefaultswap.hpp.

◆ sensitivityDecomposition_

CreditPortfolioSensitivityDecomposition sensitivityDecomposition_
private

Definition at line 58 of file indexcreditdefaultswap.hpp.