"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(qveris): resolve adjusted fields as a complete set, never per field
cgycorey58ac87e2f6ad
diff --git a/agent/backtest/loaders/qveris_loader.py b/agent/backtest/loaders/qveris_loader.pyindex 981fd6363..b83e51bf7 100644--- a/agent/backtest/loaders/qveris_loader.py+++ b/agent/backtest/loaders/qveris_loader.py@@ -51,14 +51,42 @@ _DATE_KEYS = ("time","period",)+#: The four fields that must all be present for a record to become a bar.+_PRICE_QUARTET = ("open", "high", "low", "close")++#: Aliases for an unadjusted bar._FIELD_ALIASES = {"open": ("open", "o", "1. open"),"high": ("high", "h", "2. high"),"low": ("low", "l", "3. low"),- "close": ("close", "c", "adj_close", "adjusted_close", "4. close"),+ "close": ("close", "c", "4. close"),"volume": ("volume", "vol", "v", "5. volume", "6. volume"),}+#: Volume names that make no adjustment claim, shared by both tables. The+#: family split exists to stop unadjusted and adjusted *price levels* sharing a+#: bar; volume is not a price level, so an adjusted bar must still report a+#: volume the payload calls ``volume`` (``6. volume`` is Alpha Vantage's label+#: for the adjusted series' volume) instead of silently reporting NaN. The+#: unadjusted tuple is referenced rather than copied so the two cannot drift and+#: the unadjusted path stays byte-identical.+_PLAIN_VOLUME_ALIASES = _FIELD_ALIASES["volume"]++#: Aliases for an adjusted bar, resolved as a complete alternate set rather+#: than field by field. ``adj_close`` used to sit in the unadjusted ``close``+#: aliases while it had no ``adj_open``/``adj_high``/``adj_low`` siblings,+#: which cut both ways (#1494): a record carrying only adjusted fields failed+#: the OHLC check on every row, so a billed call produced no bars, while a+#: record carrying unadjusted open/high/low with only ``adj_close`` was+#: accepted as one bar mixing unadjusted and adjusted price levels.+_ADJUSTED_FIELD_ALIASES = {+ "open": ("adj_open", "adjusted_open"),+ "high": ("adj_high", "adjusted_high"),+ "low": ("adj_low", "adjusted_low"),+ "close": ("adj_close", "adjusted_close"),+ "volume": ("adj_volume", "adjusted_volume") + _PLAIN_VOLUME_ALIASES,+}+@dataclass(frozen=True)class QVerisConfig:@@ -656,14 +684,22 @@ def _looks_like_date(value: Any) -> bool:return True+def _resolve_field_set(keys: Iterable[str]) -> dict[str, tuple[str, ...]] | None:+ """Return the alias table that supplies one complete price quartet.++ The unadjusted table wins when a record carries both sets, which is the+ preference the previous single-table lookup had. ``None`` means neither+ table is complete, so a bar is never assembled from a mix of unadjusted and+ adjusted fields.+ """+ for table in (_FIELD_ALIASES, _ADJUSTED_FIELD_ALIASES):+ if all(any(alias in keys for alias in table[field]) for field in _PRICE_QUARTET):+ return table+ return None++def _record_has_ohlc(record: dict[str, Any]) -> bool:- lowered = {str(key).lower() for key in record}- return all(any(alias in lowered for alias in aliases) for aliases in (- _FIELD_ALIASES["open"],- _FIELD_ALIASES["high"],- _FIELD_ALIASES["low"],- _FIELD_ALIASES["close"],- ))+ return _resolve_field_set({str(key).lower() for key in record}) is not Nonedef _normalize_record(record: dict[str, Any]) -> dict[str, Any] | None:@@ -671,10 +707,19 @@ def _normalize_record(record: dict[str, Any]) -> dict[str, Any] | None:date = _first_value(lowered, _DATE_KEYS)if date is None:return None+ table = _resolve_field_set(lowered)+ if table is None:+ return Nonerow = {"trade_date": date}- for field, aliases in _FIELD_ALIASES.items():- row[field] = _first_value(lowered, aliases)- if any(row[field] is None for field in ("open", "high", "low", "close")):+ for field in _OHLCV_COLUMNS:+ row[field] = _first_value(lowered, table[field])+ if row["volume"] is None:+ # Volume carries no adjustment claim, so it is the one column resolved+ # across both families: the chosen table first (so which name wins is+ # unchanged), the other only to fill the gap the choice would leave.+ fallback = _ADJUSTED_FIELD_ALIASES if table is _FIELD_ALIASES else _FIELD_ALIASES+ row["volume"] = _first_value(lowered, fallback["volume"])+ if any(row[field] is None for field in _PRICE_QUARTET):return Nonereturn rowdiff --git a/agent/tests/test_qveris_loader.py b/agent/tests/test_qveris_loader.pyindex a75f84e16..3e2c02443 100644--- a/agent/tests/test_qveris_loader.py+++ b/agent/tests/test_qveris_loader.py@@ -536,3 +536,171 @@ def test_explicit_unavailable_qveris_does_not_fallback_to_network():with pytest.raises(NoAvailableSourceError) as excinfo:get_loader_cls_with_fallback("qveris")assert "qveris" in str(excinfo.value).lower()+++def test_adjusted_only_records_become_bars():+ """A capability publishing only ``adj_*`` fields must still yield bars.++ ``qveris_finance.mkt_bars_adjusted`` returns ``adj_open``/``adj_high``/+ ``adj_low``/``adj_close`` plus ``adj_factor`` and no unadjusted price+ fields, so every row failed the OHLC check and a billed call produced "no+ parseable bars" (#1494).+ """+ result = {+ "data": [+ {+ "date": "2024-01-02",+ "adj_open": 1.0,+ "adj_high": 1.2,+ "adj_low": 0.9,+ "adj_close": 1.1,+ "adj_volume": 100,+ "adj_factor": 1.0,+ },+ {+ "date": "2024-01-03",+ "adj_open": 1.1,+ "adj_high": 1.3,+ "adj_low": 1.0,+ "adj_close": 1.25,+ "adj_volume": 120,+ "adj_factor": 1.0,+ },+ ]+ }++ frame = qv._result_to_frame(result, "2024-01-01", "2024-01-05")++ assert frame is not None+ assert list(frame["close"]) == pytest.approx([1.1, 1.25])+ assert list(frame["volume"]) == pytest.approx([100.0, 120.0])+++def test_a_bar_never_mixes_unadjusted_and_adjusted_levels():+ """Unadjusted open/high/low with only ``adj_close`` is not a bar.++ Accepting that record assembles raw price levels against an adjusted close+ in a single bar, silently, under whatever caliber the source declares+ (#1494).+ """+ result = {+ "data": [+ {+ "date": "2024-01-02",+ "open": 1.0,+ "high": 1.2,+ "low": 0.9,+ "adj_close": 1.1,+ }+ ]+ }++ assert qv._result_to_frame(result, "2024-01-01", "2024-01-05") is None+++def test_unadjusted_fields_win_when_both_sets_are_present():+ """A record carrying both sets keeps resolving to the unadjusted quartet."""+ result = {+ "data": [+ {+ "date": "2024-01-02",+ "open": 1.0,+ "high": 1.2,+ "low": 0.9,+ "close": 1.1,+ "adj_open": 10.0,+ "adj_high": 12.0,+ "adj_low": 9.0,+ "adj_close": 11.0,+ }+ ]+ }++ frame = qv._result_to_frame(result, "2024-01-01", "2024-01-05")++ assert frame is not None+ assert float(frame["close"].iloc[0]) == pytest.approx(1.1)++[email protected]("volume_key", ["volume", "vol", "v", "5. volume", "6. volume"])+def test_adjusted_bars_keep_volume_under_a_plain_volume_key(volume_key):+ """Volume is not a price level, so an adjusted bar keeps a plain volume.++ Splitting the alias tables on price *levels* made the table a unit for every+ column, so an adjusted-price record naming its volume ``volume`` — the most+ natural key, and ``6. volume`` is how Alpha Vantage labels its adjusted+ series — resolved a complete price quartet and then silently reported a NaN+ volume. ``_result_to_frame`` only drops NaN price rows, so the NaN reached+ the frame and the loader cache.+ """+ result = {+ "data": [+ {+ "date": "2024-01-02",+ "adj_open": 1.0,+ "adj_high": 1.2,+ "adj_low": 0.9,+ "adj_close": 1.1,+ volume_key: 100,+ }+ ]+ }++ frame = qv._result_to_frame(result, "2024-01-01", "2024-01-05")++ assert frame is not None+ assert float(frame["volume"].iloc[0]) == pytest.approx(100.0)+++def test_adjusted_volume_wins_over_a_plain_volume_key():+ """When the adjusted family names its own volume, that one still wins."""+ result = {+ "data": [+ {+ "date": "2024-01-02",+ "adj_open": 1.0,+ "adj_high": 1.2,+ "adj_low": 0.9,+ "adj_close": 1.1,+ "adj_volume": 200,+ "volume": 100,+ }+ ]+ }++ frame = qv._result_to_frame(result, "2024-01-01", "2024-01-05")++ assert frame is not None+ assert float(frame["volume"].iloc[0]) == pytest.approx(200.0)++def test_plain_quartet_still_reports_an_adjusted_only_volume():+ """Volume is not a price level, so neither family may strand the other's name.++ When a payload publishes both price families and names its volume only+ ``adj_volume``, the unadjusted quartet wins (the documented preference)+ and the volume lookup then followed the chosen table alone — so the bar+ resolved with a real price and a NaN volume, which ``_result_to_frame`` keeps+ because it only drops NaN price rows.+ """+ result = {+ "data": [+ {+ "date": "2024-01-02",+ "open": 1.0,+ "high": 1.2,+ "low": 0.9,+ "close": 1.1,+ "adj_open": 2.0,+ "adj_high": 2.2,+ "adj_low": 1.9,+ "adj_close": 2.1,+ "adj_volume": 200,+ }+ ]+ }++ frame = qv._result_to_frame(result, "2024-01-01", "2024-01-05")++ assert frame is not None+ assert float(frame["close"].iloc[0]) == pytest.approx(1.1)+ assert float(frame["volume"].iloc[0]) == pytest.approx(200.0)