Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(risk): mark insolvent empty cross accounts as liquidated

santhreal18f89c7952e2
diff --git a/agent/backtest/perpetual_risk.py b/agent/backtest/perpetual_risk.py
index 28d449f69..260bf6669 100644
--- a/agent/backtest/perpetual_risk.py
+++ b/agent/backtest/perpetual_risk.py
@@ -395,15 +395,16 @@ class CrossMarginRiskModel:
risk.unrealized_pnl for risk in risks
)
maintenance = sum(risk.maintenance_margin for risk in risks)
+ is_liquidated = margin_balance <= maintenance
liquidation_targets = (
tuple(position.symbol for position in account.positions)
- if risks and margin_balance <= maintenance
+ if is_liquidated
else ()
)
return _risk_snapshot(
account,
risks,
fidelity_flags,
- "account_liquidation" if liquidation_targets else "healthy",
+ "account_liquidation" if is_liquidated else "healthy",
liquidation_targets,
)
diff --git a/agent/tests/test_perpetual_risk.py b/agent/tests/test_perpetual_risk.py
index 86a5926b2..c5489b886 100644
--- a/agent/tests/test_perpetual_risk.py
+++ b/agent/tests/test_perpetual_risk.py
@@ -378,6 +378,14 @@ def test_empty_accounts_are_healthy(margin_mode: str) -> None:
assert snapshot.margin_balance == pytest.approx(1_000.0)
+def test_cross_empty_account_with_negative_balance_is_liquidated() -> None:
+ account = AccountState(-100.0, (), "cross")
+ snapshot = CrossMarginRiskModel().evaluate(account, {})
+ assert snapshot.status == "account_liquidation"
+ assert snapshot.liquidation_targets == ()
+ assert snapshot.margin_balance == pytest.approx(-100.0)
+ assert snapshot.maintenance_margin == pytest.approx(0.0)
+
@pytest.mark.parametrize(
("evaluate", "account", "match"),
[