QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
indexmanager.hpp
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) 2004, 2005, 2006 StatPro Italia srl
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file indexmanager.hpp
21 \brief global repository for past index fixings
22*/
23
24#ifndef quantlib_index_manager_hpp
25#define quantlib_index_manager_hpp
26
28#include <ql/timeseries.hpp>
31#include <algorithm>
32#include <cctype>
33
34namespace QuantLib {
35
36 //! global repository for past index fixings
37 /*! \note index names are case insensitive */
38 class IndexManager : public Singleton<IndexManager> {
39 friend class Singleton<IndexManager>;
40 friend class Index;
41
42 private:
43 IndexManager() = default;
44
45 public:
46 //! returns all names of the indexes for which fixings were stored
47 std::vector<std::string> histories() const;
48 //! clears all stored fixings
49 void clearHistories();
50
51 // deprecated in order to be moved into the private section
52
53 /*! \deprecated Use Index::hasHistoricalFixing instead.
54 Deprecated in version 1.37.
55 */
56 [[deprecated("Use Index::hasHistoricalFixing instead")]]
57 bool hasHistory(const std::string& name) const;
58
59 /*! \deprecated Use Index::timeSeries instead.
60 Deprecated in version 1.37.
61 */
62 [[deprecated("Use Index::timeSeries instead")]]
63 const TimeSeries<Real>& getHistory(const std::string& name) const;
64
65 /*! \deprecated Use Index::clearFixings instead.
66 Deprecated in version 1.37.
67 */
68 [[deprecated("Use Index::clearFixings instead")]]
69 void clearHistory(const std::string& name);
70
71 /*! \deprecated Use Index::hasHistoricalFixing instead.
72 Deprecated in version 1.37.
73 */
74 [[deprecated("Use Index::hasHistoricalFixing instead")]]
75 bool hasHistoricalFixing(const std::string& name, const Date& fixingDate) const;
76
77 /*! \deprecated Use Index::addFixings instead.
78 Deprecated in version 1.37.
79 */
80 [[deprecated("Use Index::addFixings instead")]]
81 void setHistory(const std::string& name, TimeSeries<Real> history);
82
83 /*! \deprecated Register with the relevant index instead.
84 Deprecated in version 1.37.
85 */
86 [[deprecated("Register with the relevant index instead")]]
87 ext::shared_ptr<Observable> notifier(const std::string& name) const;
88
89 private:
91 bool operator()(const std::string& s1, const std::string& s2) const {
92 return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), [](const auto& c1, const auto& c2) {
93 return std::toupper(static_cast<unsigned char>(c1)) < std::toupper(static_cast<unsigned char>(c2));
94 });
95 }
96 };
97
98 mutable std::map<std::string, TimeSeries<Real>, CaseInsensitiveCompare> data_;
99 mutable std::map<std::string, ext::shared_ptr<Observable>> notifiers_;
100
101 //! add a fixing
102 void addFixing(const std::string& name,
103 const Date& fixingDate,
104 Real fixing,
105 bool forceOverwrite = false);
106 //! add fixings
107 template <class DateIterator, class ValueIterator>
108 void addFixings(const std::string& name,
109 DateIterator dBegin,
110 DateIterator dEnd,
111 ValueIterator vBegin,
112 bool forceOverwrite = false,
113 const std::function<bool(const Date& d)>& isValidFixingDate = {}) {
114 auto& h = data_[name];
115 bool noInvalidFixing = true, noDuplicatedFixing = true;
116 Date invalidDate, duplicatedDate;
117 Real nullValue = Null<Real>();
118 Real invalidValue = Null<Real>();
119 Real duplicatedValue = Null<Real>();
120 while (dBegin != dEnd) {
121 bool validFixing = isValidFixingDate ? isValidFixingDate(*dBegin) : true;
122 Real currentValue = h[*dBegin];
123 bool missingFixing = forceOverwrite || currentValue == nullValue;
124 if (validFixing) {
125 if (missingFixing)
126 h[*(dBegin++)] = *(vBegin++);
127 else if (close(currentValue, *(vBegin))) {
128 ++dBegin;
129 ++vBegin;
130 } else {
131 noDuplicatedFixing = false;
132 duplicatedDate = *(dBegin++);
133 duplicatedValue = *(vBegin++);
134 }
135 } else {
136 noInvalidFixing = false;
137 invalidDate = *(dBegin++);
138 invalidValue = *(vBegin++);
139 }
140 }
142 notifier(name)->notifyObservers();
144 QL_REQUIRE(noInvalidFixing, "At least one invalid fixing provided: "
145 << invalidDate.weekday() << " " << invalidDate << ", "
146 << invalidValue);
147 QL_REQUIRE(noDuplicatedFixing, "At least one duplicated fixing provided: "
148 << duplicatedDate << ", " << duplicatedValue
149 << " while " << h[duplicatedDate]
150 << " value is already present");
151 }
152 };
153
154}
155
156
157#endif
Concrete date class.
Definition: date.hpp:125
Weekday weekday() const
Definition: date.hpp:391
purely virtual base class for indexes
Definition: index.hpp:45
global repository for past index fixings
std::map< std::string, TimeSeries< Real >, CaseInsensitiveCompare > data_
std::map< std::string, ext::shared_ptr< Observable > > notifiers_
const TimeSeries< Real > & getHistory(const std::string &name) const
bool hasHistoricalFixing(const std::string &name, const Date &fixingDate) const
void setHistory(const std::string &name, TimeSeries< Real > history)
void clearHistories()
clears all stored fixings
void addFixing(const std::string &name, const Date &fixingDate, Real fixing, bool forceOverwrite=false)
add a fixing
ext::shared_ptr< Observable > notifier(const std::string &name) const
void addFixings(const std::string &name, DateIterator dBegin, DateIterator dEnd, ValueIterator vBegin, bool forceOverwrite=false, const std::function< bool(const Date &d)> &isValidFixingDate={})
add fixings
std::vector< std::string > histories() const
returns all names of the indexes for which fixings were stored
void clearHistory(const std::string &name)
bool hasHistory(const std::string &name) const
template class providing a null value for a given type.
Definition: null.hpp:59
Basic support for the singleton pattern.
Definition: singleton.hpp:58
Container for historical data.
Definition: timeseries.hpp:51
floating-point comparisons
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Date d
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:37
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
observable and assignable proxy to concrete value
#define QL_DEPRECATED_DISABLE_WARNING
Definition: qldefines.hpp:216
#define QL_DEPRECATED_ENABLE_WARNING
Definition: qldefines.hpp:217
basic support for the singleton pattern
bool operator()(const std::string &s1, const std::string &s2) const
Container for historical data.