Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
List of all members
CommodityBasisPriceCurve< Interpolator > Class Template Reference

Commodity basis price curve. More...

#include <qle/termstructures/commoditybasispricecurve.hpp>

+ Inheritance diagram for CommodityBasisPriceCurve< Interpolator >:
+ Collaboration diagram for CommodityBasisPriceCurve< Interpolator >:

Public Member Functions

Constructors
 CommodityBasisPriceCurve (const QuantLib::Date &referenceDate, const std::map< QuantLib::Date, QuantLib::Handle< QuantLib::Quote > > &basisData, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &basisFec, const QuantLib::ext::shared_ptr< CommodityIndex > &baseIndex, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &baseFec, bool addBasis=true, QuantLib::Size monthOffset=0, bool priceAsHistFixing=true, const Interpolator &interpolator=Interpolator())
 Curve constructed from dates and quotes. More...
 
Observer interface
void update () override
 
LazyObject interface
void performCalculations () const override
 
TermStructure interface
QuantLib::Date maxDate () const override
 
QuantLib::Time maxTime () const override
 
PriceTermStructure interface
QuantLib::Time minTime () const override
 The minimum time for which the curve can return values. More...
 
std::vector< QuantLib::Date > pillarDates () const override
 The pillar dates for the PriceTermStructure. More...
 
const QuantLib::Currency & currency () const override
 The currency in which prices are expressed. More...
 
Inspectors
const std::vector< QuantLib::Time > & times () const
 
const std::vector< QuantLib::Real > & prices () const
 
- Public Member Functions inherited from CommodityBasisPriceTermStructure
 CommodityBasisPriceTermStructure (const QuantLib::Date &referenceDate, const QuantLib::Calendar &cal, const QuantLib::DayCounter &dc, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &basisFec, const QuantLib::ext::shared_ptr< CommodityIndex > &baseIndex, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &baseFec, bool addBasis=true, QuantLib::Size monthOffset=0, bool averagingBaseCashflow=false, bool priceAsHistoricalFixing=true)
 
 CommodityBasisPriceTermStructure (const QuantLib::Date &referenceDate, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &basisFec, const QuantLib::ext::shared_ptr< CommodityIndex > &baseIndex, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &baseFec, bool addBasis=true, QuantLib::Size monthOffset=0, bool averagingBaseCashflow=false, bool priceAsHistoricalFixing=true)
 
const QuantLib::ext::shared_ptr< FutureExpiryCalculator > & basisFutureExpiryCalculator () const
 Inspectors. More...
 
const QuantLib::ext::shared_ptr< CommodityIndex > & baseIndex () const
 
const QuantLib::ext::shared_ptr< FutureExpiryCalculator > & baseFutureExpiryCalculator () const
 
bool addBasis () const
 
bool averagingBaseCashflow () const
 
bool priceAsHistoricalFixing () const
 
QuantLib::Size monthOffset () const
 
- Public Member Functions inherited from PriceTermStructure
 PriceTermStructure (const QuantLib::DayCounter &dc=QuantLib::DayCounter())
 
 PriceTermStructure (const QuantLib::Date &referenceDate, const QuantLib::Calendar &cal=QuantLib::Calendar(), const QuantLib::DayCounter &dc=QuantLib::DayCounter())
 
 PriceTermStructure (QuantLib::Natural settlementDays, const QuantLib::Calendar &cal, const QuantLib::DayCounter &dc=QuantLib::DayCounter())
 
QuantLib::Real price (QuantLib::Time t, bool extrapolate=false) const
 
QuantLib::Real price (const QuantLib::Date &d, bool extrapolate=false) const
 
void update () override
 

PriceTermStructure implementation

std::map< QuantLib::Date, QuantLib::Handle< QuantLib::Quote > > basisData_
 
std::vector< QuantLib::Date > dates_
 
std::vector< Time > basisTimes_
 
std::vector< Real > basisValues_
 
Interpolation basisInterpolation_
 
std::map< QuantLib::Date, QuantLib::ext::shared_ptr< CashFlow > > baseLeg_
 The commodity cashflows will give the base curve prices. More...
 
std::map< QuantLib::Size, QuantLib::Size > legIndexMap_
 
QuantLib::Real priceImpl (QuantLib::Time t) const override
 Price calculation. More...
 

Additional Inherited Members

- Protected Member Functions inherited from PriceTermStructure
void checkRange (QuantLib::Time t, bool extrapolate) const
 Extra time range check for minimum time, then calls TermStructure::checkRange. More...
 
- Protected Attributes inherited from CommodityBasisPriceTermStructure
QuantLib::ext::shared_ptr< FutureExpiryCalculatorbasisFec_
 
QuantLib::ext::shared_ptr< CommodityIndexbaseIndex_
 
QuantLib::ext::shared_ptr< FutureExpiryCalculatorbaseFec_
 
bool addBasis_
 
QuantLib::Size monthOffset_
 
bool averagingBaseCashflow_
 
bool priceAsHistoricalFixing_
 

Detailed Description

template<class Interpolator>
class QuantExt::CommodityBasisPriceCurve< Interpolator >

Commodity basis price curve.

Class representing an outright commodity price curve created from a base price curve and a collection of basis quotes that are added to or subtracted from the base curve. This class is intended to be used only for commodity future basis price curves.

There is an assumption in the curve construction that the frequency of the base future contract is the same as the frequency of the basis future contract. In other words, if the base future contract is monthly then the basis future contract is monthly for example.

Definition at line 53 of file commoditybasispricecurve.hpp.

Constructor & Destructor Documentation

◆ CommodityBasisPriceCurve()

CommodityBasisPriceCurve ( const QuantLib::Date &  referenceDate,
const std::map< QuantLib::Date, QuantLib::Handle< QuantLib::Quote > > &  basisData,
const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &  basisFec,
const QuantLib::ext::shared_ptr< CommodityIndex > &  baseIndex,
const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &  baseFec,
bool  addBasis = true,
QuantLib::Size  monthOffset = 0,
bool  priceAsHistFixing = true,
const Interpolator &  interpolator = Interpolator() 
)

Curve constructed from dates and quotes.

Definition at line 125 of file commoditybasispricecurve.hpp.

130 : CommodityBasisPriceTermStructure(referenceDate, basisFec, baseIndex, baseFec, addBasis, monthOffset, false,
131 priceAsHistFixing),
132 QuantLib::InterpolatedCurve<Interpolator>(interpolator), basisData_(basisData) {
133 QL_REQUIRE(baseIndex != nullptr && !baseIndex->priceCurve().empty(),
134 "CommodityBasisPriceCurve requires baseIndex with priceCurve");
135 using QuantLib::Date;
136 using QuantLib::Schedule;
137 using QuantLib::io::iso_date;
138 using QuantLib::io::ordinal;
139 using std::distance;
140 using std::find;
141 using std::max;
142 using std::min;
143 using std::sort;
144 using std::vector;
145
146 // Observe the quotes
147 for (auto it = basisData_.cbegin(); it != basisData_.cend();) {
148 if (it->first < referenceDate) {
149 // If the basis quote's date is before the reference date, remove it.
150 basisData_.erase(it++);
151 } else {
152 // Otherwise, process the basis quote
153 dates_.push_back(it->first);
154 basisTimes_.push_back(timeFromReference(it->first));
155 if (addBasis_)
156 basisValues_.push_back(it->second->value());
157 else
158 basisValues_.push_back(-it->second->value());
159 registerWith(it->second);
160 ++it;
161 }
162 }
163
164 // Set up the interpolation to be used on the basis
165 basisInterpolation_ = interpolator.interpolate(basisTimes_.begin(), basisTimes_.end(), basisValues_.begin());
166
167 // Initialise this curve's times with the basis pillars. We will add more pillars below.
168 this->times_ = basisTimes_;
169
170 // Get the first basis contract expiry date on or after the curve reference date.
171 Date basisExpiry = basisFec_->nextExpiry(true, referenceDate);
172
173 // Get the first basis contract expiry date on or after the max date. Here, max date is defined as the maximum of
174 // 1) last pillar date of base price curve and 2) basis curve data.
175 Date maxDate = max(baseIndex_->priceCurve()->maxDate(), basisData_.rbegin()->first);
176 Date end = basisFec_->nextExpiry(true, maxDate);
177
178 // Populate the base cashflows.
179 while (basisExpiry <= end) {
180 Date basisContractDate = basisFec_->contractDate(basisExpiry);
181 basisContractDate = Date(1, basisContractDate.month(), basisContractDate.year()) - monthOffset_ * Months;
182 Date periodStart = basisContractDate - monthOffset_ * Months;
183 Date periodEnd = (periodStart + 1 * Months) - 1 * Days;
185 periodStart, periodEnd, baseIndex_, baseFec_, averagingBaseCashflow_);
186
187 // Only add to this->times_ if it is not already there. We can use dates_ for this check.
188 if (find(dates_.begin(), dates_.end(), basisExpiry) == dates_.end()) {
189 this->times_.push_back(timeFromReference(basisExpiry));
190 dates_.push_back(basisExpiry);
191 }
192
193 basisExpiry = basisFec_->nextExpiry(true, basisExpiry + 1 * Days);
194 }
195
196 // Sort the times and dates vector and ensure no duplicates in the times vector.
197 sort(this->times_.begin(), this->times_.end());
198 sort(dates_.begin(), dates_.end());
199 auto it = unique(this->times_.begin(), this->times_.end(), [](double s, double t) { return close(s, t); });
200 QL_REQUIRE(it == this->times_.end(), "Unexpected duplicate time, " << *it << ", in the times vector.");
201 this->data_.resize(this->times_.size());
202
203 // Set up the underlying interpolation on times_ and data_
204 QuantLib::InterpolatedCurve<Interpolator>::setupInterpolation();
205}
std::map< QuantLib::Date, QuantLib::ext::shared_ptr< CashFlow > > baseLeg_
The commodity cashflows will give the base curve prices.
std::map< QuantLib::Date, QuantLib::Handle< QuantLib::Quote > > basisData_
QuantLib::Date maxDate() const override
QuantLib::ext::shared_ptr< FutureExpiryCalculator > baseFec_
const QuantLib::ext::shared_ptr< CommodityIndex > & baseIndex() const
QuantLib::ext::shared_ptr< FutureExpiryCalculator > basisFec_
QuantLib::ext::shared_ptr< CommodityIndex > baseIndex_
CommodityBasisPriceTermStructure(const QuantLib::Date &referenceDate, const QuantLib::Calendar &cal, const QuantLib::DayCounter &dc, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &basisFec, const QuantLib::ext::shared_ptr< CommodityIndex > &baseIndex, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &baseFec, bool addBasis=true, QuantLib::Size monthOffset=0, bool averagingBaseCashflow=false, bool priceAsHistoricalFixing=true)
QuantLib::ext::shared_ptr< CashFlow > makeCommodityCashflowForBasisFuture(const QuantLib::Date &start, const QuantLib::Date &end, const QuantLib::ext::shared_ptr< CommodityIndex > &baseIndex, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &baseFec, bool baseIsAveraging, const QuantLib::Date &paymentDate)
Make a commodity indexed cashflow.
Definition: commodity.cpp:7
CompiledFormula max(CompiledFormula x, const CompiledFormula &y)
+ Here is the call graph for this function:

Member Function Documentation

◆ update()

void update
override

Definition at line 207 of file commoditybasispricecurve.hpp.

207{ QuantLib::LazyObject::update(); }

◆ performCalculations()

void performCalculations
override

Definition at line 209 of file commoditybasispricecurve.hpp.

209 {
210
211 // Update the basis interpolation object
212 Size basisIdx = 0;
213 for (const auto& kv : basisData_) {
214 basisValues_[basisIdx] = addBasis_ ? kv.second->value() : -kv.second->value();
215 basisIdx++;
216 }
217 basisInterpolation_.update();
218
219 // Update this curve's interpolation
220 for (Size i = 0; i < this->times_.size(); i++) {
221
222 Real baseValue = 0.0;
223 auto it = baseLeg_.find(dates_[i]);
224 if (it != baseLeg_.end()) {
225 // If we have associated the basis date with a base cashflow.
226 baseValue = it->second->amount();
227 } else {
228 // If we didn't associate the basis date with a base cashflow, just ask the base pts at t. This will have
229 // happened if the basis date that was passed in was not a basis contract expiry date wrt basisFec_.
230 baseValue = baseIndex_->priceCurve()->price(this->times_[i], true);
231 }
232
233 // Get the basis with flat extrapolation
234 Real basis = 0.0;
235 if (this->times_[i] < basisTimes_.front()) {
236 basis = basisValues_.front();
237 } else if (this->times_[i] > basisTimes_.back()) {
238 basis = basisValues_.back();
239 } else {
240 basis = basisInterpolation_(this->times_[i], true);
241 }
242
243 // Update the outright value
244 this->data_[i] = baseValue + basis;
245 }
246 this->interpolation_.update();
247}

◆ maxDate()

QuantLib::Date maxDate
override

Definition at line 249 of file commoditybasispricecurve.hpp.

249 {
250 return dates_.back();
251}
+ Here is the caller graph for this function:

◆ maxTime()

QuantLib::Time maxTime
override

Definition at line 253 of file commoditybasispricecurve.hpp.

253 {
254 return this->times_.back();
255}

◆ minTime()

QuantLib::Time minTime
overridevirtual

The minimum time for which the curve can return values.

Reimplemented from PriceTermStructure.

Definition at line 257 of file commoditybasispricecurve.hpp.

257 {
258 return this->times_.front();
259}

◆ pillarDates()

std::vector< QuantLib::Date > pillarDates ( ) const
overridevirtual

The pillar dates for the PriceTermStructure.

Implements PriceTermStructure.

Definition at line 261 of file commoditybasispricecurve.hpp.

261 {
262 return dates_;
263}

◆ currency()

const QuantLib::Currency & currency ( ) const
overridevirtual

The currency in which prices are expressed.

Implements PriceTermStructure.

Definition at line 89 of file commoditybasispricecurve.hpp.

89{ return baseIndex_->priceCurve()->currency(); }

◆ times()

const std::vector< QuantLib::Time > & times ( ) const

Definition at line 94 of file commoditybasispricecurve.hpp.

94{ return this->times_; }

◆ prices()

const std::vector< QuantLib::Real > & prices ( ) const

Definition at line 95 of file commoditybasispricecurve.hpp.

95{ return this->data_; }

◆ priceImpl()

QuantLib::Real priceImpl ( QuantLib::Time  ) const
overrideprotectedvirtual

Price calculation.

Implements PriceTermStructure.

Definition at line 265 of file commoditybasispricecurve.hpp.

265 {
266 calculate();
267 return this->interpolation_(t, true);
268}

Member Data Documentation

◆ basisData_

std::map<QuantLib::Date, QuantLib::Handle<QuantLib::Quote> > basisData_
private

Definition at line 105 of file commoditybasispricecurve.hpp.

◆ dates_

std::vector<QuantLib::Date> dates_
private

Definition at line 106 of file commoditybasispricecurve.hpp.

◆ basisTimes_

std::vector<Time> basisTimes_
mutableprivate

Interpolator used for interpolating the basis if needed. Basis interpolation uses the same interpolator as the curve itself. A second template parameter could be added for this in future if it needs to be relaxed.

Definition at line 111 of file commoditybasispricecurve.hpp.

◆ basisValues_

std::vector<Real> basisValues_
mutableprivate

Definition at line 112 of file commoditybasispricecurve.hpp.

◆ basisInterpolation_

Interpolation basisInterpolation_
mutableprivate

Definition at line 113 of file commoditybasispricecurve.hpp.

◆ baseLeg_

std::map<QuantLib::Date, QuantLib::ext::shared_ptr<CashFlow> > baseLeg_
private

The commodity cashflows will give the base curve prices.

Definition at line 116 of file commoditybasispricecurve.hpp.

◆ legIndexMap_

std::map<QuantLib::Size, QuantLib::Size> legIndexMap_
private

Map where the key is the index of a time in the times_ vector and the value is the index of the cashflow in the baseLeg_ to associate with that time.

Definition at line 121 of file commoditybasispricecurve.hpp.