Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
eqcommoptionsurfacestripper.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/termstructures/eqcommoptionsurfacestripper.hpp
20 \brief Imply equity or commodity volatility surface from put/call price surfaces
21 \ingroup termstructures
22*/
23
24#ifndef quantext_eq_comm_option_surface_stripper_hpp
25#define quantext_eq_comm_option_surface_stripper_hpp
26
31#include <ql/exercise.hpp>
32#include <ql/instruments/vanillaoption.hpp>
33#include <ql/math/solvers1d/brent.hpp>
34#include <ql/patterns/lazyobject.hpp>
35#include <ql/processes/blackscholesprocess.hpp>
36#include <ql/quotes/simplequote.hpp>
37#include <ql/termstructures/yieldtermstructure.hpp>
38
39namespace QuantExt {
40
41/*! A simple struct to group together options used by a Solver1D instance.
42
43 This declaration may be moved to a common header if useful for other classes.
44*/
46 //! The maximum number of evaluations. Default used if not set.
47 QuantLib::Size maxEvaluations = QuantLib::Null<QuantLib::Size>();
48 //! The accuracy for the search.
49 QuantLib::Real accuracy = QuantLib::Null<QuantLib::Real>();
50 //! The initial guess for the search.
51 QuantLib::Real initialGuess = QuantLib::Null<QuantLib::Real>();
52 //! Set the minimum and maximum search.
53 std::pair<QuantLib::Real, QuantLib::Real> minMax =
54 std::make_pair(QuantLib::Null<QuantLib::Real>(), QuantLib::Null<QuantLib::Real>());
55 //! Set the step size for the search.
56 QuantLib::Real step = QuantLib::Null<QuantLib::Real>();
57 //! The lower bound of the search domain. A \c Null<Real>() indicates that the bound should not be set.
58 QuantLib::Real lowerBound = QuantLib::Null<QuantLib::Real>();
59 //! The upper bound of the search domain. A \c Null<Real>() indicates that the bound should not be set.
60 QuantLib::Real upperBound = QuantLib::Null<QuantLib::Real>();
61};
62
63//! Abstract base class for the option stripper
64class OptionSurfaceStripper : public QuantLib::LazyObject {
65
66public:
67 OptionSurfaceStripper(const QuantLib::ext::shared_ptr<OptionInterpolatorBase>& callSurface,
68 const QuantLib::ext::shared_ptr<OptionInterpolatorBase>& putSurface,
69 const QuantLib::Calendar& calendar,
70 const QuantLib::DayCounter& dayCounter,
71 QuantLib::Exercise::Type type = QuantLib::Exercise::European,
72 bool lowerStrikeConstExtrap = true,
73 bool upperStrikeConstExtrap = true,
74 bool timeFlatExtrapolation = false,
75 bool preferOutOfTheMoney = false,
76 Solver1DOptions solverOptions = {});
77
78 //! \name LazyObject interface
79 //@{
80 void performCalculations() const override;
81 //@}
82
83 //! Return the stripped volatility structure.
84 QuantLib::ext::shared_ptr<QuantLib::BlackVolTermStructure> volSurface();
85
86protected:
87 //! Generate the relevant Black Scholes process for the underlying.
88 virtual QuantLib::ext::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> process(
89 const QuantLib::ext::shared_ptr<QuantLib::SimpleQuote>& volatilityQuote) const = 0;
90
91 //! Return the forward price at a given date.
92 virtual QuantLib::Real forward(const QuantLib::Date& date) const = 0;
93
94 QuantLib::ext::shared_ptr<OptionInterpolatorBase> callSurface_;
95 QuantLib::ext::shared_ptr<OptionInterpolatorBase> putSurface_;
96 const QuantLib::Calendar& calendar_;
97 const QuantLib::DayCounter& dayCounter_;
98 QuantLib::Exercise::Type type_;
103
104private:
105 //! Function object used in solving.
107 public:
108 PriceError(const QuantLib::VanillaOption& option, QuantLib::SimpleQuote& volatility,
109 QuantLib::Real targetPrice);
110
111 QuantLib::Real operator()(QuantLib::Volatility volatility) const;
112
113 private:
114 const QuantLib::VanillaOption& option_;
115 QuantLib::SimpleQuote& volatility_;
116 QuantLib::Real targetPrice_;
117 };
118
119 //! Retrieve the vector of strikes at a given expiry date.
120 std::vector<QuantLib::Real> strikes(const QuantLib::Date& expiry, bool isCall) const;
121
122 /*! Imply the volatility at a given \p expiry and \p strike for the given option \p type. The exercise type is
123 indicated by the member variable \c type_ and the target price is read off the relevant price surface i.e.
124 either \c callSurface_ or \c putSurface_.
125
126 If the root finding fails, a \c Null<Real>() is returned.
127 */
128 QuantLib::Real implyVol(QuantLib::Date expiry,
129 QuantLib::Real strike,
130 QuantLib::Option::Type type,
131 QuantLib::ext::shared_ptr<QuantLib::PricingEngine> engine,
132 QuantLib::SimpleQuote& volQuote) const;
133
134 // Apply the solver options.
135 void setUpSolver();
136
137 mutable QuantLib::ext::shared_ptr<QuantLib::BlackVolTermStructure> volSurface_;
138
139 //! Solver used when implying volatility from price.
140 Brent brent_;
142
143 //! Set to \c true if we must strip volatilities from prices.
145
146 //! Store the function that will be called each time to solve for volatility
147 std::function<Real(const PriceError&)> solver_;
148};
149
151
152public:
153 EquityOptionSurfaceStripper(const QuantLib::Handle<QuantExt::EquityIndex2>& equityIndex,
154 const QuantLib::ext::shared_ptr<OptionInterpolatorBase>& callSurface,
155 const QuantLib::ext::shared_ptr<OptionInterpolatorBase>& putSurface,
156 const QuantLib::Calendar& calendar,
157 const QuantLib::DayCounter& dayCounter,
158 QuantLib::Exercise::Type type = QuantLib::Exercise::European,
159 bool lowerStrikeConstExtrap = true,
160 bool upperStrikeConstExtrap = true,
161 bool timeFlatExtrapolation = false,
162 bool preferOutOfTheMoney = false,
163 Solver1DOptions solverOptions = {});
164
165protected:
166 //! \name OptionSurfaceStripper interface
167 //@{
168 QuantLib::ext::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> process(
169 const QuantLib::ext::shared_ptr<QuantLib::SimpleQuote>& volatilityQuote) const override;
170
171 QuantLib::Real forward(const QuantLib::Date& date) const override;
172 //@}
173
174private:
175 QuantLib::Handle<QuantExt::EquityIndex2> equityIndex_;
176};
177
179
180public:
182 const QuantLib::Handle<QuantExt::PriceTermStructure>& priceCurve,
183 const QuantLib::Handle<QuantLib::YieldTermStructure>& discountCurve,
184 const QuantLib::ext::shared_ptr<OptionInterpolatorBase>& callSurface,
185 const QuantLib::ext::shared_ptr<OptionInterpolatorBase>& putSurface,
186 const QuantLib::Calendar& calendar,
187 const QuantLib::DayCounter& dayCounter,
188 QuantLib::Exercise::Type type = QuantLib::Exercise::European,
189 bool lowerStrikeConstExtrap = true,
190 bool upperStrikeConstExtrap = true,
191 bool timeFlatExtrapolation = false,
192 bool preferOutOfTheMoney = false,
193 Solver1DOptions solverOptions = {});
194
195protected:
196 //! \name OptionSurfaceStripper interface
197 //@{
198 QuantLib::ext::shared_ptr<QuantLib::GeneralizedBlackScholesProcess> process(
199 const QuantLib::ext::shared_ptr<QuantLib::SimpleQuote>& volatilityQuote) const override;
200
201 QuantLib::Real forward(const QuantLib::Date& date) const override;
202 //@}
203
204private:
205 QuantLib::Handle<QuantExt::PriceTermStructure> priceCurve_;
206 QuantLib::Handle<QuantLib::YieldTermStructure> discountCurve_;
207};
208
209}
210#endif
QuantLib::Handle< QuantExt::PriceTermStructure > priceCurve_
QuantLib::Handle< QuantLib::YieldTermStructure > discountCurve_
QuantLib::Real forward(const QuantLib::Date &date) const override
Return the forward price at a given date.
QuantLib::ext::shared_ptr< QuantLib::GeneralizedBlackScholesProcess > process(const QuantLib::ext::shared_ptr< QuantLib::SimpleQuote > &volatilityQuote) const override
Generate the relevant Black Scholes process for the underlying.
QuantLib::Handle< QuantExt::EquityIndex2 > equityIndex_
QuantLib::Real forward(const QuantLib::Date &date) const override
Return the forward price at a given date.
QuantLib::ext::shared_ptr< QuantLib::GeneralizedBlackScholesProcess > process(const QuantLib::ext::shared_ptr< QuantLib::SimpleQuote > &volatilityQuote) const override
Generate the relevant Black Scholes process for the underlying.
QuantLib::Real operator()(QuantLib::Volatility volatility) const
Abstract base class for the option stripper.
const QuantLib::DayCounter & dayCounter_
virtual QuantLib::ext::shared_ptr< QuantLib::GeneralizedBlackScholesProcess > process(const QuantLib::ext::shared_ptr< QuantLib::SimpleQuote > &volatilityQuote) const =0
Generate the relevant Black Scholes process for the underlying.
QuantLib::ext::shared_ptr< QuantLib::BlackVolTermStructure > volSurface_
bool havePrices_
Set to true if we must strip volatilities from prices.
virtual QuantLib::Real forward(const QuantLib::Date &date) const =0
Return the forward price at a given date.
QuantLib::ext::shared_ptr< QuantLib::BlackVolTermStructure > volSurface()
Return the stripped volatility structure.
QuantLib::ext::shared_ptr< OptionInterpolatorBase > putSurface_
std::function< Real(const PriceError &)> solver_
Store the function that will be called each time to solve for volatility.
Brent brent_
Solver used when implying volatility from price.
QuantLib::Real implyVol(QuantLib::Date expiry, QuantLib::Real strike, QuantLib::Option::Type type, QuantLib::ext::shared_ptr< QuantLib::PricingEngine > engine, QuantLib::SimpleQuote &volQuote) const
QuantLib::ext::shared_ptr< OptionInterpolatorBase > callSurface_
equity index class for holding equity fixing histories and forwarding.
Surface to store option prices.
Term structure of prices.
std::pair< QuantLib::Real, QuantLib::Real > minMax
Set the minimum and maximum search.
QuantLib::Real accuracy
The accuracy for the search.
QuantLib::Real initialGuess
The initial guess for the search.
QuantLib::Real upperBound
The upper bound of the search domain. A Null<Real>() indicates that the bound should not be set.
QuantLib::Real lowerBound
The lower bound of the search domain. A Null<Real>() indicates that the bound should not be set.
QuantLib::Real step
Set the step size for the search.
QuantLib::Size maxEvaluations
The maximum number of evaluations. Default used if not set.
vector< Real > strikes