33 XMLNode* n = XMLUtils::getChildNode(node,
"CreditLinkedSwapData");
34 creditCurveId_ = XMLUtils::getChildValue(n,
"CreditCurveId");
35 settlesAccrual_ = XMLUtils::getChildValueAsBool(n,
"SettlesAccrual",
false,
true);
36 fixedRecoveryRate_ = XMLUtils::getChildValueAsDouble(n,
"FixedRecoveryRate",
false, Null<Real>());
38 if (
auto c = XMLUtils::getChildNode(n,
"DefaultPaymentTime")) {
39 if (XMLUtils::getNodeValue(c) ==
"atDefault")
41 else if (XMLUtils::getNodeValue(c) ==
"atPeriodEnd")
43 else if (XMLUtils::getNodeValue(c) ==
"atMaturity")
46 QL_FAIL(
"default payment time '" << XMLUtils::getNodeValue(c)
47 <<
"' not known, expected atDefault, atPeriodEnd, atMaturity");
50 if (
auto tmp = XMLUtils::getChildNode(n,
"IndependentPayments")) {
51 for (
auto const& d : XMLUtils::getChildrenNodes(tmp,
"LegData")) {
52 independentPayments_.push_back(
LegData());
53 independentPayments_.back().fromXML(d);
56 if (
auto tmp = XMLUtils::getChildNode(n,
"ContingentPayments")) {
57 for (
auto const& d : XMLUtils::getChildrenNodes(tmp,
"LegData")) {
58 contingentPayments_.push_back(
LegData());
59 contingentPayments_.back().fromXML(d);
62 if (
auto tmp = XMLUtils::getChildNode(n,
"DefaultPayments")) {
63 for (
auto const& d : XMLUtils::getChildrenNodes(tmp,
"LegData")) {
64 defaultPayments_.push_back(
LegData());
65 defaultPayments_.back().fromXML(d);
68 if (
auto tmp = XMLUtils::getChildNode(n,
"RecoveryPayments")) {
69 for (
auto const& d : XMLUtils::getChildrenNodes(tmp,
"LegData")) {
70 recoveryPayments_.push_back(
LegData());
71 recoveryPayments_.back().fromXML(d);
79 XMLUtils::appendNode(n, d);
80 XMLUtils::addChild(doc, d,
"CreditCurveId", creditCurveId_);
84 XMLUtils::addChild(doc, d,
"DefaultPaymentTime",
"atDefault");
85 }
else if (
defaultPaymentTime_ == QuantExt::CreditDefaultSwap::ProtectionPaymentTime::atPeriodEnd) {
86 XMLUtils::addChild(doc, d,
"DefaultPaymentTime",
"atPeriodEnd");
87 }
else if (
defaultPaymentTime_ == QuantExt::CreditDefaultSwap::ProtectionPaymentTime::atMaturity) {
88 XMLUtils::addChild(doc, d,
"DefaultPaymentTime",
"atMaturity");
90 QL_FAIL(
"toXML(): unexpected DefaultPaymentTime");
93 XMLUtils::appendNode(d, d1);
94 for (
auto& l : independentPayments_) {
95 XMLUtils::appendNode(d1, l.toXML(doc));
98 XMLUtils::appendNode(d, d2);
99 for (
auto& l : contingentPayments_) {
100 XMLUtils::appendNode(d2, l.toXML(doc));
103 XMLUtils::appendNode(d, d3);
104 for (
auto& l : defaultPayments_) {
105 XMLUtils::appendNode(d3, l.toXML(doc));
108 XMLUtils::appendNode(d, d4);
109 for (
auto& l : recoveryPayments_) {
110 XMLUtils::appendNode(d4, l.toXML(doc));
115void CreditLinkedSwap::build(
const QuantLib::ext::shared_ptr<EngineFactory>& engineFactory) {
117 DLOG(
"Building credit linked swap " <<
id());
121 additionalData_[
"isdaAssetClass"] = string(
"Interest Rate");
122 additionalData_[
"isdaBaseProduct"] = string(
"Exotic");
123 additionalData_[
"isdaSubProduct"] = string(
"");
124 additionalData_[
"isdaTransaction"] = string(
"");
128 npvCurrency_.clear();
129 for (
auto const& l : independentPayments_) {
130 QL_REQUIRE(npvCurrency_ ==
"" || l.currency() == npvCurrency_,
131 "CreditLinkedSwap: all leg currencies must be the same, found " << npvCurrency_ <<
" and "
133 npvCurrency_ = l.currency();
138 auto builder = QuantLib::ext::dynamic_pointer_cast<CreditLinkedSwapEngineBuilder>(engineFactory->builder(tradeType()));
139 QL_REQUIRE(builder,
"CreditLinkedSwap: wrong builder, expected CreditLinkedSwapEngineBuilder");
140 auto configuration = builder->configuration(MarketContext::pricing);
144 std::vector<QuantExt::CreditLinkedSwap::LegType> legTypes;
145 for (
auto const& l : independentPayments_) {
146 auto legBuilder = engineFactory->legBuilder(l.legType());
147 legs_.push_back(legBuilder->buildLeg(l, engineFactory, requiredFixings_, configuration));
149 legTypes.push_back(QuantExt::CreditLinkedSwap::LegType::IndependentPayments);
151 for (
auto const& l : contingentPayments_) {
152 auto legBuilder = engineFactory->legBuilder(l.legType());
153 legs_.push_back(legBuilder->buildLeg(l, engineFactory, requiredFixings_, configuration));
155 legTypes.push_back(QuantExt::CreditLinkedSwap::LegType::ContingentPayments);
157 for (
auto const& l : defaultPayments_) {
158 auto legBuilder = engineFactory->legBuilder(l.legType());
159 legs_.push_back(legBuilder->buildLeg(l, engineFactory, requiredFixings_, configuration));
161 legTypes.push_back(QuantExt::CreditLinkedSwap::LegType::DefaultPayments);
163 for (
auto const& l : recoveryPayments_) {
164 auto legBuilder = engineFactory->legBuilder(l.legType());
165 legs_.push_back(legBuilder->buildLeg(l, engineFactory, requiredFixings_, configuration));
167 legTypes.push_back(QuantExt::CreditLinkedSwap::LegType::RecoveryPayments);
178 instrument_ = QuantLib::ext::make_shared<VanillaInstrument>(qlInstr);
182 notionalCurrency_ = npvCurrency_;
183 legCurrencies_ = std::vector<std::string>(
legs_.size(), npvCurrency_);
184 maturity_ = qlInstr->maturity();
188 qlInstr->setPricingEngine(builder->engine(npvCurrency_, creditCurveId_));
189 setSensitivityTemplate(*builder);
193 DLOG(
"Finished building credit linked swap " <<
id());
194 DLOG(
"Currency : " << npvCurrency_);
195 DLOG(
"IndependentPayments legs: " << independentPayments_.size());
196 DLOG(
"ContingentPayments legs: " << contingentPayments_.size());
197 DLOG(
"DefaultPayments legs: " << defaultPayments_.size());
198 DLOG(
"RecoveryPayments legs: " << recoveryPayments_.size());
201QuantLib::Real CreditLinkedSwap::notional()
const {
203 for (
auto const& l :
legs_)
std::vector< bool > legPayers_
QuantExt::CreditDefaultSwap::ProtectionPaymentTime defaultPaymentTime_
void fromXML(XMLNode *node) override
Serializable object holding leg data.
Small XML Document wrapper class.
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
credit linked swap data model
Currency parseCurrency(const string &s)
Convert text to QuantLib::Currency.
#define DLOG(text)
Logging Macro (Level = Debug)
Real currentNotional(const Leg &leg)
Serializable Credit Default Swap.