Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
averagespotpricehelper.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::Quote;
27using QuantLib::Real;
28using QuantLib::Visitor;
29
30namespace QuantExt {
31
32AverageSpotPriceHelper::AverageSpotPriceHelper(const Handle<Quote>& price,
33 const QuantLib::ext::shared_ptr<CommoditySpotIndex>& index,
34 const Date& start,
35 const Date& end,
36 const Calendar& calendar,
37 bool useBusinessDays)
38 : PriceHelper(price) {
39 init(index, start, end, calendar, useBusinessDays);
40}
41
42AverageSpotPriceHelper::AverageSpotPriceHelper(Real price,
43 const QuantLib::ext::shared_ptr<CommoditySpotIndex>& index,
44 const Date& start,
45 const Date& end,
46 const Calendar& calendar,
47 bool useBusinessDays)
48 : PriceHelper(price) {
49 init(index, start, end, calendar, useBusinessDays);
50}
51
52void AverageSpotPriceHelper::init(const QuantLib::ext::shared_ptr<CommoditySpotIndex>& index,
53 const Date& start, const Date& end, const Calendar& calendar, bool useBusinessDays) {
54
55 // Make a copy of the commodity spot index linked to this price helper's price term structure handle,
56 // termStructureHandle_.
57 auto indexClone = QuantLib::ext::make_shared<CommoditySpotIndex>(index->underlyingName(),
58 index->fixingCalendar(), termStructureHandle_);
59
60 // While bootstrapping is happening, this price helper's price term structure handle, termStructureHandle_, will
61 // be updated multiple times. We don't want the index notified each time.
62 indexClone->unregisterWith(termStructureHandle_);
63 registerWith(indexClone);
64
65 // Create the averaging cashflow referencing the commodity spot index. Need to specify all the defaults
66 // here just to amend the final default parameter i.e. set excludeStartDate to false.
67 averageCashflow_ = QuantLib::ext::make_shared<CommodityIndexedAverageCashFlow>(1.0, start, end, end, indexClone,
68 calendar, 0.0, 1.0, false, 0, 0, nullptr, true, false, useBusinessDays);
69
70 // Get the date index pairs involved in the averaging. The earliest date is the date of the first element
71 // and the latest date is the date of the last element.
72 const auto& mp = averageCashflow_->indices();
73 earliestDate_ = mp.begin()->first;
74 pillarDate_ = mp.rbegin()->first;
75}
76
77Real AverageSpotPriceHelper::impliedQuote() const {
78 QL_REQUIRE(termStructure_, "AverageSpotPriceHelper term structure not set.");
79 return averageCashflow_->amount();
80}
81
82void AverageSpotPriceHelper::setTermStructure(PriceTermStructure* ts) {
83 QuantLib::ext::shared_ptr<PriceTermStructure> temp(ts, null_deleter());
84 // Do not set the relinkable handle as an observer i.e. registerAsObserver is false here.
85 termStructureHandle_.linkTo(temp, false);
86 PriceHelper::setTermStructure(ts);
87}
88
89void AverageSpotPriceHelper::accept(AcyclicVisitor& v) {
90 if (auto vis = dynamic_cast<Visitor<AverageSpotPriceHelper>*>(&v))
91 vis->visit(*this);
92 else
93 PriceHelper::accept(v);
94}
95
96QuantLib::ext::shared_ptr<CommodityIndexedAverageCashFlow> AverageSpotPriceHelper::averageCashflow() const {
97 return averageCashflow_;
98}
99
100}
Price helper for average of spot price over a period.
AverageSpotPriceHelper(const QuantLib::Handle< QuantLib::Quote > &price, const QuantLib::ext::shared_ptr< CommoditySpotIndex > &index, const QuantLib::Date &start, const QuantLib::Date &end, const QuantLib::Calendar &calendar=QuantLib::Calendar(), bool useBusinessDays=true)