Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
CommodityIndexedAverageCashFlow Class Reference

#include <qle/cashflows/commodityindexedaveragecashflow.hpp>

+ Inheritance diagram for CommodityIndexedAverageCashFlow:
+ Collaboration diagram for CommodityIndexedAverageCashFlow:

Public Types

enum class  PaymentTiming { InAdvance , InArrears }
 

Public Member Functions

 CommodityIndexedAverageCashFlow (QuantLib::Real quantity, const QuantLib::Date &startDate, const QuantLib::Date &endDate, const QuantLib::Date &paymentDate, const ext::shared_ptr< CommodityIndex > &index, const QuantLib::Calendar &pricingCalendar=QuantLib::Calendar(), QuantLib::Real spread=0.0, QuantLib::Real gearing=1.0, bool useFuturePrice=false, QuantLib::Natural deliveryDateRoll=0, QuantLib::Natural futureMonthOffset=0, const ext::shared_ptr< FutureExpiryCalculator > &calc=nullptr, bool includeEndDate=true, bool excludeStartDate=true, bool useBusinessDays=true, CommodityQuantityFrequency quantityFrequency=CommodityQuantityFrequency::PerCalculationPeriod, QuantLib::Natural hoursPerDay=QuantLib::Null< QuantLib::Natural >(), QuantLib::Natural dailyExpiryOffset=QuantLib::Null< QuantLib::Natural >(), bool unrealisedQuantity=false, const boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > &offPeakPowerData=boost::none, const ext::shared_ptr< FxIndex > &fxIndex=nullptr)
 Constructor taking an explicit paymentDate. More...
 
 CommodityIndexedAverageCashFlow (QuantLib::Real quantity, const QuantLib::Date &startDate, const QuantLib::Date &endDate, QuantLib::Natural paymentLag, QuantLib::Calendar paymentCalendar, QuantLib::BusinessDayConvention paymentConvention, const ext::shared_ptr< CommodityIndex > &index, const QuantLib::Calendar &pricingCalendar=QuantLib::Calendar(), QuantLib::Real spread=0.0, QuantLib::Real gearing=1.0, PaymentTiming paymentTiming=PaymentTiming::InArrears, bool useFuturePrice=false, QuantLib::Natural deliveryDateRoll=0, QuantLib::Natural futureMonthOffset=0, const ext::shared_ptr< FutureExpiryCalculator > &calc=nullptr, bool includeEndDate=true, bool excludeStartDate=true, const QuantLib::Date &paymentDateOverride=Date(), bool useBusinessDays=true, CommodityQuantityFrequency quantityFrequency=CommodityQuantityFrequency::PerCalculationPeriod, QuantLib::Natural hoursPerDay=QuantLib::Null< QuantLib::Natural >(), QuantLib::Natural dailyExpiryOffset=QuantLib::Null< QuantLib::Natural >(), bool unrealisedQuantity=false, const boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > &offPeakPowerData=boost::none, const ext::shared_ptr< FxIndex > &fxIndex=nullptr)
 Constructor that deduces payment date from endDate using payment conventions. More...
 
Inspectors
const QuantLib::Date & startDate () const
 
const QuantLib::Date & endDate () const
 
ext::shared_ptr< CommodityIndexindex () const
 
QuantLib::Natural deliveryDateRoll () const
 
QuantLib::Natural futureMonthOffset () const
 
bool useBusinessDays () const
 
CommodityQuantityFrequency quantityFrequency () const
 
QuantLib::Natural hoursPerDay () const
 
QuantLib::Natural dailyExpiryOffset () const
 
bool unrealisedQuantity () const
 
const boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > & offPeakPowerData () const
 
const std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > & indices () const override
 
QuantLib::Real periodQuantity () const override
 
Event interface
QuantLib::Date date () const override
 
CashFlow interface
QuantLib::Real amount () const override
 
Visitability
void accept (QuantLib::AcyclicVisitor &v) override
 
- Public Member Functions inherited from CommodityCashFlow
 CommodityCashFlow (QuantLib::Real quantity, QuantLib::Real spread, QuantLib::Real gearing, bool useFuturePrice, const ext::shared_ptr< CommodityIndex > &index, const ext::shared_ptr< FxIndex > &fxIndex)
 
QuantLib::Real quantity () const
 
QuantLib::Real spread () const
 
QuantLib::Real gearing () const
 
bool useFuturePrice () const
 
ext::shared_ptr< CommodityIndexindex () const
 
ext::shared_ptr< FxIndexfxIndex () const
 
virtual const std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > & indices () const =0
 Return a map of pricing date and corresponding commodity index. More...
 
virtual QuantLib::Date lastPricingDate () const =0
 
virtual QuantLib::Real periodQuantity () const =0
 
virtual QuantLib::Real fixing () const =0
 
void accept (QuantLib::AcyclicVisitor &v) override
 

CommodityCashFlow interface

QuantLib::Date startDate_
 
QuantLib::Date endDate_
 
QuantLib::Date paymentDate_
 
QuantLib::Calendar pricingCalendar_
 
QuantLib::Natural deliveryDateRoll_
 
QuantLib::Natural futureMonthOffset_
 
bool includeEndDate_
 
bool excludeStartDate_
 
std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > indices_
 
bool useBusinessDays_
 
CommodityQuantityFrequency quantityFrequency_
 
QuantLib::Natural hoursPerDay_
 
QuantLib::Natural dailyExpiryOffset_
 
bool unrealisedQuantity_
 
QuantLib::Real periodQuantity_
 
boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > offPeakPowerData_
 
QuantLib::Real averagePrice_
 
std::map< QuantLib::Date, QuantLib::Real > weights_
 
QuantLib::Date lastPricingDate () const override
 
QuantLib::Real fixing () const override
 
void performCalculations () const override
 
void init (const ext::shared_ptr< FutureExpiryCalculator > &calc)
 Shared initialisation. More...
 
void updateQuantity ()
 Set the period quantity based on the quantity and quantity frequency parameter. More...
 

Additional Inherited Members

- Protected Attributes inherited from CommodityCashFlow
QuantLib::Real quantity_
 
QuantLib::Real spread_
 
QuantLib::Real gearing_
 
bool useFuturePrice_
 
ext::shared_ptr< CommodityIndexindex_
 
ext::shared_ptr< FxIndexfxIndex_
 
QuantLib::Real amount_
 

Detailed Description

Cash flow dependent on the average of commodity spot prices or futures settlement prices over a period.

The cash flow takes a start date and an end date. The set of valid pricing dates is determined from and including the start date to but excluding the end date. The cash flow amount is then the arithmetic average of the commodity spot prices or next commodity future settlement prices on each valid pricing date times the quantity. The next commodity future is determined relative to each pricing date so the settlement prices for multiple commodity contracts may be involved in the averaging.

Definition at line 46 of file commodityindexedaveragecashflow.hpp.

Member Enumeration Documentation

◆ PaymentTiming

enum class PaymentTiming
strong

Constructor & Destructor Documentation

◆ CommodityIndexedAverageCashFlow() [1/2]

CommodityIndexedAverageCashFlow ( QuantLib::Real  quantity,
const QuantLib::Date &  startDate,
const QuantLib::Date &  endDate,
const QuantLib::Date &  paymentDate,
const ext::shared_ptr< CommodityIndex > &  index,
const QuantLib::Calendar &  pricingCalendar = QuantLib::Calendar(),
QuantLib::Real  spread = 0.0,
QuantLib::Real  gearing = 1.0,
bool  useFuturePrice = false,
QuantLib::Natural  deliveryDateRoll = 0,
QuantLib::Natural  futureMonthOffset = 0,
const ext::shared_ptr< FutureExpiryCalculator > &  calc = nullptr,
bool  includeEndDate = true,
bool  excludeStartDate = true,
bool  useBusinessDays = true,
CommodityQuantityFrequency  quantityFrequency = CommodityQuantityFrequency::PerCalculationPeriod,
QuantLib::Natural  hoursPerDay = QuantLib::Null< QuantLib::Natural >(),
QuantLib::Natural  dailyExpiryOffset = QuantLib::Null< QuantLib::Natural >(),
bool  unrealisedQuantity = false,
const boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > &  offPeakPowerData = boost::none,
const ext::shared_ptr< FxIndex > &  fxIndex = nullptr 
)

Constructor taking an explicit paymentDate.

◆ CommodityIndexedAverageCashFlow() [2/2]

CommodityIndexedAverageCashFlow ( QuantLib::Real  quantity,
const QuantLib::Date &  startDate,
const QuantLib::Date &  endDate,
QuantLib::Natural  paymentLag,
QuantLib::Calendar  paymentCalendar,
QuantLib::BusinessDayConvention  paymentConvention,
const ext::shared_ptr< CommodityIndex > &  index,
const QuantLib::Calendar &  pricingCalendar = QuantLib::Calendar(),
QuantLib::Real  spread = 0.0,
QuantLib::Real  gearing = 1.0,
PaymentTiming  paymentTiming = PaymentTiming::InArrears,
bool  useFuturePrice = false,
QuantLib::Natural  deliveryDateRoll = 0,
QuantLib::Natural  futureMonthOffset = 0,
const ext::shared_ptr< FutureExpiryCalculator > &  calc = nullptr,
bool  includeEndDate = true,
bool  excludeStartDate = true,
const QuantLib::Date &  paymentDateOverride = Date(),
bool  useBusinessDays = true,
CommodityQuantityFrequency  quantityFrequency = CommodityQuantityFrequency::PerCalculationPeriod,
QuantLib::Natural  hoursPerDay = QuantLib::Null< QuantLib::Natural >(),
QuantLib::Natural  dailyExpiryOffset = QuantLib::Null< QuantLib::Natural >(),
bool  unrealisedQuantity = false,
const boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > &  offPeakPowerData = boost::none,
const ext::shared_ptr< FxIndex > &  fxIndex = nullptr 
)

Constructor that deduces payment date from endDate using payment conventions.

Member Function Documentation

◆ startDate()

const QuantLib::Date & startDate ( ) const

◆ endDate()

const QuantLib::Date & endDate ( ) const

◆ index()

ext::shared_ptr< CommodityIndex > index ( ) const

Definition at line 86 of file commodityindexedaveragecashflow.hpp.

86{ return index_; }
ext::shared_ptr< CommodityIndex > index_

◆ deliveryDateRoll()

QuantLib::Natural deliveryDateRoll ( ) const

◆ futureMonthOffset()

QuantLib::Natural futureMonthOffset ( ) const

◆ useBusinessDays()

bool useBusinessDays ( ) const

◆ quantityFrequency()

CommodityQuantityFrequency quantityFrequency ( ) const

Definition at line 90 of file commodityindexedaveragecashflow.hpp.

◆ hoursPerDay()

QuantLib::Natural hoursPerDay ( ) const

◆ dailyExpiryOffset()

QuantLib::Natural dailyExpiryOffset ( ) const

◆ unrealisedQuantity()

bool unrealisedQuantity ( ) const

◆ offPeakPowerData()

const boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > & offPeakPowerData ( ) const

Definition at line 94 of file commodityindexedaveragecashflow.hpp.

94 {
95 return offPeakPowerData_;
96 }
boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > offPeakPowerData_

◆ indices()

const std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > & indices ( ) const
overridevirtual

Return the index used to get the price for each pricing date in the period. The map keys are the pricing dates. For a given key date, the map value holds the commodity index used to give the price on that date. If the averaging does not reference future contract settlement prices, i.e. useFirstFuture() is false, the commodity index is simply the commodity spot index passed in the constructor. If the averaging references future contract settlement prices, i.e. useFirstFuture() is true, the commodity index is the commodity future contract index relevant for that pricing date.

Implements CommodityCashFlow.

Definition at line 105 of file commodityindexedaveragecashflow.hpp.

105{ return indices_; }
std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > indices_

◆ periodQuantity()

QuantLib::Real periodQuantity ( ) const
overridevirtual

Quantity for the full calculation period i.e. the effective quantity after taking into account the quantity frequency setting.

Implements CommodityCashFlow.

Definition at line 110 of file commodityindexedaveragecashflow.hpp.

◆ date()

QuantLib::Date date ( ) const
override

◆ amount()

Real amount ( ) const
override

Definition at line 108 of file commodityindexedaveragecashflow.cpp.

108 {
109 calculate();
110 return amount_;
111}

◆ accept()

void accept ( QuantLib::AcyclicVisitor &  v)
override

Definition at line 118 of file commodityindexedaveragecashflow.cpp.

118 {
119 if (Visitor<CommodityIndexedAverageCashFlow>* v1 = dynamic_cast<Visitor<CommodityIndexedAverageCashFlow>*>(&v))
120 v1->visit(*this);
121 else
123}
void accept(QuantLib::AcyclicVisitor &v) override

◆ lastPricingDate()

QuantLib::Date lastPricingDate ( ) const
overridevirtual

Implements CommodityCashFlow.

Definition at line 131 of file commodityindexedaveragecashflow.hpp.

131 {
132 if (indices_.empty()) {
133 return Date();
134 } else {
135 return indices_.rbegin()->first;
136 }
137 }

◆ fixing()

Real fixing ( ) const
overridevirtual

Implements CommodityCashFlow.

Definition at line 113 of file commodityindexedaveragecashflow.cpp.

113 {
114 calculate();
115 return averagePrice_;
116}

◆ performCalculations()

void performCalculations ( ) const
overrideprivate

Definition at line 84 of file commodityindexedaveragecashflow.cpp.

84 {
85
86 // Calculate the average price
87 averagePrice_ = 0.0;
88 Real fxRate = 0.0;
89 if (weights_.empty()) {
90 for (const auto& kv : indices_) {
91 fxRate = (fxIndex_)? this->fxIndex()->fixing(kv.first):1.0;
92 averagePrice_ += fxRate * kv.second->fixing(kv.first);
93 }
94 averagePrice_ /= indices_.size();
95 } else {
96 // weights_ will be populated when offPeakPowerData_ is provided.
97 for (const auto& kv : indices_) {
98 fxRate = (fxIndex_)? this->fxIndex()->fixing(kv.first):1.0;
99 averagePrice_ += fxRate * kv.second->fixing(kv.first) * weights_.at(kv.first);
100 }
101 }
102
103 // Amount is just average price times quantity
104 // In case of Foreign currency settlement, the spread must be expressed in Foreign currency units
106}
ext::shared_ptr< FxIndex > fxIndex() const
ext::shared_ptr< FxIndex > fxIndex_
std::map< QuantLib::Date, QuantLib::Real > weights_

◆ init()

void init ( const ext::shared_ptr< FutureExpiryCalculator > &  calc)
private

Shared initialisation.

Definition at line 125 of file commodityindexedaveragecashflow.cpp.

125 {
126
127 // If pricing calendar is not set, use the index fixing calendar
128 if (pricingCalendar_ == Calendar()) {
129 pricingCalendar_ = index_->fixingCalendar();
130 }
131
132 // If we are going to reference a future settlement price, check that we have a valid expiry calculator
133 if (useFuturePrice_) {
134 QL_REQUIRE(calc, "CommodityIndexedCashFlow needs a valid future expiry calculator when using first future");
135 }
136
137 // Store the relevant index for each pricing date taking account of the flags and the pricing calendar
140
141 QL_REQUIRE(!pds.empty(), "CommodityIndexedAverageCashFlow: found no pricing dates between "
142 << io::iso_date(startDate_) << " and " << io::iso_date(endDate_) << ".");
143
144 // Populate the indices_ map with the correct values.
145 if (!useFuturePrice_) {
146
147 // If not using future prices, just observe spot on every pricing date.
148 for (const Date& pd : pds) {
149 indices_.push_back({pd,index_});
150 }
151
152 } else {
153
154 // If using future prices, first fill indices assuming delivery date roll is 0.
155 for (const Date& pd : pds) {
156 auto expiry = calc->nextExpiry(true, pd, futureMonthOffset_);
157
158 // If given an offset for each expiry, apply it now.
159 if (dailyExpiryOffset_ != Null<Natural>()) {
160 expiry = index_->fixingCalendar().advance(expiry, dailyExpiryOffset_ * Days);
161 }
162
163 indices_.push_back({pd, index_->clone(expiry)});
164 }
165
166 // Update indices_ where necessary if delivery date roll is greater than 0.
167 if (deliveryDateRoll_ > 0) {
168
169 Date expiry;
170 Date prevExpiry;
171 Date rollDate;
172
173 for (auto& kv : indices_) {
174
175 // If expiry is different from previous expiry, update the roll date.
176 expiry = kv.second->expiryDate();
177 if (expiry != prevExpiry) {
178 rollDate = pricingCalendar_.advance(expiry,
179 -static_cast<Integer>(deliveryDateRoll_), Days);
180 }
181 prevExpiry = expiry;
182
183 // If the pricing date is after the roll date, we use the next expiry.
184 if (kv.first > rollDate) {
185 expiry = calc->nextExpiry(false, expiry);
186 kv.second = index_->clone(expiry);
187 }
188
189 }
190 }
191 }
192
193 // Register with each of the indices.
194 for (auto& kv : indices_) {
195 registerWith(kv.second);
196 }
197
198 // If offPeakPowerData_ is provided, populate weights_
199 if (offPeakPowerData_) {
200 Calendar peakCalendar = offPeakPowerData_->first;
201 Real offPeakHours = offPeakPowerData_->second;
202 Real total = 0;
203 for (const auto& kv : indices_) {
204 if (peakCalendar.isHoliday(kv.first))
205 total += weights_[kv.first] = 24;
206 else
207 total += weights_[kv.first] = offPeakHours;
208 }
209 for (auto& kv : weights_)
210 kv.second /= total;
211 }
212
213 // Must be called here after indices_ has been populated.
215
216}
void updateQuantity()
Set the period quantity based on the quantity and quantity frequency parameter.
set< Date > pricingDates(const Date &s, const Date &e, const Calendar &pricingCalendar, bool excludeStart, bool includeEnd, bool useBusinessDays)
+ Here is the call graph for this function:

◆ updateQuantity()

void updateQuantity ( )
private

Set the period quantity based on the quantity and quantity frequency parameter.

Definition at line 218 of file commodityindexedaveragecashflow.cpp.

218 {
219
220 using CQF = CommodityQuantityFrequency;
221 switch (quantityFrequency_)
222 {
223 case CQF::PerCalculationPeriod:
226 Date today = Settings::instance().evaluationDate();
227 if (startDate_ <= today && today < endDate_) {
228 // In both cases, unrealised equal to 0 should probably be an error but leave quantity unaltered.
229 if (offPeakPowerData_) {
230 QL_REQUIRE(!weights_.empty(), "Expected to have weights when we have off-peak power data.");
231 auto unrealised = accumulate(weights_.begin(), weights_.end(), 0.0,
232 [&today](Real sum, const pair<Date, Real>& p) {
233 return p.first > today ? sum + p.second : sum; });
234 if (unrealised > 0) {
235 periodQuantity_ /= unrealised;
236 }
237 } else {
238 auto unrealised = count_if(indices_.begin(), indices_.end(),
239 [&today](const pair<Date, ext::shared_ptr<CommodityIndex>>& p) { return p.first > today; });
240 if (unrealised > 0) {
241 periodQuantity_ = periodQuantity_ * indices_.size() / unrealised;
242 }
243 }
244 }
245 }
246 break;
247 case CQF::PerPricingDay:
249 break;
250 case CQF::PerHour:
251 if (offPeakPowerData_) {
252 Calendar peakCalendar = offPeakPowerData_->first;
253 Real offPeakHours = offPeakPowerData_->second;
254 periodQuantity_ = 0.0;
255 for (const auto& kv : indices_) {
256 if (peakCalendar.isHoliday(kv.first))
257 periodQuantity_ += 24.0 * quantity_;
258 else
259 periodQuantity_ += offPeakHours * quantity_;
260 }
261 } else {
262 QL_REQUIRE(hoursPerDay_ != Null<Natural>(), "If a commodity quantity frequency of PerHour is used, " <<
263 "a valid hoursPerDay value should be supplied.");
265 }
266 break;
267 case CQF::PerCalendarDay:
268 // Rarely used but kept because it has already been documented and released.
269 periodQuantity_ = quantity_ * ((endDate_ - startDate_ - 1.0) + (!excludeStartDate_ ? 1.0 : 0.0) +
270 (includeEndDate_ ? 1.0 : 0.0));
271 break;
272 default:
273 // Do nothing
274 break;
275 }
276
277}
CommodityQuantityFrequency
Enumeration indicating the frequency associated with a commodity quantity.
Real sum(const Cash &c, const Cash &d)
Definition: bondbasket.cpp:107
+ Here is the call graph for this function:

Member Data Documentation

◆ startDate_

QuantLib::Date startDate_
private

Definition at line 145 of file commodityindexedaveragecashflow.hpp.

◆ endDate_

QuantLib::Date endDate_
private

Definition at line 146 of file commodityindexedaveragecashflow.hpp.

◆ paymentDate_

QuantLib::Date paymentDate_
private

Definition at line 147 of file commodityindexedaveragecashflow.hpp.

◆ pricingCalendar_

QuantLib::Calendar pricingCalendar_
private

Definition at line 148 of file commodityindexedaveragecashflow.hpp.

◆ deliveryDateRoll_

QuantLib::Natural deliveryDateRoll_
private

Definition at line 149 of file commodityindexedaveragecashflow.hpp.

◆ futureMonthOffset_

QuantLib::Natural futureMonthOffset_
private

Definition at line 150 of file commodityindexedaveragecashflow.hpp.

◆ includeEndDate_

bool includeEndDate_
private

Definition at line 151 of file commodityindexedaveragecashflow.hpp.

◆ excludeStartDate_

bool excludeStartDate_
private

Definition at line 152 of file commodityindexedaveragecashflow.hpp.

◆ indices_

std::vector<std::pair<QuantLib::Date, ext::shared_ptr<CommodityIndex> > > indices_
private

Definition at line 153 of file commodityindexedaveragecashflow.hpp.

◆ useBusinessDays_

bool useBusinessDays_
private

Definition at line 154 of file commodityindexedaveragecashflow.hpp.

◆ quantityFrequency_

CommodityQuantityFrequency quantityFrequency_
private

Definition at line 155 of file commodityindexedaveragecashflow.hpp.

◆ hoursPerDay_

QuantLib::Natural hoursPerDay_
private

Definition at line 156 of file commodityindexedaveragecashflow.hpp.

◆ dailyExpiryOffset_

QuantLib::Natural dailyExpiryOffset_
private

Definition at line 157 of file commodityindexedaveragecashflow.hpp.

◆ unrealisedQuantity_

bool unrealisedQuantity_
private

Definition at line 158 of file commodityindexedaveragecashflow.hpp.

◆ periodQuantity_

QuantLib::Real periodQuantity_
private

Definition at line 159 of file commodityindexedaveragecashflow.hpp.

◆ offPeakPowerData_

boost::optional<std::pair<QuantLib::Calendar, QuantLib::Real> > offPeakPowerData_
private

Definition at line 160 of file commodityindexedaveragecashflow.hpp.

◆ averagePrice_

QuantLib::Real averagePrice_
mutableprivate

Definition at line 161 of file commodityindexedaveragecashflow.hpp.

◆ weights_

std::map<QuantLib::Date, QuantLib::Real> weights_
private

Definition at line 164 of file commodityindexedaveragecashflow.hpp.