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

Commodity average basis price curve. More...

#include <qle/termstructures/commodityaveragebasispricecurve.hpp>

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

Public Member Functions

Constructors
 CommodityAverageBasisPriceCurve (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 > &index, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &baseFec, bool addBasis=true, 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_
 
QuantLib::Leg baseLeg_
 The averaging 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::CommodityAverageBasisPriceCurve< Interpolator >

Commodity average 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. The base curve is averaged over the period defined the basis quote.

Definition at line 47 of file commodityaveragebasispricecurve.hpp.

Constructor & Destructor Documentation

◆ CommodityAverageBasisPriceCurve()

CommodityAverageBasisPriceCurve ( 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 > &  index,
const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &  baseFec,
bool  addBasis = true,
bool  priceAsHistFixing = true,
const Interpolator &  interpolator = Interpolator() 
)

Curve constructed from dates and quotes.

Definition at line 119 of file commodityaveragebasispricecurve.hpp.

124 : CommodityBasisPriceTermStructure(referenceDate, basisFec, index, baseFec, addBasis, 0, true, priceAsHistFixing),
125 QuantLib::InterpolatedCurve<Interpolator>(interpolator), basisData_(basisData) {
126 QL_REQUIRE(baseIndex_ != nullptr && !baseIndex_->priceCurve().empty(),
127 "CommodityAverageBasisPriceCurve requires baseIndex with priceCurve");
128 using QuantLib::Date;
129 using QuantLib::Schedule;
130 using QuantLib::io::iso_date;
131 using QuantLib::io::ordinal;
132 using std::distance;
133 using std::find;
134 using std::max;
135 using std::sort;
136 using std::vector;
137
138 // Observe the quotes
139 for (auto it = basisData_.cbegin(); it != basisData_.cend();) {
140 if (it->first < referenceDate) {
141 // If the basis quote's date is before the reference date, remove it.
142 basisData_.erase(it++);
143 } else {
144 // Otherwise, process the basis quote
145 dates_.push_back(it->first);
146 basisTimes_.push_back(timeFromReference(it->first));
147 if (addBasis_)
148 basisValues_.push_back(it->second->value());
149 else
150 basisValues_.push_back(-it->second->value());
151 registerWith(it->second);
152 ++it;
153 }
154 }
155
156 // Set up the interpolation to be used on the basis
157 basisInterpolation_ = interpolator.interpolate(basisTimes_.begin(), basisTimes_.end(), basisValues_.begin());
158
159 // Initialise this curve's times with the basis pillars. We will add more pillars below.
160 this->times_ = basisTimes_;
161
162 // Get the first basis contract expiry date strictly prior to the curve reference date.
163 Date start = basisFec_->priorExpiry(false, referenceDate);
164
165 // Get the first basis contract expiry date on or after the max date. Here, max date is defined as the maximum of
166 // 1) last pillar date of base price curve and 2) basis curve data.
167 Date maxDate = max(baseIndex_->priceCurve()->maxDate(), basisData_.rbegin()->first);
168 Date end = basisFec_->nextExpiry(true, maxDate);
169
170 // Create the leg schedule using a vector of dates which are the successive basis contract expiry dates
171 QL_REQUIRE(start < end, "Expected that the start date, " << io::iso_date(start)
172 << ", would be strictly less than the end date, "
173 << io::iso_date(end) << ".");
174 vector<Date> expiries{ start + 1 * Days };
175 vector<Time> scheduleTimes;
176 while (start < end) {
177 start = basisFec_->nextExpiry(true, start + 1 * Days);
178 expiries.push_back(start);
179 Time t = timeFromReference(start);
180 // Only add to this->times_ if it is not already there. We can use dates_ for this check.
181 if (find(dates_.begin(), dates_.end(), start) == dates_.end()) {
182 this->times_.push_back(t);
183 dates_.push_back(start);
184 }
185 scheduleTimes.push_back(t);
186 }
187 QL_REQUIRE(start == end, "Expected that the start date, " << io::iso_date(start) << ", to equal the end date, "
188 << io::iso_date(end)
189 << ", after creating the sequence of expiry dates.");
190
191 // Sort the times and dates vector and ensure no duplicates in the times vector.
192 sort(this->times_.begin(), this->times_.end());
193 sort(dates_.begin(), dates_.end());
194 auto it = unique(this->times_.begin(), this->times_.end(), [](double s, double t) { return close(s, t); });
195 QL_REQUIRE(it == this->times_.end(), "Unexpected duplicate time, " << *it << ", in the times vector.");
196 this->data_.resize(this->times_.size());
197
198 // Populate the leg of cashflows.
199 baseLeg_ = CommodityIndexedAverageLeg(Schedule(expiries), baseIndex_)
200 .withFutureExpiryCalculator(baseFec_)
201 .useFuturePrice(true)
202 .withQuantities(1.0);
203 QL_REQUIRE(baseLeg_.size() == scheduleTimes.size(), "Unexpected number of averaging cashflows in the leg: "
204 << "got " << baseLeg_.size() << " but expected "
205 << scheduleTimes.size());
206
207 // Populate the legIndexMap_
208 for (Size i = 0; i < this->times_.size(); i++) {
209 for (Size j = 0; j < scheduleTimes.size(); j++) {
210
211 if (this->times_[i] < scheduleTimes[j] || close(this->times_[i], scheduleTimes[j])) {
212 QL_REQUIRE(legIndexMap_.find(i) == legIndexMap_.end(),
213 "Should not already have a mapping for the " << ordinal(i) << " time.");
214 legIndexMap_[i] = j;
215 break;
216 }
217
218 if (j == scheduleTimes.size()) {
219 QL_FAIL("Could not map the " << ordinal(i) << " time, " << this->times_[i] << ", to a cashflow.");
220 }
221 }
222 }
223
224 // Set up the underlying interpolation on times_ and data_
225 QuantLib::InterpolatedCurve<Interpolator>::setupInterpolation();
226}
std::map< QuantLib::Size, QuantLib::Size > legIndexMap_
std::map< QuantLib::Date, QuantLib::Handle< QuantLib::Quote > > basisData_
QuantLib::Leg baseLeg_
The averaging cashflows will give the base curve prices.
QuantLib::ext::shared_ptr< FutureExpiryCalculator > baseFec_
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)
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 228 of file commodityaveragebasispricecurve.hpp.

228 {
229 QuantLib::LazyObject::update();
230}

◆ performCalculations()

void performCalculations
override

Definition at line 232 of file commodityaveragebasispricecurve.hpp.

232 {
233
234 // Update the basis interpolation object
235 Size basisIdx = 0;
236 for (const auto& kv : basisData_) {
237 basisValues_[basisIdx] = addBasis_ ? kv.second->value() : -kv.second->value();
238 basisIdx++;
239 }
240 basisInterpolation_.update();
241
242 // Update this curve's interpolation
243 for (Size i = 0; i < this->times_.size(); i++) {
244
245 Real baseValue = baseLeg_[legIndexMap_.at(i)]->amount();
246
247 // Get the basis with flat extrapolation
248 Real basis = 0.0;
249 if (this->times_[i] < basisTimes_.front()) {
250 basis = basisValues_.front();
251 } else if (this->times_[i] > basisTimes_.back()) {
252 basis = basisValues_.back();
253 } else {
254 basis = basisInterpolation_(this->times_[i], true);
255 }
256
257 // Update the outright value
258 this->data_[i] = baseValue + basis;
259 }
260 this->interpolation_.update();
261}

◆ maxDate()

QuantLib::Date maxDate
override

Definition at line 263 of file commodityaveragebasispricecurve.hpp.

263 {
264 return dates_.back();
265}
+ Here is the caller graph for this function:

◆ maxTime()

QuantLib::Time maxTime
override

Definition at line 267 of file commodityaveragebasispricecurve.hpp.

267 {
268 return this->times_.back();
269}

◆ minTime()

QuantLib::Time minTime
overridevirtual

The minimum time for which the curve can return values.

Reimplemented from PriceTermStructure.

Definition at line 271 of file commodityaveragebasispricecurve.hpp.

271 {
272 return this->times_.front();
273}

◆ pillarDates()

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

The pillar dates for the PriceTermStructure.

Implements PriceTermStructure.

Definition at line 276 of file commodityaveragebasispricecurve.hpp.

276 {
277 return dates_;
278}

◆ currency()

const QuantLib::Currency & currency ( ) const
overridevirtual

The currency in which prices are expressed.

Implements PriceTermStructure.

Definition at line 82 of file commodityaveragebasispricecurve.hpp.

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

◆ times()

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

Definition at line 87 of file commodityaveragebasispricecurve.hpp.

87{ return this->times_; }

◆ prices()

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

Definition at line 88 of file commodityaveragebasispricecurve.hpp.

88{ return this->data_; }

◆ priceImpl()

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

Price calculation.

Implements PriceTermStructure.

Definition at line 281 of file commodityaveragebasispricecurve.hpp.

281 {
282 calculate();
283 return this->interpolation_(t, true);
284}

Member Data Documentation

◆ basisData_

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

Definition at line 98 of file commodityaveragebasispricecurve.hpp.

◆ dates_

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

Definition at line 100 of file commodityaveragebasispricecurve.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 105 of file commodityaveragebasispricecurve.hpp.

◆ basisValues_

std::vector<Real> basisValues_
mutableprivate

Definition at line 106 of file commodityaveragebasispricecurve.hpp.

◆ basisInterpolation_

Interpolation basisInterpolation_
mutableprivate

Definition at line 107 of file commodityaveragebasispricecurve.hpp.

◆ baseLeg_

QuantLib::Leg baseLeg_
private

The averaging cashflows will give the base curve prices.

Definition at line 110 of file commodityaveragebasispricecurve.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 115 of file commodityaveragebasispricecurve.hpp.