"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(backtest): isolate opening-plan failures
Haozhe Wu63a26adf83cd
diff --git a/agent/backtest/engines/base.py b/agent/backtest/engines/base.pyindex d8c79faf0..c8ec5bfcc 100644--- a/agent/backtest/engines/base.py+++ b/agent/backtest/engines/base.py@@ -586,16 +586,23 @@ class BaseEngine(ABC):open_targets.append((c, target_w, data_map.get(c)))def _plans(scale: float) -> list[_OpenOrder]:- return [- order- for c, target_w, frame in open_targets- if (- order := self._plan_open_order(+ result: list[_OpenOrder] = []+ for c, target_w, frame in open_targets:+ try:+ order = self._plan_open_order(c, target_w * scale, frame, ts, equity)- )- is not None- ]+ except Exception as exc:+ logger.warning(+ "Rebalance open plan failed for %s at %s: %s",+ c,+ ts,+ exc,+ )+ continue+ if order is not None:+ result.append(order)+ return resultplanned = _plans(1.0)if sum(order.cost for order in planned) > self.capital + 1e-9:diff --git a/agent/tests/test_engine_robustness.py b/agent/tests/test_engine_robustness.pyindex 8929e5812..1593a37f9 100644--- a/agent/tests/test_engine_robustness.py+++ b/agent/tests/test_engine_robustness.py@@ -119,15 +119,15 @@ class TestSymbolIsolation:engine = ChinaAEngine({"initial_cash": 1_000_000})- # Patch _rebalance to throw for BAD only- original_rebalance = ChinaAEngine._rebalance+ # Patch the opening-plan boundary to throw for BAD only.+ original_plan = ChinaAEngine._plan_open_order- def _exploding_rebalance(self, symbol, target_weight, df, ts, equity):+ def _exploding_plan(self, symbol, target_weight, df, ts, equity):if symbol == "BAD":raise RuntimeError("Simulated failure for BAD")- return original_rebalance(self, symbol, target_weight, df, ts, equity)+ return original_plan(self, symbol, target_weight, df, ts, equity)- with patch.object(ChinaAEngine, "_rebalance", _exploding_rebalance):+ with patch.object(ChinaAEngine, "_plan_open_order", _exploding_plan):# Should NOT raise — exception is caught internallyengine._execute_bars(dates, data_map, close_df, target_pos, valid_codes)