Files
momentum-system/tests/unit/runtime/test_flatten_close.py
T
momentum devandClaude Fable 5 390d6660ea fix: halt-aware flatten — let a working market sell rest instead of churning
A market sell that sits unfilled means the symbol is halted (or session
closed); cancelling and resubmitting it every exit-manager pass fills no
faster and spams the broker — YXT 2026-08-05: 87 market sells across a
10-minute LULD halt, 86 cancelled, plus one exit_naked_stop journal event
every ~4s. On a halt-down the resting sell is also the position's only
protection.

cancel_protective_and_close now returns False (nothing submitted) when a
market sell for the symbol is already working, and only cancels
non-market protective sells before closing. Exit-manager paths
(catastrophe / naked-stop / first-red / trail) journal the exit only when
a close was actually submitted.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-05 19:01:18 -05:00

65 lines
1.9 KiB
Python

"""cancel_protective_and_close: halt-aware liquidation (the YXT 2026-08-05
churn — 87 market sells through a 10-minute LULD halt, 86 cancelled)."""
from runtime.flatten import cancel_protective_and_close
class FakeClient:
def __init__(self):
self.cancelled: list[str] = []
self.closed: list[str] = []
def cancel_order(self, order_id):
self.cancelled.append(order_id)
def close_position(self, symbol, qty=None):
self.closed.append(symbol)
def _order(**kw):
base = {"symbol": "YXT", "side": "sell", "type": "market",
"status": "accepted", "id": "o1", "legs": []}
base.update(kw)
return base
def test_resting_market_sell_is_left_alone():
client = FakeClient()
submitted = cancel_protective_and_close(client, "YXT", orders=[_order()])
assert submitted is False
assert client.cancelled == []
assert client.closed == []
def test_protective_stop_is_cancelled_then_closed():
client = FakeClient()
submitted = cancel_protective_and_close(
client, "YXT", orders=[_order(type="stop", id="stop1")])
assert submitted is True
assert client.cancelled == ["stop1"]
assert client.closed == ["YXT"]
def test_no_orders_just_closes():
client = FakeClient()
assert cancel_protective_and_close(client, "YXT", orders=[]) is True
assert client.closed == ["YXT"]
def test_dead_market_sell_does_not_block_close():
# a filled/cancelled market sell no longer reserves shares — must not be
# mistaken for an in-flight liquidation
client = FakeClient()
submitted = cancel_protective_and_close(
client, "YXT", orders=[_order(status="canceled")])
assert submitted is True
assert client.closed == ["YXT"]
def test_other_symbols_market_sell_ignored():
client = FakeClient()
submitted = cancel_protective_and_close(
client, "YXT", orders=[_order(symbol="SWIM")])
assert submitted is True
assert client.closed == ["YXT"]