QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdblackscholesbarrierengine.cpp
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>
23#include <ql/instruments/vanillaoption.hpp>
24#include <ql/math/distributions/normaldistribution.hpp>
25#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
26#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
27#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
28#include <ql/methods/finitedifferences/solvers/fdmblackscholessolver.hpp>
29#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
30#include <ql/methods/finitedifferences/utilities/fdmdirichletboundary.hpp>
31#include <ql/methods/finitedifferences/utilities/fdmdividendhandler.hpp>
32#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
33#include <ql/pricingengines/barrier/fdblackscholesbarrierengine.hpp>
34#include <ql/pricingengines/barrier/fdblackscholesrebateengine.hpp>
35#include <ql/pricingengines/vanilla/fdblackscholesvanillaengine.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)), explicitDividends_(false),
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)), explicitDividends_(true),
66 tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
67 schemeDesc_(schemeDesc), localVol_(localVol),
68 illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
69
71 }
72
74
75 // dividends will eventually be moved out of arguments, but for now we need the switch
76 QL_DEPRECATED_DISABLE_WARNING
78 QL_DEPRECATED_ENABLE_WARNING
79
80 // 1. Mesher
81 const ext::shared_ptr<StrikedTypePayoff> payoff =
82 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
83
84 QL_REQUIRE(payoff, "non-striked type payoff given");
85 QL_REQUIRE(payoff->strike() > 0.0, "strike must be positive");
86
87 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
88 "only european style option are supported");
89
90 const auto spot = process_->x0();
91 QL_REQUIRE(spot > 0.0, "negative or null underlying given");
92 QL_REQUIRE(!triggered(spot), "barrier touched");
93
94 const Time maturity = process_->time(arguments_.exercise->lastDate());
95
96 Real xMin=Null<Real>();
97 Real xMax=Null<Real>();
100 xMin = std::log(arguments_.barrier);
101 }
104 xMax = std::log(arguments_.barrier);
105 }
106
107 const ext::shared_ptr<Fdm1dMesher> equityMesher(
109 xGrid_, process_, maturity, payoff->strike(),
110 xMin, xMax, 0.0001, 1.5,
111 std::make_pair(Null<Real>(), Null<Real>()),
112 dividendSchedule));
113
114 const ext::shared_ptr<FdmMesher> mesher (
115 ext::make_shared<FdmMesherComposite>(equityMesher));
116
117 // 2. Calculator
118 ext::shared_ptr<FdmInnerValueCalculator> calculator(
119 ext::make_shared<FdmLogInnerValue>(payoff, mesher, 0));
120
121 // 3. Step conditions
122 std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
123 std::list<std::vector<Time> > stoppingTimes;
124
125 // 3.1 Step condition if discrete dividends
126 ext::shared_ptr<FdmDividendHandler> dividendCondition(
127 ext::make_shared<FdmDividendHandler>(dividendSchedule, mesher,
128 process_->riskFreeRate()->referenceDate(),
129 process_->riskFreeRate()->dayCounter(), 0));
130
131 if (!dividendSchedule.empty()) {
132 stepConditions.push_back(dividendCondition);
133 std::vector<Time> dividendTimes = dividendCondition->dividendTimes();
134 // this effectively excludes times after maturity
135 for (auto& t: dividendTimes)
136 t = std::min(maturity, t);
137 stoppingTimes.push_back(dividendTimes);
138 }
139
140 ext::shared_ptr<FdmStepConditionComposite> conditions(
141 ext::make_shared<FdmStepConditionComposite>(stoppingTimes, stepConditions));
142
143 // 4. Boundary conditions
144 FdmBoundaryConditionSet boundaries;
147 boundaries.push_back(
148 ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
150
151 }
152
155 boundaries.push_back(
156 ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
158 }
159
160 // 5. Solver
161 FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
162 maturity, tGrid_, dampingSteps_ };
163
164 ext::shared_ptr<FdmBlackScholesSolver> solver(
165 ext::make_shared<FdmBlackScholesSolver>(
167 payoff->strike(), solverDesc, schemeDesc_,
169
170 results_.value = solver->valueAt(spot);
171 results_.delta = solver->deltaAt(spot);
172 results_.gamma = solver->gammaAt(spot);
173 results_.theta = solver->thetaAt(spot);
174
175 // 6. Calculate vanilla option and rebate for in-barriers
178 // Cast the payoff
179 ext::shared_ptr<StrikedTypePayoff> payoff =
180 ext::dynamic_pointer_cast<StrikedTypePayoff>(
181 arguments_.payoff);
182 // Calculate the vanilla option
183
184 VanillaOption vanillaOption(payoff, arguments_.exercise);
185
186 vanillaOption.setPricingEngine(
187 ext::make_shared<FdBlackScholesVanillaEngine>(
188 process_, dividendSchedule, tGrid_, xGrid_,
189 0, // dampingSteps
191
192 // Calculate the rebate value
196 payoff, arguments_.exercise);
197
198 const Size min_grid_size = 50;
199 const Size rebateDampingSteps
200 = (dampingSteps_ > 0) ? std::min(Size(1), dampingSteps_/2) : 0;
201
202 rebateOption.setPricingEngine(ext::make_shared<FdBlackScholesRebateEngine>(
203 process_, dividendSchedule, tGrid_, std::max(min_grid_size, xGrid_/5),
204 rebateDampingSteps, schemeDesc_, localVol_,
206
207 results_.value = vanillaOption.NPV() + rebateOption.NPV() - results_.value;
208 results_.delta = vanillaOption.delta() + rebateOption.delta() - results_.delta;
209 results_.gamma = vanillaOption.gamma() + rebateOption.gamma() - results_.gamma;
210 results_.theta = vanillaOption.theta() + rebateOption.theta() - results_.theta;
211 }
212 }
213}
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.
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
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.