19#include <oret/toplevelfixture.hpp>
20#include <boost/test/unit_test.hpp>
21#include <oret/datapaths.hpp>
42using std::setprecision;
50QuantLib::ext::shared_ptr<Portfolio> buildPortfolio(
const Date& asof,
const string& inputDir) {
52 Settings::instance().evaluationDate() = asof;
54 auto conventions = QuantLib::ext::make_shared<Conventions>();
55 conventions->fromFile(TEST_INPUT_FILE(
string(inputDir +
"/conventions.xml")));
56 InstrumentConventions::instance().setConventions(conventions);
58 auto curveConfigs = QuantLib::ext::make_shared<CurveConfigurations>();
59 curveConfigs->fromFile(TEST_INPUT_FILE(
string(inputDir +
"/curveconfig.xml")));
61 auto todaysMarketParameters = QuantLib::ext::make_shared<TodaysMarketParameters>();
62 todaysMarketParameters->fromFile(TEST_INPUT_FILE(
string(inputDir +
"/todaysmarket.xml")));
64 auto loader = QuantLib::ext::make_shared<CSVLoader>(TEST_INPUT_FILE(
string(inputDir +
"/market.txt")),
65 TEST_INPUT_FILE(
string(inputDir +
"/fixings.txt")),
false);
67 auto tm = QuantLib::ext::make_shared<TodaysMarket>(asof, todaysMarketParameters, loader,
curveConfigs);
69 QuantLib::ext::shared_ptr<EngineData>
data = QuantLib::ext::make_shared<EngineData>();
70 data->fromFile(TEST_INPUT_FILE(
string(inputDir +
"/pricingengine.xml")));
72 auto rdm = QuantLib::ext::make_shared<BasicReferenceDataManager>(
73 TEST_INPUT_FILE(
string(inputDir +
"/reference_data.xml")));
75 map<MarketContext, string> configurations;
76 vector<QuantLib::ext::shared_ptr<EngineBuilder>> extraEngineBuilders{
77 QuantLib::ext::make_shared<MidPointIndexCdsEngineBuilder>(),
78 QuantLib::ext::make_shared<BlackIndexCdsOptionEngineBuilder>()
80 vector<QuantLib::ext::shared_ptr<LegBuilder>> extraLegBuilders;
81 QuantLib::ext::shared_ptr<EngineFactory> engineFactory = QuantLib::ext::make_shared<EngineFactory>(
data, tm, configurations, rdm);
83 auto portfolio = QuantLib::ext::make_shared<Portfolio>();
84 portfolio->fromFile(TEST_INPUT_FILE(
string(inputDir +
"/portfolio.xml")));
85 portfolio->build(engineFactory);
91void checkNpvs(
const QuantLib::ext::shared_ptr<Portfolio>& portfolio, Real tol, Real relTol = Null<Real>()) {
92 for (
const auto& [tradeId, trade] : portfolio->trades()) {
93 auto npv = trade->instrument()->NPV();
94 if (relTol != Null<Real>()) {
95 auto opt = QuantLib::ext::dynamic_pointer_cast<ore::data::IndexCreditDefaultSwapOption>(trade);
96 Real expNpv = opt->option().premiumData().premiumData().front().amount;
97 Real relDiff = npv / expNpv;
98 BOOST_TEST_CONTEXT(
"trade_id,npv,expNpv,relDiff:" << tradeId << fixed << setprecision(2) <<
99 "," << npv <<
"," << expNpv <<
"," << setprecision(6) << relDiff) {
100 BOOST_CHECK(std::abs(npv) < tol || std::abs(relDiff) < relTol);
103 BOOST_TEST_CONTEXT(
"trade_id,npv:" << tradeId << fixed << setprecision(2) <<
"," << npv) {
104 BOOST_CHECK_SMALL(npv, tol);
112BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, ore::test::TopLevelFixture)
114BOOST_AUTO_TEST_SUITE(CdsIndexOptionTest)
128 BOOST_TEST_MESSAGE(
"Testing pricing for spread strike, no existing defaults, using index curve ...");
129 checkNpvs(buildPortfolio(Date(22, Apr, 2021),
"cdx_ig_36_v1_2021-04-22_index"), 6.5);
133 BOOST_TEST_MESSAGE(
"Testing pricing for spread strike, no existing defaults, using underlying curves ...");
134 checkNpvs(buildPortfolio(Date(22, Apr, 2021),
"cdx_ig_36_v1_2021-04-22_underlyings"), 12.0);
138 BOOST_TEST_MESSAGE(
"Testing pricing for price strike, no existing defaults, using index curve ...");
139 checkNpvs(buildPortfolio(Date(22, Apr, 2021),
"cdx_hy_36_v1_2021-04-22_index"), 10, 0.105);
145 BOOST_TEST_MESSAGE(
"Testing pricing for price strike, no existing defaults, using underlying curves ...");
146 checkNpvs(buildPortfolio(Date(22, Apr, 2021),
"cdx_hy_36_v1_2021-04-22_underlyings"), 20, 0.25);
149BOOST_AUTO_TEST_SUITE_END()
150BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(testSpreadStrikeNoDefaultsIndexCurve)
Black CDS option engine builder for index CDS options.
Midpoint Engine Builder class for IndexCreditDefaultSwaps.
Market Datum Loader Implementation.
A class to hold pricing engine parameters.
Reference data model and serialization.
vector< string > curveConfigs
An concrete implementation of the Market class that loads todays market and builds the required curve...