Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
commodityindexedaveragecashflow.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file qle/cashflows/commodityindexedaveragecashflow.hpp
20 \brief Cash flow dependent on the average commodity spot price or future's settlement price over a period.
21 If settled in a foreign currency (domestic: currency on which the underlying curve is traded, foreing: settlement
22 currency) the FX is applied day by day. This approach cannot be appied to averaged underlying curves.
23 */
24
25#ifndef quantext_commodity_indexed_average_cash_flow_hpp
26#define quantext_commodity_indexed_average_cash_flow_hpp
27
28#include <ql/cashflow.hpp>
29#include <ql/patterns/visitor.hpp>
30#include <ql/time/schedule.hpp>
35
36namespace QuantExt {
37
38/*! Cash flow dependent on the average of commodity spot prices or futures settlement prices over a period.
39
40 The cash flow takes a start date and an end date. The set of valid pricing dates is determined from and including
41 the start date to but excluding the end date. The cash flow amount is then the arithmetic average of the commodity
42 spot prices or next commodity future settlement prices on each valid pricing date times the quantity. The next
43 commodity future is determined relative to each pricing date so the settlement prices for multiple commodity
44 contracts may be involved in the averaging.
45*/
47
48public:
50
51 //! Constructor taking an explicit \p paymentDate
53 QuantLib::Real quantity, const QuantLib::Date& startDate, const QuantLib::Date& endDate,
54 const QuantLib::Date& paymentDate, const ext::shared_ptr<CommodityIndex>& index,
55 const QuantLib::Calendar& pricingCalendar = QuantLib::Calendar(), QuantLib::Real spread = 0.0,
56 QuantLib::Real gearing = 1.0, bool useFuturePrice = false, QuantLib::Natural deliveryDateRoll = 0,
57 QuantLib::Natural futureMonthOffset = 0, const ext::shared_ptr<FutureExpiryCalculator>& calc = nullptr,
58 bool includeEndDate = true, bool excludeStartDate = true, bool useBusinessDays = true,
60 QuantLib::Natural hoursPerDay = QuantLib::Null<QuantLib::Natural>(),
61 QuantLib::Natural dailyExpiryOffset = QuantLib::Null<QuantLib::Natural>(), bool unrealisedQuantity = false,
62 const boost::optional<std::pair<QuantLib::Calendar, QuantLib::Real>>& offPeakPowerData = boost::none,
63 const ext::shared_ptr<FxIndex>& fxIndex = nullptr);
64
65 //! Constructor that deduces payment date from \p endDate using payment conventions
67 QuantLib::Real quantity, const QuantLib::Date& startDate, const QuantLib::Date& endDate,
68 QuantLib::Natural paymentLag, QuantLib::Calendar paymentCalendar,
69 QuantLib::BusinessDayConvention paymentConvention, const ext::shared_ptr<CommodityIndex>& index,
70 const QuantLib::Calendar& pricingCalendar = QuantLib::Calendar(), QuantLib::Real spread = 0.0,
71 QuantLib::Real gearing = 1.0, PaymentTiming paymentTiming = PaymentTiming::InArrears,
72 bool useFuturePrice = false, QuantLib::Natural deliveryDateRoll = 0, QuantLib::Natural futureMonthOffset = 0,
73 const ext::shared_ptr<FutureExpiryCalculator>& calc = nullptr, bool includeEndDate = true,
74 bool excludeStartDate = true, const QuantLib::Date& paymentDateOverride = Date(), bool useBusinessDays = true,
76 QuantLib::Natural hoursPerDay = QuantLib::Null<QuantLib::Natural>(),
77 QuantLib::Natural dailyExpiryOffset = QuantLib::Null<QuantLib::Natural>(), bool unrealisedQuantity = false,
78 const boost::optional<std::pair<QuantLib::Calendar, QuantLib::Real>>& offPeakPowerData = boost::none,
79 const ext::shared_ptr<FxIndex>& fxIndex = nullptr);
80
81 //! \name Inspectors
82 //@{
83
84 const QuantLib::Date& startDate() const { return startDate_; }
85 const QuantLib::Date& endDate() const { return endDate_; }
86 ext::shared_ptr<CommodityIndex> index() const { return index_; }
87 QuantLib::Natural deliveryDateRoll() const { return deliveryDateRoll_; }
88 QuantLib::Natural futureMonthOffset() const { return futureMonthOffset_; }
89 bool useBusinessDays() const { return useBusinessDays_; }
91 QuantLib::Natural hoursPerDay() const { return hoursPerDay_; }
92 QuantLib::Natural dailyExpiryOffset() const { return dailyExpiryOffset_; }
93 bool unrealisedQuantity() const { return unrealisedQuantity_; }
94 const boost::optional<std::pair<QuantLib::Calendar, QuantLib::Real>>& offPeakPowerData() const {
95 return offPeakPowerData_;
96 }
97
98 /*! Return the index used to get the price for each pricing date in the period. The map keys are the pricing dates.
99 For a given key date, the map value holds the commodity index used to give the price on that date. If the
100 averaging does not reference future contract settlement prices, i.e. \c useFirstFuture() is \c false, the
101 commodity index is simply the commodity spot index passed in the constructor. If the averaging references
102 future contract settlement prices, i.e. \c useFirstFuture() is \c true, the commodity index is the commodity
103 future contract \em index relevant for that pricing date.
104 */
105 const std::vector<std::pair<QuantLib::Date, ext::shared_ptr<CommodityIndex>>>& indices() const override { return indices_; }
106
107 /*! Quantity for the full calculation period i.e. the effective quantity after taking into account the
108 quantity frequency setting.
109 */
110 QuantLib::Real periodQuantity() const override { return periodQuantity_; }
111 //@}
112
113 //! \name Event interface
114 //@{
115 QuantLib::Date date() const override { return paymentDate_; }
116 //@}
117
118 //! \name CashFlow interface
119 //@{
120 QuantLib::Real amount() const override;
121 //@}
122
123 //! \name Visitability
124 //@{
125 void accept(QuantLib::AcyclicVisitor& v) override;
126 //@}
127
128 //@}
129 //! \name CommodityCashFlow interface
130 //@{
131 QuantLib::Date lastPricingDate() const override {
132 if (indices_.empty()) {
133 return Date();
134 } else {
135 return indices_.rbegin()->first;
136 }
137 }
138
139 QuantLib::Real fixing() const override;
140 //@}
141
142private:
143 void performCalculations() const override;
144
145 QuantLib::Date startDate_;
146 QuantLib::Date endDate_;
147 QuantLib::Date paymentDate_;
148 QuantLib::Calendar pricingCalendar_;
149 QuantLib::Natural deliveryDateRoll_;
150 QuantLib::Natural futureMonthOffset_;
153 std::vector<std::pair<QuantLib::Date, ext::shared_ptr<CommodityIndex>>> indices_;
156 QuantLib::Natural hoursPerDay_;
157 QuantLib::Natural dailyExpiryOffset_;
159 QuantLib::Real periodQuantity_;
160 boost::optional<std::pair<QuantLib::Calendar, QuantLib::Real>> offPeakPowerData_;
161 mutable QuantLib::Real averagePrice_;
162
163 // Populated only when offPeakPowerData_ is provided.
164 std::map<QuantLib::Date, QuantLib::Real> weights_;
165
166 //! Shared initialisation
167 void init(const ext::shared_ptr<FutureExpiryCalculator>& calc);
168
169 //! Set the period quantity based on the quantity and quantity frequency parameter
170 void updateQuantity();
171};
172
173//! Helper class building a sequence of commodity indexed average cashflows
175
176public:
177 CommodityIndexedAverageLeg(const QuantLib::Schedule& schedule, const ext::shared_ptr<CommodityIndex>& index);
179 CommodityIndexedAverageLeg& withQuantities(const std::vector<QuantLib::Real>& quantities);
180 CommodityIndexedAverageLeg& withPaymentLag(QuantLib::Natural paymentLag);
181 CommodityIndexedAverageLeg& withPaymentCalendar(const QuantLib::Calendar& paymentCalendar);
182 CommodityIndexedAverageLeg& withPaymentConvention(QuantLib::BusinessDayConvention paymentConvention);
183 CommodityIndexedAverageLeg& withPricingCalendar(const QuantLib::Calendar& pricingCalendar);
185 CommodityIndexedAverageLeg& withSpreads(const std::vector<QuantLib::Real>& spreads);
186 CommodityIndexedAverageLeg& withGearings(QuantLib::Real gearing);
187 CommodityIndexedAverageLeg& withGearings(const std::vector<QuantLib::Real>& gearings);
189 CommodityIndexedAverageLeg& useFuturePrice(bool flag = false);
190 CommodityIndexedAverageLeg& withDeliveryDateRoll(QuantLib::Natural deliveryDateRoll);
191 CommodityIndexedAverageLeg& withFutureMonthOffset(QuantLib::Natural futureMonthOffset);
193 withFutureExpiryCalculator(const ext::shared_ptr<FutureExpiryCalculator>& calc = nullptr);
194 CommodityIndexedAverageLeg& payAtMaturity(bool flag = false);
197 CommodityIndexedAverageLeg& withPaymentDates(const std::vector<QuantLib::Date>& paymentDates);
200 CommodityIndexedAverageLeg& withHoursPerDay(QuantLib::Natural hoursPerDay);
201 CommodityIndexedAverageLeg& withDailyExpiryOffset(QuantLib::Natural dailyExpiryOffset);
204 withOffPeakPowerData(const boost::optional<std::pair<QuantLib::Calendar, QuantLib::Real>>& offPeakPowerData);
205 CommodityIndexedAverageLeg& withFxIndex(const ext::shared_ptr<FxIndex>& fxIndex);
206
207 operator Leg() const;
208
209private:
210 Schedule schedule_;
211 ext::shared_ptr<CommodityIndex> index_;
212 std::vector<QuantLib::Real> quantities_;
213 QuantLib::Natural paymentLag_;
214 QuantLib::Calendar paymentCalendar_;
215 QuantLib::BusinessDayConvention paymentConvention_;
216 QuantLib::Calendar pricingCalendar_;
217 std::vector<QuantLib::Real> spreads_;
218 std::vector<QuantLib::Real> gearings_;
221 QuantLib::Natural deliveryDateRoll_;
222 QuantLib::Natural futureMonthOffset_;
223 ext::shared_ptr<FutureExpiryCalculator> calc_;
227 std::vector<QuantLib::Date> paymentDates_;
230 QuantLib::Natural hoursPerDay_;
231 QuantLib::Natural dailyExpiryOffset_;
233 boost::optional<std::pair<QuantLib::Calendar, QuantLib::Real>> offPeakPowerData_;
234 ext::shared_ptr<FxIndex> fxIndex_;
235};
236
237} // namespace QuantExt
238
239#endif
QuantLib::Real spread() const
ext::shared_ptr< FxIndex > fxIndex() const
QuantLib::Real gearing() const
ext::shared_ptr< CommodityIndex > index_
QuantLib::Real quantity() const
std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > indices_
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.
void accept(QuantLib::AcyclicVisitor &v) override
void updateQuantity()
Set the period quantity based on the quantity and quantity frequency parameter.
const boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > & offPeakPowerData() const
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.
boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > offPeakPowerData_
void init(const ext::shared_ptr< FutureExpiryCalculator > &calc)
Shared initialisation.
std::map< QuantLib::Date, QuantLib::Real > weights_
const std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > & indices() const override
ext::shared_ptr< CommodityIndex > index() const
Helper class building a sequence of commodity indexed average cashflows.
CommodityIndexedAverageLeg & useFuturePrice(bool flag=false)
CommodityIndexedAverageLeg & withPaymentLag(QuantLib::Natural paymentLag)
CommodityIndexedAverageLeg & includeEndDate(bool flag=true)
CommodityIndexedAverageLeg & excludeStartDate(bool flag=true)
CommodityIndexedAverageLeg & withFutureMonthOffset(QuantLib::Natural futureMonthOffset)
CommodityIndexedAverageLeg & withGearings(QuantLib::Real gearing)
CommodityIndexedAverageLeg & withSpreads(const std::vector< QuantLib::Real > &spreads)
CommodityIndexedAverageLeg & paymentTiming(CommodityIndexedAverageCashFlow::PaymentTiming paymentTiming)
CommodityIndexedAverageLeg & withDeliveryDateRoll(QuantLib::Natural deliveryDateRoll)
ext::shared_ptr< FutureExpiryCalculator > calc_
CommodityIndexedAverageLeg & withQuantityFrequency(CommodityQuantityFrequency quantityFrequency)
CommodityIndexedAverageLeg & withPaymentConvention(QuantLib::BusinessDayConvention paymentConvention)
CommodityIndexedAverageLeg & withPaymentDates(const std::vector< QuantLib::Date > &paymentDates)
boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > offPeakPowerData_
CommodityIndexedAverageLeg & withDailyExpiryOffset(QuantLib::Natural dailyExpiryOffset)
CommodityIndexedAverageLeg & withQuantities(const std::vector< QuantLib::Real > &quantities)
CommodityIndexedAverageLeg & withQuantities(QuantLib::Real quantity)
CommodityIndexedAverageLeg & withPaymentCalendar(const QuantLib::Calendar &paymentCalendar)
CommodityIndexedAverageLeg & withHoursPerDay(QuantLib::Natural hoursPerDay)
CommodityIndexedAverageLeg & useBusinessDays(bool flag=true)
CommodityIndexedAverageLeg & withFutureExpiryCalculator(const ext::shared_ptr< FutureExpiryCalculator > &calc=nullptr)
CommodityIndexedAverageLeg & withPricingCalendar(const QuantLib::Calendar &pricingCalendar)
CommodityIndexedAverageLeg & withGearings(const std::vector< QuantLib::Real > &gearings)
CommodityIndexedAverageLeg & withSpreads(QuantLib::Real spread)
CommodityIndexedAverageLeg & unrealisedQuantity(bool flag=false)
CommodityIndexedAverageCashFlow::PaymentTiming paymentTiming_
CommodityIndexedAverageLeg & withOffPeakPowerData(const boost::optional< std::pair< QuantLib::Calendar, QuantLib::Real > > &offPeakPowerData)
CommodityIndexedAverageLeg & withFxIndex(const ext::shared_ptr< FxIndex > &fxIndex)
CommodityIndexedAverageLeg & payAtMaturity(bool flag=false)
Some data and logic shared among commodity cashflows.
commodity index class for holding commodity spot and futures price histories and forwarding.
Base class for classes that perform date calculations for future contracts.
FX index class.
CommodityQuantityFrequency
Enumeration indicating the frequency associated with a commodity quantity.