Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): preserve healthy zero-balance cross accounts

warren6189f55a75d6759
diff --git a/agent/backtest/perpetual_risk.py b/agent/backtest/perpetual_risk.py
index 260bf6669..0f275df15 100644
--- a/agent/backtest/perpetual_risk.py
+++ b/agent/backtest/perpetual_risk.py
@@ -395,7 +395,14 @@ class CrossMarginRiskModel:
risk.unrealized_pnl for risk in risks
)
maintenance = sum(risk.maintenance_margin for risk in risks)
- is_liquidated = margin_balance <= maintenance
+ # An empty cross account has no maintenance requirement. Zero is a
+ # valid, flat account; only a negative residual balance is insolvent.
+ # Accounts with positions keep the usual inclusive maintenance test.
+ is_liquidated = (
+ bool(risks) and margin_balance <= maintenance
+ ) or (
+ not risks and margin_balance < 0
+ )
liquidation_targets = (
tuple(position.symbol for position in account.positions)
if is_liquidated
diff --git a/agent/tests/test_perpetual_risk.py b/agent/tests/test_perpetual_risk.py
index c5489b886..75496b61f 100644
--- a/agent/tests/test_perpetual_risk.py
+++ b/agent/tests/test_perpetual_risk.py
@@ -386,6 +386,18 @@ def test_cross_empty_account_with_negative_balance_is_liquidated() -> None:
assert snapshot.margin_balance == pytest.approx(-100.0)
assert snapshot.maintenance_margin == pytest.approx(0.0)
+
+def test_cross_empty_account_with_zero_balance_is_healthy() -> None:
+ account = AccountState(0.0, (), "cross")
+
+ snapshot = CrossMarginRiskModel().evaluate(account, {})
+
+ assert snapshot.status == "healthy"
+ assert snapshot.liquidation_targets == ()
+ assert snapshot.margin_balance == pytest.approx(0.0)
+ assert snapshot.maintenance_margin == pytest.approx(0.0)
+
+
@pytest.mark.parametrize(
("evaluate", "account", "match"),
[