QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
asx.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) 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 <http://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>
27#include <boost/algorithm/string/case_conv.hpp>
28#include <boost/utility/string_view.hpp>
29#include <string>
30#include <cctype>
31
32namespace QuantLib {
33
34 namespace {
35 const boost::string_view All_MONTH_CODES = "FGHJKMNQUVXZ";
36 }
37
38 bool ASX::isASXdate(const Date& date, bool mainCycle) {
39 if (date.weekday()!=Friday)
40 return false;
41
42 Day d = date.dayOfMonth();
43 if (d<8 || d>14)
44 return false;
45
46 if (!mainCycle) return true;
47
48 switch (date.month()) {
49 case March:
50 case June:
51 case September:
52 case December:
53 return true;
54 default:
55 return false;
56 }
57 }
58
59 bool ASX::isASXcode(const std::string& in, bool mainCycle) {
60 if (in.length() != 2)
61 return false;
62
63 // 2nd character of code needs to be digit
64 if (std::isdigit(static_cast<unsigned char>(in[1])) == 0)
65 return false;
66
67 // 1st character needs to represent the correct month
68 const boost::string_view validMonthCodes = mainCycle ? "HMUZ" : All_MONTH_CODES;
69 return validMonthCodes.find(std::toupper(in[0])) != boost::string_view::npos;
70 }
71
72 std::string ASX::code(const Date& date) {
74 date << " is not an ASX date");
75
76 // month() is 1-based!
77 const char monthCode = All_MONTH_CODES[date.month()-1];
78 const char yearDigit = static_cast<char>(static_cast<int>('0') + (date.year() % 10));
79 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 return code;
88 }
89
90 Date ASX::date(const std::string& asxCode,
91 const Date& refDate) {
92 QL_REQUIRE(isASXcode(asxCode, false),
93 asxCode << " is not a valid ASX code");
94
95 const Date referenceDate = (refDate != Date() ?
96 refDate :
97 Date(Settings::instance().evaluationDate()));
98
99 const char ms = std::toupper(asxCode.front());
100 const std::size_t idxZeroBased = All_MONTH_CODES.find(ms);
101 QL_ASSERT(idxZeroBased != All_MONTH_CODES.npos, "invalid ASX month letter. code: " + asxCode);
102
103 // QuantLib::Month is 1-based!
104 const auto m = static_cast<QuantLib::Month>(idxZeroBased + 1);
105
106 // convert 2nd char to year digit
107 Year y = static_cast<int>(asxCode[1]) - static_cast<int>('0');
108 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 if (y==0 && referenceDate.year()<=1909) y+=10;
113 const Year referenceYear = (referenceDate.year() % 10);
114 y += referenceDate.year() - referenceYear;
115 Date result = ASX::nextDate(Date(1, m, y), false);
116 return (result >= referenceDate) ? result : ASX::nextDate(Date(1, m, y+10), false);
117 }
118
119 Date ASX::nextDate(const Date& date, bool mainCycle) {
120 Date refDate = (date == Date() ?
121 Date(Settings::instance().evaluationDate()) :
122 date);
123 Year y = refDate.year();
124 QuantLib::Month m = refDate.month();
125
126 Size offset = mainCycle ? 3 : 1;
127 Size skipMonths = offset-(m%offset);
128 if (skipMonths != offset || refDate.dayOfMonth() > 14) {
129 skipMonths += Size(m);
130 if (skipMonths<=12) {
131 m = QuantLib::Month(skipMonths);
132 } else {
133 m = QuantLib::Month(skipMonths-12);
134 y += 1;
135 }
136 }
137
138 Date result = Date::nthWeekday(2, Friday, m, y);
139 if (result<=refDate)
140 result = nextDate(Date(15, m, y), mainCycle);
141 return result;
142 }
143
144 Date ASX::nextDate(const std::string& ASXcode,
145 bool mainCycle,
146 const Date& referenceDate) {
147 Date asxDate = date(ASXcode, referenceDate);
148 return nextDate(asxDate+1, mainCycle);
149 }
150
151 std::string ASX::nextCode(const Date& d,
152 bool mainCycle) {
153 Date date = nextDate(d, mainCycle);
154 return code(date);
155 }
156
157 std::string ASX::nextCode(const std::string& asxCode,
158 bool mainCycle,
159 const Date& referenceDate) {
160 Date date = nextDate(asxCode, mainCycle, referenceDate);
161 return code(date);
162 }
163
164}
ASX-related date functions.
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
static Date nthWeekday(Size n, Weekday w, Month m, Year y)
n-th given weekday in the given month and year
Definition: date.cpp:802
static Settings & instance()
access to the unique instance
Definition: singleton.hpp:104
Classes used to parse data for input.
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_ASSERT(condition, message)
throw an error if the given condition is not verified
Definition: errors.hpp:104
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
@ March
Definition: date.hpp:59
@ June
Definition: date.hpp:62
@ September
Definition: date.hpp:65
@ Friday
Definition: weekday.hpp:46
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
global repository for run-time library settings
static Date date(const std::string &asxCode, const Date &referenceDate=Date())
Definition: asx.cpp:90
static std::string nextCode(const Date &d=Date(), bool mainCycle=true)
next ASX code following the given date
Definition: asx.cpp:151
static Date nextDate(const Date &d=Date(), bool mainCycle=true)
next ASX date following the given date
Definition: asx.cpp:119
static bool isASXcode(const std::string &in, bool mainCycle=true)
returns whether or not the given string is an ASX code
Definition: asx.cpp:59
static std::string code(const Date &asxDate)
Definition: asx.cpp:72
static bool isASXdate(const Date &d, bool mainCycle=true)
returns whether or not the given date is an ASX date
Definition: asx.cpp:38