diff --git a/ql/experimental/commodities/commodityindex.cpp b/ql/experimental/commodities/commodityindex.cpp index cc3b7b6ab20..c89a30ddbb3 100644 --- a/ql/experimental/commodities/commodityindex.cpp +++ b/ql/experimental/commodities/commodityindex.cpp @@ -37,8 +37,6 @@ namespace QuantLib { calendar_(std::move(calendar)), lotQuantity_(lotQuantity), forwardCurve_(std::move(forwardCurve)), exchangeContracts_(std::move(exchangeContracts)), nearbyOffset_(nearbyOffset) { - quotes_ = IndexManager::instance().getHistory(indexName); - IndexManager::instance().setHistory(indexName, quotes_); registerWith(Settings::instance().evaluationDate()); registerWith(IndexManager::instance().notifier(name())); diff --git a/ql/experimental/commodities/commodityindex.hpp b/ql/experimental/commodities/commodityindex.hpp index fe1f8597a08..357b37a63ca 100644 --- a/ql/experimental/commodities/commodityindex.hpp +++ b/ql/experimental/commodities/commodityindex.hpp @@ -25,15 +25,14 @@ #define quantlib_commodity_index_hpp #include -#include +#include namespace QuantLib { class TermStructure; //! base class for commodity indexes - class CommodityIndex : public Observable, - public Observer { + class CommodityIndex : public Index { public: CommodityIndex(const std::string& name, CommodityType commodityType, @@ -46,7 +45,11 @@ namespace QuantLib { int nearbyOffset); //! \name Index interface //@{ - std::string name() const; + 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; //@} //! \name Observer interface //@{ @@ -57,31 +60,71 @@ namespace QuantLib { const CommodityType& commodityType() const; const Currency& currency() const; const UnitOfMeasure& unitOfMeasure() const; - const Calendar& calendar() const; const ext::shared_ptr& forwardCurve() const; Real lotQuantity() const; - - Real price(const Date& date); Real forwardPrice(const Date& date) const; Date lastQuoteDate() const; + bool empty() const; + bool forwardCurveEmpty() const; //@} - void addQuote(const Date& quoteDate, Real quote); + /*! \deprecated Use fixingCalendar instead. + Deprecated in version 1.37. + */ + [[deprecated("Use fixingCalendar instead")]] + const Calendar& calendar() const { + return calendar_; + } + + /*! \deprecated Use fixing instead. + Deprecated in version 1.37. + */ + [[deprecated("Use fixing instead")]] + Real price(const Date& date) { + return fixing(date); + } + + /*! \deprecated Use addFixing instead. + Deprecated in version 1.37. + */ + [[deprecated("Use fixing instead")]] + void addQuote(const Date& quoteDate, Real quote) { + addFixing(quoteDate, quote); + } + + /*! \deprecated Use addFixings instead. + Deprecated in version 1.37. + */ + [[deprecated("Use addFixings instead")]] void addQuotes(const std::map& quotes) { - std::string tag = name(); - quotes_ = IndexManager::instance().getHistory(tag); for (auto quote : quotes) { - quotes_[quote.first] = quote.second; + addQuote(quote.first, quote.second); } - IndexManager::instance().setHistory(tag, quotes_); } - void clearQuotes() const; - //! returns TRUE if the quote date is valid - bool isValidQuoteDate(const Date& quoteDate) const; - bool empty() const; - bool forwardCurveEmpty() const; - const TimeSeries& quotes() const; + /*! \deprecated Use clearFixings instead. + Deprecated in version 1.37. + */ + [[deprecated("Use clearFixings instead")]] + void clearQuotes() { + clearFixings(); + } + + /*! \deprecated Use isValidFixingDate instead. + Deprecated in version 1.37. + */ + [[deprecated("Use isValidFixingDate instead")]] + bool isValidQuoteDate(const Date& quoteDate) const { + return isValidFixingDate(quoteDate); + } + + /*! \deprecated Use timeSeries instead. + Deprecated in version 1.37. + */ + [[deprecated("Use timeSeries instead")]] + const TimeSeries& quotes() const { + return timeSeries(); + } friend std::ostream& operator<<(std::ostream&, const CommodityIndex&); protected: @@ -91,7 +134,6 @@ namespace QuantLib { Currency currency_; Calendar calendar_; Real lotQuantity_; - TimeSeries quotes_; ext::shared_ptr forwardCurve_; Real forwardCurveUomConversionFactor_ = 1; ext::shared_ptr exchangeContracts_; @@ -113,6 +155,18 @@ namespace QuantLib { return name_; } + inline Calendar CommodityIndex::fixingCalendar() const { + return calendar_; + } + + inline bool CommodityIndex::isValidFixingDate(const Date& fixingDate) const { + return fixingCalendar().isBusinessDay(fixingDate); + } + + inline Real CommodityIndex::fixing(const Date& date, bool) const { + return pastFixing(date); + } + inline const CommodityType& CommodityIndex::commodityType() const { return commodityType_; } @@ -125,10 +179,6 @@ namespace QuantLib { return currency_; } - inline const Calendar& CommodityIndex::calendar() const { - return calendar_; - } - inline Real CommodityIndex::lotQuantity() const { return lotQuantity_; } @@ -138,21 +188,6 @@ namespace QuantLib { return forwardCurve_; } - inline const TimeSeries& CommodityIndex::quotes() const { - return quotes_; - } - - inline Real CommodityIndex::price(const Date& date) { - auto hq = quotes_.find(date); - if (hq->second == Null()) { - ++hq; - if (hq == quotes_.end()) - //if (hq->second == Null()) - return Null(); - } - return hq->second; - } - inline Real CommodityIndex::forwardPrice(const Date& date) const { try { Real forwardPrice = @@ -165,13 +200,11 @@ namespace QuantLib { } inline Date CommodityIndex::lastQuoteDate() const { - if (quotes_.empty()) - return Date::minDate(); - return quotes_.lastDate(); + return timeSeries().lastDate(); } inline bool CommodityIndex::empty() const { - return quotes_.empty(); + return timeSeries().empty(); } inline bool CommodityIndex::forwardCurveEmpty() const { @@ -180,24 +213,6 @@ namespace QuantLib { return false; } - inline void CommodityIndex::addQuote(const Date& quoteDate, Real quote) { - //QL_REQUIRE(isValidQuoteDate(quoteDate), - // "Quote date " << quoteDate.weekday() << ", " << - // quoteDate << " is not valid"); - std::string tag = name(); - quotes_ = IndexManager::instance().getHistory(tag); - quotes_[quoteDate] = quote; - IndexManager::instance().setHistory(tag, quotes_); - } - - inline void CommodityIndex::clearQuotes() const { - IndexManager::instance().clearHistory(name()); - } - - inline bool CommodityIndex::isValidQuoteDate(const Date& quoteDate) const { - return calendar().isBusinessDay(quoteDate); - } - } #endif diff --git a/ql/experimental/commodities/energybasisswap.cpp b/ql/experimental/commodities/energybasisswap.cpp index afb0d3e086a..6dcffbe3d3b 100644 --- a/ql/experimental/commodities/energybasisswap.cpp +++ b/ql/experimental/commodities/energybasisswap.cpp @@ -170,8 +170,8 @@ namespace QuantLib { Real receiveQuoteValue = 0; if (stepDate <= lastQuoteDate) { - payQuoteValue = payIndex_->price(stepDate); - receiveQuoteValue = receiveIndex_->price(stepDate); + payQuoteValue = payIndex_->fixing(stepDate); + receiveQuoteValue = receiveIndex_->fixing(stepDate); } else { payQuoteValue = payIndex_->forwardPrice(stepDate); receiveQuoteValue = diff --git a/ql/experimental/commodities/energyfuture.cpp b/ql/experimental/commodities/energyfuture.cpp index 49c65ebd968..c02647e9bbb 100644 --- a/ql/experimental/commodities/energyfuture.cpp +++ b/ql/experimental/commodities/energyfuture.cpp @@ -76,7 +76,7 @@ namespace QuantLib { Date lastQuoteDate = index_->lastQuoteDate(); if (lastQuoteDate >= evaluationDate - 1) { - quoteValue = index_->price(evaluationDate); + quoteValue = index_->fixing(evaluationDate); } else { quoteValue = index_->forwardPrice(evaluationDate); std::ostringstream message; diff --git a/ql/experimental/commodities/energyvanillaswap.cpp b/ql/experimental/commodities/energyvanillaswap.cpp index ad5825288b6..7b073eeb5cd 100644 --- a/ql/experimental/commodities/energyvanillaswap.cpp +++ b/ql/experimental/commodities/energyvanillaswap.cpp @@ -140,7 +140,7 @@ namespace QuantLib { Real quoteValue = 0; if (stepDate <= lastQuoteDate) { - quoteValue = index_->price(stepDate); + quoteValue = index_->fixing(stepDate); } else { quoteValue = index_->forwardPrice(stepDate); }