MCPcopy Create free account
hub / github.com/Open-Quant/openquant / bar_diagnostics

Function bar_diagnostics

python/openquant/bars.py:138–167  ·  view source on GitHub ↗
(df: pl.DataFrame)

Source from the content-addressed store, hash-verified

136
137
138def bar_diagnostics(df: pl.DataFrame) -> dict[str, float]:
139 clean = data.clean_ohlcv(df).sort(["symbol", "ts"])
140 returns = (
141 clean.with_columns(
142 (
143 (pl.col("close") - pl.col("close").shift(1).over("symbol"))
144 / pl.col("close").shift(1).over("symbol")
145 ).alias("ret")
146 )
147 .drop_nulls(subset=["ret"])
148 .select("ret")
149 .to_series()
150 .to_list()
151 )
152 if len(returns) < 3:
153 return {
154 "n_bars": float(clean.height),
155 "lag1_return_autocorr": 0.0,
156 "lag1_sq_return_autocorr": 0.0,
157 "return_std": 0.0,
158 }
159 sq = [r * r for r in returns]
160 mean_r = sum(returns) / len(returns)
161 std_r = math.sqrt(sum((r - mean_r) ** 2 for r in returns) / (len(returns) - 1))
162 return {
163 "n_bars": float(clean.height),
164 "lag1_return_autocorr": _lag1_autocorr(returns),
165 "lag1_sq_return_autocorr": _lag1_autocorr(sq),
166 "return_std": std_r,
167 }

Callers

nothing calls this directly

Calls 1

_lag1_autocorrFunction · 0.85

Tested by

no test coverage detected