QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
binomialdoublebarrierengine.hpp
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
24#ifndef quantlib_binomial_double_barrier_engine_hpp
25#define quantlib_binomial_double_barrier_engine_hpp
26
27#include <ql/experimental/barrieroption/discretizeddoublebarrieroption.hpp>
28#include <ql/math/distributions/normaldistribution.hpp>
29#include <ql/methods/lattices/binomialtree.hpp>
30#include <ql/methods/lattices/bsmlattice.hpp>
31#include <ql/processes/blackscholesprocess.hpp>
32#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
33#include <ql/termstructures/yield/flatforward.hpp>
34#include <utility>
35
36namespace QuantLib {
37
39
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
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
@ 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
Definition: any.hpp:35
STL namespace.