QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
index.hpp
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 StatPro Italia srl
6 Copyright (C) 2007, 2008 Ferdinando Ametrano
7 Copyright (C) 2007 Chiara Fornarola
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
27#ifndef quantlib_index_hpp
28#define quantlib_index_hpp
29
30#include <ql/indexes/indexmanager.hpp>
31#include <ql/math/comparison.hpp>
32#include <ql/time/calendar.hpp>
33
34namespace QuantLib {
35
37
44 class Index : public Observable {
45 public:
46 ~Index() override = default;
48
52 virtual std::string name() const = 0;
54 virtual Calendar fixingCalendar() const = 0;
56 virtual bool isValidFixingDate(const Date& fixingDate) const = 0;
58 bool hasHistoricalFixing(const Date& fixingDate) const;
60
63 virtual Real fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const = 0;
67 }
69
72 virtual bool allowsNativeFixings() { return true; }
74
77 virtual void addFixing(const Date& fixingDate, Real fixing, bool forceOverwrite = false);
79
82 void addFixings(const TimeSeries<Real>& t, bool forceOverwrite = false);
84
87 template <class DateIterator, class ValueIterator>
88 void addFixings(DateIterator dBegin,
89 DateIterator dEnd,
90 ValueIterator vBegin,
91 bool forceOverwrite = false) {
93 std::string tag = name();
95 bool noInvalidFixing = true, noDuplicatedFixing = true;
96 Date invalidDate, duplicatedDate;
97 Real nullValue = Null<Real>();
98 Real invalidValue = Null<Real>();
99 Real duplicatedValue = Null<Real>();
100 while (dBegin != dEnd) {
101 bool validFixing = isValidFixingDate(*dBegin);
102 Real currentValue = h[*dBegin];
103 bool missingFixing = forceOverwrite || currentValue == nullValue;
104 if (validFixing) {
105 if (missingFixing)
106 h[*(dBegin++)] = *(vBegin++);
107 else if (close(currentValue, *(vBegin))) {
108 ++dBegin;
109 ++vBegin;
110 } else {
111 noDuplicatedFixing = false;
112 duplicatedDate = *(dBegin++);
113 duplicatedValue = *(vBegin++);
114 }
115 } else {
116 noInvalidFixing = false;
117 invalidDate = *(dBegin++);
118 invalidValue = *(vBegin++);
119 }
120 }
122 QL_REQUIRE(noInvalidFixing, "At least one invalid fixing provided: "
123 << invalidDate.weekday() << " " << invalidDate << ", "
124 << invalidValue);
125 QL_REQUIRE(noDuplicatedFixing, "At least one duplicated fixing provided: "
126 << duplicatedDate << ", " << duplicatedValue
127 << " while " << h[duplicatedDate]
128 << " value is already present");
129 }
131 void clearFixings();
132
133 private:
136 };
137
138 inline bool Index::hasHistoricalFixing(const Date& fixingDate) const {
139 return IndexManager::instance().hasHistoricalFixing(name(), fixingDate);
140 }
141
142}
143
144#endif
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
Weekday weekday() const
Definition: date.hpp:395
purely virtual base class for indexes
Definition: index.hpp:44
virtual Calendar fixingCalendar() const =0
returns the calendar defining valid fixing dates
virtual Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const =0
returns the fixing at the given date
bool hasHistoricalFixing(const Date &fixingDate) const
returns whether a historical fixing was stored for the given date
Definition: index.hpp:138
void clearFixings()
clears all stored historical fixings
Definition: index.cpp:45
virtual bool allowsNativeFixings()
check if index allows for native fixings.
Definition: index.hpp:72
const TimeSeries< Real > & timeSeries() const
returns the fixing TimeSeries
Definition: index.hpp:65
virtual void addFixing(const Date &fixingDate, Real fixing, bool forceOverwrite=false)
stores the historical fixing at the given date
Definition: index.cpp:24
void addFixings(const TimeSeries< Real > &t, bool forceOverwrite=false)
stores historical fixings from a TimeSeries
Definition: index.cpp:33
virtual std::string name() const =0
Returns the name of the index.
virtual bool isValidFixingDate(const Date &fixingDate) const =0
returns TRUE if the fixing date is a valid one
void addFixings(DateIterator dBegin, DateIterator dEnd, ValueIterator vBegin, bool forceOverwrite=false)
stores historical fixings at the given dates
Definition: index.hpp:88
~Index() override=default
void checkNativeFixingsAllowed()
check if index allows for native fixings
Definition: index.cpp:50
const TimeSeries< Real > & getHistory(const std::string &name) const
returns the (possibly empty) history of the index fixings
bool hasHistoricalFixing(const std::string &name, const Date &fixingDate) const
returns whether a specific historical fixing was stored for the index and date
void setHistory(const std::string &name, TimeSeries< Real > history)
stores the historical fixings of the index
template class providing a null value for a given type.
Definition: null.hpp:76
Object that notifies its changes to a set of observers.
Definition: observable.hpp:62
static IndexManager & instance()
access to the unique instance
Definition: singleton.hpp:104
Container for historical data.
Definition: timeseries.hpp:51
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163