Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
averagefuturepricehelper.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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#include <ql/utilities/null_deleter.hpp>
21
22using QuantLib::AcyclicVisitor;
23using QuantLib::Calendar;
24using QuantLib::Date;
25using QuantLib::Handle;
26using QuantLib::Natural;
27using QuantLib::Quote;
28using QuantLib::Real;
29using QuantLib::Visitor;
30
31namespace QuantExt {
32
34 const QuantLib::ext::shared_ptr<CommodityIndex>& index,
35 const Date& start,
36 const Date& end,
37 const QuantLib::ext::shared_ptr<FutureExpiryCalculator>& calc,
38 const Calendar& calendar,
39 Natural deliveryDateRoll,
40 Natural futureMonthOffset,
41 bool useBusinessDays,
42 Natural dailyExpiryOffset)
43 : PriceHelper(price) {
44 init(index, start, end, calc, calendar, deliveryDateRoll, futureMonthOffset, useBusinessDays, dailyExpiryOffset);
45}
46
47AverageFuturePriceHelper::AverageFuturePriceHelper(Real price,
48 const QuantLib::ext::shared_ptr<CommodityIndex>& index,
49 const Date& start,
50 const Date& end,
51 const QuantLib::ext::shared_ptr<FutureExpiryCalculator>& calc,
52 const Calendar& calendar,
53 Natural deliveryDateRoll,
54 Natural futureMonthOffset,
55 bool useBusinessDays,
56 Natural dailyExpiryOffset)
57 : PriceHelper(price) {
58 init(index, start, end, calc, calendar, deliveryDateRoll, futureMonthOffset, useBusinessDays, dailyExpiryOffset);
59}
60
61void AverageFuturePriceHelper::init(const QuantLib::ext::shared_ptr<CommodityIndex>& index,
62 const Date& start, const Date& end, const QuantLib::ext::shared_ptr<FutureExpiryCalculator>& calc,
63 const Calendar& calendar, Natural deliveryDateRoll, Natural futureMonthOffset,
64 bool useBusinessDays, Natural dailyExpiryOffset) {
65
66 // Make a copy of the commodity index linked to this price helper's price term structure handle,
67 // termStructureHandle_.
68 auto indexClone = index->clone(Date(), termStructureHandle_);
69
70 // While bootstrapping is happening, this price helper's price term structure handle, termStructureHandle_, will
71 // be updated multiple times. We don't want the index notified each time.
72 indexClone->unregisterWith(termStructureHandle_);
73 registerWith(indexClone);
74
75 // Create the averaging cashflow referencing the commodity index.
76 averageCashflow_ = QuantLib::ext::make_shared<CommodityIndexedAverageCashFlow>(1.0, start, end, end, indexClone,
77 calendar, 0.0, 1.0, true, deliveryDateRoll, futureMonthOffset, calc, true, false, useBusinessDays,
78 CommodityQuantityFrequency::PerCalculationPeriod, Null<Natural>(), dailyExpiryOffset);
79
80 // Get the date index pairs involved in the averaging. The earliest date is the expiry date of the future contract
81 // referenced in the first element and the latest date is the expiry date of the future contract referenced in
82 // the last element.
83 const auto& mp = averageCashflow_->indices();
84 earliestDate_ = mp.begin()->second->expiryDate();
85 pillarDate_ = mp.rbegin()->second->expiryDate();
86}
87
88Real AverageFuturePriceHelper::impliedQuote() const {
89 QL_REQUIRE(termStructure_, "AverageFuturePriceHelper term structure not set.");
90 averageCashflow_->update();
91 return averageCashflow_->amount();
92}
93
94void AverageFuturePriceHelper::setTermStructure(PriceTermStructure* ts) {
95 QuantLib::ext::shared_ptr<PriceTermStructure> temp(ts, null_deleter());
96 // Do not set the relinkable handle as an observer i.e. registerAsObserver is false here.
97 termStructureHandle_.linkTo(temp, false);
98 PriceHelper::setTermStructure(ts);
99}
100
101void AverageFuturePriceHelper::accept(AcyclicVisitor& v) {
102 if (auto vis = dynamic_cast<Visitor<AverageFuturePriceHelper>*>(&v))
103 vis->visit(*this);
104 else
105 PriceHelper::accept(v);
106}
107
108QuantLib::ext::shared_ptr<CommodityIndexedAverageCashFlow> AverageFuturePriceHelper::averageCashflow() const {
109 return averageCashflow_;
110}
111
112void AverageFuturePriceHelper::deepUpdate() {
113 if (averageCashflow_)
114 averageCashflow_->update();
115 update();
116}
117
118} // namespace QuantExt
Price helper for average of future settlement prices over a period.
AverageFuturePriceHelper(const QuantLib::Handle< QuantLib::Quote > &price, const QuantLib::ext::shared_ptr< CommodityIndex > &index, const QuantLib::Date &start, const QuantLib::Date &end, const ext::shared_ptr< FutureExpiryCalculator > &calc, const QuantLib::Calendar &calendar=QuantLib::Calendar(), QuantLib::Natural deliveryDateRoll=0, QuantLib::Natural futureMonthOffset=0, bool useBusinessDays=true, QuantLib::Natural dailyExpiryOffset=QuantLib::Null< QuantLib::Natural >())