QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
commoditycurve.hpp
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
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
35namespace QuantLib {
36
39 friend class CommodityIndex;
40 public:
41 // constructor
42 CommodityCurve(std::string name,
46 const Calendar& calendar,
47 const std::vector<Date>& dates,
48 std::vector<Real> prices,
50
51 CommodityCurve(std::string name,
55 const Calendar& calendar,
57
59
60 const std::string& name() 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;
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_;
96 mutable std::vector<Date> dates_;
97 mutable std::vector<Time> times_;
98 mutable std::vector<Real> data_;
101 ext::shared_ptr<CommodityCurve> basisOfCurve_;
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
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>&
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(dates_[i], 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
157 const Date& d,
158 const ext::shared_ptr<ExchangeContracts>& exchangeContracts,
159 Integer nearbyOffset) const {
160 Date date = nearbyOffset > 0 ?
161 underlyingPriceDate(d, exchangeContracts, nearbyOffset) : d;
162 Time t = timeFromReference(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
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(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
194 if (basisOfCurve_ != nullptr) {
195 Real basisCurvePriceValue = 0;
196 try {
197 basisCurvePriceValue =
198 basisOfCurve_->priceImpl(t)
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
210 return interpolation_(t, true);
211 }
212
213}
214
215
216#endif
Actual/365 (Fixed) day count convention.
calendar class
Definition: calendar.hpp:61
Commodity term structure.
Real basisOfPrice(const Date &d) const
const ext::shared_ptr< CommodityCurve > & basisOfCurve() const
void setBasisOfCurve(const ext::shared_ptr< CommodityCurve > &basisOfCurve)
const std::string & name() const
const std::vector< Real > & prices() const
std::vector< Date > dates_
friend std::ostream & operator<<(std::ostream &out, const CommodityCurve &curve)
Date underlyingPriceDate(const Date &date, const ext::shared_ptr< ExchangeContracts > &exchangeContracts, Integer nearbyOffset) const
const std::vector< Date > & dates() const
const Currency & currency() const
std::vector< Time > times_
std::vector< std::pair< Date, Real > > nodes() const
const std::vector< Time > & times() const
Real priceImpl(Time t) const
Date maxDate() const override
the latest date for which the curve can return values
void setPrices(std::map< Date, Real > &prices)
Real price(const Date &d, const ext::shared_ptr< ExchangeContracts > &exchangeContracts, Integer nearbyOffset) const
const CommodityType & commodityType() const
Real basisOfPriceImpl(Time t) const
std::vector< Real > data_
const UnitOfMeasure & unitOfMeasure() const
ext::shared_ptr< CommodityCurve > basisOfCurve_
base class for commodity indexes
Currency specification
Definition: currency.hpp:36
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Forward-flat interpolation factory and traits.
base class for 1-D interpolations.
Basic term-structure functionality.
virtual Calendar calendar() const
the calendar used for reference and/or option date calculation
Time timeFromReference(const Date &date) const
date/time conversion
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
Unit of measure specification
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
bool operator==(const Currency &c1, const Currency &c2)
Definition: currency.hpp:191