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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2006 Piter Dias
Copyright (C) 2011 StatPro Italia srl
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/daycounters/business252.hpp>
#include <map>
namespace QuantLib {
namespace {
typedef std::map<Year, std::map<Month, Date::serial_type> > Cache;
typedef std::map<Year, Date::serial_type> OuterCache;
std::map<std::string, Cache> monthlyFigures_;
std::map<std::string, OuterCache> yearlyFigures_;
bool sameYear(const Date& d1, const Date& d2) {
return d1.year() == d2.year();
}
bool sameMonth(const Date& d1, const Date& d2) {
return d1.year() == d2.year() && d1.month() == d2.month();
}
Date::serial_type businessDays(Cache& cache,
const Calendar& calendar,
Month month, Year year) {
if (cache[year][month] == 0) {
// calculate and store.
Date d1 = Date(1,month,year);
Date d2 = d1 + 1*Months;
cache[year][month] = calendar.businessDaysBetween(d1, d2);
}
return cache[year][month];
}
Date::serial_type businessDays(OuterCache& outerCache,
Cache& cache,
const Calendar& calendar,
Year year) {
if (outerCache[year] == 0) {
// calculate and store.
Date::serial_type total = 0;
for (Integer i=1; i<=12; ++i) {
total += businessDays(cache,calendar,
Month(i), year);
}
outerCache[year] = total;
}
return outerCache[year];
}
}
std::string Business252::Impl::name() const {
std::ostringstream out;
out << "Business/252(" << calendar_.name() << ")";
return out.str();
}
Date::serial_type Business252::Impl::dayCount(const Date& d1,
const Date& d2) const {
if (sameMonth(d1,d2) || d1 >= d2) {
// we treat the case of d1 > d2 here, since we'd need a
// second cache to get it right (our cached figures are
// for first included, last excluded and might have to be
// changed going the other way.)
return calendar_.businessDaysBetween(d1, d2);
} else if (sameYear(d1,d2)) {
Cache& cache = monthlyFigures_[calendar_.name()];
Date::serial_type total = 0;
Date d;
// first, we get to the beginning of next month.
d = Date(1,d1.month(),d1.year()) + 1*Months;
total += calendar_.businessDaysBetween(d1, d);
// then, we add any whole months (whose figures might be
// cached already) in the middle of our period.
while (!sameMonth(d,d2)) {
total += businessDays(cache, calendar_,
d.month(), d.year());
d += 1*Months;
}
// finally, we get to the end of the period.
total += calendar_.businessDaysBetween(d, d2);
return total;
} else {
Cache& cache = monthlyFigures_[calendar_.name()];
OuterCache& outerCache = yearlyFigures_[calendar_.name()];
Date::serial_type total = 0;
Date d;
// first, we get to the beginning of next year.
// The first bit gets us to the end of this month...
d = Date(1,d1.month(),d1.year()) + 1*Months;
total += calendar_.businessDaysBetween(d1, d);
// ...then we add any remaining months, possibly cached
for (Integer m = Integer(d1.month())+1; m <= 12; ++m) {
total += businessDays(cache, calendar_,
Month(m), d.year());
}
// then, we add any whole year in the middle of our period.
d = Date(1,January,d1.year()+1);
while (!sameYear(d,d2)) {
total += businessDays(outerCache, cache,
calendar_, d.year());
d += 1*Years;
}
// finally, we get to the end of the period.
// First, we add whole months...
for (Integer m = 1; m<Integer(d2.month()); ++m) {
total += businessDays(cache, calendar_,
Month(m), d2.year());
}
// ...then the last bit.
d = Date(1,d2.month(),d2.year());
total += calendar_.businessDaysBetween(d, d2);
return total;
}
}
Time Business252::Impl::yearFraction(const Date& d1,
const Date& d2,
const Date&,
const Date&) const {
return dayCount(d1, d2)/252.0;
}
}
|