QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdblackscholesbarrierengine.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 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>
36#include <utility>
37
38namespace QuantLib {
39
41 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
42 Size tGrid,
43 Size xGrid,
44 Size dampingSteps,
45 const FdmSchemeDesc& schemeDesc,
46 bool localVol,
47 Real illegalLocalVolOverwrite)
48 : process_(std::move(process)),
49 tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
50 schemeDesc_(schemeDesc), localVol_(localVol),
51 illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
52
54 }
55
57 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
58 DividendSchedule dividends,
59 Size tGrid,
60 Size xGrid,
61 Size dampingSteps,
62 const FdmSchemeDesc& schemeDesc,
63 bool localVol,
64 Real illegalLocalVolOverwrite)
65 : process_(std::move(process)), dividends_(std::move(dividends)),
66 tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
67 schemeDesc_(schemeDesc), localVol_(localVol),
68 illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
69
71 }
72
74
75 // 1. Mesher
76 const ext::shared_ptr<StrikedTypePayoff> payoff =
77 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
78
79 QL_REQUIRE(payoff, "non-striked type payoff given");
80 QL_REQUIRE(payoff->strike() > 0.0, "strike must be positive");
81
82 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
83 "only european style option are supported");
84
85 const auto spot = process_->x0();
86 QL_REQUIRE(spot > 0.0, "negative or null underlying given");
87 QL_REQUIRE(!triggered(spot), "barrier touched");
88
89 const Time maturity = process_->time(arguments_.exercise->lastDate());
90
91 Real xMin=Null<Real>();
92 Real xMax=Null<Real>();
95 xMin = std::log(arguments_.barrier);
96 }
99 xMax = std::log(arguments_.barrier);
100 }
101
102 const ext::shared_ptr<Fdm1dMesher> equityMesher(
104 xGrid_, process_, maturity, payoff->strike(),
105 xMin, xMax, 0.0001, 1.5,
106 std::make_pair(Null<Real>(), Null<Real>()),
107 dividends_));
108
109 const ext::shared_ptr<FdmMesher> mesher (
110 ext::make_shared<FdmMesherComposite>(equityMesher));
111
112 // 2. Calculator
113 ext::shared_ptr<FdmInnerValueCalculator> calculator(
114 ext::make_shared<FdmLogInnerValue>(payoff, mesher, 0));
115
116 // 3. Step conditions
117 std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
118 std::list<std::vector<Time> > stoppingTimes;
119
120 // 3.1 Step condition if discrete dividends
121 ext::shared_ptr<FdmDividendHandler> dividendCondition(
122 ext::make_shared<FdmDividendHandler>(dividends_, mesher,
123 process_->riskFreeRate()->referenceDate(),
124 process_->riskFreeRate()->dayCounter(), 0));
125
126 if (!dividends_.empty()) {
127 stepConditions.push_back(dividendCondition);
128 std::vector<Time> dividendTimes = dividendCondition->dividendTimes();
129 // this effectively excludes times after maturity
130 for (auto& t: dividendTimes)
131 t = std::min(maturity, t);
132 stoppingTimes.push_back(dividendTimes);
133 }
134
135 ext::shared_ptr<FdmStepConditionComposite> conditions(
136 ext::make_shared<FdmStepConditionComposite>(stoppingTimes, stepConditions));
137
138 // 4. Boundary conditions
139 FdmBoundaryConditionSet boundaries;
142 boundaries.push_back(
143 ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
145
146 }
147
150 boundaries.push_back(
151 ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
153 }
154
155 // 5. Solver
156 FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
157 maturity, tGrid_, dampingSteps_ };
158
159 ext::shared_ptr<FdmBlackScholesSolver> solver(
160 ext::make_shared<FdmBlackScholesSolver>(
162 payoff->strike(), solverDesc, schemeDesc_,
164
165 results_.value = solver->valueAt(spot);
166 results_.delta = solver->deltaAt(spot);
167 results_.gamma = solver->gammaAt(spot);
168 results_.theta = solver->thetaAt(spot);
169
170 // 6. Calculate vanilla option and rebate for in-barriers
173 // Cast the payoff
174 ext::shared_ptr<StrikedTypePayoff> payoff =
175 ext::dynamic_pointer_cast<StrikedTypePayoff>(
176 arguments_.payoff);
177 // Calculate the vanilla option
178
179 VanillaOption vanillaOption(payoff, arguments_.exercise);
180
181 vanillaOption.setPricingEngine(
182 ext::make_shared<FdBlackScholesVanillaEngine>(
184 0, // dampingSteps
186
187 // Calculate the rebate value
191 payoff, arguments_.exercise);
192
193 const Size min_grid_size = 50;
194 const Size rebateDampingSteps
195 = (dampingSteps_ > 0) ? std::min(Size(1), dampingSteps_/2) : 0;
196
197 rebateOption.setPricingEngine(ext::make_shared<FdBlackScholesRebateEngine>(
198 process_, dividends_, tGrid_, std::max(min_grid_size, xGrid_/5),
199 rebateDampingSteps, schemeDesc_, localVol_,
201
202 results_.value = vanillaOption.NPV() + rebateOption.NPV() - results_.value;
203 results_.delta = vanillaOption.delta() + rebateOption.delta() - results_.delta;
204 results_.gamma = vanillaOption.gamma() + rebateOption.gamma() - results_.gamma;
205 results_.theta = vanillaOption.theta() + rebateOption.theta() - results_.theta;
206 }
207 }
208}
bool triggered(Real underlying) const
Barrier option on a single asset.
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
FdBlackScholesBarrierEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process, Size tGrid=100, Size xGrid=100, Size dampingSteps=0, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Douglas(), bool localVol=false, Real illegalLocalVolOverwrite=-Null< Real >())
Shared handle to an observable.
Definition: handle.hpp:41
Real NPV() const
returns the net present value of the instrument.
Definition: instrument.hpp:167
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
Definition: instrument.cpp:35
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Vanilla option (no discrete dividends, no barriers) on a single asset.
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Option exercise classes and payoff function.
Finite-differences Black/Scholes barrier-option engine.
Finite-differences Black/Scholes barrier option rebate helper engine.
Finite-differences Black Scholes vanilla option engine.
1-d mesher for the Black-Scholes process (in ln(S))
Dirichlet boundary conditions for differential operators.
dividend handler for fdm method for one equity direction
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
FdmMesher which is a composite of Fdm1dMesher.
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
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.
normal, cumulative and inverse cumulative distributions
Vanilla option on a single asset.