@devries-cameron20 / open-alice / commits / db0f719
Fork 0

Alice — an open-source AI trading bot covering equities, crypto, commodities, forex, and macro

Implement clean_ohlcv function for OHLC data cleaning

Added a function to clean OHLC data by dropping rows with missing or non-finite values.

Cameron “Camo” De Vries committed Nov 6, 2026 at 03:21 UTC db0f719cbe4fd8836fb32539bce1795f23ee3b67
1 file changed +19
src/open_alice/data.py
+19
@@ -2,6 +2,7 @@
2
3 from __future__ import annotations
4
5 +import numpy as np
6 import pandas as pd
7 import yfinance as yf
8
@@ -27,9 +28,27 @@ def fetch_ohlcv(
28 raise ValueError(f"No data returned for symbol: {symbol}")
29 df = pd.DataFrame(raw)
30 df.index = pd.DatetimeIndex(df.index).tz_localize(None)
31 + df = clean_ohlcv(df)
32 + if df.empty:
33 + raise ValueError(f"No valid price rows for symbol: {symbol}")
34 return df
35
36
37 +def clean_ohlcv(df: pd.DataFrame) -> pd.DataFrame:
38 + """Drop rows with missing or non-finite OHLC values.
39 +
40 + Providers sometimes return bars with no valid price (for example an
41 + incomplete current session). Those rows must never reach the engines,
42 + or trades get recorded at NaN prices.
43 + """
44 + cols = [c for c in ("Open", "High", "Low", "Close") if c in df.columns]
45 + if not cols:
46 + return df
47 + values = df[cols].to_numpy(dtype=float, na_value=np.nan)
48 + finite_mask = np.isfinite(values).all(axis=1)
49 + return df[finite_mask]
50 +
51 +
52 def add_returns(df: pd.DataFrame) -> pd.DataFrame:
53 """Add a simple daily returns column."""
54 result = df.copy()