QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
binomialdoublebarrierengine.hpp
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) 2015 Thema Consulting SA
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file binomialdoublebarrierengine.hpp
21 \brief Binomial Double Barrier option engine
22*/
23
24#ifndef quantlib_binomial_double_barrier_engine_hpp
25#define quantlib_binomial_double_barrier_engine_hpp
26
34#include <utility>
35
36namespace QuantLib {
37
38 //! Pricing engine for double barrier options using binomial trees
39 /*! \ingroup barrierengines
40
41 \note This engine requires a the discretized option classes.
42 By default uses a standard binomial implementation, but it can
43 also work with DiscretizedDermanKaniDoubleBarrierOption to
44 implement a Derman-Kani optimization.
45
46 \test the correctness of the returned values is tested by
47 checking it against analytic results.
48 */
49 template <class T, class D = DiscretizedDoubleBarrierOption>
51 public:
52 BinomialDoubleBarrierEngine(ext::shared_ptr<GeneralizedBlackScholesProcess> process,
53 Size timeSteps)
54 : process_(std::move(process)), timeSteps_(timeSteps) {
55 QL_REQUIRE(timeSteps>0,
56 "timeSteps must be positive, " << timeSteps <<
57 " not allowed");
59 }
60 void calculate() const override;
61
62 private:
63 ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
65 };
66
67
68 // template definitions
69
70 template <class T, class D>
72
73 DayCounter rfdc = process_->riskFreeRate()->dayCounter();
74 DayCounter divdc = process_->dividendYield()->dayCounter();
75 DayCounter voldc = process_->blackVolatility()->dayCounter();
76 Calendar volcal = process_->blackVolatility()->calendar();
77
78 Real s0 = process_->stateVariable()->value();
79 QL_REQUIRE(s0 > 0.0, "negative or null underlying given");
80 Volatility v = process_->blackVolatility()->blackVol(
81 arguments_.exercise->lastDate(), s0);
82 Date maturityDate = arguments_.exercise->lastDate();
83 Rate r = process_->riskFreeRate()->zeroRate(maturityDate,
84 rfdc, Continuous, NoFrequency);
85 Rate q = process_->dividendYield()->zeroRate(maturityDate,
86 divdc, Continuous, NoFrequency);
87 Date referenceDate = process_->riskFreeRate()->referenceDate();
88
89 // binomial trees with constant coefficient
90 Handle<YieldTermStructure> flatRiskFree(
91 ext::shared_ptr<YieldTermStructure>(
92 new FlatForward(referenceDate, r, rfdc)));
93 Handle<YieldTermStructure> flatDividends(
94 ext::shared_ptr<YieldTermStructure>(
95 new FlatForward(referenceDate, q, divdc)));
97 ext::shared_ptr<BlackVolTermStructure>(
98 new BlackConstantVol(referenceDate, volcal, v, voldc)));
99
100 ext::shared_ptr<StrikedTypePayoff> payoff =
101 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
102 QL_REQUIRE(payoff, "non-striked payoff given");
103
104 Time maturity = rfdc.yearFraction(referenceDate, maturityDate);
105
106 ext::shared_ptr<StochasticProcess1D> bs(
108 process_->stateVariable(),
109 flatDividends, flatRiskFree, flatVol));
110
111 TimeGrid grid(maturity, timeSteps_);
112
113 ext::shared_ptr<T> tree(new T(bs, maturity, timeSteps_,
114 payoff->strike()));
115
116 ext::shared_ptr<BlackScholesLattice<T> > lattice(
117 new BlackScholesLattice<T>(tree, r, maturity, timeSteps_));
118
119 D option(arguments_, *process_, grid);
120 option.initialize(lattice, maturity);
121
122 // Partial derivatives calculated from various points in the
123 // binomial tree
124 // (see J.C.Hull, "Options, Futures and other derivatives", 6th edition, pp 397/398)
125
126 // Rollback to third-last step, and get underlying prices (s2) &
127 // option values (p2) at this point
128 option.rollback(grid[2]);
129 Array va2(option.values());
130 QL_ENSURE(va2.size() == 3, "Expect 3 nodes in grid at second step");
131 Real p2u = va2[2]; // up
132 Real p2m = va2[1]; // mid
133 Real p2d = va2[0]; // down (low)
134 Real s2u = lattice->underlying(2, 2); // up price
135 Real s2m = lattice->underlying(2, 1); // middle price
136 Real s2d = lattice->underlying(2, 0); // down (low) price
137
138 // calculate gamma by taking the first derivate of the two deltas
139 Real delta2u = (p2u - p2m)/(s2u-s2m);
140 Real delta2d = (p2m-p2d)/(s2m-s2d);
141 Real gamma = (delta2u - delta2d) / ((s2u-s2d)/2);
142
143 // Rollback to second-last step, and get option values (p1) at
144 // this point
145 option.rollback(grid[1]);
146 Array va(option.values());
147 QL_ENSURE(va.size() == 2, "Expect 2 nodes in grid at first step");
148 Real p1u = va[1];
149 Real p1d = va[0];
150 Real s1u = lattice->underlying(1, 1); // up (high) price
151 Real s1d = lattice->underlying(1, 0); // down (low) price
152
153 Real delta = (p1u - p1d) / (s1u - s1d);
154
155 // Finally, rollback to t=0
156 option.rollback(0.0);
157 Real p0 = option.presentValue();
158
159 results_.value = p0;
160 results_.delta = delta;
161 results_.gamma = gamma;
162 // theta can be approximated by calculating the numerical derivative
163 // between mid value at third-last step and at t0. The underlying price
164 // is the same, only time varies.
165 results_.theta = (p2m - p0) / grid[2];
166 }
167
168}
169
170
171#endif
Binomial tree class.
Black constant volatility, no time dependence, no strike dependence.
Black-Scholes processes.
Binomial trees under the BSM model.
const Instrument::results * results_
Definition: cdsoption.cpp:63
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Pricing engine for double barrier options using binomial trees.
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
BinomialDoubleBarrierEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process, Size timeSteps)
Constant Black volatility, no time-strike dependence.
Simple binomial lattice approximating the Black-Scholes model.
Definition: bsmlattice.hpp:36
calendar class
Definition: calendar.hpp:61
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Time yearFraction(const Date &, const Date &, const Date &refPeriodStart=Date(), const Date &refPeriodEnd=Date()) const
Returns the period between two dates as a fraction of year.
Definition: daycounter.hpp:128
Double-Barrier-option engine base class
Flat interest-rate curve.
Definition: flatforward.hpp:37
Generalized Black-Scholes stochastic process.
Shared handle to an observable.
Definition: handle.hpp:41
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
time grid class
Definition: timegrid.hpp:43
discretized double barrier option
#define QL_ENSURE(condition, message)
throw an error if the given post-condition is not verified
Definition: errors.hpp:130
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
flat forward rate term structure
@ NoFrequency
null frequency
Definition: frequency.hpp:37
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
STL namespace.
normal, cumulative and inverse cumulative distributions
ext::shared_ptr< YieldTermStructure > q
ext::shared_ptr< YieldTermStructure > r
ext::shared_ptr< BlackVolTermStructure > v