The backstop behind the EOD-flatten fix: even with the EOD flatten working, a carry can survive (loop down at EOD, or the held-qty 403 before it was fixed) and bleed for days — ATPC sat 4 days over the Juneteenth gap, -$651. This closes any position carried from a PRIOR session, at the open, before it bleeds. - runtime/flatten.find_overnight_carries (pure, tested): open positions with no BUY filled today (ET) = carried overnight. Today's fresh names have a buy today, so they're never touched. - ExecutionService.flatten_overnight_carries: detects carries from the broker (positions + filled buys) and closes them via the robust cancel-legs-then-close path — WITHOUT halting the session (unlike close_session), so today's trading continues. Emits an open_catchup_flatten risk event. - run_live_paper step_open_catchup_flatten: once per session, during RTH, gated by TRADING_OPEN_CATCHUP_FLATTEN_ENABLED (def 1). Loud log when it flattens a carry. Dry-run against the live book: correctly targets ONLY ATPC (6/18 carry), leaves today's DAMD/ECX. 38 runtime/safety tests pass. Needs a loop restart to activate. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
"""Overnight-carry detection for the open catch-up flatten."""
|
|
|
|
from datetime import date
|
|
|
|
from runtime.flatten import find_overnight_carries
|
|
|
|
TODAY = date(2026, 6, 22)
|
|
|
|
|
|
def _buy(sym, filled_at):
|
|
return {"symbol": sym, "filled_at": filled_at}
|
|
|
|
|
|
def test_position_bought_today_is_not_a_carry():
|
|
# 14:00Z == 10:00 ET on 6/22 -> bought today
|
|
carries = find_overnight_carries({"DAMD"}, [_buy("DAMD", "2026-06-22T14:00:00Z")], TODAY)
|
|
assert carries == []
|
|
|
|
|
|
def test_position_with_no_buy_today_is_a_carry():
|
|
# only a prior-day buy -> carried overnight
|
|
carries = find_overnight_carries({"ATPC"}, [_buy("ATPC", "2026-06-18T14:26:00Z")], TODAY)
|
|
assert carries == ["ATPC"]
|
|
|
|
|
|
def test_mixed_book_returns_only_carries():
|
|
open_syms = {"ATPC", "DAMD", "NIXX"}
|
|
fills = [
|
|
_buy("ATPC", "2026-06-18T14:26:00Z"), # carry
|
|
_buy("DAMD", "2026-06-22T14:34:00Z"), # today
|
|
_buy("NIXX", "2026-06-22T14:00:00Z"), # today
|
|
]
|
|
assert find_overnight_carries(open_syms, fills, TODAY) == ["ATPC"]
|
|
|
|
|
|
def test_late_utc_fill_maps_to_prior_et_day():
|
|
# 01:00Z on 6/22 == 21:00 ET on 6/21 -> a prior session, so a carry
|
|
carries = find_overnight_carries({"XYZ"}, [_buy("XYZ", "2026-06-22T01:00:00Z")], TODAY)
|
|
assert carries == ["XYZ"]
|
|
|
|
|
|
def test_no_open_positions_no_carries():
|
|
assert find_overnight_carries(set(), [_buy("AAA", "2026-06-18T14:00:00Z")], TODAY) == []
|
|
|
|
|
|
def test_open_with_no_fills_at_all_is_a_carry():
|
|
# open but we have zero buy-fill records for it -> treat as carried (fail safe)
|
|
assert find_overnight_carries({"GHOST"}, [], TODAY) == ["GHOST"]
|