QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
canada.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/time/calendars/canada.hpp>
22#include <ql/errors.hpp>
23
24namespace QuantLib {
25
27 // all calendar instances share the same implementation instance
28 static ext::shared_ptr<Calendar::Impl> settlementImpl(
30 static ext::shared_ptr<Calendar::Impl> tsxImpl(new Canada::TsxImpl);
31 switch (market) {
32 case Settlement:
33 impl_ = settlementImpl;
34 break;
35 case TSX:
36 impl_ = tsxImpl;
37 break;
38 default:
39 QL_FAIL("unknown market");
40 }
41 }
42
44 Weekday w = date.weekday();
45 Day d = date.dayOfMonth(), dd = date.dayOfYear();
46 Month m = date.month();
47 Year y = date.year();
48 Day em = easterMonday(y);
49 if (isWeekend(w)
50 // New Year's Day (possibly moved to Monday)
51 || ((d == 1 || ((d == 2 || d == 3) && w == Monday)) && m == January)
52 // Family Day (third Monday in February, since 2008)
53 || ((d >= 15 && d <= 21) && w == Monday && m == February
54 && y >= 2008)
55 // Good Friday
56 || (dd == em-3)
57 // The Monday on or preceding 24 May (Victoria Day)
58 || (d > 17 && d <= 24 && w == Monday && m == May)
59 // July 1st, possibly moved to Monday (Canada Day)
60 || ((d == 1 || ((d == 2 || d == 3) && w == Monday)) && m==July)
61 // first Monday of August (Provincial Holiday)
62 || (d <= 7 && w == Monday && m == August)
63 // first Monday of September (Labor Day)
64 || (d <= 7 && w == Monday && m == September)
65 // September 30th, possibly moved to Monday
66 // (National Day for Truth and Reconciliation, since 2021)
67 || (((d == 30 && m == September) || (d <= 2 && m == October && w == Monday)) && y >= 2021)
68 // second Monday of October (Thanksgiving Day)
69 || (d > 7 && d <= 14 && w == Monday && m == October)
70 // November 11th (possibly moved to Monday)
71 || ((d == 11 || ((d == 12 || d == 13) && w == Monday))
72 && m == November)
73 // Christmas (possibly moved to Monday or Tuesday)
74 || ((d == 25 || (d == 27 && (w == Monday || w == Tuesday)))
75 && m == December)
76 // Boxing Day (possibly moved to Monday or Tuesday)
77 || ((d == 26 || (d == 28 && (w == Monday || w == Tuesday)))
78 && m == December)
79 )
80 return false; // NOLINT(readability-simplify-boolean-expr)
81 return true;
82 }
83
84 bool Canada::TsxImpl::isBusinessDay(const Date& date) const {
85 Weekday w = date.weekday();
86 Day d = date.dayOfMonth(), dd = date.dayOfYear();
87 Month m = date.month();
88 Year y = date.year();
89 Day em = easterMonday(y);
90 if (isWeekend(w)
91 // New Year's Day (possibly moved to Monday)
92 || ((d == 1 || ((d == 2 || d == 3) && w == Monday)) && m == January)
93 // Family Day (third Monday in February, since 2008)
94 || ((d >= 15 && d <= 21) && w == Monday && m == February
95 && y >= 2008)
96 // Good Friday
97 || (dd == em-3)
98 // The Monday on or preceding 24 May (Victoria Day)
99 || (d > 17 && d <= 24 && w == Monday && m == May)
100 // July 1st, possibly moved to Monday (Canada Day)
101 || ((d == 1 || ((d == 2 || d == 3) && w == Monday)) && m==July)
102 // first Monday of August (Provincial Holiday)
103 || (d <= 7 && w == Monday && m == August)
104 // first Monday of September (Labor Day)
105 || (d <= 7 && w == Monday && m == September)
106 // second Monday of October (Thanksgiving Day)
107 || (d > 7 && d <= 14 && w == Monday && m == October)
108 // Christmas (possibly moved to Monday or Tuesday)
109 || ((d == 25 || (d == 27 && (w == Monday || w == Tuesday)))
110 && m == December)
111 // Boxing Day (possibly moved to Monday or Tuesday)
112 || ((d == 26 || (d == 28 && (w == Monday || w == Tuesday)))
113 && m == December)
114 )
115 return false; // NOLINT(readability-simplify-boolean-expr)
116 return true;
117 }
118
119}
static Day easterMonday(Year)
expressed relative to first day of year
Definition: calendar.cpp:193
bool isWeekend(Weekday) const override
Definition: calendar.cpp:189
bool isWeekend(Weekday w) const
Definition: calendar.hpp:255
ext::shared_ptr< Impl > impl_
Definition: calendar.hpp:72
bool isBusinessDay(const Date &) const override
Definition: canada.cpp:43
bool isBusinessDay(const Date &) const override
Definition: canada.cpp:84
Canada(Market market=Settlement)
Definition: canada.cpp:26
@ TSX
Toronto stock exchange calendar.
Definition: canada.hpp:87
@ Settlement
generic settlement calendar
Definition: canada.hpp:86
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
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
@ August
Definition: date.hpp:64
@ January
Definition: date.hpp:57
@ July
Definition: date.hpp:63
@ May
Definition: date.hpp:61
@ February
Definition: date.hpp:58
@ November
Definition: date.hpp:67
@ October
Definition: date.hpp:66
@ September
Definition: date.hpp:65
@ Monday
Definition: weekday.hpp:42
@ Tuesday
Definition: weekday.hpp:43
Definition: any.hpp:35