Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix: Compute risk-xray returns before calendar alignment so missing prices cannot fabricate multi-bar one-period returns.

Rudy Celekli5bd03149b3ed
diff --git a/README.md b/README.md
index b359b3ac8..a97291a33 100644
--- a/README.md
+++ b/README.md
@@ -421,7 +421,7 @@ The Web UI adds a read-only **Portfolio** page that aggregates holdings across t
| **Per-source provenance** | Every holding names the connection it came from, valued in USD with a CNY conversion. |
| **Failed sources excluded** | A source that errors is reported as an error and left out of the totals — never carried forward — and the snapshot is marked incomplete. |
| **Immutable snapshots** | Each refresh is stored in `~/.vibe-trading/portfolio/portfolio.sqlite3`; credential-free settings live in `~/.vibe-trading/portfolio.json` and `connections.json`. |
-| **Export & analysis** | CSV export, plus a sanitized `portfolio_summary` agent tool whose `risk_xray_args` pass straight into `portfolio_risk_xray`. The same snapshot prints in the terminal with `vibe-trading portfolio show` (`refresh` / `sources` alongside). |
+| **Export & analysis** | CSV export, plus a sanitized `portfolio_summary` agent tool whose `risk_xray_args` pass straight into `portfolio_risk_xray`. The same snapshot prints in the terminal with `vibe-trading portfolio show` (`refresh` / `sources` alongside). Risk x-ray uses only observed adjacent returns; it does not bridge missing prices into artificial one-bar returns. |
Broker-reported source currencies are preserved during valuation: HKD account totals and positions, including Futu `HK.*` holdings, are converted with the snapshot USD/HKD rate before USD and CNY values are displayed. Older snapshots remain stored, but value history compares only snapshots produced by the current valuation methodology to avoid false gains or losses after a valuation fix.
diff --git a/agent/backtest/risk_xray.py b/agent/backtest/risk_xray.py
index be877f04c..c5ad449ac 100644
--- a/agent/backtest/risk_xray.py
+++ b/agent/backtest/risk_xray.py
@@ -150,7 +150,10 @@ def compute_risk_xray(
"fewer than 2 shared trading days after aligning calendars across symbols"
)
- returns = aligned.pct_change(fill_method=None).dropna(how="any")
+ # Compute each one-bar return before dropping incomplete shared dates.
+ # Otherwise an interior missing close becomes a multi-bar move presented
+ # as one observation and distorts volatility and tail risk.
+ returns = frame[kept].pct_change(fill_method=None).dropna(how="any")
if returns.empty:
raise ValueError("no overlapping return observations across symbols")
diff --git a/agent/tests/test_risk_xray_return_gaps_regressions.py b/agent/tests/test_risk_xray_return_gaps_regressions.py
new file mode 100644
index 000000000..9436c7694
--- /dev/null
+++ b/agent/tests/test_risk_xray_return_gaps_regressions.py
@@ -0,0 +1,28 @@
+import numpy as np
+import pandas as pd
+import pytest
+from backtest.risk_xray import compute_risk_xray
+
+
+def test_risk_xray_returns_do_not_bridge_missing_prices():
+ closes = pd.DataFrame(
+ {
+ "a": [100.0, 110.0, np.nan, 120.0, 132.0, 125.0],
+ "b": [100.0, 105.0, 107.0, 109.0, 112.0, 115.0],
+ },
+ index=pd.date_range("2025-01-01", periods=6),
+ )
+ observed = closes.pct_change(fill_method=None).dropna()
+ portfolio = observed.mean(axis=1)
+ actual = compute_risk_xray(closes, {"a": 0.5, "b": 0.5}, min_history=2)
+ assert actual["inputs"]["return_observations"] == len(observed)
+ assert actual["volatility"]["daily_vol"] == pytest.approx(portfolio.std(ddof=1))
+
+
+def test_risk_xray_refuses_prices_with_no_adjacent_shared_returns():
+ closes = pd.DataFrame(
+ {"a": [100.0, np.nan, 110.0, np.nan, 120.0]},
+ index=pd.date_range("2025-01-01", periods=5),
+ )
+ with pytest.raises(ValueError, match="no overlapping return"):
+ compute_risk_xray(closes, {"a": 1.0}, min_history=2)