Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
commodityindex.cpp
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/equityindex.cpp
20 \brief equity index class for holding equity fixing histories and forwarding.
21 \ingroup indexes
22*/
23
25#include <boost/make_shared.hpp>
26#include <string>
27
28using std::string;
30
31namespace QuantExt {
32
33CommodityIndex::CommodityIndex(const std::string& underlyingName, const Date& expiryDate,
34 const Calendar& fixingCalendar, const Handle<QuantExt::PriceTermStructure>& curve)
35 : underlyingName_(underlyingName), expiryDate_(expiryDate), fixingCalendar_(fixingCalendar),
36 curve_(curve), keepDays_(false) {
37 init();
38}
39
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) {
44 init();
45}
46
47void CommodityIndex::init() {
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}
70
71Real CommodityIndex::fixing(const Date& fixingDate, bool forecastTodaysFixing) const {
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}
104
105Real CommodityIndex::pastFixing(const Date& fixingDate) const {
106 QL_REQUIRE(isValidFixingDate(fixingDate), fixingDate << " is not a valid fixing date");
107 return timeSeries()[fixingDate];
108}
109
110Real CommodityIndex::forecastFixing(const Time& fixingTime) const {
111 if (isFuturesIndex_)
112 return curve_->price(expiryDate_);
113 else
114 return curve_->price(fixingTime);
115}
116Real CommodityIndex::forecastFixing(const Date& fixingDate) const {
117 if (isFuturesIndex_)
118 return curve_->price(expiryDate_);
119 else
120 return curve_->price(fixingDate);
121}
122
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);
127}
128
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);
134}
135
136} // namespace QuantExt
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.