Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
commodityindexedcashflow.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/commodityindexedcashflow.hpp
20 \brief Cash flow dependent on a single commodity spot price or future's settlement price
21 */
22
23#ifndef quantext_commodity_indexed_cash_flow_hpp
24#define quantext_commodity_indexed_cash_flow_hpp
25
26#include <boost/optional.hpp>
27#include <ql/cashflow.hpp>
28#include <ql/patterns/visitor.hpp>
29#include <ql/time/schedule.hpp>
33
34namespace QuantExt {
35
36//! Cash flow dependent on a single commodity spot price or futures settlement price on a given pricing date
38
39public:
41
42 //! Constructor taking an explicit \p pricingDate and \p paymentDate
43 CommodityIndexedCashFlow(QuantLib::Real quantity, const QuantLib::Date& pricingDate,
44 const QuantLib::Date& paymentDate, const ext::shared_ptr<CommodityIndex>& index,
45 QuantLib::Real spread = 0.0, QuantLib::Real gearing = 1.0, bool useFuturePrice = false,
46 const Date& contractDate = Date(),
47 const ext::shared_ptr<FutureExpiryCalculator>& calc = nullptr,
48 QuantLib::Natural dailyExpiryOffset = QuantLib::Null<QuantLib::Natural>(),
49 const ext::shared_ptr<FxIndex>& fxIndex = nullptr);
50
51 /*! Constructor taking a period \p startDate, \p endDate and some conventions. The pricing date and payment date
52 are derived from the start date and end date using the conventions.
53 */
54 CommodityIndexedCashFlow(QuantLib::Real quantity, const QuantLib::Date& startDate, const QuantLib::Date& endDate,
55 const ext::shared_ptr<CommodityIndex>& index, QuantLib::Natural paymentLag,
56 const QuantLib::Calendar& paymentCalendar,
57 QuantLib::BusinessDayConvention paymentConvention, QuantLib::Natural pricingLag,
58 const QuantLib::Calendar& pricingLagCalendar, QuantLib::Real spread = 0.0,
59 QuantLib::Real gearing = 1.0, PaymentTiming paymentTiming = PaymentTiming::InArrears,
60 bool isInArrears = true, bool useFuturePrice = false, bool useFutureExpiryDate = true,
61 QuantLib::Natural futureMonthOffset = 0,
62 const ext::shared_ptr<FutureExpiryCalculator>& calc = nullptr,
63 const QuantLib::Date& paymentDateOverride = Date(),
64 const QuantLib::Date& pricingDateOverride = Date(),
65 QuantLib::Natural dailyExpiryOffset = QuantLib::Null<QuantLib::Natural>(),
66 const ext::shared_ptr<FxIndex>& fxIndex = nullptr,
67 const bool spotAveragingFrontCoupon = false,
68 const QuantLib::Calendar& pricingCalendar = QuantLib::Calendar(),
69 bool includeEndDate = true,
70 bool excludeStartDate = true);
71
72 //! \name Inspectors
73 //@{
74
75 const QuantLib::Date& pricingDate() const { return pricingDate_; }
77 QuantLib::Natural futureMonthOffset() const { return futureMonthOffset_; }
78 QuantLib::Real periodQuantity() const override { return periodQuantity_; }
79 QuantLib::Natural dailyExpiryOffset() const { return dailyExpiryOffset_; }
80
81 //@}
82 //! \name CommodityCashFlow interface
83 //@{
84 const std::vector<std::pair<QuantLib::Date, ext::shared_ptr<CommodityIndex>>>& indices() const override { return indices_; }
85
86 // Index used to compute the accrued average price from historical spot fixing
87 const ext::shared_ptr<CommodityIndex>& spotIndex() const { return spotIndex_;}
88
89 const std::set<QuantLib::Date>& spotAveragingPricingDates() const { return spotAveragingPricingDates_;}
90
91 // Return true if its averaging front cashflow with a balance of a month
92 bool isAveragingFrontMonthCashflow(const QuantLib::Date& asof) const;
93
94 QuantLib::Date lastPricingDate() const override { return pricingDate(); }
95
96 QuantLib::Real fixing() const override;
97 //@}
98
99 //! \name Event interface
100 //@{
101 QuantLib::Date date() const override { return paymentDate_; }
102 //@}
103
104 //! \name CashFlow interface
105 //@{
106 QuantLib::Real amount() const override;
107 //@}
108
109 //! \name Visitability
110 //@{
111 void accept(QuantLib::AcyclicVisitor& v) override;
112 //@}
113
114 //! Allow the full calculation period quantity to be updated.
115 void setPeriodQuantity(QuantLib::Real periodQuantity);
116
117private:
118 void performCalculations() const override;
119
120 QuantLib::Date pricingDate_;
121 QuantLib::Date paymentDate_;
123 QuantLib::Natural futureMonthOffset_;
124 QuantLib::Real periodQuantity_;
125 QuantLib::Natural dailyExpiryOffset_;
126 std::vector<std::pair<QuantLib::Date, ext::shared_ptr<CommodityIndex>>> indices_;
127 // Flag used to identify averaging coupons with averaging underlyings who uses spot fixings
128 // for the historical fixings instead of the averaging future fixing
130 std::set<QuantLib::Date> spotAveragingPricingDates_;
131 ext::shared_ptr<CommodityIndex> spotIndex_;
132 mutable QuantLib::Real price_;
133 //! Shared initialisation
134 void init(const ext::shared_ptr<FutureExpiryCalculator>& calc,
135 const QuantLib::Date& contractDate = QuantLib::Date(),
136 const PaymentTiming paymentTiming = PaymentTiming::InArrears,
137 const QuantLib::Date& startDate = QuantLib::Date(), const QuantLib::Date& endDate = QuantLib::Date(),
138 const QuantLib::Natural paymentLag = 0,
139 const QuantLib::BusinessDayConvention paymentConvention = QuantLib::Unadjusted,
140 const QuantLib::Calendar& paymentCalendar = QuantLib::NullCalendar(),
141 const QuantLib::Calendar& pricingCalendar = QuantLib::Calendar(), bool includeEndDate = true,
142 bool excludeStartDate = true);
143};
144
145//! Helper class building a sequence of commodity indexed cashflows
147
148public:
149 CommodityIndexedLeg(const QuantLib::Schedule& schedule, const ext::shared_ptr<CommodityIndex>& index);
150 CommodityIndexedLeg& withQuantities(QuantLib::Real quantity);
151 CommodityIndexedLeg& withQuantities(const std::vector<QuantLib::Real>& quantities);
152 CommodityIndexedLeg& withPaymentLag(QuantLib::Natural paymentLag);
153 CommodityIndexedLeg& withPaymentCalendar(const QuantLib::Calendar& paymentCalendar);
154 CommodityIndexedLeg& withPaymentConvention(QuantLib::BusinessDayConvention paymentConvention);
155 CommodityIndexedLeg& withPricingLag(QuantLib::Natural pricingLag);
156 CommodityIndexedLeg& withPricingLagCalendar(const QuantLib::Calendar& pricingLagCalendar);
157 CommodityIndexedLeg& withSpreads(QuantLib::Real spread);
158 CommodityIndexedLeg& withSpreads(const std::vector<QuantLib::Real>& spreads);
159 CommodityIndexedLeg& withGearings(QuantLib::Real gearing);
160 CommodityIndexedLeg& withGearings(const std::vector<QuantLib::Real>& gearings);
162 CommodityIndexedLeg& inArrears(bool flag = true);
163 CommodityIndexedLeg& useFuturePrice(bool flag = false);
164 CommodityIndexedLeg& useFutureExpiryDate(bool flag = true);
165 CommodityIndexedLeg& withFutureMonthOffset(QuantLib::Natural futureMonthOffset);
166 CommodityIndexedLeg& withFutureExpiryCalculator(const ext::shared_ptr<FutureExpiryCalculator>& calc = nullptr);
167 CommodityIndexedLeg& payAtMaturity(bool flag = false);
168 CommodityIndexedLeg& withPricingDates(const std::vector<QuantLib::Date>& pricingDates);
169 CommodityIndexedLeg& withPaymentDates(const std::vector<QuantLib::Date>& paymentDates);
170 CommodityIndexedLeg& withDailyExpiryOffset(QuantLib::Natural dailyExpiryOffset);
171 CommodityIndexedLeg& withFxIndex(const ext::shared_ptr<FxIndex>& fxIndex);
172 CommodityIndexedLeg& withIsAveraging(const bool isAveraging);
173 CommodityIndexedLeg& withPricingCalendar(const QuantLib::Calendar& pricingCalendar);
174 CommodityIndexedLeg& includeEndDate(bool flag = true);
175 CommodityIndexedLeg& excludeStartDate(bool flag = true);
176
177 operator Leg() const;
178
179private:
180 Schedule schedule_;
181 ext::shared_ptr<CommodityIndex> index_;
182 std::vector<QuantLib::Real> quantities_;
183 QuantLib::Natural paymentLag_;
184 QuantLib::Calendar paymentCalendar_;
185 QuantLib::BusinessDayConvention paymentConvention_;
186 QuantLib::Natural pricingLag_;
187 QuantLib::Calendar pricingLagCalendar_;
188 std::vector<QuantLib::Real> spreads_;
189 std::vector<QuantLib::Real> gearings_;
194 QuantLib::Natural futureMonthOffset_;
195 ext::shared_ptr<FutureExpiryCalculator> calc_;
197 std::vector<QuantLib::Date> pricingDates_;
198 std::vector<QuantLib::Date> paymentDates_;
199 QuantLib::Natural dailyExpiryOffset_;
200 ext::shared_ptr<FxIndex> fxIndex_;
202 QuantLib::Calendar pricingCalendar_;
205};
206
207} // namespace QuantExt
208
209#endif
QuantLib::Real spread() const
ext::shared_ptr< FxIndex > fxIndex() const
QuantLib::Real gearing() const
ext::shared_ptr< CommodityIndex > index() const
QuantLib::Real quantity() const
Cash flow dependent on a single commodity spot price or futures settlement price on a given pricing d...
std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > indices_
void init(const ext::shared_ptr< FutureExpiryCalculator > &calc, const QuantLib::Date &contractDate=QuantLib::Date(), const PaymentTiming paymentTiming=PaymentTiming::InArrears, const QuantLib::Date &startDate=QuantLib::Date(), const QuantLib::Date &endDate=QuantLib::Date(), const QuantLib::Natural paymentLag=0, const QuantLib::BusinessDayConvention paymentConvention=QuantLib::Unadjusted, const QuantLib::Calendar &paymentCalendar=QuantLib::NullCalendar(), const QuantLib::Calendar &pricingCalendar=QuantLib::Calendar(), bool includeEndDate=true, bool excludeStartDate=true)
Shared initialisation.
QuantLib::Real fixing() const override
CommodityIndexedCashFlow(QuantLib::Real quantity, const QuantLib::Date &pricingDate, const QuantLib::Date &paymentDate, const ext::shared_ptr< CommodityIndex > &index, QuantLib::Real spread=0.0, QuantLib::Real gearing=1.0, bool useFuturePrice=false, const Date &contractDate=Date(), const ext::shared_ptr< FutureExpiryCalculator > &calc=nullptr, QuantLib::Natural dailyExpiryOffset=QuantLib::Null< QuantLib::Natural >(), const ext::shared_ptr< FxIndex > &fxIndex=nullptr)
Constructor taking an explicit pricingDate and paymentDate.
QuantLib::Real periodQuantity() const override
QuantLib::Date lastPricingDate() const override
void accept(QuantLib::AcyclicVisitor &v) override
const QuantLib::Date & pricingDate() const
QuantLib::Real amount() const override
const std::set< QuantLib::Date > & spotAveragingPricingDates() const
std::set< QuantLib::Date > spotAveragingPricingDates_
bool isAveragingFrontMonthCashflow(const QuantLib::Date &asof) const
ext::shared_ptr< CommodityIndex > spotIndex_
const std::vector< std::pair< QuantLib::Date, ext::shared_ptr< CommodityIndex > > > & indices() const override
Return a map of pricing date and corresponding commodity index.
void setPeriodQuantity(QuantLib::Real periodQuantity)
Allow the full calculation period quantity to be updated.
CommodityIndexedCashFlow(QuantLib::Real quantity, const QuantLib::Date &startDate, const QuantLib::Date &endDate, const ext::shared_ptr< CommodityIndex > &index, QuantLib::Natural paymentLag, const QuantLib::Calendar &paymentCalendar, QuantLib::BusinessDayConvention paymentConvention, QuantLib::Natural pricingLag, const QuantLib::Calendar &pricingLagCalendar, QuantLib::Real spread=0.0, QuantLib::Real gearing=1.0, PaymentTiming paymentTiming=PaymentTiming::InArrears, bool isInArrears=true, bool useFuturePrice=false, bool useFutureExpiryDate=true, QuantLib::Natural futureMonthOffset=0, const ext::shared_ptr< FutureExpiryCalculator > &calc=nullptr, const QuantLib::Date &paymentDateOverride=Date(), const QuantLib::Date &pricingDateOverride=Date(), QuantLib::Natural dailyExpiryOffset=QuantLib::Null< QuantLib::Natural >(), const ext::shared_ptr< FxIndex > &fxIndex=nullptr, const bool spotAveragingFrontCoupon=false, const QuantLib::Calendar &pricingCalendar=QuantLib::Calendar(), bool includeEndDate=true, bool excludeStartDate=true)
const ext::shared_ptr< CommodityIndex > & spotIndex() const
QuantLib::Date date() const override
Helper class building a sequence of commodity indexed cashflows.
CommodityIndexedLeg & excludeStartDate(bool flag=true)
CommodityIndexedLeg & withPricingLagCalendar(const QuantLib::Calendar &pricingLagCalendar)
CommodityIndexedLeg & paymentTiming(CommodityIndexedCashFlow::PaymentTiming paymentTiming)
QuantLib::BusinessDayConvention paymentConvention_
CommodityIndexedLeg & payAtMaturity(bool flag=false)
CommodityIndexedLeg & withIsAveraging(const bool isAveraging)
CommodityIndexedCashFlow::PaymentTiming paymentTiming_
CommodityIndexedLeg & includeEndDate(bool flag=true)
ext::shared_ptr< FutureExpiryCalculator > calc_
std::vector< QuantLib::Date > paymentDates_
CommodityIndexedLeg & withFutureMonthOffset(QuantLib::Natural futureMonthOffset)
CommodityIndexedLeg & withQuantities(QuantLib::Real quantity)
CommodityIndexedLeg & withPaymentDates(const std::vector< QuantLib::Date > &paymentDates)
CommodityIndexedLeg & withQuantities(const std::vector< QuantLib::Real > &quantities)
CommodityIndexedLeg & withFxIndex(const ext::shared_ptr< FxIndex > &fxIndex)
CommodityIndexedLeg & withFutureExpiryCalculator(const ext::shared_ptr< FutureExpiryCalculator > &calc=nullptr)
CommodityIndexedLeg & withPaymentLag(QuantLib::Natural paymentLag)
CommodityIndexedLeg & inArrears(bool flag=true)
CommodityIndexedLeg & withGearings(QuantLib::Real gearing)
CommodityIndexedLeg & withDailyExpiryOffset(QuantLib::Natural dailyExpiryOffset)
std::vector< QuantLib::Date > pricingDates_
CommodityIndexedLeg & withPaymentCalendar(const QuantLib::Calendar &paymentCalendar)
CommodityIndexedLeg & withPricingDates(const std::vector< QuantLib::Date > &pricingDates)
ext::shared_ptr< CommodityIndex > index_
std::vector< QuantLib::Real > quantities_
CommodityIndexedLeg & withSpreads(QuantLib::Real spread)
CommodityIndexedLeg & useFuturePrice(bool flag=false)
CommodityIndexedLeg & withPricingCalendar(const QuantLib::Calendar &pricingCalendar)
std::vector< QuantLib::Real > spreads_
CommodityIndexedLeg & withPaymentConvention(QuantLib::BusinessDayConvention paymentConvention)
CommodityIndexedLeg & withSpreads(const std::vector< QuantLib::Real > &spreads)
std::vector< QuantLib::Real > gearings_
CommodityIndexedLeg & withPricingLag(QuantLib::Natural pricingLag)
CommodityIndexedLeg & useFutureExpiryDate(bool flag=true)
CommodityIndexedLeg & withGearings(const std::vector< QuantLib::Real > &gearings)
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.
set< Date > pricingDates(const Date &s, const Date &e, const Calendar &pricingCalendar, bool excludeStart, bool includeEnd, bool useBusinessDays)