quantlab.label.predefined.fret.Volatility#
- class quantlab.label.predefined.fret.Volatility(factor_config)[source]#
Bases:
_OpenToOpenLabelForward n-bar open-to-open volatility label.
At signal timestamp
tthe label is the sample standard deviation (ddof=1) of the one-bar returnsadjOpen[k] / adjOpen[k - 1] - 1forkint + 2 .. t + n + 1, timessqrt(n): the volatility over thenbars a position entered at the next bar’s adjusted open is held, on the same span scale asReturnof the samen. LikeReturnit is aForwardlabel withspan = nanddelay = 1, solookahead_bars()isn + 1. It is NaN where any open in the window is missing and at timestamps withoutn + 1later bars.The output column is
vol_{n}. Itskindis"volatility", so a model predicting it on the label’s own scale also reports the level metricsqlikeandvariance_ratio(seequantlab.model.evaluation.volatility_level_metrics).- Parameters:
- factor_configFactorConfig
The KunQuant factor config of the trailing volatility. Set
data_columnsto["adjOpen"]andkwargs["n_forward_periods"]to the horizonn, at least 2 (one return has no sample standard deviation).
- Raises:
- ValueError
If
n_forward_periodsis below 2.
Examples
One symbol whose opens alternate between two one-bar returns. Every window of two returns then holds one of each.
>>> import numpy as np, pandas as pd, xarray as xr >>> from quantlab.backend.zarr import XrBackend >>> from quantlab.factor.config import FactorConfig >>> from quantlab.dataset.config import DatasetConfig >>> from quantlab.dataset.stock import StockDataset >>> from quantlab.label.predefined.fret import Volatility >>> opens = 10.0 * np.cumprod([1.0, 1.1, 0.9, 1.1, 0.9, 1.1, 0.9, 1.1]) >>> XrBackend().to_internal(xr.Dataset( ... {"adjOpen": (["timestamp", "symbol"], opens[:, None])}, ... coords={"timestamp": pd.date_range("2024-01-01", periods=8), "symbol": ["A"]}, ... )).write("data/stock.zarr") XrBackend() >>> dataset = StockDataset(DatasetConfig( ... raw_data_dir_path="data/raw", zarr_file_path="data/stock.zarr", ... market="us_equity", frequency="1d", ... )) >>> vol = Volatility(FactorConfig( ... warmup_bars=3, dataset=dataset, mode="batch", data_columns=["adjOpen"], ... kwargs={"n_forward_periods": 2}, njobs=1, file_path="data/vol.zarr", ... )) >>> vol.lookahead_bars(), vol.span_bars(), vol.get_factor_names() (3, 2, ('vol_2',)) >>> vol.compute("2024-01-01", "2024-01-08")["vol_2"].values[:, 0].round(4) array([0.2, 0.2, 0.2, 0.2, 0.2, nan, nan, nan], dtype=float32)
- kind = 'volatility'#
What the label measures:
"return", or"volatility"for a label whose predicted level is used as a volatility. Model evaluation reads it to add level metrics, never the label’s class.