Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loaders): include fractional seconds on the end day

Rudy Celekli8baee99a44d2
diff --git a/README.md b/README.md
index a8eebe9b7..9d46779e5 100644
--- a/README.md
+++ b/README.md
@@ -820,6 +820,8 @@ When the opt-in market-data cache is enabled, local-source cache entries are
scoped to the configured file, query, and column mapping. Changing a symbol's
source declaration does not reuse bars from its previous source. The settled-data
cache policy for an unchanged declaration remains the same.
+Local data date ranges include the entire UTC end day, including subsecond
+timestamps; bars at the following midnight are excluded before aggregation.
</details>
diff --git a/agent/backtest/loaders/local_loader.py b/agent/backtest/loaders/local_loader.py
index 9932799b3..54fbf090a 100644
--- a/agent/backtest/loaders/local_loader.py
+++ b/agent/backtest/loaders/local_loader.py
@@ -370,8 +370,10 @@ class DataLoader:
# Treat ``end_date`` as inclusive of the whole day so intraday bars on
# the end day survive the filter (a bare midnight bound dropped them,
# which would defeat any sub-daily ``interval``).
- end = pd.Timestamp(end_date) + pd.Timedelta(days=1) - pd.Timedelta(seconds=1)
- df = df[(df.index >= start) & (df.index <= end)]
+ end = pd.Timestamp(end_date) + pd.Timedelta(days=1)
+ # A half-open boundary includes fractional seconds on the end day
+ # without admitting the following midnight.
+ df = df[(df.index >= start) & (df.index < end)]
if df.empty:
return None
df = _resample_to_interval(df, interval, symbol)
diff --git a/agent/tests/test_local_loader_end_day.py b/agent/tests/test_local_loader_end_day.py
new file mode 100644
index 000000000..e69b8eadb
--- /dev/null
+++ b/agent/tests/test_local_loader_end_day.py
@@ -0,0 +1,51 @@
+"""Inclusive local-data date ranges include the final fractional second."""
+
+from pathlib import Path
+
+import pandas as pd
+import pytest
+
+from backtest.loaders import base, local_loader
+
+
[email protected]("interval", ["1m", "1D"])
+def test_end_day_includes_subsecond_bars_but_not_next_midnight(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, interval: str
+) -> None:
+ """Exercise real CSV parsing, range filtering, and OHLCV aggregation."""
+ path = tmp_path / "bars.csv"
+ pd.DataFrame(
+ {
+ "date": [
+ "2025-01-01T23:59:59.999999999Z",
+ "2025-01-02T23:59:58.999999999Z",
+ "2025-01-02T23:59:59.999999999Z",
+ "2025-01-03T00:00:00.000000000Z",
+ ],
+ "open": [5, 10, 20, 30],
+ "high": [6, 11, 21, 31],
+ "low": [4, 9, 19, 29],
+ "close": [5, 10, 20, 30],
+ "volume": [50, 100, 200, 300],
+ }
+ ).to_csv(path, index=False)
+ monkeypatch.setattr(base, "loader_cache_enabled", lambda: False)
+ monkeypatch.setattr(
+ local_loader,
+ "_load_config",
+ lambda: {"sources": [{"symbol": "AAA.US", "type": "csv", "path": str(path)}]},
+ )
+
+ frame = local_loader.DataLoader().fetch(
+ ["AAA.US"], "2025-01-02", "2025-01-02", interval=interval
+ )["AAA.US"]
+
+ assert len(frame) == 1
+ assert frame.iloc[0].to_dict() == {
+ "open": 10,
+ "high": 21,
+ "low": 9,
+ "close": 20,
+ "volume": 300,
+ }
+ assert frame.index[0].date().isoformat() == "2025-01-02"