Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
dategeneration.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 "toplevelfixture.hpp"
20#include <boost/test/unit_test.hpp>
21#include <ql/time/calendars/target.hpp>
23#include <ql/time/schedule.hpp>
24
25using namespace QuantLib;
26using namespace QuantExt;
27using namespace boost::unit_test_framework;
28using namespace std;
29
30namespace check {
31
32 void checkDates(const Schedule& s,
33 const std::vector<Date>& expected) {
34 BOOST_CHECK(s.size() == expected.size());
35 for (Size i=0; i<expected.size(); ++i) {
36 if (s[i] != expected[i]) {
37 BOOST_ERROR("expected " << expected[i]
38 << " at index " << i << ", "
39 "found " << s[i]);
40 }
41 }
42 }
43 void checkDay(const Schedule& s,
44 Weekday w) {
45 //we check all but the first and last dates
46 for (Size i=1; i<s.size() - 1; ++i) {
47 if (s[i].weekday() != w) {
48 BOOST_ERROR("expected " << w
49 << " at index " << i << ", "
50 "found " << s[i].weekday());
51 }
52 }
53 }
54} // namespace check
55
56BOOST_FIXTURE_TEST_SUITE(QuantExtTestSuite, qle::test::TopLevelFixture)
57
58BOOST_AUTO_TEST_SUITE(DateGenerationTest)
59
60BOOST_AUTO_TEST_CASE(testThirdThursday) {
61
62 BOOST_TEST_MESSAGE("Testing ThirdThursday DateGeneration rule");
63
64 std::vector<Date> expectedDates;
65
66 expectedDates.push_back(Date(12, December, 2016));
67 expectedDates.push_back(Date(16, March, 2017));
68 expectedDates.push_back(Date(15, June, 2017));
69 expectedDates.push_back(Date(21, September, 2017));
70 expectedDates.push_back(Date(21, December, 2017));
71 expectedDates.push_back(Date(15, March, 2018));
72 expectedDates.push_back(Date(21, June, 2018));
73 expectedDates.push_back(Date(20, September, 2018));
74 expectedDates.push_back(Date(12, December, 2018));
75
76 Schedule s =
77 MakeSchedule().from(Date(12, December, 2016))
78 .to(Date(12, December, 2016) + Period(2, Years))
79 .withCalendar(TARGET())
80 .withTenor(3*Months)
81 .withConvention(ModifiedFollowing)
82 .withTerminationDateConvention(Unadjusted)
83 .withRule(DateGeneration::ThirdThursday);
84
85 check::checkDates(s, expectedDates);
86 check::checkDay(s, Thursday);
87}
88
89BOOST_AUTO_TEST_CASE(testThirdFriday) {
90
91 BOOST_TEST_MESSAGE("Testing ThirdFriday DateGeneration rule");
92
93 std::vector<Date> expectedDates;
94
95 expectedDates.push_back(Date(12, December, 2016));
96 expectedDates.push_back(Date(17, March, 2017));
97 expectedDates.push_back(Date(16, June, 2017));
98 expectedDates.push_back(Date(15, September, 2017));
99 expectedDates.push_back(Date(15, December, 2017));
100 expectedDates.push_back(Date(16, March, 2018));
101 expectedDates.push_back(Date(15, June, 2018));
102 expectedDates.push_back(Date(21, September, 2018));
103 expectedDates.push_back(Date(12, December, 2018));
104
105 Schedule s =
106 MakeSchedule().from(Date(12, December, 2016))
107 .to(Date(12, December, 2016) + Period(2, Years))
108 .withCalendar(TARGET())
109 .withTenor(3*Months)
110 .withConvention(ModifiedFollowing)
111 .withTerminationDateConvention(Unadjusted)
112 .withRule(DateGeneration::ThirdFriday);
113
114 check::checkDates(s, expectedDates);
115 check::checkDay(s, Friday);
116}
117
118BOOST_AUTO_TEST_CASE(testMondayAfterThirdFriday) {
119
120 BOOST_TEST_MESSAGE("Testing MondayAfterThirdFriday DateGeneration rule");
121
122 std::vector<Date> expectedDates;
123
124 expectedDates.push_back(Date(12, December, 2016));
125 expectedDates.push_back(Date(20, March, 2017));
126 expectedDates.push_back(Date(19, June, 2017));
127 expectedDates.push_back(Date(18, September, 2017));
128 expectedDates.push_back(Date(18, December, 2017));
129 expectedDates.push_back(Date(19, March, 2018));
130 expectedDates.push_back(Date(18, June, 2018));
131 expectedDates.push_back(Date(24, September, 2018));
132 expectedDates.push_back(Date(12, December, 2018));
133
134 Schedule s =
135 MakeSchedule().from(Date(12, December, 2016))
136 .to(Date(12, December, 2016) + Period(2, Years))
137 .withCalendar(TARGET())
138 .withTenor(3*Months)
139 .withConvention(ModifiedFollowing)
140 .withTerminationDateConvention(Unadjusted)
141 .withRule(DateGeneration::MondayAfterThirdFriday);
142
143 check::checkDates(s, expectedDates);
144 check::checkDay(s, Monday);
145}
146
147BOOST_AUTO_TEST_CASE(testTuesdayAfterThirdFriday) {
148
149 BOOST_TEST_MESSAGE("Testing TuesdayAfterThirdFriday DateGeneration rule");
150
151 std::vector<Date> expectedDates;
152
153 expectedDates.push_back(Date(12, December, 2016));
154 expectedDates.push_back(Date(21, March, 2017));
155 expectedDates.push_back(Date(20, June, 2017));
156 expectedDates.push_back(Date(19, September, 2017));
157 expectedDates.push_back(Date(19, December, 2017));
158 expectedDates.push_back(Date(20, March, 2018));
159 expectedDates.push_back(Date(19, June, 2018));
160 expectedDates.push_back(Date(25, September, 2018));
161 expectedDates.push_back(Date(12, December, 2018));
162
163 Schedule s =
164 MakeSchedule().from(Date(12, December, 2016))
165 .to(Date(12, December, 2016) + Period(2, Years))
166 .withCalendar(TARGET())
167 .withTenor(3*Months)
168 .withConvention(ModifiedFollowing)
169 .withTerminationDateConvention(Unadjusted)
170 .withRule(DateGeneration::TuesdayAfterThirdFriday);
171
172 check::checkDates(s, expectedDates);
173 check::checkDay(s, Tuesday);
174}
175
176BOOST_AUTO_TEST_SUITE_END()
177
178BOOST_AUTO_TEST_SUITE_END()
Belgian calendar.
BOOST_AUTO_TEST_CASE(testThirdThursday)
void checkDates(const Schedule &s, const std::vector< Date > &expected)
void checkDay(const Schedule &s, Weekday w)
Fixture that can be used at top level.