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