25#include <boost/make_shared.hpp>
34 const Calendar& fixingCalendar,
const Handle<QuantExt::PriceTermStructure>& curve)
35 : underlyingName_(underlyingName), expiryDate_(expiryDate), fixingCalendar_(fixingCalendar),
36 curve_(curve), keepDays_(false) {
40CommodityIndex::CommodityIndex(
const string& underlyingName,
const Date& expiryDate,
41 const Calendar& fixingCalendar,
bool keepDays,
const Handle<PriceTermStructure>& curve)
42 : underlyingName_(underlyingName), expiryDate_(expiryDate), fixingCalendar_(fixingCalendar),
43 curve_(curve), keepDays_(keepDays) {
47void CommodityIndex::init() {
49 if (expiryDate_ == Date()) {
51 name_ =
"COMM-" + underlyingName_;
52 isFuturesIndex_ =
false;
56 o <<
"COMM-" << underlyingName_ <<
"-" << QuantLib::io::iso_date(expiryDate_);
60 name_.erase(name_.length() - 3);
62 isFuturesIndex_ =
true;
66 registerWith(Settings::instance().evaluationDate());
67 registerWith(IndexManager::instance().notifier(name()));
71Real CommodityIndex::fixing(
const Date& fixingDate,
bool forecastTodaysFixing)
const {
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);
81 if (fixingDate > today || (fixingDate == today && forecastTodaysFixing))
82 return forecastFixing(fixingDate);
84 Real result = Null<Decimal>();
86 if (fixingDate < today || Settings::instance().enforcesTodaysHistoricFixings()) {
89 result = pastFixing(fixingDate);
90 QL_REQUIRE(result != Null<Real>(),
"Missing " << name() <<
" fixing for " << fixingDate);
94 result = pastFixing(fixingDate);
98 if (result == Null<Real>())
99 return forecastFixing(fixingDate);
105Real CommodityIndex::pastFixing(
const Date& fixingDate)
const {
106 QL_REQUIRE(isValidFixingDate(fixingDate), fixingDate <<
" is not a valid fixing date");
107 return timeSeries()[fixingDate];
110Real CommodityIndex::forecastFixing(
const Time& fixingTime)
const {
112 return curve_->price(expiryDate_);
114 return curve_->price(fixingTime);
116Real CommodityIndex::forecastFixing(
const Date& fixingDate)
const {
118 return curve_->price(expiryDate_);
120 return curve_->price(fixingDate);
123QuantLib::ext::shared_ptr<CommodityIndex> CommoditySpotIndex::clone(
const Date& expiryDate,
124 const boost::optional<Handle<PriceTermStructure>>& ts)
const {
125 const auto& pts = ts ? *ts : priceCurve();
126 return QuantLib::ext::make_shared<CommoditySpotIndex>(underlyingName(), fixingCalendar(), pts);
129QuantLib::ext::shared_ptr<CommodityIndex> CommodityFuturesIndex::clone(
const Date& expiry,
130 const boost::optional<Handle<PriceTermStructure>>& ts)
const {
131 const auto& pts = ts ? *ts : priceCurve();
132 const auto& ed = expiry == Date() ? expiryDate() : expiry;
133 return QuantLib::ext::make_shared<CommodityFuturesIndex>(underlyingName(), ed, fixingCalendar(), keepDays(), pts);
CommodityIndex(const std::string &underlyingName, const QuantLib::Date &expiryDate, const Calendar &fixingCalendar, const Handle< QuantExt::PriceTermStructure > &priceCurve=Handle< QuantExt::PriceTermStructure >())
commodity index class for holding commodity spot and futures price histories and forwarding.