Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
commoditybasisfutureindex.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 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
20
21namespace QuantExt {
23 const QuantLib::Date& expiryDate,
24 const QuantLib::Calendar& fixingCalendar,
25 const QuantLib::ext::shared_ptr<FutureExpiryCalculator>& basisFec,
26 const QuantLib::ext::shared_ptr<QuantExt::CommodityIndex>& baseIndex,
27 const QuantLib::ext::shared_ptr<FutureExpiryCalculator>& baseFec,
28 const QuantLib::Handle<QuantExt::PriceTermStructure>& priceCurve,
29 const bool addBasis, const QuantLib::Size monthOffset,
30 const bool baseIsAveraging, const bool priceAsHistoricalFixing)
31 : CommodityFuturesIndex(underlyingName, expiryDate, fixingCalendar, priceCurve), basisFec_(basisFec),
32 baseIndex_(baseIndex), baseFec_(baseFec), addBasis_(addBasis), monthOffset_(monthOffset),
33 baseIsAveraging_(baseIsAveraging), priceAsHistoricalFixing_(priceAsHistoricalFixing) {
34 QL_REQUIRE(expiryDate_ != Date(), "non-empty expiry date expected for CommodityFuturesIndex");
35 QL_REQUIRE(baseIndex_ != nullptr, "non-null baseIndex required for CommodityBasisFutureIndex");
36 QL_REQUIRE(basisFec_ != nullptr,
37 "non-null future expiry calculator for the basis conventions CommodityBasisFutureIndex");
38 QL_REQUIRE(baseFec_ != nullptr,
39 "non-null future expiry calculator for the base conventions CommodityBasisFutureIndex");
40 registerWith(baseIndex);
41 if (priceAsHistoricalFixing_ == false)
43}
44
46 const std::string& underlyingName, const QuantLib::Date& expiryDate, const QuantLib::Calendar& fixingCalendar,
47 const QuantLib::ext::shared_ptr<CommodityBasisPriceTermStructure>& priceCurve)
48 : CommodityBasisFutureIndex(underlyingName, expiryDate, fixingCalendar, priceCurve->basisFutureExpiryCalculator(),
49 priceCurve->baseIndex(), priceCurve->baseFutureExpiryCalculator(),
50 Handle<PriceTermStructure>(priceCurve), priceCurve->addBasis(),
51 priceCurve->monthOffset(), priceCurve->averagingBaseCashflow(),
52 priceCurve->priceAsHistoricalFixing()) {}
53
54QuantLib::ext::shared_ptr<CommodityIndex>
55CommodityBasisFutureIndex::clone(const QuantLib::Date& expiry,
56 const boost::optional<QuantLib::Handle<PriceTermStructure>>& ts) const {
57 const auto& pts = ts ? *ts : priceCurve();
58 const auto& ed = expiry == Date() ? expiryDate() : expiry;
59 return QuantLib::ext::make_shared<CommodityBasisFutureIndex>(underlyingName(), ed, fixingCalendar(), basisFec_, baseIndex_,
62}
63
64QuantLib::ext::shared_ptr<QuantLib::CashFlow> CommodityBasisFutureIndex::baseCashflow(const QuantLib::Date& paymentDate) const {
65 // Fail-safe if expiryDate_ is not a future expiry date
66 auto nextFutureExpiry = basisFec_->nextExpiry(true, expiryDate_);
67 // Imply the contract month from the future expiry data
68 auto contractDate = basisFec_->contractDate(nextFutureExpiry);
69 Date periodStart = Date(1,contractDate.month(), contractDate.year()) - monthOffset_ * Months;
70 Date periodEnd = (periodStart + 1 * Months) - 1 * Days;
71 // Build the corresponding base-future casflow
73 paymentDate);
74}
75
76QuantLib::Real CommodityBasisFutureIndex::pastFixing(const QuantLib::Date& fixingDate) const {
77 auto basisFixing = CommodityFuturesIndex::pastFixing(fixingDate);
79 return basisFixing;
80 } else if (basisFixing == QuantLib::Null<Real>()) {
81 return QuantLib::Null<Real>();
82 } else {
83 return addBasis_ ? cashflow_->amount() + basisFixing : cashflow_->amount() - basisFixing;
84 }
85}
86} // namespace QuantExt
QuantLib::ext::shared_ptr< FutureExpiryCalculator > baseFec_
CommodityBasisFutureIndex(const std::string &underlyingName, const QuantLib::Date &expiryDate, const QuantLib::Calendar &fixingCalendar, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &basisFec, const QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > &baseIndex, const QuantLib::ext::shared_ptr< FutureExpiryCalculator > &baseFec, const QuantLib::Handle< QuantExt::PriceTermStructure > &priceCurve=QuantLib::Handle< QuantExt::PriceTermStructure >(), const bool addBasis=true, const QuantLib::Size monthOffset=0, const bool baseIsAveraging=false, const bool priceAsHistoricalFixing=true)
const QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > & baseIndex()
QuantLib::ext::shared_ptr< QuantExt::CommodityIndex > baseIndex_
QuantLib::ext::shared_ptr< FutureExpiryCalculator > basisFec_
QuantLib::ext::shared_ptr< CommodityIndex > clone(const QuantLib::Date &expiryDate=QuantLib::Date(), const boost::optional< QuantLib::Handle< PriceTermStructure > > &ts=boost::none) const override
Implement the base clone. Ajust the base future to match the same contract month.
QuantLib::ext::shared_ptr< QuantLib::CashFlow > cashflow_
QuantLib::Real pastFixing(const QuantLib::Date &fixingDate) const override
QuantLib::ext::shared_ptr< QuantLib::CashFlow > baseCashflow(const QuantLib::Date &paymentDate=QuantLib::Date()) const
const QuantLib::Date & expiryDate() const
Calendar fixingCalendar() const override
const Handle< QuantExt::PriceTermStructure > & priceCurve() const
std::string underlyingName() const
virtual Real pastFixing(const Date &fixingDate) const override
returns a past fixing at the given date
commodity basis future index class for holding price histories and forwarding.
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