QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
unitedstates.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2004, 2005 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
7 Copyright (C) 2017 Peter Caspers
8 Copyright (C) 2017 Oleg Kulkov
9 Copyright (C) 2023 Skandinaviska Enskilda Banken AB (publ)
10
11 This file is part of QuantLib, a free-software/open-source library
12 for financial quantitative analysts and developers - http://quantlib.org/
13
14 QuantLib is free software: you can redistribute it and/or modify it
15 under the terms of the QuantLib license. You should have received a
16 copy of the license along with this program; if not, please email
17 <quantlib-dev@lists.sf.net>. The license is also available online at
18 <http://quantlib.org/license.shtml>.
19
20 This program is distributed in the hope that it will be useful, but WITHOUT
21 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. See the license for more details.
23*/
24
26#include <ql/errors.hpp>
27
28namespace QuantLib {
29
30 namespace {
31
32 // a few rules used by multiple calendars
33
34 bool isWashingtonBirthday(Day d, Month m, Year y, Weekday w) {
35 if (y >= 1971) {
36 // third Monday in February
37 return (d >= 15 && d <= 21) && w == Monday && m == February;
38 } else {
39 // February 22nd, possibly adjusted
40 return (d == 22 || (d == 23 && w == Monday)
41 || (d == 21 && w == Friday)) && m == February;
42 }
43 }
44
45 bool isMemorialDay(Day d, Month m, Year y, Weekday w) {
46 if (y >= 1971) {
47 // last Monday in May
48 return d >= 25 && w == Monday && m == May;
49 } else {
50 // May 30th, possibly adjusted
51 return (d == 30 || (d == 31 && w == Monday)
52 || (d == 29 && w == Friday)) && m == May;
53 }
54 }
55
56 bool isLaborDay(Day d, Month m, Year y, Weekday w) {
57 // first Monday in September
58 return d <= 7 && w == Monday && m == September;
59 }
60
61 bool isColumbusDay(Day d, Month m, Year y, Weekday w) {
62 // second Monday in October
63 return (d >= 8 && d <= 14) && w == Monday && m == October
64 && y >= 1971;
65 }
66
67 bool isVeteransDay(Day d, Month m, Year y, Weekday w) {
68 if (y <= 1970 || y >= 1978) {
69 // November 11th, adjusted
70 return (d == 11 || (d == 12 && w == Monday) ||
71 (d == 10 && w == Friday)) && m == November;
72 } else {
73 // fourth Monday in October
74 return (d >= 22 && d <= 28) && w == Monday && m == October;
75 }
76 }
77
78 bool isVeteransDayNoSaturday(Day d, Month m, Year y, Weekday w) {
79 if (y <= 1970 || y >= 1978) {
80 // November 11th, adjusted, but no Saturday to Friday
81 return (d == 11 || (d == 12 && w == Monday)) && m == November;
82 } else {
83 // fourth Monday in October
84 return (d >= 22 && d <= 28) && w == Monday && m == October;
85 }
86 }
87
88 bool isJuneteenth(Day d, Month m, Year y, Weekday w, bool moveToFriday = true) {
89 // declared in 2021, but only observed by exchanges since 2022
90 return (d == 19 || (d == 20 && w == Monday) || ((d == 18 && w == Friday) && moveToFriday))
91 && m == June && y >= 2022;
92 }
93 }
94
96 // all calendar instances on the same market share the same implementation instance
97 static auto settlementImpl = ext::make_shared<UnitedStates::SettlementImpl>();
98 static auto liborImpactImpl = ext::make_shared<UnitedStates::LiborImpactImpl>();
99 static auto nyseImpl = ext::make_shared<UnitedStates::NyseImpl>();
100 static auto governmentImpl = ext::make_shared<UnitedStates::GovernmentBondImpl>();
101 static auto nercImpl = ext::make_shared<UnitedStates::NercImpl>();
102 static auto federalReserveImpl = ext::make_shared<UnitedStates::FederalReserveImpl>();
103 static auto sofrImpl = ext::make_shared<UnitedStates::SofrImpl>();
104
105 switch (market) {
106 case Settlement:
107 impl_ = settlementImpl;
108 break;
109 case LiborImpact:
110 impl_ = liborImpactImpl;
111 break;
112 case NYSE:
113 impl_ = nyseImpl;
114 break;
115 case GovernmentBond:
116 impl_ = governmentImpl;
117 break;
118 case SOFR:
119 impl_ = sofrImpl;
120 break;
121 case NERC:
122 impl_ = nercImpl;
123 break;
124 case FederalReserve:
125 impl_ = federalReserveImpl;
126 break;
127 default:
128 QL_FAIL("unknown market");
129 }
130 }
131
132
134 Weekday w = date.weekday();
135 Day d = date.dayOfMonth();
136 Month m = date.month();
137 Year y = date.year();
138 if (isWeekend(w)
139 // New Year's Day (possibly moved to Monday if on Sunday)
140 || ((d == 1 || (d == 2 && w == Monday)) && m == January)
141 // (or to Friday if on Saturday)
142 || (d == 31 && w == Friday && m == December)
143 // Martin Luther King's birthday (third Monday in January)
144 || ((d >= 15 && d <= 21) && w == Monday && m == January
145 && y >= 1983)
146 // Washington's birthday (third Monday in February)
147 || isWashingtonBirthday(d, m, y, w)
148 // Memorial Day (last Monday in May)
149 || isMemorialDay(d, m, y, w)
150 // Juneteenth (Monday if Sunday or Friday if Saturday)
151 || isJuneteenth(d, m, y, w)
152 // Independence Day (Monday if Sunday or Friday if Saturday)
153 || ((d == 4 || (d == 5 && w == Monday) ||
154 (d == 3 && w == Friday)) && m == July)
155 // Labor Day (first Monday in September)
156 || isLaborDay(d, m, y, w)
157 // Columbus Day (second Monday in October)
158 || isColumbusDay(d, m, y, w)
159 // Veteran's Day (Monday if Sunday or Friday if Saturday)
160 || isVeteransDay(d, m, y, w)
161 // Thanksgiving Day (fourth Thursday in November)
162 || ((d >= 22 && d <= 28) && w == Thursday && m == November)
163 // Christmas (Monday if Sunday or Friday if Saturday)
164 || ((d == 25 || (d == 26 && w == Monday) ||
165 (d == 24 && w == Friday)) && m == December))
166 return false; // NOLINT(readability-simplify-boolean-expr)
167 return true;
168 }
169
171 // Since 2015 Independence Day only impacts Libor if it falls
172 // on a weekday
173 Weekday w = date.weekday();
174 Day d = date.dayOfMonth();
175 Month m = date.month();
176 Year y = date.year();
177 if (((d == 5 && w == Monday) ||
178 (d == 3 && w == Friday)) && m == July && y >= 2015)
179 return true;
181 }
182
184 Weekday w = date.weekday();
185 Day d = date.dayOfMonth(), dd = date.dayOfYear();
186 Month m = date.month();
187 Year y = date.year();
188 Day em = easterMonday(y);
189 if (isWeekend(w)
190 // New Year's Day (possibly moved to Monday if on Sunday)
191 || ((d == 1 || (d == 2 && w == Monday)) && m == January)
192 // Washington's birthday (third Monday in February)
193 || isWashingtonBirthday(d, m, y, w)
194 // Good Friday
195 || (dd == em-3)
196 // Memorial Day (last Monday in May)
197 || isMemorialDay(d, m, y, w)
198 // Juneteenth (Monday if Sunday or Friday if Saturday)
199 || isJuneteenth(d, m, y, w)
200 // Independence Day (Monday if Sunday or Friday if Saturday)
201 || ((d == 4 || (d == 5 && w == Monday) ||
202 (d == 3 && w == Friday)) && m == July)
203 // Labor Day (first Monday in September)
204 || isLaborDay(d, m, y, w)
205 // Thanksgiving Day (fourth Thursday in November)
206 || ((d >= 22 && d <= 28) && w == Thursday && m == November)
207 // Christmas (Monday if Sunday or Friday if Saturday)
208 || ((d == 25 || (d == 26 && w == Monday) ||
209 (d == 24 && w == Friday)) && m == December)
210 ) return false;
211
212 if (y >= 1998 && (d >= 15 && d <= 21) && w == Monday && m == January)
213 // Martin Luther King's birthday (third Monday in January)
214 return false;
215
216 if ((y <= 1968 || (y <= 1980 && y % 4 == 0)) && m == November
217 && d <= 7 && w == Tuesday)
218 // Presidential election days
219 return false;
220
221 // Special closings
222 if (// President Bush's Funeral
223 (y == 2018 && m == December && d == 5)
224 // Hurricane Sandy
225 || (y == 2012 && m == October && (d == 29 || d == 30))
226 // President Ford's funeral
227 || (y == 2007 && m == January && d == 2)
228 // President Reagan's funeral
229 || (y == 2004 && m == June && d == 11)
230 // September 11-14, 2001
231 || (y == 2001 && m == September && (11 <= d && d <= 14))
232 // President Nixon's funeral
233 || (y == 1994 && m == April && d == 27)
234 // Hurricane Gloria
235 || (y == 1985 && m == September && d == 27)
236 // 1977 Blackout
237 || (y == 1977 && m == July && d == 14)
238 // Funeral of former President Lyndon B. Johnson.
239 || (y == 1973 && m == January && d == 25)
240 // Funeral of former President Harry S. Truman
241 || (y == 1972 && m == December && d == 28)
242 // National Day of Participation for the lunar exploration.
243 || (y == 1969 && m == July && d == 21)
244 // Funeral of former President Eisenhower.
245 || (y == 1969 && m == March && d == 31)
246 // Closed all day - heavy snow.
247 || (y == 1969 && m == February && d == 10)
248 // Day after Independence Day.
249 || (y == 1968 && m == July && d == 5)
250 // June 12-Dec. 31, 1968
251 // Four day week (closed on Wednesdays) - Paperwork Crisis
252 || (y == 1968 && dd >= 163 && w == Wednesday)
253 // Day of mourning for Martin Luther King Jr.
254 || (y == 1968 && m == April && d == 9)
255 // Funeral of President Kennedy
256 || (y == 1963 && m == November && d == 25)
257 // Day before Decoration Day
258 || (y == 1961 && m == May && d == 29)
259 // Day after Christmas
260 || (y == 1958 && m == December && d == 26)
261 // Christmas Eve
262 || ((y == 1954 || y == 1956 || y == 1965)
263 && m == December && d == 24)
264 ) return false;
265
266 return true;
267 }
268
269
271 Weekday w = date.weekday();
272 Day d = date.dayOfMonth(), dd = date.dayOfYear();
273 Month m = date.month();
274 Year y = date.year();
275 Day em = easterMonday(y);
276 if (isWeekend(w)
277 // New Year's Day (possibly moved to Monday if on Sunday)
278 || ((d == 1 || (d == 2 && w == Monday)) && m == January)
279 // Martin Luther King's birthday (third Monday in January)
280 || ((d >= 15 && d <= 21) && w == Monday && m == January
281 && y >= 1983)
282 // Washington's birthday (third Monday in February)
283 || isWashingtonBirthday(d, m, y, w)
284 // Good Friday (2015, 2021, 2023 are half day due to NFP/SIFMA;
285 // see <https://www.sifma.org/resources/general/holiday-schedule/>)
286 || (dd == em-3 && y != 2015 && y != 2021 && y != 2023)
287 // Memorial Day (last Monday in May)
288 || isMemorialDay(d, m, y, w)
289 // Juneteenth (Monday if Sunday or Friday if Saturday)
290 || isJuneteenth(d, m, y, w)
291 // Independence Day (Monday if Sunday or Friday if Saturday)
292 || ((d == 4 || (d == 5 && w == Monday) ||
293 (d == 3 && w == Friday)) && m == July)
294 // Labor Day (first Monday in September)
295 || isLaborDay(d, m, y, w)
296 // Columbus Day (second Monday in October)
297 || isColumbusDay(d, m, y, w)
298 // Veteran's Day (Monday if Sunday)
299 || isVeteransDayNoSaturday(d, m, y, w)
300 // Thanksgiving Day (fourth Thursday in November)
301 || ((d >= 22 && d <= 28) && w == Thursday && m == November)
302 // Christmas (Monday if Sunday or Friday if Saturday)
303 || ((d == 25 || (d == 26 && w == Monday) ||
304 (d == 24 && w == Friday)) && m == December))
305 return false;
306
307 // Special closings
308 if (// President Bush's Funeral
309 (y == 2018 && m == December && d == 5)
310 // Hurricane Sandy
311 || (y == 2012 && m == October && (d == 30))
312 // President Reagan's funeral
313 || (y == 2004 && m == June && d == 11)
314 ) return false;
315
316 return true;
317 }
318
319
321 // so far (that is, up to 2023 at the time of this change) SOFR never fixed
322 // on Good Friday. We're extrapolating that pattern. This might change if
323 // a fixing on Good Friday occurs in future years.
324 const Day dY = date.dayOfYear();
325 const Year y = date.year();
326
327 // Good Friday
328 if (dY == (easterMonday(y) - 3))
329 return false;
330
332 }
333
334
336 Weekday w = date.weekday();
337 Day d = date.dayOfMonth();
338 Month m = date.month();
339 Year y = date.year();
340 if (isWeekend(w)
341 // New Year's Day (possibly moved to Monday if on Sunday)
342 || ((d == 1 || (d == 2 && w == Monday)) && m == January)
343 // Memorial Day (last Monday in May)
344 || isMemorialDay(d, m, y, w)
345 // Independence Day (Monday if Sunday)
346 || ((d == 4 || (d == 5 && w == Monday)) && m == July)
347 // Labor Day (first Monday in September)
348 || isLaborDay(d, m, y, w)
349 // Thanksgiving Day (fourth Thursday in November)
350 || ((d >= 22 && d <= 28) && w == Thursday && m == November)
351 // Christmas (Monday if Sunday)
352 || ((d == 25 || (d == 26 && w == Monday)) && m == December))
353 return false; // NOLINT(readability-simplify-boolean-expr)
354 return true;
355 }
356
357
359 // see https://www.frbservices.org/about/holiday-schedules for details
360 Weekday w = date.weekday();
361 Day d = date.dayOfMonth();
362 Month m = date.month();
363 Year y = date.year();
364 if (isWeekend(w)
365 // New Year's Day (possibly moved to Monday if on Sunday)
366 || ((d == 1 || (d == 2 && w == Monday)) && m == January)
367 // Martin Luther King's birthday (third Monday in January)
368 || ((d >= 15 && d <= 21) && w == Monday && m == January
369 && y >= 1983)
370 // Washington's birthday (third Monday in February)
371 || isWashingtonBirthday(d, m, y, w)
372 // Memorial Day (last Monday in May)
373 || isMemorialDay(d, m, y, w)
374 // Juneteenth (Monday if Sunday)
375 || isJuneteenth(d, m, y, w, false)
376 // Independence Day (Monday if Sunday)
377 || ((d == 4 || (d == 5 && w == Monday)) && m == July)
378 // Labor Day (first Monday in September)
379 || isLaborDay(d, m, y, w)
380 // Columbus Day (second Monday in October)
381 || isColumbusDay(d, m, y, w)
382 // Veteran's Day (Monday if Sunday)
383 || isVeteransDayNoSaturday(d, m, y, w)
384 // Thanksgiving Day (fourth Thursday in November)
385 || ((d >= 22 && d <= 28) && w == Thursday && m == November)
386 // Christmas (Monday if Sunday)
387 || ((d == 25 || (d == 26 && w == Monday)) && m == December))
388 return false; // NOLINT(readability-simplify-boolean-expr)
389 return true;
390 }
391
392}
bool isWeekend(Weekday) const override
Definition: calendar.cpp:195
bool isWeekend(Weekday w) const
Definition: calendar.hpp:255
ext::shared_ptr< Impl > impl_
Definition: calendar.hpp:72
Concrete date class.
Definition: date.hpp:125
Month month() const
Definition: date.cpp:82
Year year() const
Definition: date.cpp:93
Day dayOfMonth() const
Definition: date.hpp:400
Weekday weekday() const
Definition: date.hpp:395
Day dayOfYear() const
One-based (Jan 1st = 1)
Definition: date.hpp:404
bool isBusinessDay(const Date &) const override
bool isBusinessDay(const Date &) const override
bool isBusinessDay(const Date &) const override
bool isBusinessDay(const Date &) const override
bool isBusinessDay(const Date &) const override
bool isBusinessDay(const Date &) const override
bool isBusinessDay(const Date &) const override
@ FederalReserve
Federal Reserve Bankwire System.
@ NYSE
New York stock exchange calendar.
@ LiborImpact
Libor impact calendar.
@ GovernmentBond
government-bond calendar
@ NERC
off-peak days for NERC
@ Settlement
generic settlement calendar
@ SOFR
SOFR fixing calendar.
UnitedStates(Market market)
Classes and functions for error handling.
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Date d
Integer Year
Year number.
Definition: date.hpp:87
Integer Day
Day number.
Definition: date.hpp:53
Month
Month names.
Definition: date.hpp:57
@ December
Definition: date.hpp:68
@ January
Definition: date.hpp:57
@ July
Definition: date.hpp:63
@ May
Definition: date.hpp:61
@ March
Definition: date.hpp:59
@ February
Definition: date.hpp:58
@ April
Definition: date.hpp:60
@ November
Definition: date.hpp:67
@ October
Definition: date.hpp:66
@ June
Definition: date.hpp:62
@ September
Definition: date.hpp:65
@ Wednesday
Definition: weekday.hpp:44
@ Monday
Definition: weekday.hpp:42
@ Tuesday
Definition: weekday.hpp:43
@ Thursday
Definition: weekday.hpp:45
@ Friday
Definition: weekday.hpp:46
Definition: any.hpp:35
US calendars.