QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
commoditycurve.hpp
Go to the documentation of this file.
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>
31#include <ql/currency.hpp>
34
35namespace QuantLib {
36
37 //! Commodity term structure
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
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;
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:
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
115 return commodityType_;
116 }
117
119 return unitOfMeasure_;
120 }
121
122 inline const Currency& CommodityCurve::currency() const {
123 return currency_;
124 }
125
126 inline const std::string& CommodityCurve::name() const {
127 return name_;
128 }
129
131 return dates_.back();
132 }
133
134 inline const std::vector<Time>& CommodityCurve::times() const {
135 return times_;
136 }
137
138 inline const std::vector<Date>& CommodityCurve::dates() const {
139 return dates_;
140 }
141
142 inline const std::vector<Real>& CommodityCurve::prices() const {
143 return data_;
144 }
145
146 inline bool CommodityCurve::empty() const {
147 return dates_.empty();
148 }
149
150 inline const ext::shared_ptr<CommodityCurve>&
152 return basisOfCurve_;
153 }
154
155 inline std::vector<std::pair<Date,Real> > CommodityCurve::nodes() const {
156 std::vector<std::pair<Date,Real> > results(dates_.size());
157 for (Size i = 0; i < dates_.size(); ++i)
158 results[i] = std::make_pair(dates_[i], data_[i]);
159 return results;
160 }
161
164 return basisOfPriceImpl(t);
165 }
166
167 // gets a price that can include an arbitrary number of basis curves
169 const Date& d,
170 const ext::shared_ptr<ExchangeContracts>& exchangeContracts,
171 Integer nearbyOffset) const {
172 Date date = nearbyOffset > 0 ?
173 underlyingPriceDate(d, exchangeContracts, nearbyOffset) : d;
174 Time t = timeFromReference(date);
175 Real priceValue = 0;
176 try {
177 priceValue = priceImpl(t);
178 } catch (const std::exception& e) {
179 QL_FAIL("error retrieving price for curve [" << name() << "]: "
180 << e.what());
181 }
182 return priceValue + basisOfPriceImpl(t);
183 }
184
185 // get the date for the underlying price, in the case of nearby
186 // curves, rolls on the underlying contract expiry
188 const Date& date,
189 const ext::shared_ptr<ExchangeContracts>& exchangeContracts,
190 Integer nearbyOffset) const {
191 QL_REQUIRE(nearbyOffset > 0, "nearby offset must be > 0");
192 ExchangeContracts::const_iterator ic =
193 exchangeContracts->lower_bound(date);
194 if (ic != exchangeContracts->end()) {
195 for (int i = 0; i < nearbyOffset-1 && ic!=exchangeContracts->end(); ++i)
196 ++ic;
197 QL_REQUIRE(ic != exchangeContracts->end(),
198 "not enough nearby contracts available for curve ["
199 << name() << "] for date [" << date << "].");
200 return ic->second.underlyingStartDate();
201 }
202 return date;
203 }
204
206 if (basisOfCurve_ != nullptr) {
207 Real basisCurvePriceValue = 0;
208 try {
209 basisCurvePriceValue =
210 basisOfCurve_->priceImpl(t)
212 } catch (const std::exception& e) {
213 QL_FAIL("error retrieving price for curve [" << name() <<
214 "]: " << e.what());
215 }
216 return basisCurvePriceValue + basisOfCurve_->basisOfPriceImpl(t);
217 }
218 return 0;
219 }
220
222 return interpolation_(t, true);
223 }
224
225}
226
227
228#endif
Actual/365 (Fixed) day counter.
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
commodity type
Currency specification.
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Exchange contract.
Date d
forward-flat interpolation between discrete points
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:231
base class for term structures
Unit of measure.