Skip to content

"Vibe-Trading: Your Personal Trading Agent"

test(backtest): cover wick-only crypto liquidation, cite fill convention (#1291)

cgycoreyb7e78496a28d
diff --git a/agent/backtest/engines/_market_hooks.py b/agent/backtest/engines/_market_hooks.py
index c5df095a1..890b2a498 100644
--- a/agent/backtest/engines/_market_hooks.py
+++ b/agent/backtest/engines/_market_hooks.py
@@ -306,7 +306,7 @@ def check_crypto_liquidation(
Does NOT execute the liquidation -- caller handles that.
"""
pos = positions.get(symbol)
- # A 1x long's bankruptcy price is zero, so leverage <= 1 exempts it.
+ # A 1x long's bankruptcy price is zero, so leverage <= 1 exempts a 1x long.
# 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).
diff --git a/agent/backtest/engines/composite.py b/agent/backtest/engines/composite.py
index 6046bc2e3..edd2652f3 100644
--- a/agent/backtest/engines/composite.py
+++ b/agent/backtest/engines/composite.py
@@ -261,6 +261,9 @@ class CompositeEngine(BaseEngine):
if check_crypto_liquidation(symbol, bar, self.positions):
pos = self.positions.get(symbol)
if pos is not None:
+ # The hook may fire on the bar's adverse extremum (high/low);
+ # daily bars cannot observe intra-bar timing, so the fill is
+ # priced at the close and then slippaged adversely.
mark_price = float(bar.get("close", pos.entry_price))
liq_price = crypto_sub.apply_slippage(mark_price, -pos.direction)
self._close_position(symbol, liq_price, timestamp, "liquidation")
diff --git a/agent/backtest/engines/crypto.py b/agent/backtest/engines/crypto.py
index 72c253297..d08ff584d 100644
--- a/agent/backtest/engines/crypto.py
+++ b/agent/backtest/engines/crypto.py
@@ -609,6 +609,9 @@ class CryptoEngine(BaseEngine):
if check_crypto_liquidation(symbol, bar, self.positions):
pos = self.positions.get(symbol)
if pos is not None:
+ # The hook may fire on the bar's adverse extremum (high/low);
+ # daily bars cannot observe intra-bar timing, so the fill is
+ # priced at the close and then slippaged adversely.
mark_price = float(bar.get("close", pos.entry_price))
liq_price = self.apply_slippage(mark_price, -pos.direction)
self._close_position(symbol, liq_price, timestamp, "liquidation")
diff --git a/agent/tests/test_crypto_engine.py b/agent/tests/test_crypto_engine.py
index e87b25560..d95533f7c 100644
--- a/agent/tests/test_crypto_engine.py
+++ b/agent/tests/test_crypto_engine.py
@@ -435,6 +435,22 @@ class TestLiquidation:
engine.on_bar("BTC-USDT", bar, ts)
assert "BTC-USDT" not in engine.positions
+ def test_wick_only_trigger_liquidates(self) -> None:
+ """A levered long whose low pierces maintenance is liquidated even when
+ the close recovers; the fill is priced at the close (#1291)."""
+ engine = _make_engine(leverage=2.0)
+ engine.positions["BTC-USDT"] = Position(
+ "BTC-USDT", 1, 100.0, pd.Timestamp("2025-01-01"), 10.0, leverage=2.0,
+ )
+ # Hook marks at low=30: margin 500, unrealized -700 -> equity -200,
+ # <= maint (300 * 0.004 = 1.2), so liquidation fires on the wick alone.
+ bar = pd.Series({"close": 100.0, "high": 101.0, "low": 30.0})
+ ts = pd.Timestamp("2025-01-02")
+ engine.on_bar("BTC-USDT", bar, ts)
+ assert "BTC-USDT" not in engine.positions
+ assert len(engine.trades) == 1
+ assert engine.trades[0].exit_reason == "liquidation"
+
# ---------------------------------------------------------------------------
# Tiered maintenance margin