QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
garmanklass.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) 2006 Joseph Wang
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 garmanklass.hpp
21 \brief Volatility estimators using high low data
22*/
23
24#ifndef quantlib_garman_klass_hpp
25#define quantlib_garman_klass_hpp
26
28#include <ql/prices.hpp>
29
30namespace QuantLib {
31
32 //! Garman-Klass volatility model
33 /*! This class implements a concrete volatility model based on
34 high low formulas using the method of Garman and Klass in
35 their paper "On the Estimation of the Security Price from
36 Historical Data" at
37 http://www.fea.com/resources/pdf/a_estimation_of_security_price.pdf
38
39 Volatilities are assumed to be expressed on an annual basis.
40 */
42 public LocalVolatilityEstimator<IntervalPrice> {
43 protected:
45 virtual Real calculatePoint(const IntervalPrice &p) = 0;
46 public:
51 TimeSeries<IntervalPrice>::const_iterator prev, next, cur, start;
52 start = quoteSeries.begin();
53 for (cur = start; cur != quoteSeries.end(); ++cur) {
54 retval[cur->first] =
55 std::sqrt(std::fabs(calculatePoint(cur->second))/
57 }
58 return retval;
59 }
60 };
61
63 public GarmanKlassAbstract {
64 public:
67 protected:
68 Real calculatePoint(const IntervalPrice& p) override {
69 Real c = std::log(p.close() / p.open());
70 return c * c;
71 }
72 };
73
74 /* This template factors out common functionality found in
75 classes which rely on the difference between the previous day's
76 close price and today's open price. */
77 template <class T>
78 class GarmanKlassOpenClose : public T {
79 protected:
82 public:
83 GarmanKlassOpenClose(Real y, Real marketOpenFraction,
84 Real a) :
85 T(y), f_(marketOpenFraction), a_(a) {};
88 TimeSeries<IntervalPrice>::const_iterator prev, next, cur, start;
89 start = quoteSeries.begin();
90 ++start;
91 for (cur = start; cur != quoteSeries.end(); ++cur) {
92 prev = cur; --prev;
93 Real c0 = std::log(prev->second.close());
94 Real o1 = std::log(cur->second.open());
95 Real sigma2 =
96 a_ * (o1 - c0) * (o1 - c0) / f_ +
97 (1-a_) * T::calculatePoint(cur->second) /
98 (1-f_);
99
100 retval[cur->first] = std::sqrt(sigma2/T::yearFraction_);
101 }
102 return retval;
103 }
104 };
105
106
108 public GarmanKlassOpenClose<GarmanKlassSimpleSigma> {
109 public:
110 GarmanKlassSigma1(Real y, Real marketOpenFraction) :
112 marketOpenFraction,
113 0.5) {};
114 };
115
116
118 public GarmanKlassAbstract {
119 public:
122 protected:
123 Real calculatePoint(const IntervalPrice& p) override {
124 Real u = std::log(p.high() / p.open());
125 Real d = std::log(p.low() / p.open());
126 return (u - d) * (u - d) / 4.0 / std::log(2.0);
127 }
128 };
129
130
132 public GarmanKlassOpenClose<ParkinsonSigma> {
133 public:
134 GarmanKlassSigma3(Real y, Real marketOpenFraction) :
136 marketOpenFraction,
137 0.17) {};
138 };
139
140
141
143 public GarmanKlassAbstract {
144 public:
147 protected:
148 Real calculatePoint(const IntervalPrice& p) override {
149 Real u = std::log(p.high() / p.open());
150 Real d = std::log(p.low() / p.open());
151 Real c = std::log(p.close() / p.open());
152 return 0.511 * (u - d) * (u - d) - 0.019 * (c * (u + d) - 2 * u * d) - 0.383 * c * c;
153 }
154 };
155
157 public GarmanKlassAbstract {
158 public:
161 protected:
162 Real calculatePoint(const IntervalPrice& p) override {
163 Real u = std::log(p.high() / p.open());
164 Real d = std::log(p.low() / p.open());
165 Real c = std::log(p.close() / p.open());
166 return 0.5 * (u - d) * (u - d) - (2.0 * std::log(2.0) - 1.0) * c * c;
167 }
168 };
169
171 public GarmanKlassOpenClose<GarmanKlassSigma4> {
172 public:
173 GarmanKlassSigma6(Real y, Real marketOpenFraction) :
175 marketOpenFraction,
176 0.012) {};
177 };
178}
179
180
181#endif
Garman-Klass volatility model.
Definition: garmanklass.hpp:42
TimeSeries< Volatility > calculate(const TimeSeries< IntervalPrice > &quoteSeries) override
Definition: garmanklass.hpp:49
virtual Real calculatePoint(const IntervalPrice &p)=0
TimeSeries< Volatility > calculate(const TimeSeries< IntervalPrice > &quoteSeries) override
Definition: garmanklass.hpp:86
GarmanKlassOpenClose(Real y, Real marketOpenFraction, Real a)
Definition: garmanklass.hpp:83
GarmanKlassSigma1(Real y, Real marketOpenFraction)
GarmanKlassSigma3(Real y, Real marketOpenFraction)
Real calculatePoint(const IntervalPrice &p) override
Real calculatePoint(const IntervalPrice &p) override
GarmanKlassSigma6(Real y, Real marketOpenFraction)
Real calculatePoint(const IntervalPrice &p) override
Definition: garmanklass.hpp:68
interval price
Definition: prices.hpp:66
Real low() const
Definition: prices.hpp:78
Real open() const
Definition: prices.hpp:75
Real close() const
Definition: prices.hpp:76
Real high() const
Definition: prices.hpp:77
Real calculatePoint(const IntervalPrice &p) override
Container for historical data.
Definition: timeseries.hpp:51
Container::const_iterator const_iterator
Definition: timeseries.hpp:110
const_iterator begin() const
Definition: timeseries.hpp:158
const_iterator end() const
Definition: timeseries.hpp:159
Date d
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
price classes
Volatility term structures.