Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
creditdefaultswapdata.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include <boost/test/unit_test.hpp>
20#include <oret/toplevelfixture.hpp>
21
24#include <ql/currencies/europe.hpp>
25
26using namespace std;
27using namespace boost::unit_test_framework;
28using namespace QuantLib;
29using namespace ore::data;
30
31namespace {
32
33LegData premiumLegData() {
34
35 ScheduleData scheduleData(
36 ScheduleRules("2019-10-02", "2024-12-20", "3M", "WeekendsOnly", "Following", "Unadjusted", "CDS2015"));
37
38 auto fixedLegData = QuantLib::ext::make_shared<FixedLegData>(vector<Real>(1, 0.01));
39
40 return LegData(fixedLegData, true, "EUR", scheduleData, "A360", vector<Real>(1, 1000000), vector<string>(),
41 "Following");
42}
43
44} // namespace
45
46BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
47
48BOOST_AUTO_TEST_SUITE(CdsReferenceInformationTests)
49
50BOOST_AUTO_TEST_CASE(testToFromXml) {
51
52 BOOST_TEST_MESSAGE("Testing toXML and fromXml for CdsReferenceInformation");
53
54 // Explicitly create a CdsReferenceInformation object
55 string referenceEntityId = "RED:2H6677";
56 CdsTier tier = CdsTier::SNRFOR;
57 Currency currency = EURCurrency();
58 CdsDocClause docClause = CdsDocClause::MM14;
59 CdsReferenceInformation inRef(referenceEntityId, tier, currency, docClause);
60
61 // Check the id() is as expected
62 string expId = referenceEntityId + "|" + to_string(tier) + "|" + currency.code() + "|" + to_string(docClause);
63 BOOST_CHECK_EQUAL(inRef.id(), expId);
64
65 // Use toXml to serialise to string
66 string xmlStr = inRef.toXMLString();
67
68 // Use fromXml to populate empty CdsReferenceInformation object
70 outRef.fromXMLString(xmlStr);
71
72 // Check against the original object
73 BOOST_CHECK_EQUAL(inRef.referenceEntityId(), outRef.referenceEntityId());
74 BOOST_CHECK_EQUAL(inRef.tier(), outRef.tier());
75 BOOST_CHECK_EQUAL(inRef.currency(), outRef.currency());
76 BOOST_CHECK_EQUAL(inRef.docClause(), outRef.docClause());
77 BOOST_CHECK_EQUAL(inRef.id(), outRef.id());
78}
79
80BOOST_AUTO_TEST_SUITE_END()
81
82BOOST_AUTO_TEST_SUITE(CreditDefaultSwapDataTests)
83
84BOOST_AUTO_TEST_CASE(testConstructionWithExplicitCreditCurveId) {
85
86 // Construct with explicit credit curve ID
87 string cdsCurveId = "RED:2H6677|SNRFOR|EUR|MM14";
88 CreditDefaultSwapData cdsData("DB", cdsCurveId, premiumLegData());
89
90 // Perform some checks
91 BOOST_CHECK_EQUAL(cdsData.issuerId(), "DB");
92 BOOST_CHECK(cdsData.settlesAccrual());
93 BOOST_CHECK_EQUAL(cdsData.protectionPaymentTime(), QuantExt::CreditDefaultSwap::ProtectionPaymentTime::atDefault);
94 BOOST_CHECK_EQUAL(cdsData.protectionStart(), Date());
95 BOOST_CHECK_EQUAL(cdsData.upfrontDate(), Date());
96 BOOST_CHECK_EQUAL(cdsData.upfrontFee(), Null<Real>());
97 BOOST_CHECK_EQUAL(cdsData.recoveryRate(), Null<Real>());
98 BOOST_CHECK(cdsData.referenceObligation().empty());
99
100 // Check the credit curve Id and reference information are as expected
101 BOOST_CHECK_EQUAL(cdsData.creditCurveId(), cdsCurveId);
102 BOOST_CHECK(!cdsData.referenceInformation());
103
104 // Use toXml to serialise to string
105 string xmlStr = cdsData.toXMLString();
106
107 // Use fromXml to populate empty CreditDefaultSwapData object
108 CreditDefaultSwapData xmlCdsData;
109 xmlCdsData.fromXMLString(xmlStr);
110
111 // Check that the CreditDefaultSwapData object from XML is the same as the explicitly created one
112 BOOST_CHECK_EQUAL(cdsData.issuerId(), xmlCdsData.issuerId());
113 BOOST_CHECK_EQUAL(cdsData.settlesAccrual(), xmlCdsData.settlesAccrual());
114 BOOST_CHECK_EQUAL(cdsData.protectionPaymentTime(), xmlCdsData.protectionPaymentTime());
115 BOOST_CHECK_EQUAL(cdsData.protectionStart(), xmlCdsData.protectionStart());
116 BOOST_CHECK_EQUAL(cdsData.upfrontDate(), xmlCdsData.upfrontDate());
117 BOOST_CHECK_EQUAL(cdsData.upfrontFee(), xmlCdsData.upfrontFee());
118 BOOST_CHECK_EQUAL(cdsData.recoveryRate(), xmlCdsData.recoveryRate());
119 BOOST_CHECK_EQUAL(cdsData.referenceObligation(), xmlCdsData.referenceObligation());
120 BOOST_CHECK_EQUAL(cdsData.creditCurveId(), xmlCdsData.creditCurveId());
121 // The following check is removed because we initialize reference information from the curve id
122 // where possible because it has 4 tokens serparated by |
123 // BOOST_CHECK(!xmlCdsData.referenceInformation());
124}
125
126BOOST_AUTO_TEST_CASE(testConstructionWithCdsReferenceInformation) {
127
128 // CdsReferenceInformation object
129 string referenceEntityId = "RED:2H6677";
130 CdsTier tier = CdsTier::SNRFOR;
131 Currency currency = EURCurrency();
132 CdsDocClause docClause = CdsDocClause::MM14;
133 CdsReferenceInformation referenceInfo(referenceEntityId, tier, currency, docClause);
134
135 // Construct with CDS reference information
136 CreditDefaultSwapData cdsData("DB", referenceInfo, premiumLegData());
137
138 // Perform some checks
139 BOOST_CHECK_EQUAL(cdsData.issuerId(), "DB");
140 BOOST_CHECK(cdsData.settlesAccrual());
141 BOOST_CHECK_EQUAL(cdsData.protectionPaymentTime(), QuantExt::CreditDefaultSwap::ProtectionPaymentTime::atDefault);
142 BOOST_CHECK_EQUAL(cdsData.protectionStart(), Date());
143 BOOST_CHECK_EQUAL(cdsData.upfrontDate(), Date());
144 BOOST_CHECK_EQUAL(cdsData.upfrontFee(), Null<Real>());
145 BOOST_CHECK_EQUAL(cdsData.recoveryRate(), Null<Real>());
146 BOOST_CHECK(cdsData.referenceObligation().empty());
147
148 // Check the credit curve Id and reference information are as expected
149 BOOST_CHECK_EQUAL(cdsData.creditCurveId(), referenceInfo.id());
150 BOOST_CHECK(cdsData.referenceInformation());
151 BOOST_CHECK_EQUAL(referenceInfo.referenceEntityId(), cdsData.referenceInformation()->referenceEntityId());
152 BOOST_CHECK_EQUAL(referenceInfo.tier(), cdsData.referenceInformation()->tier());
153 BOOST_CHECK_EQUAL(referenceInfo.currency(), cdsData.referenceInformation()->currency());
154 BOOST_CHECK_EQUAL(referenceInfo.docClause(), cdsData.referenceInformation()->docClause());
155 BOOST_CHECK_EQUAL(referenceInfo.id(), cdsData.referenceInformation()->id());
156
157 // Use toXml to serialise to string
158 string xmlStr = cdsData.toXMLString();
159
160 // Use fromXml to populate empty CreditDefaultSwapData object
161 CreditDefaultSwapData xmlCdsData;
162 xmlCdsData.fromXMLString(xmlStr);
163
164 // Check that the CreditDefaultSwapData object from XML is the same as the explicitly created one
165 BOOST_CHECK_EQUAL(cdsData.issuerId(), xmlCdsData.issuerId());
166 BOOST_CHECK_EQUAL(cdsData.settlesAccrual(), xmlCdsData.settlesAccrual());
167 BOOST_CHECK_EQUAL(cdsData.protectionPaymentTime(), xmlCdsData.protectionPaymentTime());
168 BOOST_CHECK_EQUAL(cdsData.protectionStart(), xmlCdsData.protectionStart());
169 BOOST_CHECK_EQUAL(cdsData.upfrontDate(), xmlCdsData.upfrontDate());
170 BOOST_CHECK_EQUAL(cdsData.upfrontFee(), xmlCdsData.upfrontFee());
171 BOOST_CHECK_EQUAL(cdsData.recoveryRate(), xmlCdsData.recoveryRate());
172 BOOST_CHECK_EQUAL(cdsData.referenceObligation(), xmlCdsData.referenceObligation());
173 BOOST_CHECK_EQUAL(cdsData.creditCurveId(), xmlCdsData.creditCurveId());
174 BOOST_CHECK(xmlCdsData.referenceInformation());
175 BOOST_CHECK_EQUAL(referenceInfo.referenceEntityId(), xmlCdsData.referenceInformation()->referenceEntityId());
176 BOOST_CHECK_EQUAL(referenceInfo.tier(), xmlCdsData.referenceInformation()->tier());
177 BOOST_CHECK_EQUAL(referenceInfo.currency(), xmlCdsData.referenceInformation()->currency());
178 BOOST_CHECK_EQUAL(referenceInfo.docClause(), xmlCdsData.referenceInformation()->docClause());
179 BOOST_CHECK_EQUAL(referenceInfo.id(), xmlCdsData.referenceInformation()->id());
180}
181
182BOOST_AUTO_TEST_SUITE_END()
183
184BOOST_AUTO_TEST_SUITE_END()
const std::string & referenceEntityId() const
const QuantLib::Currency & currency() const
const boost::optional< CdsReferenceInformation > & referenceInformation() const
const std::string & referenceObligation() const
CDS Reference Obligation.
Serializable object holding leg data.
Definition: legdata.hpp:844
Serializable schedule data.
Definition: schedule.hpp:202
Serializable object holding schedule Rules data.
Definition: schedule.hpp:37
std::string toXMLString() const
Parse from XML string.
Definition: xmlutils.cpp:168
void fromXMLString(const std::string &xml)
Parse from XML string.
Definition: xmlutils.cpp:162
A class to hold credit default swap data.
CdsDocClause
CDS documentation clause enumeration.
CdsTier
CDS debt tier enumeration.
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
BOOST_AUTO_TEST_CASE(testToFromXml)
string conversion utilities