Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/index.hpp
Line
Count
Source (jump to first uncovered line)
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
23
/*! \file index.hpp
24
    \brief virtual base class for indexes
25
*/
26
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/patterns/observable.hpp>
33
#include <ql/time/calendar.hpp>
34
35
namespace QuantLib {
36
37
    //! purely virtual base class for indexes
38
    /*! \warning this class performs no check that the
39
                 provided/requested fixings are for dates in the past,
40
                 i.e. for dates less than or equal to the evaluation
41
                 date. It is up to the client code to take care of
42
                 possible inconsistencies due to "seeing in the
43
                 future"
44
    */
45
    class Index : public Observable, public Observer {
46
      public:
47
0
        ~Index() override = default;
48
        //! Returns the name of the index.
49
        /*! \warning This method is used for output and comparison
50
                     between indexes. It is <b>not</b> meant to be
51
                     used for writing switch-on-type code.
52
        */
53
        virtual std::string name() const = 0;
54
        //! returns the calendar defining valid fixing dates
55
        virtual Calendar fixingCalendar() const = 0;
56
        //! returns TRUE if the fixing date is a valid one
57
        virtual bool isValidFixingDate(const Date& fixingDate) const = 0;
58
        //! returns whether a historical fixing was stored for the given date
59
        bool hasHistoricalFixing(const Date& fixingDate) const;
60
        //! returns the fixing at the given date
61
        /*! the date passed as arguments must be the actual calendar
62
            date of the fixing; no settlement days must be used.
63
        */
64
        virtual Real fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const = 0;
65
        //! returns a past fixing at the given date
66
        /*! the date passed as arguments must be the actual calendar
67
            date of the fixing; no settlement days must be used.
68
        */
69
        virtual Real pastFixing(const Date& fixingDate) const;
70
        //! returns the fixing TimeSeries
71
0
        const TimeSeries<Real>& timeSeries() const {
72
0
            QL_DEPRECATED_DISABLE_WARNING
73
0
            return IndexManager::instance().getHistory(name());
74
0
            QL_DEPRECATED_ENABLE_WARNING
75
0
        }
76
        //! check if index allows for native fixings.
77
        /*! If this returns false, calls to addFixing and similar
78
            methods will raise an exception.
79
        */
80
0
        virtual bool allowsNativeFixings() { return true; }
81
        //! stores the historical fixing at the given date
82
        /*! the date passed as arguments must be the actual calendar
83
            date of the fixing; no settlement days must be used.
84
        */
85
        //! \name Observer interface
86
        //@{
87
        void update() override;
88
        //@}
89
        virtual void addFixing(const Date& fixingDate, Real fixing, bool forceOverwrite = false);
90
        //! stores historical fixings from a TimeSeries
91
        /*! the dates in the TimeSeries must be the actual calendar
92
            dates of the fixings; no settlement days must be used.
93
        */
94
        void addFixings(const TimeSeries<Real>& t, bool forceOverwrite = false);
95
        //! stores historical fixings at the given dates
96
        /*! the dates passed as arguments must be the actual calendar
97
            dates of the fixings; no settlement days must be used.
98
        */
99
        template <class DateIterator, class ValueIterator>
100
        void addFixings(DateIterator dBegin,
101
                        DateIterator dEnd,
102
                        ValueIterator vBegin,
103
0
                        bool forceOverwrite = false) {
104
0
            checkNativeFixingsAllowed();
105
0
            IndexManager::instance().addFixings(
106
0
                name(), dBegin, dEnd, vBegin, forceOverwrite,
107
0
                [this](const Date& d) { return isValidFixingDate(d); });
Unexecuted instantiation: QuantLib::Index::addFixings<QuantLib::Date const*, double*>(QuantLib::Date const*, QuantLib::Date const*, double*, bool)::{lambda(QuantLib::Date const&)#1}::operator()(QuantLib::Date const&) const
Unexecuted instantiation: QuantLib::Index::addFixings<std::__1::__wrap_iter<QuantLib::Date*>, std::__1::__wrap_iter<double*> >(std::__1::__wrap_iter<QuantLib::Date*>, std::__1::__wrap_iter<QuantLib::Date*>, std::__1::__wrap_iter<double*>, bool)::{lambda(QuantLib::Date const&)#1}::operator()(QuantLib::Date const&) const
108
0
        }
Unexecuted instantiation: void QuantLib::Index::addFixings<QuantLib::Date const*, double*>(QuantLib::Date const*, QuantLib::Date const*, double*, bool)
Unexecuted instantiation: void QuantLib::Index::addFixings<std::__1::__wrap_iter<QuantLib::Date*>, std::__1::__wrap_iter<double*> >(std::__1::__wrap_iter<QuantLib::Date*>, std::__1::__wrap_iter<QuantLib::Date*>, std::__1::__wrap_iter<double*>, bool)
109
        //! clears all stored historical fixings
110
        void clearFixings();
111
112
      protected:
113
0
        ext::shared_ptr<Observable> notifier() const {
114
0
            QL_DEPRECATED_DISABLE_WARNING
115
0
            return IndexManager::instance().notifier(name());
116
0
            QL_DEPRECATED_ENABLE_WARNING
117
0
        }
118
119
      private:
120
        //! check if index allows for native fixings
121
        void checkNativeFixingsAllowed();
122
123
    };
124
125
0
    inline bool Index::hasHistoricalFixing(const Date& fixingDate) const {
126
0
        QL_DEPRECATED_DISABLE_WARNING
127
0
        return IndexManager::instance().hasHistoricalFixing(name(), fixingDate);
128
0
        QL_DEPRECATED_ENABLE_WARNING
129
0
    }
130
131
0
    inline Real Index::pastFixing(const Date& fixingDate) const {
132
0
        QL_REQUIRE(isValidFixingDate(fixingDate), fixingDate << " is not a valid fixing date");
133
0
        return timeSeries()[fixingDate];
134
0
    }
135
136
0
    inline void Index::update() {
137
0
        notifyObservers();
138
0
    }
139
140
}
141
142
#endif