QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
business252.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) 2006 Piter Dias
5 Copyright (C) 2011 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
22#include <map>
23
24namespace QuantLib {
25
26 namespace {
27
28 typedef std::map<Year, std::map<Month, Date::serial_type> > Cache;
29 typedef std::map<Year, Date::serial_type> OuterCache;
30
31 std::map<std::string, Cache> monthlyFigures_;
32 std::map<std::string, OuterCache> yearlyFigures_;
33
34 bool sameYear(const Date& d1, const Date& d2) {
35 return d1.year() == d2.year();
36 }
37
38 bool sameMonth(const Date& d1, const Date& d2) {
39 return d1.year() == d2.year() && d1.month() == d2.month();
40 }
41
42 Date::serial_type businessDays(Cache& cache,
43 const Calendar& calendar,
44 Month month, Year year) {
45 if (cache[year][month] == 0) {
46 // calculate and store.
47 Date d1 = Date(1,month,year);
48 Date d2 = d1 + 1*Months;
49 cache[year][month] = calendar.businessDaysBetween(d1, d2);
50 }
51 return cache[year][month];
52 }
53
54 Date::serial_type businessDays(OuterCache& outerCache,
55 Cache& cache,
56 const Calendar& calendar,
57 Year year) {
58 if (outerCache[year] == 0) {
59 // calculate and store.
60 Date::serial_type total = 0;
61 for (Integer i=1; i<=12; ++i) {
62 total += businessDays(cache,calendar,
63 Month(i), year);
64 }
65 outerCache[year] = total;
66 }
67 return outerCache[year];
68 }
69
70 }
71
72 std::string Business252::Impl::name() const {
73 std::ostringstream out;
74 out << "Business/252(" << calendar_.name() << ")";
75 return out.str();
76 }
77
79 const Date& d2) const {
80 if (sameMonth(d1,d2) || d1 >= d2) {
81 // we treat the case of d1 > d2 here, since we'd need a
82 // second cache to get it right (our cached figures are
83 // for first included, last excluded and might have to be
84 // changed going the other way.)
85 return calendar_.businessDaysBetween(d1, d2);
86 } else if (sameYear(d1,d2)) {
87 Cache& cache = monthlyFigures_[calendar_.name()];
88 Date::serial_type total = 0;
89 Date d;
90 // first, we get to the beginning of next month.
91 d = Date(1,d1.month(),d1.year()) + 1*Months;
92 total += calendar_.businessDaysBetween(d1, d);
93 // then, we add any whole months (whose figures might be
94 // cached already) in the middle of our period.
95 while (!sameMonth(d,d2)) {
96 total += businessDays(cache, calendar_,
97 d.month(), d.year());
98 d += 1*Months;
99 }
100 // finally, we get to the end of the period.
101 total += calendar_.businessDaysBetween(d, d2);
102 return total;
103 } else {
104 Cache& cache = monthlyFigures_[calendar_.name()];
105 OuterCache& outerCache = yearlyFigures_[calendar_.name()];
106 Date::serial_type total = 0;
107 Date d;
108 // first, we get to the beginning of next year.
109 // The first bit gets us to the end of this month...
110 d = Date(1,d1.month(),d1.year()) + 1*Months;
111 total += calendar_.businessDaysBetween(d1, d);
112 // ...then we add any remaining months, possibly cached
113 for (Integer m = Integer(d1.month())+1; m <= 12; ++m) {
114 total += businessDays(cache, calendar_,
115 Month(m), d.year());
116 }
117 // then, we add any whole year in the middle of our period.
118 d = Date(1,January,d1.year()+1);
119 while (!sameYear(d,d2)) {
120 total += businessDays(outerCache, cache,
121 calendar_, d.year());
122 d += 1*Years;
123 }
124 // finally, we get to the end of the period.
125 // First, we add whole months...
126 for (Integer m = 1; m<Integer(d2.month()); ++m) {
127 total += businessDays(cache, calendar_,
128 Month(m), d2.year());
129 }
130 // ...then the last bit.
131 d = Date(1,d2.month(),d2.year());
132 total += calendar_.businessDaysBetween(d, d2);
133 return total;
134 }
135 }
136
138 const Date& d2,
139 const Date&,
140 const Date&) const {
141 return dayCount(d1, d2)/252.0;
142 }
143
144}
business/252 day counter
Date::serial_type dayCount(const Date &d1, const Date &d2) const override
to be overloaded by more complex day counters
Definition: business252.cpp:78
Time yearFraction(const Date &d1, const Date &d2, const Date &, const Date &) const override
std::string name() const override
Definition: business252.cpp:72
std::string name() const
Returns the name of the calendar.
Definition: calendar.hpp:206
Concrete date class.
Definition: date.hpp:125
Month month() const
Definition: date.cpp:82
Year year() const
Definition: date.cpp:93
std::int_fast32_t serial_type
serial number type
Definition: date.hpp:128
Date::serial_type dayCount(const Date &, const Date &) const
Returns the number of days between two dates.
Definition: daycounter.hpp:122
Date d
Integer Year
Year number.
Definition: date.hpp:87
Month
Month names.
Definition: date.hpp:57
@ January
Definition: date.hpp:57
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_INTEGER Integer
integer number
Definition: types.hpp:35
Definition: any.hpp:35