QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdblackscholesvanillaengine.cpp
Go to the documentation of this file.
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Andreas Gaida
5 Copyright (C) 2008, 2009 Ralph Schreyer
6 Copyright (C) 2008, 2009 Klaus Spanderen
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/exercise.hpp>
34
35namespace QuantLib {
36
38 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
39 Size tGrid,
40 Size xGrid,
41 Size dampingSteps,
42 const FdmSchemeDesc& schemeDesc,
43 bool localVol,
44 Real illegalLocalVolOverwrite,
45 CashDividendModel cashDividendModel)
46 : process_(std::move(process)), tGrid_(tGrid), xGrid_(xGrid),
47 dampingSteps_(dampingSteps), schemeDesc_(schemeDesc), localVol_(localVol),
48 illegalLocalVolOverwrite_(illegalLocalVolOverwrite), cashDividendModel_(cashDividendModel) {
49 registerWith(process_);
50 }
51
53 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
54 DividendSchedule dividends,
55 Size tGrid,
56 Size xGrid,
57 Size dampingSteps,
58 const FdmSchemeDesc& schemeDesc,
59 bool localVol,
60 Real illegalLocalVolOverwrite,
61 CashDividendModel cashDividendModel)
62 : process_(std::move(process)), dividends_(std::move(dividends)),
63 tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps), schemeDesc_(schemeDesc),
64 localVol_(localVol), illegalLocalVolOverwrite_(illegalLocalVolOverwrite),
65 cashDividendModel_(cashDividendModel) {
66 registerWith(process_);
67 }
68
70 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
71 ext::shared_ptr<FdmQuantoHelper> quantoHelper,
72 Size tGrid,
73 Size xGrid,
74 Size dampingSteps,
75 const FdmSchemeDesc& schemeDesc,
76 bool localVol,
77 Real illegalLocalVolOverwrite,
78 CashDividendModel cashDividendModel)
79 : process_(std::move(process)),
80 tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
81 schemeDesc_(schemeDesc), localVol_(localVol),
82 illegalLocalVolOverwrite_(illegalLocalVolOverwrite), quantoHelper_(std::move(quantoHelper)),
83 cashDividendModel_(cashDividendModel) {
84 registerWith(process_);
85 registerWith(quantoHelper_);
86 }
87
89 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
90 DividendSchedule dividends,
91 ext::shared_ptr<FdmQuantoHelper> quantoHelper,
92 Size tGrid,
93 Size xGrid,
94 Size dampingSteps,
95 const FdmSchemeDesc& schemeDesc,
96 bool localVol,
97 Real illegalLocalVolOverwrite,
98 CashDividendModel cashDividendModel)
99 : process_(std::move(process)), dividends_(std::move(dividends)),
100 tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
101 schemeDesc_(schemeDesc), localVol_(localVol),
102 illegalLocalVolOverwrite_(illegalLocalVolOverwrite), quantoHelper_(std::move(quantoHelper)),
103 cashDividendModel_(cashDividendModel) {
104 registerWith(process_);
105 registerWith(quantoHelper_);
106 }
107
108
110
111 // 0. Cash dividend model
112 const Date exerciseDate = arguments_.exercise->lastDate();
113 const Time maturity = process_->time(exerciseDate);
114 const Date settlementDate = process_->riskFreeRate()->referenceDate();
115
116 Real spotAdjustment = 0.0;
117 DividendSchedule dividendSchedule = DividendSchedule();
118
119 ext::shared_ptr<EscrowedDividendAdjustment> escrowedDivAdj;
120
121 switch (cashDividendModel_) {
122 case Spot:
123 dividendSchedule = dividends_;
124 break;
125 case Escrowed:
126 if (arguments_.exercise->type() != Exercise::European)
127 // add dividend dates as stopping times
128 for (const auto& cf: dividends_)
129 dividendSchedule.push_back(
130 ext::make_shared<FixedDividend>(0.0, cf->date()));
131
132 QL_REQUIRE(quantoHelper_ == nullptr,
133 "Escrowed dividend model is not supported for Quanto-Options");
134
135 escrowedDivAdj = ext::make_shared<EscrowedDividendAdjustment>(
137 process_->riskFreeRate(),
138 process_->dividendYield(),
139 [&](Date d){ return process_->time(d); },
140 maturity
141 );
142
143 spotAdjustment =
144 escrowedDivAdj->dividendAdjustment(process_->time(settlementDate));
145
146 QL_REQUIRE(process_->x0() + spotAdjustment > 0.0,
147 "spot minus dividends becomes negative");
148
149 break;
150 default:
151 QL_FAIL("unknwon cash dividend model");
152 }
153
154 // 1. Mesher
155 const ext::shared_ptr<StrikedTypePayoff> payoff =
156 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
157
158 const ext::shared_ptr<Fdm1dMesher> equityMesher =
159 ext::make_shared<FdmBlackScholesMesher>(
160 xGrid_, process_, maturity, payoff->strike(),
161 Null<Real>(), Null<Real>(), 0.0001, 1.5,
162 std::pair<Real, Real>(payoff->strike(), 0.1),
163 dividendSchedule, quantoHelper_,
164 spotAdjustment);
165
166 const ext::shared_ptr<FdmMesher> mesher =
167 ext::make_shared<FdmMesherComposite>(equityMesher);
168
169 // 2. Calculator
170 ext::shared_ptr<FdmInnerValueCalculator> calculator;
171 switch (cashDividendModel_) {
172 case Spot:
173 calculator = ext::make_shared<FdmLogInnerValue>(
174 payoff, mesher, 0);
175 break;
176 case Escrowed:
177 calculator = ext::make_shared<FdmEscrowedLogInnerValueCalculator>(
178 escrowedDivAdj, payoff, mesher, 0);
179 break;
180 default:
181 QL_FAIL("unknwon cash dividend model");
182 }
183
184 // 3. Step conditions
185 const ext::shared_ptr<FdmStepConditionComposite> conditions =
187 dividendSchedule, arguments_.exercise, mesher, calculator,
188 process_->riskFreeRate()->referenceDate(),
189 process_->riskFreeRate()->dayCounter());
190
191 // 4. Boundary conditions
192 const FdmBoundaryConditionSet boundaries;
193
194 // 5. Solver
195 FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
196 maturity, tGrid_, dampingSteps_ };
197
198 const ext::shared_ptr<FdmBlackScholesSolver> solver(
199 ext::make_shared<FdmBlackScholesSolver>(
201 payoff->strike(), solverDesc, schemeDesc_,
204
205 const Real spot = process_->x0() + spotAdjustment;
206
207 results_.value = solver->valueAt(spot);
208 results_.delta = solver->deltaAt(spot);
209 results_.gamma = solver->gammaAt(spot);
210 results_.theta = solver->thetaAt(spot);
211 }
212
214 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
215 : process_(std::move(process)),
216 schemeDesc_(ext::make_shared<FdmSchemeDesc>(FdmSchemeDesc::Douglas())),
217 illegalLocalVolOverwrite_(-Null<Real>()) {}
218
221 const ext::shared_ptr<FdmQuantoHelper>& quantoHelper) {
222 quantoHelper_ = quantoHelper;
223 return *this;
224 }
225
228 tGrid_ = tGrid;
229 return *this;
230 }
231
234 xGrid_ = xGrid;
235 return *this;
236 }
237
240 dampingSteps_ = dampingSteps;
241 return *this;
242 }
243
246 const FdmSchemeDesc& schemeDesc) {
247 schemeDesc_ = ext::make_shared<FdmSchemeDesc>(schemeDesc);
248 return *this;
249 }
250
253 localVol_ = localVol;
254 return *this;
255 }
256
259 Real illegalLocalVolOverwrite) {
260 illegalLocalVolOverwrite_ = illegalLocalVolOverwrite;
261 return *this;
262 }
263
266 const std::vector<Date>& dividendDates,
267 const std::vector<Real>& dividendAmounts) {
268 dividends_ = DividendVector(dividendDates, dividendAmounts);
269 return *this;
270 }
271
275 cashDividendModel_ = cashDividendModel;
276 return *this;
277 }
278
279 MakeFdBlackScholesVanillaEngine::operator
280 ext::shared_ptr<PricingEngine>() const {
281 return ext::make_shared<FdBlackScholesVanillaEngine>(
282 process_,
283 dividends_,
284 quantoHelper_,
285 tGrid_, xGrid_, dampingSteps_,
286 *schemeDesc_,
287 localVol_,
288 illegalLocalVolOverwrite_,
289 cashDividendModel_);
290 }
291
292}
Black-Scholes processes.
const Instrument::results * results_
Definition: cdsoption.cpp:63
Concrete date class.
Definition: date.hpp:125
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
ext::shared_ptr< FdmQuantoHelper > quantoHelper_
FdBlackScholesVanillaEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >, Size tGrid=100, Size xGrid=100, Size dampingSteps=0, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Douglas(), bool localVol=false, Real illegalLocalVolOverwrite=-Null< Real >(), CashDividendModel cashDividendModel=Spot)
static ext::shared_ptr< FdmStepConditionComposite > vanillaComposite(const DividendSchedule &schedule, const ext::shared_ptr< Exercise > &exercise, const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< FdmInnerValueCalculator > &calculator, const Date &refDate, const DayCounter &dayCounter)
Shared handle to an observable.
Definition: handle.hpp:41
MakeFdBlackScholesVanillaEngine & withIllegalLocalVolOverwrite(Real illegalLocalVolOverwrite)
MakeFdBlackScholesVanillaEngine & withLocalVol(bool localVol)
MakeFdBlackScholesVanillaEngine & withCashDividendModel(FdBlackScholesVanillaEngine::CashDividendModel cashDividendModel)
MakeFdBlackScholesVanillaEngine & withTGrid(Size tGrid)
MakeFdBlackScholesVanillaEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process)
FdBlackScholesVanillaEngine::CashDividendModel cashDividendModel_
MakeFdBlackScholesVanillaEngine & withDampingSteps(Size dampingSteps)
MakeFdBlackScholesVanillaEngine & withQuantoHelper(const ext::shared_ptr< FdmQuantoHelper > &quantoHelper)
MakeFdBlackScholesVanillaEngine & withXGrid(Size xGrid)
MakeFdBlackScholesVanillaEngine & withCashDividends(const std::vector< Date > &dividendDates, const std::vector< Real > &dividendAmounts)
MakeFdBlackScholesVanillaEngine & withFdmSchemeDesc(const FdmSchemeDesc &schemeDesc)
template class providing a null value for a given type.
Definition: null.hpp:76
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Date d
Option exercise classes and payoff function.
Finite-differences Black Scholes vanilla option engine.
1-d mesher for the Black-Scholes process (in ln(S))
inner value for a escrowed dividend model
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
FdmMesher which is a composite of Fdm1dMesher.
helper class storing market data needed for the quanto adjustment.
composite of fdm step conditions
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
std::vector< ext::shared_ptr< Dividend > > DividendVector(const std::vector< Date > &dividendDates, const std::vector< Real > &dividends)
helper function building a sequence of fixed dividends
Definition: dividend.cpp:35
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.