| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2008 J. Erik Radmall |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | /*! \file commoditycurve.hpp |
| 21 | \brief Commodity curve |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_commodity_curve_hpp |
| 25 | #define quantlib_commodity_curve_hpp |
| 26 | |
| 27 | #include <ql/termstructure.hpp> |
| 28 | #include <ql/experimental/commodities/commoditytype.hpp> |
| 29 | #include <ql/experimental/commodities/unitofmeasure.hpp> |
| 30 | #include <ql/experimental/commodities/exchangecontract.hpp> |
| 31 | #include <ql/currency.hpp> |
| 32 | #include <ql/math/interpolations/forwardflatinterpolation.hpp> |
| 33 | #include <ql/time/daycounters/actual365fixed.hpp> |
| 34 | |
| 35 | namespace QuantLib { |
| 36 | |
| 37 | //! Commodity term structure |
| 38 | class CommodityCurve : public TermStructure { |
| 39 | friend class CommodityIndex; |
| 40 | public: |
| 41 | // constructor |
| 42 | CommodityCurve(std::string name, |
| 43 | CommodityType commodityType, |
| 44 | Currency currency, |
| 45 | UnitOfMeasure unitOfMeasure, |
| 46 | const Calendar& calendar, |
| 47 | const std::vector<Date>& dates, |
| 48 | std::vector<Real> prices, |
| 49 | const DayCounter& dayCounter = Actual365Fixed()); |
| 50 | |
| 51 | CommodityCurve(std::string name, |
| 52 | CommodityType commodityType, |
| 53 | Currency currency, |
| 54 | UnitOfMeasure unitOfMeasure, |
| 55 | const Calendar& calendar, |
| 56 | const DayCounter& dayCounter = Actual365Fixed()); |
| 57 | |
| 58 | //! \name Inspectors |
| 59 | //@{ |
| 60 | const std::string& name() const; |
| 61 | const CommodityType& commodityType() const; |
| 62 | const UnitOfMeasure& unitOfMeasure() const; |
| 63 | const Currency& currency() const; |
| 64 | Date maxDate() const override; |
| 65 | const std::vector<Time>& times() const; |
| 66 | const std::vector<Date>& dates() const; |
| 67 | const std::vector<Real>& prices() const; |
| 68 | std::vector<std::pair<Date,Real> > nodes() const; |
| 69 | bool empty() const; |
| 70 | |
| 71 | void setPrices(std::map<Date, Real>& prices); |
| 72 | void setBasisOfCurve( |
| 73 | const ext::shared_ptr<CommodityCurve>& basisOfCurve); |
| 74 | |
| 75 | Real price( |
| 76 | const Date& d, |
| 77 | const ext::shared_ptr<ExchangeContracts>& exchangeContracts, |
| 78 | Integer nearbyOffset) const; |
| 79 | Real basisOfPrice(const Date& d) const; |
| 80 | Date underlyingPriceDate( |
| 81 | const Date& date, |
| 82 | const ext::shared_ptr<ExchangeContracts>& exchangeContracts, |
| 83 | Integer nearbyOffset) const; |
| 84 | |
| 85 | const ext::shared_ptr<CommodityCurve>& basisOfCurve() const; |
| 86 | |
| 87 | friend std::ostream& operator<<(std::ostream& out, |
| 88 | const CommodityCurve& curve); |
| 89 | protected: |
| 90 | Real basisOfPriceImpl(Time t) const; |
| 91 | |
| 92 | std::string name_; |
| 93 | CommodityType commodityType_; |
| 94 | UnitOfMeasure unitOfMeasure_; |
| 95 | Currency currency_; |
| 96 | mutable std::vector<Date> dates_; |
| 97 | mutable std::vector<Time> times_; |
| 98 | mutable std::vector<Real> data_; |
| 99 | mutable Interpolation interpolation_; |
| 100 | ForwardFlat interpolator_; |
| 101 | ext::shared_ptr<CommodityCurve> basisOfCurve_; |
| 102 | Real basisOfCurveUomConversionFactor_; |
| 103 | |
| 104 | Real priceImpl(Time t) const; |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | // inline definitions |
| 109 | |
| 110 | inline bool operator==(const CommodityCurve& c1, const CommodityCurve& c2) { |
| 111 | return c1.name() == c2.name(); |
| 112 | } |
| 113 | |
| 114 | inline const std::string& CommodityCurve::name() const { |
| 115 | return name_; |
| 116 | } |
| 117 | |
| 118 | inline Date CommodityCurve::maxDate() const { |
| 119 | return dates_.back(); |
| 120 | } |
| 121 | |
| 122 | inline const std::vector<Time>& CommodityCurve::times() const { |
| 123 | return times_; |
| 124 | } |
| 125 | |
| 126 | inline const std::vector<Date>& CommodityCurve::dates() const { |
| 127 | return dates_; |
| 128 | } |
| 129 | |
| 130 | inline const std::vector<Real>& CommodityCurve::prices() const { |
| 131 | return data_; |
| 132 | } |
| 133 | |
| 134 | inline bool CommodityCurve::empty() const { |
| 135 | return dates_.empty(); |
| 136 | } |
| 137 | |
| 138 | inline const ext::shared_ptr<CommodityCurve>& |
| 139 | CommodityCurve::basisOfCurve() const { |
| 140 | return basisOfCurve_; |
| 141 | } |
| 142 | |
| 143 | inline std::vector<std::pair<Date,Real> > CommodityCurve::nodes() const { |
| 144 | std::vector<std::pair<Date,Real> > results(dates_.size()); |
| 145 | for (Size i = 0; i < dates_.size(); ++i) |
| 146 | results[i] = std::make_pair(x&: dates_[i], y&: data_[i]); |
| 147 | return results; |
| 148 | } |
| 149 | |
| 150 | inline Real CommodityCurve::basisOfPrice(const Date& d) const { |
| 151 | Time t = timeFromReference(d); |
| 152 | return basisOfPriceImpl(t); |
| 153 | } |
| 154 | |
| 155 | // gets a price that can include an arbitrary number of basis curves |
| 156 | inline Real CommodityCurve::price( |
| 157 | const Date& d, |
| 158 | const ext::shared_ptr<ExchangeContracts>& exchangeContracts, |
| 159 | Integer nearbyOffset) const { |
| 160 | Date date = nearbyOffset > 0 ? |
| 161 | underlyingPriceDate(date: d, exchangeContracts, nearbyOffset) : d; |
| 162 | Time t = timeFromReference(d: date); |
| 163 | Real priceValue = 0; |
| 164 | try { |
| 165 | priceValue = priceImpl(t); |
| 166 | } catch (const std::exception& e) { |
| 167 | QL_FAIL("error retrieving price for curve [" << name() << "]: " |
| 168 | << e.what()); |
| 169 | } |
| 170 | return priceValue + basisOfPriceImpl(t); |
| 171 | } |
| 172 | |
| 173 | // get the date for the underlying price, in the case of nearby |
| 174 | // curves, rolls on the underlying contract expiry |
| 175 | inline Date CommodityCurve::underlyingPriceDate( |
| 176 | const Date& date, |
| 177 | const ext::shared_ptr<ExchangeContracts>& exchangeContracts, |
| 178 | Integer nearbyOffset) const { |
| 179 | QL_REQUIRE(nearbyOffset > 0, "nearby offset must be > 0" ); |
| 180 | ExchangeContracts::const_iterator ic = |
| 181 | exchangeContracts->lower_bound(x: date); |
| 182 | if (ic != exchangeContracts->end()) { |
| 183 | for (int i = 0; i < nearbyOffset-1 && ic!=exchangeContracts->end(); ++i) |
| 184 | ++ic; |
| 185 | QL_REQUIRE(ic != exchangeContracts->end(), |
| 186 | "not enough nearby contracts available for curve [" |
| 187 | << name() << "] for date [" << date << "]." ); |
| 188 | return ic->second.underlyingStartDate(); |
| 189 | } |
| 190 | return date; |
| 191 | } |
| 192 | |
| 193 | inline Real CommodityCurve::basisOfPriceImpl(Time t) const { |
| 194 | if (basisOfCurve_ != nullptr) { |
| 195 | Real basisCurvePriceValue = 0; |
| 196 | try { |
| 197 | basisCurvePriceValue = |
| 198 | basisOfCurve_->priceImpl(t) |
| 199 | * basisOfCurveUomConversionFactor_; |
| 200 | } catch (const std::exception& e) { |
| 201 | QL_FAIL("error retrieving price for curve [" << name() << |
| 202 | "]: " << e.what()); |
| 203 | } |
| 204 | return basisCurvePriceValue + basisOfCurve_->basisOfPriceImpl(t); |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | inline Real CommodityCurve::priceImpl(Time t) const { |
| 210 | return interpolation_(t, true); |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | |
| 216 | #endif |
| 217 | |