-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGKvol.m
44 lines (30 loc) · 1.07 KB
/
GKvol.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function sigma_hat = GKvol(O,H,L,Cl,k)
%GKvol() Computes historical volatility using Garman-Klass estimator
% GKvol() function computes historical volatility using Garman-Klass
% estimator and OHLC data.
% INPUT:
% O - opens
% H - highs
% L - lows
% C - close
% k - rolling window size
% OUTPUT:
% sigma_hat - historical volatility estimate
% Petr Javorik (2016) [email protected]
% http://mmquant.net/introduction-to-volatility-models-with-matlab-sma-ewma-cc-range-estimators/
% input check
assert(k <= length(H),'Window length is greater than time series length!');
assert(length(H)==length(L) &&...
length(H)==length(O) &&...
length(H)==length(Cl),'Sizes of OHLC series are not equal.');
% GK computation
% A,B,C are terms in (2) on the left in brackets respectively
sigma_hat = zeros(size(H));
A = 0.511*log(H./L).^2;
B = 0.019*log(Cl./O).*log(H.*L./O.^2);
C = 2*log(H./O).*log(L./O);
for t = k+1:length(H)
sigma_hat(t,1) = 1/k * sum(A(t-k:t-1)-B(t-k:t-1)-C(t-k:t-1));
end
sigma_hat = sqrt(sigma_hat) * sqrt(252);
end