/src/quantlib/ql/time/asx.cpp
Line | Count | Source |
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) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl |
6 | | Copyright (C) 2004, 2005, 2006 Ferdinando Ametrano |
7 | | Copyright (C) 2006 Katiuscia Manzoni |
8 | | Copyright (C) 2015 Maddalena Zanzi |
9 | | |
10 | | This file is part of QuantLib, a free-software/open-source library |
11 | | for financial quantitative analysts and developers - http://quantlib.org/ |
12 | | |
13 | | QuantLib is free software: you can redistribute it and/or modify it |
14 | | under the terms of the QuantLib license. You should have received a |
15 | | copy of the license along with this program; if not, please email |
16 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
17 | | <https://www.quantlib.org/license.shtml>. |
18 | | |
19 | | This program is distributed in the hope that it will be useful, but WITHOUT |
20 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
21 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
22 | | */ |
23 | | |
24 | | #include <ql/time/asx.hpp> |
25 | | #include <ql/settings.hpp> |
26 | | #include <ql/utilities/dataparsers.hpp> |
27 | | #include <boost/algorithm/string/case_conv.hpp> |
28 | | #include <boost/utility/string_view.hpp> |
29 | | #include <string> |
30 | | #include <cctype> |
31 | | |
32 | | namespace QuantLib { |
33 | | |
34 | | namespace { |
35 | | const boost::string_view All_MONTH_CODES = "FGHJKMNQUVXZ"; |
36 | | } |
37 | | |
38 | 5.56k | bool ASX::isASXdate(const Date& date, bool mainCycle) { |
39 | 5.56k | if (date.weekday()!=Friday) |
40 | 4.69k | return false; |
41 | | |
42 | 868 | Day d = date.dayOfMonth(); |
43 | 868 | if (d<8 || d>14) |
44 | 698 | return false; |
45 | | |
46 | 170 | if (!mainCycle) return true; |
47 | | |
48 | 170 | switch (date.month()) { |
49 | 8 | case March: |
50 | 38 | case June: |
51 | 62 | case September: |
52 | 70 | case December: |
53 | 70 | return true; |
54 | 100 | default: |
55 | 100 | return false; |
56 | 170 | } |
57 | 170 | } |
58 | | |
59 | 0 | bool ASX::isASXcode(const std::string& in, bool mainCycle) { |
60 | 0 | if (in.length() != 2) |
61 | 0 | return false; |
62 | | |
63 | | // 2nd character of code needs to be digit |
64 | 0 | if (std::isdigit(static_cast<unsigned char>(in[1])) == 0) |
65 | 0 | return false; |
66 | | |
67 | | // 1st character needs to represent the correct month |
68 | 0 | const boost::string_view validMonthCodes = mainCycle ? "HMUZ" : All_MONTH_CODES; |
69 | 0 | return validMonthCodes.find(std::toupper(in[0])) != boost::string_view::npos; |
70 | 0 | } |
71 | | |
72 | 0 | std::string ASX::code(const Date& date) { |
73 | 0 | QL_REQUIRE(isASXdate(date, false), |
74 | 0 | date << " is not an ASX date"); |
75 | | |
76 | | // month() is 1-based! |
77 | 0 | const char monthCode = All_MONTH_CODES[date.month()-1]; |
78 | 0 | const char yearDigit = static_cast<char>(static_cast<int>('0') + (date.year() % 10)); |
79 | 0 | std::string code{monthCode, yearDigit}; |
80 | |
|
81 | | #ifdef QL_EXTRA_SAFETY_CHECKS |
82 | | QL_ENSURE(isASXcode(code, false), |
83 | | "the result " << code << |
84 | | " is an invalid ASX code"); |
85 | | #endif |
86 | |
|
87 | 0 | return code; |
88 | 0 | } |
89 | | |
90 | | Date ASX::date(const std::string& asxCode, |
91 | 0 | const Date& refDate) { |
92 | 0 | QL_REQUIRE(isASXcode(asxCode, false), |
93 | 0 | asxCode << " is not a valid ASX code"); |
94 | | |
95 | 0 | const Date referenceDate = (refDate != Date() ? |
96 | 0 | refDate : |
97 | 0 | Date(Settings::instance().evaluationDate())); |
98 | |
|
99 | 0 | const char ms = std::toupper(asxCode.front()); |
100 | 0 | const std::size_t idxZeroBased = All_MONTH_CODES.find(ms); |
101 | 0 | QL_ASSERT(idxZeroBased != All_MONTH_CODES.npos, "invalid ASX month letter. code: " + asxCode); |
102 | | |
103 | | // QuantLib::Month is 1-based! |
104 | 0 | const auto m = static_cast<QuantLib::Month>(idxZeroBased + 1); |
105 | | |
106 | | // convert 2nd char to year digit |
107 | 0 | Year y = static_cast<int>(asxCode[1]) - static_cast<int>('0'); |
108 | 0 | QL_ASSERT((y>=0) && (y <= 9), "invalid ASX year digit. code: " + asxCode); |
109 | | |
110 | | /* year<1900 are not valid QuantLib years: to avoid a run-time |
111 | | exception few lines below we need to add 10 years right away */ |
112 | 0 | if (y==0 && referenceDate.year()<=1909) y+=10; |
113 | 0 | const Year referenceYear = (referenceDate.year() % 10); |
114 | 0 | y += referenceDate.year() - referenceYear; |
115 | 0 | Date result = ASX::nextDate(Date(1, m, y), false); |
116 | 0 | return (result >= referenceDate) ? result : ASX::nextDate(Date(1, m, y+10), false); |
117 | 0 | } |
118 | | |
119 | 5.74k | Date ASX::nextDate(const Date& date, bool mainCycle) { |
120 | 5.74k | Date refDate = (date == Date() ? |
121 | 0 | Date(Settings::instance().evaluationDate()) : |
122 | 5.74k | date); |
123 | 5.74k | Year y = refDate.year(); |
124 | 5.74k | QuantLib::Month m = refDate.month(); |
125 | | |
126 | 5.74k | Size offset = mainCycle ? 3 : 1; |
127 | 5.74k | Size skipMonths = offset-(m%offset); |
128 | 5.74k | if (skipMonths != offset || refDate.dayOfMonth() > 14) { |
129 | 4.84k | skipMonths += Size(m); |
130 | 4.84k | if (skipMonths<=12) { |
131 | 4.67k | m = QuantLib::Month(skipMonths); |
132 | 4.67k | } else { |
133 | 170 | m = QuantLib::Month(skipMonths-12); |
134 | 170 | y += 1; |
135 | 170 | } |
136 | 4.84k | } |
137 | | |
138 | 5.74k | Date result = Date::nthWeekday(2, Friday, m, y); |
139 | 5.74k | if (result<=refDate) |
140 | 180 | result = nextDate(Date(15, m, y), mainCycle); |
141 | 5.74k | return result; |
142 | 5.74k | } |
143 | | |
144 | | Date ASX::nextDate(const std::string& ASXcode, |
145 | | bool mainCycle, |
146 | 0 | const Date& referenceDate) { |
147 | 0 | Date asxDate = date(ASXcode, referenceDate); |
148 | 0 | return nextDate(asxDate+1, mainCycle); |
149 | 0 | } |
150 | | |
151 | | std::string ASX::nextCode(const Date& d, |
152 | 0 | bool mainCycle) { |
153 | 0 | Date date = nextDate(d, mainCycle); |
154 | 0 | return code(date); |
155 | 0 | } |
156 | | |
157 | | std::string ASX::nextCode(const std::string& asxCode, |
158 | | bool mainCycle, |
159 | 0 | const Date& referenceDate) { |
160 | 0 | Date date = nextDate(asxCode, mainCycle, referenceDate); |
161 | 0 | return code(date); |
162 | 0 | } |
163 | | |
164 | | } |