Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
calendaradjustment.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 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 <algorithm>
20// clang-format off
21#include <boost/test/unit_test.hpp>
22#include <boost/test/data/test_case.hpp>
23// clang-format on
29#include <oret/datapaths.hpp>
30#include <oret/toplevelfixture.hpp>
31#include <ql/time/date.hpp>
32#include <string>
33
34using namespace QuantLib;
35using namespace boost::unit_test_framework;
36using namespace std;
37using namespace ore::data;
38
39using ore::test::TopLevelFixture;
40
42
43namespace {
44
45// Fixture used in test case below:
46// - Provide calendar adjustment file
47// - Provide expected calendars from another system
48class F : public TopLevelFixture {
49public:
50 CalendarAdjustmentConfig calendarAdjustments;
51 Date startDate;
52 Date endDate;
53
54 F() {
55 calendarAdjustments.fromFile(TEST_INPUT_FILE("calendaradjustments.xml"));
56 startDate = Date(1, Jan, 2019);
57 endDate = Date(31, Dec, 2020);
58 }
59
60 ~F() {}
61};
62
63struct TestDatum {
64 string calendarName;
65 std::vector<Date> holidays;
66};
67
68std::vector<TestDatum> loadExpectedHolidays() {
69 // load from file
70 string fileName = TEST_INPUT_FILE("holidays.csv");
71 std::vector<TestDatum> data;
72 ifstream file;
73 file.open(fileName);
74 QL_REQUIRE(file.is_open(), "error opening file " << fileName);
75 std::string line;
76 // skip empty lines
77 while (!file.eof()) {
78 getline(file, line);
79 boost::trim(line);
80 if (line != "") {
81 vector<string> elements;
82 boost::split(elements, line, boost::is_any_of(","), boost::token_compress_on);
83 QL_REQUIRE(elements.size() > 1, "Not enough elements in the calendar");
84 TestDatum td;
85 td.calendarName = elements.front();
86 for (Size i = 1; i < elements.size(); i++) {
87 Date d = parseDate(elements[i]);
88 if (d.weekday() != Saturday && d.weekday() != Sunday) {
89 td.holidays.push_back(parseDate(elements[i]));
90 }
91 }
92 data.push_back(td);
93 }
94 }
95 file.close();
96 return data;
97}
98} // namespace
99
100BOOST_FIXTURE_TEST_SUITE(OREDataTestSuite, F)
101
102BOOST_AUTO_TEST_SUITE(CalendarAdjustmentsTests)
103
104// BOOST_DATA_TEST_CASE(testCalendarAdjustmentRealCalendars, bdata::make(loadExpectedHolidays()), expectedHolidays)
105BOOST_AUTO_TEST_CASE(testCalendarAdjustmentRealCalendars) {
106 // loop over expected holidays, for each calendar call parseCalendar()
107 // and check that the holidays match the expected ones
108 for (auto expectedHolidays : loadExpectedHolidays()) {
109 vector<Date> qcalHols;
110 qcalHols = parseCalendar(expectedHolidays.calendarName).holidayList(startDate, endDate, false);
111 BOOST_CHECK_EQUAL_COLLECTIONS(qcalHols.begin(), qcalHols.end(), expectedHolidays.holidays.begin(),
112 expectedHolidays.holidays.end());
113 }
114}
115
116BOOST_AUTO_TEST_CASE(testCalendarAdjustment) {
117 BOOST_TEST_MESSAGE("Testing calendar adjustments...");
118
121 BOOST_REQUIRE(cac.getCalendars().empty());
122
123 // adding UK holiday not in Quantlib calendar
124 cac.addHolidays("UK", Date(29, April, 2011));
125 // Checking that we get it back
126 // Note getCalenders get the quantlib name
127 BOOST_REQUIRE(cac.getCalendars() == set<string>({"UK settlement"}));
128
129 BOOST_REQUIRE(cac.getHolidays("UK") == set<Date>({Date(29, April, 2011)}));
130 BOOST_REQUIRE(cac.getBusinessDays("UK").empty());
131
132 cac.addBusinessDays("UK", Date(25, December, 2011));
133 BOOST_REQUIRE(cac.getBusinessDays("UK") == set<Date>({Date(25, December, 2011)}));
134 cac.addHolidays("UK", Date(25, December, 2011));
135
136 cacad.addHolidays("JPY", Date(1, May, 2019));
137 cac.append(cacad);
138 set<string> expcal = {"Japan", "UK settlement"};
139 set<string> res = {cac.getCalendars()};
140 BOOST_CHECK_EQUAL_COLLECTIONS(res.begin(), res.end(), expcal.begin(), expcal.end());
141 BOOST_REQUIRE(cac.getHolidays("JPY") == set<Date>({Date(1, May, 2019)}));
142}
143
144BOOST_AUTO_TEST_CASE(testInvalidCalendarAdjustment) {
145 BOOST_TEST_MESSAGE("Testing that incorrect CalendarAdjustments are not accepted...");
146
147 // we check that new calendars can't be declared using another new calendar as a base
148 CalendarAdjustmentConfig calendarAdjustments_1;
149 BOOST_CHECK_THROW(calendarAdjustments_1.fromFile(TEST_INPUT_FILE("invalid_calendaradjustments_1.xml")), QuantLib::Error);
150}
151
152BOOST_AUTO_TEST_SUITE_END()
153
154BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(testCalendarAdjustmentRealCalendars)
Interface for calendar modifications, additional holidays and business days.
const set< Date > & getHolidays(const string &calname) const
Returns all the holidays for a given cal name.
void addBusinessDays(const string &calname, const Date &d)
This method adds d to the list of business days for cal name.
const set< Date > & getBusinessDays(const string &calname) const
Returns all the business days for a given calname.
void addHolidays(const string &calname, const Date &d)
This method adds d to the list of holidays for cal name.
void append(const CalendarAdjustmentConfig &c)
add all holidays and business days from c to this instance
void fromFile(const std::string &filename)
Definition: xmlutils.cpp:150
utility class to access CSV files
Market Datum Loader Implementation.
Calendar parseCalendar(const string &s)
Convert text to QuantLib::Calendar.
Definition: parsers.cpp:157
Date parseDate(const string &s)
Convert std::string to QuantLib::Date.
Definition: parsers.cpp:51
@ data
Definition: log.hpp:77
Map text representations to QuantLib/QuantExt types.
string conversion utilities