openquant.volatility
Compiled from Rust (crates/pyopenquant). Signatures and types are from the shipped .pyi stubs; descriptions are the functions’ own docstrings, as help() shows them. All Python modules.
Functions
get_daily_vol
get_daily_vol(
close_timestamps: Sequence[str],
close_prices: Sequence[float],
lookback: int,
) -> list[tuple[str, float]]Daily volatility: exponentially weighted standard deviation of daily returns.
AFML Snippet 3.1, matching mlfinlab.util.volatility.get_daily_vol. For each bar, the return is the simple return p_t / p_j - 1 against the last bar j strictly more than one day earlier; the volatility is pandas' ewm(span=lookback).std() of those returns (adjust=True, unbiased). Bars with no bar more than a day before them are omitted, and the first returned value is NaN (one return has no sample variance). Bars must be in increasing time. A typical use is as the target of the labeling functions, which drop NaN targets.
Parameters
close_timestamps: list[str]Bar timestamps as
"%Y-%m-%d %H:%M:%S"(an optional fractional second is accepted), in increasing order.close_prices: list[float]Close price of each bar.
lookback: intEWM span, in number of returns.
Returns
- list[tuple[str, float]]
(timestamp, volatility)rows; empty when there are fewer than two bars orlookbackis 0.
Raises
- ValueError
If the timestamps and prices differ in length, or a timestamp does not parse.
get_garman_class_vol
get_garman_class_vol(
open: Sequence[float],
high: Sequence[float],
low: Sequence[float],
close: Sequence[float],
window: int,
) -> list[float]Garman-Klass volatility over a rolling window.
Each output is the square root of the mean over the last window bars of 0.5 ln(high / low)^2 - (2 ln 2 - 1) ln(close / open)^2, per bar (not annualised), matching mlfinlab.util.volatility.get_garman_class_vol. The first window - 1 outputs are NaN, as is any output whose window contains a NaN; window=0 gives all NaN.
Parameters
open: list[float]Open price of each bar, oldest first.
high: list[float]High price of each bar.
low: list[float]Low price of each bar.
close: list[float]Close price of each bar.
window: intNumber of bars in the rolling window.
Returns
- list[float]
Volatility per bar, same length as
open.
Raises
- ValueError
If
high,loworclosediffers in length fromopen.
get_parkinson_vol
get_parkinson_vol(
high: Sequence[float],
low: Sequence[float],
window: int,
) -> list[float]Parkinson high-low volatility over a rolling window.
Each output is sqrt(mean(ln(high / low)^2) / (4 ln 2)) over the last window bars, per bar (not annualised), matching mlfinlab's get_parksinson_vol. The first window - 1 outputs are NaN, as is any output whose window contains a NaN; window=0 gives all NaN.
Parameters
high: list[float]High price of each bar, oldest first.
low: list[float]Low price of each bar.
window: intNumber of bars in the rolling window.
Returns
- list[float]
Volatility per bar, same length as
high.
Raises
- ValueError
If
lowdiffers in length fromhigh.
get_yang_zhang_vol
get_yang_zhang_vol(
open: Sequence[float],
high: Sequence[float],
low: Sequence[float],
close: Sequence[float],
window: int,
) -> list[float]Yang-Zhang (2000) volatility over a rolling window.
For the n = window bars ending at each bar, returns sqrt(var_o + k var_c + (1 - k) var_rs) with k = 0.34 / (1.34 + (n + 1) / (n - 1)), where var_o is the sample variance of the overnight returns ln(open_i / close_{i-1}), var_c that of the open-to-close returns ln(close_i / open_i), and var_rs the mean Rogers-Satchell term. The result is per bar, not annualised. The overnight return needs the previous close, so the first window outputs are NaN; a window containing a NaN gives NaN and window < 2 gives all NaN. This differs from mlfinlab's get_yang_zhang_vol, which uses ln(close_i / open_{i-1}) and does not demean.
Parameters
open: list[float]Open price of each bar, oldest first.
high: list[float]High price of each bar.
low: list[float]Low price of each bar.
close: list[float]Close price of each bar.
window: intNumber of bars in the rolling window.
Returns
- list[float]
Volatility per bar, same length as
open.
Raises
- ValueError
If
high,loworclosediffers in length fromopen.