File: russia.cpp

package info (click to toggle)
quantlib 1.29-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 46,032 kB
  • sloc: cpp: 389,443; makefile: 6,658; sh: 4,511; lisp: 86
file content (262 lines) | stat: -rw-r--r-- 9,517 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2010 StatPro Italia srl
 Copyright (C) 2015 Dmitri Nesteruk

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

#include <ql/time/calendars/russia.hpp>
#include <ql/errors.hpp>

namespace QuantLib {

    Russia::Russia(Russia::Market market) {
        // all calendar instances share the same implementation instance
        static ext::shared_ptr<Calendar::Impl> settlementImpl(
                                                  new Russia::SettlementImpl);
        static ext::shared_ptr<Calendar::Impl> exchangeImpl(
                                                    new Russia::ExchangeImpl);

        switch (market) {
          case Settlement:
            impl_ = settlementImpl;
            break;
          case MOEX:
            impl_ = exchangeImpl;
            break;
          default:
            QL_FAIL("unknown market");
        }
    }

    namespace {

        bool isExtraHolidaySettlementImpl(Day d, Month month, Year year) {
            switch (year) {
              case 2017:
                switch (month) {
                  case February: return d == 24;
                  case May:      return d == 8;
                  case November: return d == 6;
                  default:       return false;
                }
              case 2018:
                switch (month) {
                  case March:     return d == 9;
                  case April:     return d == 30;
                  case May:       return d == 2;
                  case June:      return d == 11;
                  case December:  return d == 31;
                  default:        return false;
                }
              case 2019:
                switch (month) {
                  case May: return d == 2 || d == 3 || d == 10;
                  default: return false;
                }
              case 2020:
                switch (month) {
                  case March:     return d == 30 || d == 31;
                  case April:     return d == 1 || d == 2 || d == 3;
                  case May:       return d == 4 || d == 5;
                  default:        return false;
                }
              default:
                return false;
            }
        }

    }

    bool Russia::SettlementImpl::isBusinessDay(const Date& date) const {
        Weekday w = date.weekday();
        Day d = date.dayOfMonth();
        Month m = date.month();
        Year y = date.year();
        if (isWeekend(w)
            // New Year's holidays
            || (y <= 2005 && d <= 2 && m == January)
            || (y >= 2005 && d <= 5 && m == January)
            // in 2012, the 6th was also a holiday
            || (y == 2012 && d == 6 && m == January)
            // Christmas (possibly moved to Monday)
            || ((d == 7 || ((d == 8 || d == 9) && w == Monday)) &&
                m == January)
            // Defender of the Fatherland Day (possibly moved to Monday)
            || ((d == 23 || ((d == 24 || d == 25) && w == Monday)) &&
                m == February)
            // International Women's Day (possibly moved to Monday)
            || ((d == 8 || ((d == 9 || d == 10) && w == Monday)) &&
                m == March)
            // Labour Day (possibly moved to Monday)
            || ((d == 1 || ((d == 2 || d == 3) && w == Monday)) &&
                m == May)
            // Victory Day (possibly moved to Monday)
            || ((d == 9 || ((d == 10 || d == 11) && w == Monday)) &&
                m == May)
            // Russia Day (possibly moved to Monday)
            || ((d == 12 || ((d == 13 || d == 14) && w == Monday)) &&
                m == June)
            // Unity Day (possibly moved to Monday)
            || ((d == 4 || ((d == 5 || d == 6) && w == Monday)) &&
                m == November))
            return false; // NOLINT(readability-simplify-boolean-expr)

        if (isExtraHolidaySettlementImpl(d,m,y))
            return false;

        return true;
    }

    namespace {

        bool isWorkingWeekend(Day d, Month month, Year year) {
            switch (year) {
              case 2012:
                switch (month) {
                  case March: return d == 11;
                  case April: return d == 28;
                  case May:   return d == 5 || d == 12;
                  case June:  return d == 9;
                  default:    return false;
                }
              case 2016:
                switch (month)
                {
                case February: return d == 20;
                default: return false;
                }
              case 2018:
                switch (month) {
                  case April: return d == 28;
                  case June: return d == 9;
                  case December: return d == 29;
                  default: return false;
                }
              default:
                return false;
            }
        }

        bool isExtraHolidayExchangeImpl(Day d, Month month, Year year) {
            switch (year) {
              case 2012:
                switch (month) {
                  case January: return d == 2;
                  case March:   return d == 9;
                  case April:   return d == 30;
                  case June:    return d == 11;
                  default:      return false;
                }
              case 2013:
                switch (month) {
                  case January: return d == 1 || d == 2 || d == 3
                                    || d == 4 || d == 7;
                  default:      return false;
                }
              case 2014:
                switch (month) {
                  case January: return d == 1 || d == 2 || d == 3 || d == 7;
                  default:      return false;
                }
              case 2015:
                switch (month) {
                  case January: return d == 1 || d == 2 || d == 7;
                  default:      return false;
                }
              case 2016:
                switch (month)
                {
                case January: return d == 1 || d == 7 || d == 8;
                case May:     return d == 2 || d == 3;
                case June:    return d == 13;
                case December: return d == 30;
                default:      return false;
                }
              case 2017:
                switch (month) {
                  case January: return d == 2;
                  case May:     return d == 8;
                  default:      return false;
                }
              case 2018:
                switch (month) {
                  case January:   return d == 1 || d == 2 || d == 8;
                  case December:  return d == 31;
                  default:        return false;
                }
              case 2019:
                switch (month) {
                case January:   return d == 1 || d == 2 || d == 7;
                case December:  return d == 31;
                default:        return false;
                }
              case 2020:
                switch (month) {
                  case January:   return d == 1 || d == 2 || d == 7;
                  case February:  return d == 24;
                  case June:      return d == 24;
                  case July:      return d == 1;
                  default:        return false;
                }
              default:
                return false;
            }
        }

    }

    bool Russia::ExchangeImpl::isBusinessDay(const Date& date) const {
        Weekday w = date.weekday();
        Day d = date.dayOfMonth();
        Month m = date.month();
        Year y = date.year();

        // the exchange was formally established in 2011, so data are only
        // available from 2012 to present
        if (y < 2012)
            QL_FAIL("MOEX calendar for the year " << y
                    << " does not exist.");

        if (isWorkingWeekend(d,m,y))
            return true;

        // Known holidays
        if (isWeekend(w)
            // Defender of the Fatherland Day
            || (d == 23 && m == February)
            // International Women's Day (possibly moved to Monday)
            || ((d == 8 || ((d == 9 || d == 10) && w == Monday)) && m == March)
            // Labour Day
            || (d == 1 && m == May)
            // Victory Day (possibly moved to Monday)
            || ((d == 9 || ((d == 10 || d == 11) && w == Monday)) && m == May)
            // Russia Day
            || (d == 12 && m == June)
            // Unity Day (possibly moved to Monday)
            || ((d == 4 || ((d == 5 || d == 6) && w == Monday))
                && m == November)
            // New Years Eve
            || (d == 31 && m == December))
            return false;

        if (isExtraHolidayExchangeImpl(d,m,y))
            return false;

        return true;
    }

}