-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvolatility.py
More file actions
29 lines (21 loc) · 929 Bytes
/
Copy pathvolatility.py
File metadata and controls
29 lines (21 loc) · 929 Bytes
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
"""Volatility estimators used as inputs to the hedging strategy"""
from __future__ import annotations
import numpy as np
__all__ = ["realized_vol_logreturns", "garman_klass_vol"]
TRADING_DAYS = 252
def realized_vol_logreturns(prices: np.ndarray) -> float:
"""Annualized realized volatility from daily close-to-close log returns"""
log_returns = np.log(prices[1:] / prices[:-1])
return float(np.std(log_returns) * np.sqrt(TRADING_DAYS))
def garman_klass_vol(
open_: np.ndarray,
high: np.ndarray,
low: np.ndarray,
close: np.ndarray,
) -> float:
"""Annualized Garman-Klass volatility from OHLC data
Uses the daily estimator -> 0.5 (ln(H/L))^2 - (2 ln 2 - 1)(ln(C/O))^2, averaged then annualized"""
daily_variance = (
0.5 * np.log(high / low) ** 2 - (2.0 * np.log(2.0) - 1.0) * np.log(close / open_) ** 2
)
return float(np.sqrt(np.mean(daily_variance) * TRADING_DAYS))