"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(loaders): let indexed Parquet bars load under a configured date_format
Yufeng Hed7ceb3226107
diff --git a/README.md b/README.mdindex a1c21775f..3bea77bbd 100644--- a/README.md+++ b/README.md@@ -604,6 +604,9 @@ One `get_market_data` call, **29 market-data sources**, one of them the optional| `b3_cotahist` | Brazil (B3) | none | official daily B3 cash/odd-lot bars (raw, BRL) from the local store populated by `python -m src.br_data sync`; offline fallback for `.SA` || `local` | any | none | your own CSV / Parquet / DuckDB via `local:` prefix |+Local sources can read bar dates from a `DatetimeIndex`, with or without a+configured `date_format`; an explicitly mapped date column takes precedence.+**Fallback chains (by IP-ban risk):**- **A-share** → `tencent` · `eastmoney` · `baostock` · `akshare` · `tushare` · `gildata` · `local`diff --git a/agent/backtest/loaders/local_loader.py b/agent/backtest/loaders/local_loader.pyindex 54fbf090a..25b705377 100644--- a/agent/backtest/loaders/local_loader.py+++ b/agent/backtest/loaders/local_loader.py@@ -160,6 +160,17 @@ def _normalize_columns(col_map: dict[str, str],date_fmt: str | None,) -> pd.DataFrame | None:+ # A DatetimeIndex carries the bar dates (parquet files, and duckdb+ # relations whose query set one). An explicitly mapped date column wins+ # over the index; otherwise the index becomes the date column, whatever+ # date_fmt is configured. Dropping the index on the first path also+ # sidesteps the reset_index crash when index and column share a name.+ if isinstance(df.index, pd.DatetimeIndex):+ if col_map.get("date", "date") in df.columns:+ df = df.reset_index(drop=True)+ else:+ df = df.reset_index()+ col_map = {**col_map, "date": df.columns[0]}rename: dict[str, str] = {}for std_name, src_name in col_map.items():if std_name == "date":@@ -213,11 +224,6 @@ def _read_csv(path: str, col_map: dict[str, str], date_fmt: str | None) -> pd.Dadef _read_parquet(path: str, col_map: dict[str, str], date_fmt: str | None) -> pd.DataFrame | None:df = pd.read_parquet(path)- if isinstance(df.index, pd.DatetimeIndex):- df = df.reset_index()- if date_fmt is None and col_map.get("date", "date") not in df.columns:- col_map = dict(col_map)- col_map["date"] = df.columns[0]return _normalize_columns(df, col_map, date_fmt)diff --git a/agent/tests/test_local_loader.py b/agent/tests/test_local_loader.pyindex e3836e67f..088f4bd31 100644--- a/agent/tests/test_local_loader.py+++ b/agent/tests/test_local_loader.py@@ -297,3 +297,58 @@ def test_local_loader_normalizes_dst_offsets_to_naive_utc(pd.Timestamp("2026-01-15 14:30:00"),pd.Timestamp("2026-07-15 13:30:00"),]++[email protected]("index_name", [None, "bar_time"])[email protected]("date_format", [None, "%Y-%m-%d"])+def test_parquet_datetime_index_supplies_dates_with_any_format(+ monkeypatch: pytest.MonkeyPatch, index_name: str | None, date_format: str | None+) -> None:+ """A parquet whose dates live on the index must load with or without a+ configured date_format. The mapped date column was only repointed when no+ format was set, so a configured format dropped every bar."""+ frame = pd.DataFrame(+ {+ "open": [10.0, 11.0],+ "high": [12.0, 13.0],+ "low": [9.0, 10.0],+ "close": [11.0, 12.0],+ "volume": [100.0, 200.0],+ },+ index=pd.date_range("2026-01-01", periods=2, name=index_name),+ )+ monkeypatch.setattr(pd, "read_parquet", lambda path: frame.copy())++ result = local_loader._read_parquet(+ "bars.parquet", dict(local_loader._DEFAULT_COLUMNS), date_format+ )++ assert result is not None+ pd.testing.assert_index_equal(result.index, frame.index.rename("trade_date"))+ assert result["close"].tolist() == [11.0, 12.0]++[email protected]("index_name", ["bar_time", "date"])+def test_parquet_explicit_date_column_wins_over_datetime_index(+ monkeypatch: pytest.MonkeyPatch, index_name: str+) -> None:+ """An explicitly mapped date column takes precedence over the index. An+ index named like the column must not trip reset_index's name collision."""+ frame = pd.DataFrame(+ {+ "date": ["2026-02-01", "2026-02-02"],+ "open": [10.0, 11.0],+ "high": [12.0, 13.0],+ "low": [9.0, 10.0],+ "close": [11.0, 12.0],+ },+ index=pd.date_range("2026-01-01", periods=2, name=index_name),+ )+ monkeypatch.setattr(pd, "read_parquet", lambda path: frame.copy())+ columns = dict(local_loader._DEFAULT_COLUMNS)++ result = local_loader._read_parquet("bars.parquet", columns, "%Y-%m-%d")++ assert result is not None+ assert result.index.tolist() == list(pd.date_range("2026-02-01", periods=2))+ assert columns["date"] == "date" # the caller's mapping is never mutated