Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherit CommodityIndex from Index #2094

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ql/experimental/commodities/commodityindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down
135 changes: 75 additions & 60 deletions ql/experimental/commodities/commodityindex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
#define quantlib_commodity_index_hpp

#include <ql/experimental/commodities/commoditycurve.hpp>
#include <ql/indexes/indexmanager.hpp>
#include <ql/index.hpp>

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,
Expand All @@ -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
//@{
Expand All @@ -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<CommodityCurve>& 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<Date, Real>& quotes) {
std::string tag = name();
quotes_ = IndexManager::instance().getHistory(tag);
for (auto quote : quotes) {
quotes_[quote.first] = quote.second;
addFixing(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<Real>& 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<Real>& quotes() const {
return timeSeries();
}

friend std::ostream& operator<<(std::ostream&, const CommodityIndex&);
protected:
Expand All @@ -91,7 +134,6 @@ namespace QuantLib {
Currency currency_;
Calendar calendar_;
Real lotQuantity_;
TimeSeries<Real> quotes_;
ext::shared_ptr<CommodityCurve> forwardCurve_;
Real forwardCurveUomConversionFactor_ = 1;
ext::shared_ptr<ExchangeContracts> exchangeContracts_;
Expand All @@ -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_;
}
Expand All @@ -125,10 +179,6 @@ namespace QuantLib {
return currency_;
}

inline const Calendar& CommodityIndex::calendar() const {
return calendar_;
}

inline Real CommodityIndex::lotQuantity() const {
return lotQuantity_;
}
Expand All @@ -138,21 +188,6 @@ namespace QuantLib {
return forwardCurve_;
}

inline const TimeSeries<Real>& CommodityIndex::quotes() const {
return quotes_;
}

inline Real CommodityIndex::price(const Date& date) {
auto hq = quotes_.find(date);
if (hq->second == Null<Real>()) {
++hq;
if (hq == quotes_.end())
//if (hq->second == Null<Real>())
return Null<Real>();
}
return hq->second;
}

inline Real CommodityIndex::forwardPrice(const Date& date) const {
try {
Real forwardPrice =
Expand All @@ -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 {
Expand All @@ -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
4 changes: 2 additions & 2 deletions ql/experimental/commodities/energybasisswap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion ql/experimental/commodities/energyfuture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ql/experimental/commodities/energyvanillaswap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading