QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
commodityindex.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) 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
28#include <ql/index.hpp>
29
30namespace QuantLib {
31
32 class TermStructure;
33
34 //! base class for commodity indexes
35 class CommodityIndex : public Index {
36 public:
37 CommodityIndex(std::string name,
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 const Calendar& calendar() const {
76 return calendar_;
77 }
78
79 /*! \deprecated Use fixing instead.
80 Deprecated in version 1.37.
81 */
82 [[deprecated("Use fixing instead")]]
83 Real price(const Date& date) {
84 return fixing(date);
85 }
86
87 /*! \deprecated Use addFixing instead.
88 Deprecated in version 1.37.
89 */
90 [[deprecated("Use addFixing instead")]]
91 void addQuote(const Date& quoteDate, Real quote) {
92 addFixing(quoteDate, quote);
93 }
94
95 /*! \deprecated Use addFixings instead.
96 Deprecated in version 1.37.
97 */
98 [[deprecated("Use addFixings instead")]]
99 void addQuotes(const std::map<Date, Real>& quotes) {
100 for (auto quote : quotes) {
101 addFixing(quote.first, quote.second);
102 }
103 }
104
105 /*! \deprecated Use clearFixings instead.
106 Deprecated in version 1.37.
107 */
108 [[deprecated("Use clearFixings instead")]]
109 void clearQuotes() {
110 clearFixings();
111 }
112
113 /*! \deprecated Use isValidFixingDate instead.
114 Deprecated in version 1.37.
115 */
116 [[deprecated("Use isValidFixingDate instead")]]
117 bool isValidQuoteDate(const Date& quoteDate) const {
118 return isValidFixingDate(quoteDate);
119 }
120
121 /*! \deprecated Use timeSeries instead.
122 Deprecated in version 1.37.
123 */
124 [[deprecated("Use timeSeries instead")]]
125 const TimeSeries<Real>& quotes() const {
126 return timeSeries();
127 }
128
129 friend std::ostream& operator<<(std::ostream&, const CommodityIndex&);
130 protected:
131 std::string name_;
137 ext::shared_ptr<CommodityCurve> forwardCurve_;
139 ext::shared_ptr<ExchangeContracts> exchangeContracts_;
141 };
142
143
144 // inline definitions
145
146 inline bool operator==(const CommodityIndex& i1, const CommodityIndex& i2) {
147 return i1.name() == i2.name();
148 }
149
152 }
153
154 inline std::string CommodityIndex::name() const {
155 return name_;
156 }
157
159 return calendar_;
160 }
161
162 inline bool CommodityIndex::isValidFixingDate(const Date& fixingDate) const {
163 return fixingCalendar().isBusinessDay(fixingDate);
164 }
165
166 inline Real CommodityIndex::fixing(const Date& date, bool) const {
167 return pastFixing(date);
168 }
169
171 return commodityType_;
172 }
173
175 return unitOfMeasure_;
176 }
177
178 inline const Currency& CommodityIndex::currency() const {
179 return currency_;
180 }
181
183 return lotQuantity_;
184 }
185
186 inline const ext::shared_ptr<CommodityCurve>&
188 return forwardCurve_;
189 }
190
191 inline Real CommodityIndex::forwardPrice(const Date& date) const {
192 try {
196 } catch (const std::exception& e) {
197 QL_FAIL("error fetching forward price for index " << name_
198 << ": " << e.what());
199 }
200 }
201
203 return timeSeries().lastDate();
204 }
205
206 inline bool CommodityIndex::empty() const {
207 return timeSeries().empty();
208 }
209
211 if (forwardCurve_ != nullptr)
212 return forwardCurve_->empty();
213 return false;
214 }
215
216}
217
218#endif
calendar class
Definition: calendar.hpp:61
bool isBusinessDay(const Date &d) const
Definition: calendar.hpp:229
base class for commodity indexes
const TimeSeries< Real > & quotes() const
Calendar fixingCalendar() const override
returns the calendar defining valid fixing dates
const Calendar & calendar() const
const Currency & currency() const
const ext::shared_ptr< CommodityCurve > & forwardCurve() const
ext::shared_ptr< ExchangeContracts > exchangeContracts_
void addQuote(const Date &quoteDate, Real quote)
bool isValidQuoteDate(const Date &quoteDate) const
std::string name() const override
Returns the name of the index.
Real price(const Date &date)
bool isValidFixingDate(const Date &fixingDate) const override
returns TRUE if the fixing date is a valid one
void addQuotes(const std::map< Date, Real > &quotes)
const CommodityType & commodityType() const
Real forwardPrice(const Date &date) const
ext::shared_ptr< CommodityCurve > forwardCurve_
const UnitOfMeasure & unitOfMeasure() const
friend std::ostream & operator<<(std::ostream &, const CommodityIndex &)
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
returns the fixing at the given date
Currency specification
Definition: currency.hpp:36
Concrete date class.
Definition: date.hpp:125
purely virtual base class for indexes
Definition: index.hpp:45
void clearFixings()
clears all stored historical fixings
Definition: index.cpp:43
const TimeSeries< Real > & timeSeries() const
returns the fixing TimeSeries
Definition: index.hpp:71
virtual void addFixing(const Date &fixingDate, Real fixing, bool forceOverwrite=false)
Definition: index.cpp:24
virtual Real pastFixing(const Date &fixingDate) const
returns a past fixing at the given date
Definition: index.hpp:131
Container for historical data.
Definition: timeseries.hpp:51
bool empty() const
returns whether the series contains any data
Definition: timeseries.hpp:217
Date lastDate() const
returns the last date for which a historical datum exists
Definition: timeseries.hpp:206
Unit of measure specification
Commodity curve.
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
virtual base class for indexes
Definition: any.hpp:37
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:179