Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/pricingengines/barrier/fdblackscholesbarrierengine.cpp
Line
Count
Source (jump to first uncovered line)
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
38
namespace QuantLib {
39
40
    FdBlackScholesBarrierEngine::FdBlackScholesBarrierEngine(
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
0
    : process_(std::move(process)),
49
0
      tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
50
0
      schemeDesc_(schemeDesc), localVol_(localVol),
51
0
      illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
52
53
0
        registerWith(process_);
54
0
    }
55
56
    FdBlackScholesBarrierEngine::FdBlackScholesBarrierEngine(
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
0
    : process_(std::move(process)), dividends_(std::move(dividends)),
66
0
      tGrid_(tGrid), xGrid_(xGrid), dampingSteps_(dampingSteps),
67
0
      schemeDesc_(schemeDesc), localVol_(localVol),
68
0
      illegalLocalVolOverwrite_(illegalLocalVolOverwrite) {
69
70
0
        registerWith(process_);
71
0
    }
72
73
0
    void FdBlackScholesBarrierEngine::calculate() const {
74
75
        // 1. Mesher
76
0
        const ext::shared_ptr<StrikedTypePayoff> payoff =
77
0
            ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
78
79
0
        QL_REQUIRE(payoff, "non-striked type payoff given");
80
0
        QL_REQUIRE(payoff->strike() > 0.0, "strike must be positive");
81
82
0
        QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
83
0
                   "only european style option are supported");
84
85
0
        const auto spot = process_->x0();
86
0
        QL_REQUIRE(spot > 0.0, "negative or null underlying given");
87
0
        QL_REQUIRE(!triggered(spot), "barrier touched");
88
89
0
        const Time maturity = process_->time(arguments_.exercise->lastDate());
90
91
0
        Real xMin=Null<Real>();
92
0
        Real xMax=Null<Real>();
93
0
        if (   arguments_.barrierType == Barrier::DownIn
94
0
            || arguments_.barrierType == Barrier::DownOut) {
95
0
            xMin = std::log(arguments_.barrier);
96
0
        }
97
0
        if (   arguments_.barrierType == Barrier::UpIn
98
0
            || arguments_.barrierType == Barrier::UpOut) {
99
0
            xMax = std::log(arguments_.barrier);
100
0
        }
101
102
0
        const ext::shared_ptr<Fdm1dMesher> equityMesher(
103
0
            new FdmBlackScholesMesher(
104
0
                xGrid_, process_, maturity, payoff->strike(),
105
0
                xMin, xMax, 0.0001, 1.5,
106
0
                std::make_pair(Null<Real>(), Null<Real>()),
107
0
                dividends_));
108
        
109
0
        const ext::shared_ptr<FdmMesher> mesher (
110
0
            ext::make_shared<FdmMesherComposite>(equityMesher));
111
112
        // 2. Calculator
113
0
        ext::shared_ptr<FdmInnerValueCalculator> calculator(
114
0
            ext::make_shared<FdmLogInnerValue>(payoff, mesher, 0));
115
116
        // 3. Step conditions
117
0
        std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
118
0
        std::list<std::vector<Time> > stoppingTimes;
119
120
        // 3.1 Step condition if discrete dividends
121
0
        ext::shared_ptr<FdmDividendHandler> dividendCondition(
122
0
            ext::make_shared<FdmDividendHandler>(dividends_, mesher,
123
0
                                   process_->riskFreeRate()->referenceDate(),
124
0
                                   process_->riskFreeRate()->dayCounter(), 0));
125
126
0
        if (!dividends_.empty()) {
127
0
            stepConditions.push_back(dividendCondition);
128
0
            std::vector<Time> dividendTimes = dividendCondition->dividendTimes();
129
            // this effectively excludes times after maturity
130
0
            for (auto& t: dividendTimes)
131
0
                t = std::min(maturity, t);
132
0
            stoppingTimes.push_back(dividendTimes);
133
0
        }
134
135
0
        ext::shared_ptr<FdmStepConditionComposite> conditions(
136
0
            ext::make_shared<FdmStepConditionComposite>(stoppingTimes, stepConditions));
137
138
        // 4. Boundary conditions
139
0
        FdmBoundaryConditionSet boundaries;
140
0
        if (   arguments_.barrierType == Barrier::DownIn
141
0
            || arguments_.barrierType == Barrier::DownOut) {
142
0
            boundaries.push_back(
143
0
                ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
144
0
                                         FdmDirichletBoundary::Lower));
145
146
0
        }
147
148
0
        if (   arguments_.barrierType == Barrier::UpIn
149
0
            || arguments_.barrierType == Barrier::UpOut) {
150
0
            boundaries.push_back(
151
0
                ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
152
0
                                         FdmDirichletBoundary::Upper));
153
0
        }
154
155
        // 5. Solver
156
0
        FdmSolverDesc solverDesc = { mesher, boundaries, conditions, calculator,
157
0
                                     maturity, tGrid_, dampingSteps_ };
158
159
0
        ext::shared_ptr<FdmBlackScholesSolver> solver(
160
0
            ext::make_shared<FdmBlackScholesSolver>(
161
0
                               Handle<GeneralizedBlackScholesProcess>(process_),
162
0
                               payoff->strike(), solverDesc, schemeDesc_,
163
0
                               localVol_, illegalLocalVolOverwrite_));
164
165
0
        results_.value = solver->valueAt(spot);
166
0
        results_.delta = solver->deltaAt(spot);
167
0
        results_.gamma = solver->gammaAt(spot);
168
0
        results_.theta = solver->thetaAt(spot);
169
170
        // 6. Calculate vanilla option and rebate for in-barriers
171
0
        if (   arguments_.barrierType == Barrier::DownIn
172
0
            || arguments_.barrierType == Barrier::UpIn) {
173
            // Cast the payoff
174
0
            ext::shared_ptr<StrikedTypePayoff> payoff =
175
0
                    ext::dynamic_pointer_cast<StrikedTypePayoff>(
176
0
                                                            arguments_.payoff);
177
            // Calculate the vanilla option
178
            
179
0
            VanillaOption vanillaOption(payoff, arguments_.exercise);
180
            
181
0
            vanillaOption.setPricingEngine(
182
0
                ext::make_shared<FdBlackScholesVanillaEngine>(
183
0
                        process_, dividends_, tGrid_, xGrid_,
184
0
                        0, // dampingSteps
185
0
                        schemeDesc_, localVol_, illegalLocalVolOverwrite_));
186
187
            // Calculate the rebate value
188
0
            BarrierOption rebateOption(arguments_.barrierType,
189
0
                                       arguments_.barrier,
190
0
                                       arguments_.rebate,
191
0
                                       payoff, arguments_.exercise);
192
            
193
0
            const Size min_grid_size = 50;
194
0
            const Size rebateDampingSteps 
195
0
                = (dampingSteps_ > 0) ? std::min(Size(1), dampingSteps_/2) : 0; 
196
197
0
            rebateOption.setPricingEngine(ext::make_shared<FdBlackScholesRebateEngine>(
198
0
                            process_, dividends_, tGrid_, std::max(min_grid_size, xGrid_/5), 
199
0
                            rebateDampingSteps, schemeDesc_, localVol_, 
200
0
                            illegalLocalVolOverwrite_));
201
202
0
            results_.value = vanillaOption.NPV()   + rebateOption.NPV()   - results_.value;
203
0
            results_.delta = vanillaOption.delta() + rebateOption.delta() - results_.delta;
204
0
            results_.gamma = vanillaOption.gamma() + rebateOption.gamma() - results_.gamma;
205
0
            results_.theta = vanillaOption.theta() + rebateOption.theta() - results_.theta;
206
0
        }
207
0
    }
208
}