Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | List of all members
CommodityIndex Class Referenceabstract

Commodity Index. More...

#include <qle/indexes/commodityindex.hpp>

+ Inheritance diagram for CommodityIndex:
+ Collaboration diagram for CommodityIndex:

Public Member Functions

 CommodityIndex (const std::string &underlyingName, const QuantLib::Date &expiryDate, const Calendar &fixingCalendar, const Handle< QuantExt::PriceTermStructure > &priceCurve=Handle< QuantExt::PriceTermStructure >())
 
 CommodityIndex (const std::string &underlyingName, const QuantLib::Date &expiryDate, const Calendar &fixingCalendar, bool keepDays, const Handle< QuantExt::PriceTermStructure > &priceCurve=Handle< QuantExt::PriceTermStructure >())
 
Index interface
std::string name () const override
 
Calendar fixingCalendar () const override
 
bool isValidFixingDate (const Date &fixingDate) const override
 
Real fixing (const Date &fixingDate, bool forecastTodaysFixing=false) const override
 
Observer interface
void update () override
 
Inspectors
std::string underlyingName () const
 
const Handle< QuantExt::PriceTermStructure > & priceCurve () const
 
bool isFuturesIndex () const
 
const QuantLib::Date & expiryDate () const
 
bool keepDays () const
 
- Public Member Functions inherited from EqFxIndexBase
virtual ~EqFxIndexBase ()
 
virtual Real forecastFixing (const Time &fixingTime) const =0
 returns the fixing at the given time More...
 
virtual Real pastFixing (const Date &fixingDate) const =0
 returns a past fixing at the given date More...
 

Fixing calculations

std::string underlyingName_
 
Date expiryDate_
 
Calendar fixingCalendar_
 
Handle< QuantExt::PriceTermStructurecurve_
 
std::string name_
 
bool isFuturesIndex_
 
bool keepDays_
 
virtual Real forecastFixing (const Date &fixingDate) const
 
virtual Real forecastFixing (const Time &fixingTime) const override
 returns the fixing at the given time More...
 
virtual Real pastFixing (const Date &fixingDate) const override
 returns a past fixing at the given date More...
 
virtual QuantLib::ext::shared_ptr< CommodityIndexclone (const QuantLib::Date &expiryDate=QuantLib::Date(), const boost::optional< QuantLib::Handle< PriceTermStructure > > &ts=boost::none) const =0
 
void init ()
 

Detailed Description

Commodity Index.

This index can represent both spot and futures prices. In the latter case the constructor needs to be called with the futures expiry date. If the expiry date is set to Date(), the index is interpreted as spot index.

If it is spot index, the index name() is set to the underlying name passed to the constructor prefixed by "COMM-".

If it is a futures index and keepDays_ is false, we set the name() to "COMM-" + underlyingName + "-" + "yyyy-mm", where "yyyy" is the expiry date's year and "mm" is the expiry date's month. The index forecast for fixing Date yields the price curve's forecast to the futures expiry instead which is beyond the fixing date. If keepDays_ is true, the date suffix in the name is "yyyy-mm-dd" i.e. we keep the full date. This is useful for commodities whose expiry cycle is less than one month e.g. daily.

Definition at line 53 of file commodityindex.hpp.

Constructor & Destructor Documentation

◆ CommodityIndex() [1/2]

CommodityIndex ( const std::string &  underlyingName,
const QuantLib::Date &  expiryDate,
const Calendar &  fixingCalendar,
const Handle< QuantExt::PriceTermStructure > &  priceCurve = Handle< QuantExt::PriceTermStructure >() 
)

spot quote is interpreted as of today

◆ CommodityIndex() [2/2]

CommodityIndex ( const std::string &  underlyingName,
const QuantLib::Date &  expiryDate,
const Calendar &  fixingCalendar,
bool  keepDays,
const Handle< QuantExt::PriceTermStructure > &  priceCurve = Handle< QuantExt::PriceTermStructure >() 
)

Member Function Documentation

◆ name()

std::string name ( ) const
override

Definition at line 62 of file commodityindex.hpp.

62{ return name_; }

◆ fixingCalendar()

Calendar fixingCalendar ( ) const
override

Definition at line 63 of file commodityindex.hpp.

63{ return fixingCalendar_; }
+ Here is the caller graph for this function:

◆ isValidFixingDate()

bool isValidFixingDate ( const Date &  fixingDate) const
override

Definition at line 64 of file commodityindex.hpp.

64{ return fixingCalendar_.isBusinessDay(fixingDate); }

◆ fixing()

Real fixing ( const Date &  fixingDate,
bool  forecastTodaysFixing = false 
) const
override

Definition at line 71 of file commodityindex.cpp.

71 {
72
73 QL_REQUIRE(isValidFixingDate(fixingDate), "Commodity index " << name() << ": fixing date " <<
74 io::iso_date(fixingDate) << " is not valid");
75 Date today = Settings::instance().evaluationDate();
76 QL_REQUIRE(expiryDate_ == Date() || fixingDate <= expiryDate_,
77 "Commodity index " << name() << ": fixing requested on fixing date (" << io::iso_date(fixingDate)
78 << ") that is past the expiry date (" << io::iso_date(expiryDate_)
79 << "). Eval date is " << today);
80
81 if (fixingDate > today || (fixingDate == today && forecastTodaysFixing))
82 return forecastFixing(fixingDate);
83
84 Real result = Null<Decimal>();
85
86 if (fixingDate < today || Settings::instance().enforcesTodaysHistoricFixings()) {
87 // must have been fixed
88 // do not catch exceptions
89 result = pastFixing(fixingDate);
90 QL_REQUIRE(result != Null<Real>(), "Missing " << name() << " fixing for " << fixingDate);
91 } else {
92 try {
93 // might have been fixed
94 result = pastFixing(fixingDate);
95 } catch (Error&) {
96 ; // fall through and forecast
97 }
98 if (result == Null<Real>())
99 return forecastFixing(fixingDate);
100 }
101
102 return result;
103}
virtual Real forecastFixing(const Date &fixingDate) const
std::string name() const override
bool isValidFixingDate(const Date &fixingDate) const override
virtual Real pastFixing(const Date &fixingDate) const override
returns a past fixing at the given date

◆ update()

void update ( )
override

Definition at line 69 of file commodityindex.hpp.

69{ notifyObservers(); }

◆ underlyingName()

std::string underlyingName ( ) const

Definition at line 73 of file commodityindex.hpp.

73{ return underlyingName_; }
+ Here is the caller graph for this function:

◆ priceCurve()

const Handle< QuantExt::PriceTermStructure > & priceCurve ( ) const

Definition at line 74 of file commodityindex.hpp.

74{ return curve_; }
Handle< QuantExt::PriceTermStructure > curve_
+ Here is the caller graph for this function:

◆ isFuturesIndex()

bool isFuturesIndex ( ) const

Definition at line 75 of file commodityindex.hpp.

◆ expiryDate()

const QuantLib::Date & expiryDate ( ) const

Definition at line 76 of file commodityindex.hpp.

76{ return expiryDate_; }
+ Here is the caller graph for this function:

◆ keepDays()

bool keepDays ( ) const

Definition at line 77 of file commodityindex.hpp.

◆ forecastFixing() [1/2]

Real forecastFixing ( const Date &  fixingDate) const
virtual

Definition at line 116 of file commodityindex.cpp.

116 {
117 if (isFuturesIndex_)
118 return curve_->price(expiryDate_);
119 else
120 return curve_->price(fixingDate);
121}

◆ forecastFixing() [2/2]

Real forecastFixing ( const Time &  fixingTime) const
overridevirtual

returns the fixing at the given time

Implements EqFxIndexBase.

Definition at line 110 of file commodityindex.cpp.

110 {
111 if (isFuturesIndex_)
112 return curve_->price(expiryDate_);
113 else
114 return curve_->price(fixingTime);
115}

◆ pastFixing()

Real pastFixing ( const Date &  fixingDate) const
overridevirtual

returns a past fixing at the given date

the date passed as arguments must be the actual calendar date of the fixing; no settlement days must be used.

Implements EqFxIndexBase.

Reimplemented in OffPeakPowerIndex.

Definition at line 105 of file commodityindex.cpp.

105 {
106 QL_REQUIRE(isValidFixingDate(fixingDate), fixingDate << " is not a valid fixing date");
107 return timeSeries()[fixingDate];
108}
QuantLib::Date fixingDate(const QuantLib::Date &d, const QuantLib::Period obsLag, const QuantLib::Frequency freq, bool interpolated)
Definition: inflation.cpp:183
+ Here is the caller graph for this function:

◆ clone()

virtual QuantLib::ext::shared_ptr< CommodityIndex > clone ( const QuantLib::Date &  expiryDate = QuantLib::Date(),
const boost::optional< QuantLib::Handle< PriceTermStructure > > &  ts = boost::none 
) const
pure virtual

Returns a copy of itself with a potentially different expiry date and pricing curve.

If the expiryDate is an empty Date, it is ignored and the CommodityIndex instance's expiry date is used. If the ts is uninitialised, it is ignored and the CommodityIndex instance's price curve is used.

Implemented in OffPeakPowerIndex, CommodityBasisFutureIndex, CommoditySpotIndex, and CommodityFuturesIndex.

◆ init()

void init ( )
protected

Definition at line 47 of file commodityindex.cpp.

47 {
48
49 if (expiryDate_ == Date()) {
50 // spot price index
51 name_ = "COMM-" + underlyingName_;
52 isFuturesIndex_ = false;
53 } else {
54 // futures price index
55 std::ostringstream o;
56 o << "COMM-" << underlyingName_ << "-" << QuantLib::io::iso_date(expiryDate_);
57 name_ = o.str();
58 if (!keepDays_) {
59 // Remove the "-dd" portion from the expiry date
60 name_.erase(name_.length() - 3);
61 }
62 isFuturesIndex_ = true;
63 }
64
65 registerWith(curve_);
66 registerWith(Settings::instance().evaluationDate());
67 registerWith(IndexManager::instance().notifier(name()));
68
69}

Member Data Documentation

◆ underlyingName_

std::string underlyingName_
protected

Definition at line 96 of file commodityindex.hpp.

◆ expiryDate_

Date expiryDate_
protected

Definition at line 97 of file commodityindex.hpp.

◆ fixingCalendar_

Calendar fixingCalendar_
protected

Definition at line 98 of file commodityindex.hpp.

◆ curve_

Handle<QuantExt::PriceTermStructure> curve_
protected

Definition at line 99 of file commodityindex.hpp.

◆ name_

std::string name_
protected

Definition at line 100 of file commodityindex.hpp.

◆ isFuturesIndex_

bool isFuturesIndex_
protected

Definition at line 101 of file commodityindex.hpp.

◆ keepDays_

bool keepDays_
protected

Definition at line 103 of file commodityindex.hpp.