Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions | Variables
cds.cpp File Reference
#include <boost/make_shared.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <ored/configuration/curveconfigurations.hpp>
#include <ored/marketdata/csvloader.hpp>
#include <ored/marketdata/loader.hpp>
#include <ored/marketdata/marketimpl.hpp>
#include <ored/marketdata/todaysmarket.hpp>
#include <ored/portfolio/builders/creditdefaultswap.hpp>
#include <ored/portfolio/creditdefaultswap.hpp>
#include <ored/portfolio/enginedata.hpp>
#include <ored/portfolio/envelope.hpp>
#include <ored/portfolio/legdata.hpp>
#include <ored/portfolio/portfolio.hpp>
#include <ored/portfolio/schedule.hpp>
#include <ored/utilities/indexparser.hpp>
#include <oret/datapaths.hpp>
#include <oret/toplevelfixture.hpp>
#include <ql/termstructures/credit/flathazardrate.hpp>
#include <ql/termstructures/volatility/swaption/swaptionconstantvol.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/time/daycounters/simpledaycounter.hpp>

Go to the source code of this file.

Functions

 BOOST_DATA_TEST_CASE (testCreditDefaultSwap, bdata::make(marketInputs) ^ bdata::make(tradeInputs) ^ bdata::make(expNpvs), mkt, trd, exp)
 
 BOOST_DATA_TEST_CASE_F (TopLevelFixture, testCreditDefaultSwapBuilding, bdata::make(trades), trade)
 
ostream & operator<< (ostream &os, const UpfrontFiles &upfrontFiles)
 
 BOOST_DATA_TEST_CASE (testUpfrontDefaultCurveConsistency, bdata::make(upfrontFiles), files)
 
 BOOST_AUTO_TEST_CASE (testUpfrontCurveBuildFailsIfNoRunningSpread)
 
 BOOST_AUTO_TEST_CASE (testSimultaneousUsageCdsQuoteTypes)
 

Variables

UpfrontFiles upfrontFiles [] = {{"market.txt", "curveconfig.xml"}, {"market_rs.txt", "curveconfig_rs.xml"}}
 

Function Documentation

◆ BOOST_DATA_TEST_CASE() [1/2]

BOOST_DATA_TEST_CASE ( testCreditDefaultSwap  ,
bdata::make(marketInputs) ^ bdata::make(tradeInputs) ^ bdata::make(expNpvs)  ,
mkt  ,
trd  ,
exp   
)

Definition at line 231 of file cds.cpp.

232 {
233
234 BOOST_CHECK_CLOSE(cdsNpv(mkt, trd), exp, 0.01);
235}
+ Here is the call graph for this function:

◆ BOOST_DATA_TEST_CASE_F()

BOOST_DATA_TEST_CASE_F ( TopLevelFixture  ,
testCreditDefaultSwapBuilding  ,
bdata::make(trades)  ,
trade   
)

Definition at line 237 of file cds.cpp.

237 {
238
239 BOOST_TEST_MESSAGE("Test the building of various CDS trades from XML");
240
241 Settings::instance().evaluationDate() = Date(3, Feb, 2016);
242
243 // Read in the trade
244 Portfolio p;
245 string portfolioFile = "trades/" + trade + ".xml";
246 p.fromFile(TEST_INPUT_FILE(portfolioFile));
247 BOOST_REQUIRE_MESSAGE(p.size() == 1, "Expected portfolio to contain a single trade");
248
249 // Use the test market
250 QuantLib::ext::shared_ptr<Market> market = QuantLib::ext::make_shared<TestMarket>(0.02, 0.4, 0.05);
251
252 // Engine data
253 QuantLib::ext::shared_ptr<EngineData> ed = QuantLib::ext::make_shared<EngineData>();
254 ed->model("CreditDefaultSwap") = "DiscountedCashflows";
255 ed->engine("CreditDefaultSwap") = "MidPointCdsEngine";
256
257 // Test that the trade builds and prices without error
258 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(ed, market);
259 BOOST_CHECK_NO_THROW(p.build(engineFactory));
260 Real npv;
261 BOOST_CHECK_NO_THROW(npv = p.trades().begin()->second->instrument()->NPV());
262 BOOST_TEST_MESSAGE("CDS NPV is: " << npv);
263}
Serializable portfolio.
Definition: portfolio.hpp:43
const std::map< std::string, QuantLib::ext::shared_ptr< Trade > > & trades() const
Return the map tradeId -> trade.
Definition: portfolio.cpp:162
QuantLib::Size size() const
Portfolio size.
Definition: portfolio.hpp:68
void build(const QuantLib::ext::shared_ptr< EngineFactory > &, const std::string &context="unspecified", const bool emitStructuredError=true)
Call build on all trades in the portfolio, the context is included in error messages.
Definition: portfolio.cpp:122
void fromFile(const std::string &filename)
Definition: xmlutils.cpp:150
+ Here is the call graph for this function:

◆ operator<<()

ostream & operator<< ( ostream &  os,
const UpfrontFiles &  upfrontFiles 
)

Definition at line 271 of file cds.cpp.

271 {
272 return os << "[" << upfrontFiles.market << "," << upfrontFiles.curveConfig << "]";
273}
UpfrontFiles upfrontFiles[]
Definition: cds.cpp:275

◆ BOOST_DATA_TEST_CASE() [2/2]

BOOST_DATA_TEST_CASE ( testUpfrontDefaultCurveConsistency  ,
bdata::make(upfrontFiles ,
files   
)

Definition at line 280 of file cds.cpp.

280 {
281
282 BOOST_TEST_MESSAGE("Testing upfront default curve consistency ...");
283
284 Date asof(6, Nov, 2020);
285 Settings::instance().evaluationDate() = asof;
286 string dir("upfront");
287 Real tol = 0.0001;
288
289 TodaysMarketFiles tmf;
290 tmf.market = files.market;
291 tmf.curveConfig = files.curveConfig;
292
293 QuantLib::ext::shared_ptr<TodaysMarket> tm;
294 BOOST_REQUIRE_NO_THROW(tm = createTodaysMarket(asof, dir, tmf));
295
296 QuantLib::ext::shared_ptr<EngineData> data = QuantLib::ext::make_shared<EngineData>();
297 data->fromFile(TEST_INPUT_FILE(string(dir + "/pricingengine.xml")));
298 QuantLib::ext::shared_ptr<EngineFactory> ef = QuantLib::ext::make_shared<EngineFactory>(data, tm);
299
300 Portfolio portfolio;
301 portfolio.fromFile(TEST_INPUT_FILE(string(dir + "/portfolio.xml")));
302 portfolio.build(ef);
303
304 for (const auto& [tradeId, trade] : portfolio.trades()) {
305 auto npv = trade->instrument()->NPV();
306 BOOST_TEST_CONTEXT("NPV of trade " << tradeId << " is " << fixed << setprecision(12) << npv) {
307 BOOST_CHECK_SMALL(trade->instrument()->NPV(), tol);
308 }
309 }
310}
@ data
Definition: log.hpp:77
+ Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [1/2]

BOOST_AUTO_TEST_CASE ( testUpfrontCurveBuildFailsIfNoRunningSpread  )

Definition at line 312 of file cds.cpp.

312 {
313
314 BOOST_TEST_MESSAGE("Testing upfront failure when no running spread ...");
315
316 TodaysMarketFiles tmf;
317 tmf.curveConfig = "curveconfig_no_rs.xml";
318
319 Date asof(6, Nov, 2020);
320 Settings::instance().evaluationDate() = Date(6, Nov, 2020);
321 BOOST_CHECK_THROW(createTodaysMarket(asof, "upfront", tmf), QuantLib::Error);
322}

◆ BOOST_AUTO_TEST_CASE() [2/2]

BOOST_AUTO_TEST_CASE ( testSimultaneousUsageCdsQuoteTypes  )

Definition at line 324 of file cds.cpp.

324 {
325
326 BOOST_TEST_MESSAGE("Testing different CDS quote types can be used together ...");
327
328 TodaysMarketFiles tmf;
329 tmf.todaysMarket = "todaysmarket_all_cds_quote_types.xml";
330
331 Date asof(6, Nov, 2020);
332 Settings::instance().evaluationDate() = Date(6, Nov, 2020);
333
334 // Check that todaysmarket instance is created without error.
335 QuantLib::ext::shared_ptr<TodaysMarket> tm;
336 BOOST_CHECK_NO_THROW(tm = createTodaysMarket(asof, "upfront", tmf));
337
338 // Check that each of the three expected curves exist and give survival probability.
339 vector<string> curveNames = {
340 "RED:8B69AP|SNRFOR|USD|CR-UPFRONT",
341 "RED:8B69AP|SNRFOR|USD|CR-PAR_SPREAD",
342 "RED:8B69AP|SNRFOR|USD|CR-CONV_SPREAD",
343 };
344
345 for (const string& curveName : curveNames) {
346 BOOST_TEST_CONTEXT("Checking default curve " << curveName) {
347 Handle<DefaultProbabilityTermStructure> dpts;
348 BOOST_CHECK_NO_THROW(dpts = tm->defaultCurve(curveName)->curve());
349 BOOST_CHECK_NO_THROW(dpts->survivalProbability(1.0));
350 }
351 }
352}

Variable Documentation

◆ upfrontFiles

UpfrontFiles upfrontFiles[] = {{"market.txt", "curveconfig.xml"}, {"market_rs.txt", "curveconfig_rs.xml"}}

Definition at line 275 of file cds.cpp.