Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
commodityindex.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file qle/indexes/commodityindex.hpp
20 \brief commodity index class for holding commodity spot and futures price histories and forwarding.
21 \ingroup indexes
22*/
23
24#ifndef quantext_commodityindex_hpp
25#define quantext_commodityindex_hpp
26
27#include <ql/currency.hpp>
28#include <ql/handle.hpp>
29#include <ql/termstructures/yieldtermstructure.hpp>
30#include <ql/time/calendar.hpp>
33
34namespace QuantExt {
35using namespace QuantLib;
36
37//! Commodity Index
38/*! This index can represent both spot and futures prices.
39 In the latter case the constructor needs to be called with the futures expiry date.
40 If the expiry date is set to Date(), the index is interpreted as spot index.
41
42 If it is spot index, the index name() is set to the underlying name passed to the constructor
43 prefixed by "COMM-".
44
45 If it is a futures index and \c keepDays_ is \c false, we set the name() to
46 "COMM-" + underlyingName + "-" + "yyyy-mm", where "yyyy" is the expiry date's year and "mm" is the expiry date's
47 month. The index forecast for fixing Date yields the price curve's forecast to the futures expiry instead which
48 is beyond the fixing date. If \c keepDays_ is \c true, the date suffix in the name is "yyyy-mm-dd" i.e. we keep
49 the full date. This is useful for commodities whose expiry cycle is less than one month e.g. daily.
50
51 \ingroup indexes
52*/
54public:
55 /*! spot quote is interpreted as of today */
56 CommodityIndex(const std::string& underlyingName, const QuantLib::Date& expiryDate, const Calendar& fixingCalendar,
57 const Handle<QuantExt::PriceTermStructure>& priceCurve = Handle<QuantExt::PriceTermStructure>());
58 CommodityIndex(const std::string& underlyingName, const QuantLib::Date& expiryDate, const Calendar& fixingCalendar,
59 bool keepDays, const Handle<QuantExt::PriceTermStructure>& priceCurve = Handle<QuantExt::PriceTermStructure>());
60 //! \name Index interface
61 //@{
62 std::string name() const override { return name_; }
63 Calendar fixingCalendar() const override { return fixingCalendar_; }
64 bool isValidFixingDate(const Date& fixingDate) const override { return fixingCalendar_.isBusinessDay(fixingDate); }
65 Real fixing(const Date& fixingDate, bool forecastTodaysFixing = false) const override;
66 //@}
67 //! \name Observer interface
68 //@{
69 void update() override { notifyObservers(); }
70 //@}
71 //! \name Inspectors
72 //@{
73 std::string underlyingName() const { return underlyingName_; }
74 const Handle<QuantExt::PriceTermStructure>& priceCurve() const { return curve_; }
75 bool isFuturesIndex() const { return isFuturesIndex_; }
76 const QuantLib::Date& expiryDate() const { return expiryDate_; }
77 bool keepDays() const { return keepDays_; }
78 //@}
79 //! \name Fixing calculations
80 //@{
81 virtual Real forecastFixing(const Date& fixingDate) const;
82 virtual Real forecastFixing(const Time& fixingTime) const override;
83 virtual Real pastFixing(const Date& fixingDate) const override;
84 // @}
85
86 /*! Returns a copy of itself with a potentially different expiry date and pricing curve.
87
88 If the \c expiryDate is an empty \c Date, it is ignored and the CommodityIndex instance's expiry
89 date is used. If the \c ts is uninitialised, it is ignored and the CommodityIndex instance's price
90 curve is used.
91 */
92 virtual QuantLib::ext::shared_ptr<CommodityIndex> clone(const QuantLib::Date& expiryDate = QuantLib::Date(),
93 const boost::optional<QuantLib::Handle<PriceTermStructure>>& ts = boost::none) const = 0;
94
95protected:
96 std::string underlyingName_;
99 Handle<QuantExt::PriceTermStructure> curve_;
100 std::string name_;
102 // Belongs in CommodityFuturesIndex but have put everything else in base class.
104
105 // Shared initialisation
106 void init();
107};
108
110public:
111 /*! spot quote is interpreted as of today */
112 CommoditySpotIndex(const std::string& underlyingName, const Calendar& fixingCalendar,
113 const Handle<QuantExt::PriceTermStructure>& priceCurve = Handle<QuantExt::PriceTermStructure>())
115 QL_REQUIRE(expiryDate_ == Date(), "empty expiry date expected in CommoditySpotIndex");
116 }
117
118 //! Implement the base clone. The \c expiryDate is ignored for a CommoditySpotIndex.
119 QuantLib::ext::shared_ptr<CommodityIndex> clone(const QuantLib::Date& expiryDate = QuantLib::Date(),
120 const boost::optional<QuantLib::Handle<PriceTermStructure>>& ts = boost::none) const override;
121};
122
124public:
126 const std::string& underlyingName, const Date& expiryDate, const Calendar& fixingCalendar,
127 const Handle<QuantExt::PriceTermStructure>& priceCurve = Handle<QuantExt::PriceTermStructure>())
129 QL_REQUIRE(expiryDate_ != Date(), "non-empty expiry date expected CommodityFuturesIndex");
130 }
131
133 const std::string& underlyingName, const Date& expiryDate, const Calendar& fixingCalendar, bool keepDays,
134 const Handle<QuantExt::PriceTermStructure>& priceCurve = Handle<QuantExt::PriceTermStructure>())
136 QL_REQUIRE(expiryDate_ != Date(), "non-empty expiry date expected CommodityFuturesIndex");
137 }
138
139 //! Implement the base clone.
140 QuantLib::ext::shared_ptr<CommodityIndex> clone(const QuantLib::Date& expiryDate = QuantLib::Date(),
141 const boost::optional<QuantLib::Handle<PriceTermStructure>>& ts = boost::none) const override;
142};
143
144} // namespace QuantExt
145
146#endif
CommodityFuturesIndex(const std::string &underlyingName, const Date &expiryDate, const Calendar &fixingCalendar, bool keepDays, const Handle< QuantExt::PriceTermStructure > &priceCurve=Handle< QuantExt::PriceTermStructure >())
QuantLib::ext::shared_ptr< CommodityIndex > clone(const QuantLib::Date &expiryDate=QuantLib::Date(), const boost::optional< QuantLib::Handle< PriceTermStructure > > &ts=boost::none) const override
Implement the base clone.
CommodityFuturesIndex(const std::string &underlyingName, const Date &expiryDate, const Calendar &fixingCalendar, const Handle< QuantExt::PriceTermStructure > &priceCurve=Handle< QuantExt::PriceTermStructure >())
virtual QuantLib::ext::shared_ptr< CommodityIndex > clone(const QuantLib::Date &expiryDate=QuantLib::Date(), const boost::optional< QuantLib::Handle< PriceTermStructure > > &ts=boost::none) const =0
CommodityIndex(const std::string &underlyingName, const QuantLib::Date &expiryDate, const Calendar &fixingCalendar, bool keepDays, const Handle< QuantExt::PriceTermStructure > &priceCurve=Handle< QuantExt::PriceTermStructure >())
const QuantLib::Date & expiryDate() const
Calendar fixingCalendar() const override
virtual Real forecastFixing(const Date &fixingDate) const
CommodityIndex(const std::string &underlyingName, const QuantLib::Date &expiryDate, const Calendar &fixingCalendar, const Handle< QuantExt::PriceTermStructure > &priceCurve=Handle< QuantExt::PriceTermStructure >())
const Handle< QuantExt::PriceTermStructure > & priceCurve() const
std::string name() const override
Handle< QuantExt::PriceTermStructure > curve_
std::string underlyingName() const
bool isValidFixingDate(const Date &fixingDate) const override
virtual Real pastFixing(const Date &fixingDate) const override
returns a past fixing at the given date
Real fixing(const Date &fixingDate, bool forecastTodaysFixing=false) const override
CommoditySpotIndex(const std::string &underlyingName, const Calendar &fixingCalendar, const Handle< QuantExt::PriceTermStructure > &priceCurve=Handle< QuantExt::PriceTermStructure >())
QuantLib::ext::shared_ptr< CommodityIndex > clone(const QuantLib::Date &expiryDate=QuantLib::Date(), const boost::optional< QuantLib::Handle< PriceTermStructure > > &ts=boost::none) const override
Implement the base clone. The expiryDate is ignored for a CommoditySpotIndex.
A common base class for the FX and Equity Indices. Provides a forecast fixing method for time so the ...
Term structure of prices.