"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(backtest): liquidate 1x shorts on adverse moves in non-strict mode (#1291)
cgycoreye209cee8d176
diff --git a/agent/backtest/engines/_market_hooks.py b/agent/backtest/engines/_market_hooks.pyindex 55cbe43f6..c5df095a1 100644--- a/agent/backtest/engines/_market_hooks.py+++ b/agent/backtest/engines/_market_hooks.py@@ -306,10 +306,23 @@ def check_crypto_liquidation(Does NOT execute the liquidation -- caller handles that."""pos = positions.get(symbol)- if pos is None or pos.leverage <= 1.0:+ # A 1x long's bankruptcy price is zero, so leverage <= 1 exempts it.+ # A 1x short has no such floor: margin is the full notional, a 2x+ # adverse move makes margin + unrealized negative, and the position+ # would ride an impossible state on the books (#1291).+ if pos is None or (pos.leverage <= 1.0 and pos.direction > 0):return False- mark_price = float(bar.get("close", pos.entry_price))+ # Mark at the adverse extremum -- bar high for a short, low for a long --+ # mirroring the strict path's "adverse" price convention, and fall back to+ # the close when the bar lacks high/low (#1291). With high/low present a+ # wick through maintenance triggers; close-only bars keep legacy behavior.+ mark_price = bar.get("high" if pos.direction < 0 else "low")+ if mark_price is None or pd.isna(mark_price):+ mark_price = bar.get("close")+ if mark_price is None or pd.isna(mark_price):+ mark_price = pos.entry_price+ mark_price = float(mark_price)margin = pos.size * pos.entry_price / pos.leverageunrealized = pos.direction * pos.size * (mark_price - pos.entry_price)diff --git a/agent/tests/test_market_hooks_liquidation.py b/agent/tests/test_market_hooks_liquidation.pynew file mode 100644index 000000000..a0578fe9c--- /dev/null+++ b/agent/tests/test_market_hooks_liquidation.py@@ -0,0 +1,81 @@+"""Non-strict crypto liquidation must not exempt 1x shorts (#1291).++A leverage <= 1 exemption makes sense for a long -- bankruptcy price is zero --+but a 1x short survives an unbounded adverse move with equity below zero. The+hook also marks at the close instead of the adverse extremum, so a wick that+would liquidate any real position is ignored when high/low are present.+"""++from __future__ import annotations++import pandas as pd++from backtest.engines._market_hooks import check_crypto_liquidation+from backtest.models import Position+++def _pos(direction: int, leverage: float = 1.0, entry: float = 100.0, size: float = 10.0) -> Position:+ return Position(+ "BTC-USDT-PERP", direction, entry, pd.Timestamp("2026-01-05"), size, leverage+ )+++def _bar(close: float, high: float | None = None, low: float | None = None) -> pd.Series:+ row = {"close": close}+ if high is not None:+ row["high"] = high+ if low is not None:+ row["low"] = low+ return pd.Series(row)+++def test_1x_short_liquidates_through_twice_the_entry_price() -> None:+ """Margin is the full notional; a 2x adverse move zeroes it."""+ bar = _bar(close=200.0, high=200.0, low=101.0)+ assert check_crypto_liquidation(+ "BTC-USDT-PERP", bar, {"BTC-USDT-PERP": _pos(direction=-1)}+ ) is True+++def test_1x_short_favorable_move_survives() -> None:+ """Dropping the exemption must not over-liquidate a profitable short."""+ bar = _bar(close=50.0, high=51.0, low=49.0)+ assert check_crypto_liquidation(+ "BTC-USDT-PERP", bar, {"BTC-USDT-PERP": _pos(direction=-1)}+ ) is False+++def test_1x_long_survives_ninety_percent_drawdown() -> None:+ """The direction-aware exemption keeps the 1x long protection."""+ bar = _bar(close=10.0, high=101.0, low=9.0)+ assert check_crypto_liquidation(+ "BTC-USDT-PERP", bar, {"BTC-USDT-PERP": _pos(direction=1)}+ ) is False+++def test_wick_through_maintenance_triggers_when_high_low_present() -> None:+ """A levered long whose low pierces the maintenance margin is liquidated."""+ bar = _bar(close=100.0, high=101.0, low=30.0)+ assert check_crypto_liquidation(+ "BTC-USDT-PERP",+ bar,+ {"BTC-USDT-PERP": _pos(direction=1, leverage=2.0)},+ ) is True+++def test_close_only_bar_keeps_legacy_behavior() -> None:+ """Without high/low, close-only bars mark at the close as before."""+ bar = _bar(close=100.0)+ assert check_crypto_liquidation(+ "BTC-USDT-PERP",+ bar,+ {"BTC-USDT-PERP": _pos(direction=1, leverage=2.0)},+ ) is False+++def test_adverse_close_without_extremum_still_triggers_short() -> None:+ """A short marked at a catastrophic close liquidates even on close-only bars."""+ bar = _bar(close=250.0)+ assert check_crypto_liquidation(+ "BTC-USDT-PERP", bar, {"BTC-USDT-PERP": _pos(direction=-1)}+ ) is True