Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/experimental/commodities/commodityindex.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) 2008 J. Erik Radmall
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 commodityindex.hpp
21
    \brief Commodity index
22
*/
23
24
#ifndef quantlib_commodity_index_hpp
25
#define quantlib_commodity_index_hpp
26
27
#include <ql/experimental/commodities/commoditycurve.hpp>
28
#include <ql/index.hpp>
29
30
namespace QuantLib {
31
32
    class TermStructure;
33
34
    //! base class for commodity indexes
35
    class CommodityIndex : public Index {
36
      public:
37
        CommodityIndex(std::string  name,
38
                       CommodityType commodityType,
39
                       Currency currency,
40
                       UnitOfMeasure unitOfMeasure,
41
                       Calendar calendar,
42
                       Real lotQuantity,
43
                       ext::shared_ptr<CommodityCurve> forwardCurve,
44
                       ext::shared_ptr<ExchangeContracts> exchangeContracts,
45
                       int nearbyOffset);
46
        //! \name Index interface
47
        //@{
48
        std::string name() const override;
49
        Calendar fixingCalendar() const override;
50
        bool isValidFixingDate(const Date& fixingDate) const override;
51
        Real fixing(const Date& fixingDate,
52
                    bool forecastTodaysFixing = false) const override;
53
        //@}
54
        //! \name Observer interface
55
        //@{
56
        void update() override;
57
        //@}
58
        //! \name Inspectors
59
        //@{
60
        const CommodityType& commodityType() const;
61
        const Currency& currency() const;
62
        const UnitOfMeasure& unitOfMeasure() const;
63
        const ext::shared_ptr<CommodityCurve>& forwardCurve() const;
64
        Real lotQuantity() const;
65
        Real forwardPrice(const Date& date) const;
66
        Date lastQuoteDate() const;
67
        bool empty() const;
68
        bool forwardCurveEmpty() const;
69
        //@}
70
71
        /*! \deprecated Use fixingCalendar instead.
72
                        Deprecated in version 1.37.
73
        */
74
        [[deprecated("Use fixingCalendar instead")]]
75
0
        const Calendar& calendar() const {
76
0
            return calendar_;
77
0
        }
78
79
        /*! \deprecated Use fixing instead.
80
                        Deprecated in version 1.37.
81
        */
82
        [[deprecated("Use fixing instead")]]
83
0
        Real price(const Date& date) {
84
0
            return fixing(date);
85
0
        }
86
87
        /*! \deprecated Use addFixing instead.
88
                        Deprecated in version 1.37.
89
        */
90
        [[deprecated("Use addFixing instead")]]
91
0
        void addQuote(const Date& quoteDate, Real quote) {
92
0
            addFixing(quoteDate, quote);
93
0
        }
94
95
        /*! \deprecated Use addFixings instead.
96
                        Deprecated in version 1.37.
97
        */
98
        [[deprecated("Use addFixings instead")]]
99
0
        void addQuotes(const std::map<Date, Real>& quotes) {
100
0
            for (auto quote : quotes) {
101
0
                addFixing(quote.first, quote.second);
102
0
            }
103
0
        }
104
105
        /*! \deprecated Use clearFixings instead.
106
                        Deprecated in version 1.37.
107
        */
108
        [[deprecated("Use clearFixings instead")]]
109
0
        void clearQuotes() {
110
0
            clearFixings();
111
0
        }
112
113
        /*! \deprecated Use isValidFixingDate instead.
114
                        Deprecated in version 1.37.
115
        */
116
        [[deprecated("Use isValidFixingDate instead")]]
117
0
        bool isValidQuoteDate(const Date& quoteDate) const {
118
0
            return isValidFixingDate(quoteDate);
119
0
        }
120
121
        /*! \deprecated Use timeSeries instead.
122
                        Deprecated in version 1.37.
123
        */
124
        [[deprecated("Use timeSeries instead")]]
125
0
        const TimeSeries<Real>& quotes() const {
126
0
            return timeSeries();
127
0
        }
128
129
        friend std::ostream& operator<<(std::ostream&, const CommodityIndex&);
130
      protected:
131
        std::string name_;
132
        CommodityType commodityType_;
133
        UnitOfMeasure unitOfMeasure_;
134
        Currency currency_;
135
        Calendar calendar_;
136
        Real lotQuantity_;
137
        ext::shared_ptr<CommodityCurve> forwardCurve_;
138
        Real forwardCurveUomConversionFactor_ = 1;
139
        ext::shared_ptr<ExchangeContracts> exchangeContracts_;
140
        Integer nearbyOffset_;
141
    };
142
143
144
    // inline definitions
145
146
0
    inline bool operator==(const CommodityIndex& i1, const CommodityIndex& i2) {
147
0
        return i1.name() == i2.name();
148
0
    }
149
150
0
    inline void CommodityIndex::update() {
151
0
        notifyObservers();
152
0
    }
153
154
0
    inline std::string CommodityIndex::name() const {
155
0
        return name_;
156
0
    }
157
158
0
    inline Calendar CommodityIndex::fixingCalendar() const {
159
0
        return calendar_;
160
0
    }
161
162
0
    inline bool CommodityIndex::isValidFixingDate(const Date& fixingDate) const {
163
0
        return fixingCalendar().isBusinessDay(fixingDate);
164
0
    }
165
166
0
    inline Real CommodityIndex::fixing(const Date& date, bool) const {
167
0
        return pastFixing(date);
168
0
    }
169
170
0
    inline const CommodityType& CommodityIndex::commodityType() const {
171
0
        return commodityType_;
172
0
    }
173
174
0
    inline const UnitOfMeasure& CommodityIndex::unitOfMeasure() const {
175
0
        return unitOfMeasure_;
176
0
    }
177
178
0
    inline const Currency& CommodityIndex::currency() const {
179
0
        return currency_;
180
0
    }
181
182
0
    inline Real CommodityIndex::lotQuantity() const {
183
0
        return lotQuantity_;
184
0
    }
185
186
    inline const ext::shared_ptr<CommodityCurve>&
187
0
    CommodityIndex::forwardCurve() const {
188
0
        return forwardCurve_;
189
0
    }
190
191
0
    inline Real CommodityIndex::forwardPrice(const Date& date) const {
192
0
        try {
193
0
            Real forwardPrice =
194
0
                forwardCurve_->price(date, exchangeContracts_, nearbyOffset_);
195
0
            return forwardPrice * forwardCurveUomConversionFactor_;
196
0
        } catch (const std::exception& e) {
197
0
            QL_FAIL("error fetching forward price for index " << name_
198
0
                    << ": " << e.what());
199
0
        }
200
0
    }
201
202
0
    inline Date CommodityIndex::lastQuoteDate() const {
203
0
        return timeSeries().lastDate();
204
0
    }
205
206
0
    inline bool CommodityIndex::empty() const {
207
0
        return timeSeries().empty();
208
0
    }
209
210
0
    inline bool CommodityIndex::forwardCurveEmpty() const {
211
0
        if (forwardCurve_ != nullptr)
212
0
            return forwardCurve_->empty();
213
0
        return false;
214
0
    }
215
216
}
217
218
#endif