fix: guarantee shadow scan freshness, long-only, user-scoped setup list

Second review round on the shadow book; all three findings were real.

- Scan freshness is now proven, not assumed. Pipeline steps run and fail
  independently, so a disabled or failed scan step still let the shadow
  step run on the newest *stored* setups -- a prior session's picks at
  stale prices. scan_all_tickers now records a run boundary
  (last_scan_run_started_at / _completed_at) only on successful
  completion; the shadow book refuses to trade unless COMPLETED is fresh
  and selects only setups with detected_at >= the run start. Deduplication
  to the latest row per ticker now happens BEFORE qualification, so a newer
  unqualified row suppresses an older qualified one rather than the reverse.

- Shadow selection is hard long-only. setup_qualifies only enforces
  long-only when min_momentum_percentile > 0, but 0 is a legal admin
  setting, and the cash accounting assumes long positions -- so the
  constraint is enforced in shadow selection regardless of gate config.

- The personal setup list excludes only the caller's own open positions.
  get_trade_setups gained exclude_open_trade_user_id; the trades route
  passes the authenticated user, while the Telegram broadcast stays global
  since it has no single owner.

New tests cover stale/absent scan markers, prior-run exclusion, newer
unqualified suppressing older qualified, long-only under a disabled gate,
and both sides of the user-scoped exclusion.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 09:31:24 +02:00
co-authored by Claude Fable 5
parent 247a92a89f
commit 6a10c8ff09
5 changed files with 230 additions and 40 deletions
@@ -603,6 +603,24 @@ async def test_get_trade_setups_can_exclude_tickers_with_open_paper_trades(
assert "OPENQ" not in discovery_symbols
assert {"CLOSEDQ", "FREEQ"}.issubset(discovery_symbols)
# Scoped to a *different* user: OPENQ is user 1's position, so user 2's
# personal list must still show it (their list is not filtered by someone
# else's holdings).
scoped_rows = await get_trade_setups(
db_session,
exclude_open_trade_tickers=True,
exclude_open_trade_user_id=2,
)
assert "OPENQ" in {row["symbol"] for row in scoped_rows}
# Scoped to the owning user: their own open position is excluded.
owner_rows = await get_trade_setups(
db_session,
exclude_open_trade_tickers=True,
exclude_open_trade_user_id=1,
)
assert "OPENQ" not in {row["symbol"] for row in owner_rows}
ticker_rows = await get_trade_setups(db_session, symbol="OPENQ")
assert [row["symbol"] for row in ticker_rows] == ["OPENQ"]
+115 -17
View File
@@ -49,11 +49,20 @@ async def _seed(session, symbols: list[str]) -> dict[str, int]:
return ids
def _setup(ticker_id: int, *, rank: float, detected: datetime, entry=100.0, stop=95.0):
target = entry + 3 * (entry - stop)
def _setup(
ticker_id: int,
*,
rank: float,
detected: datetime,
entry=100.0,
stop=95.0,
direction="long",
):
reward = abs(entry - stop) * 3
target = entry + reward if direction == "long" else entry - reward
return TradeSetup(
ticker_id=ticker_id,
direction="long",
direction=direction,
entry_price=entry,
stop_loss=stop,
target=target,
@@ -70,6 +79,20 @@ def _setup(ticker_id: int, *, rank: float, detected: datetime, entry=100.0, stop
)
async def _mark_scan(session, *, started: datetime, completed: datetime | None = None):
"""Record a successful scan run so the shadow book has something to act on."""
from app.services import rr_scanner_service as rr
completed = completed or started
await shadow_book_service.settings_store.upsert_setting(
session, rr.KEY_LAST_SCAN_STARTED, started.isoformat()
)
await shadow_book_service.settings_store.upsert_setting(
session, rr.KEY_LAST_SCAN_COMPLETED, completed.isoformat()
)
await session.commit()
class TestSizing:
def test_risks_one_percent_down_to_the_stop(self):
shares = shadow_book_service.position_shares(100_000, 1.0, 100.0, 95.0)
@@ -105,6 +128,7 @@ class TestSelection:
async def test_takes_top_ranked_up_to_capacity(self, session):
ids = await _seed(session, ["AAA", "BBB", "CCC"])
now = datetime.now(timezone.utc)
scan_start = now - timedelta(minutes=5)
session.add_all(
[
_setup(ids["AAA"], rank=0.10, detected=now),
@@ -113,9 +137,11 @@ class TestSelection:
]
)
await session.commit()
await _mark_scan(session, started=scan_start, completed=now)
await shadow_book_service.settings_store.upsert_setting(
session, shadow_book_service.KEY_CAPACITY, "2"
)
await session.commit()
summary = await shadow_book_service.open_shadow_positions(
session, activation_config=_CONFIG
@@ -140,6 +166,7 @@ class TestSelection:
)
)
await session.commit()
await _mark_scan(session, started=now - timedelta(minutes=5), completed=now)
summary = await shadow_book_service.open_shadow_positions(
session, activation_config=_CONFIG
@@ -163,6 +190,7 @@ class TestSelection:
)
)
await session.commit()
await _mark_scan(session, started=now - timedelta(minutes=5), completed=now)
summary = await shadow_book_service.open_shadow_positions(
session, activation_config=_CONFIG
@@ -172,14 +200,12 @@ class TestSelection:
assert summary["skipped_locked"] == 1
class TestSetupFreshness:
class TestScanFreshness:
@pytest.mark.asyncio
async def test_ignores_setups_from_a_previous_session(self, session):
"""If the scan step failed or was disabled, the newest stored setups are
yesterday's — trading them would enter stale picks at stale prices."""
async def test_no_scan_marker_means_no_trades(self, session):
"""A fresh DB / never-run scan must not trade anything."""
ids = await _seed(session, ["AAA"])
stale = datetime.now(timezone.utc) - timedelta(days=1)
session.add(_setup(ids["AAA"], rank=0.9, detected=stale))
session.add(_setup(ids["AAA"], rank=0.9, detected=datetime.now(timezone.utc)))
await session.commit()
summary = await shadow_book_service.open_shadow_positions(
@@ -189,22 +215,94 @@ class TestSetupFreshness:
assert summary["opened"] == 0
@pytest.mark.asyncio
async def test_one_position_per_ticker_from_duplicate_setups(self, session):
async def test_stale_scan_marker_refuses_even_fresh_looking_setups(self, session):
"""If this pipeline's scan failed/was disabled, the completion marker is
from a prior session — refuse, no matter how recent the setup rows look."""
ids = await _seed(session, ["AAA"])
now = datetime.now(timezone.utc)
session.add_all(
[
_setup(ids["AAA"], rank=0.5, detected=now - timedelta(minutes=30)),
_setup(ids["AAA"], rank=0.9, detected=now),
]
)
session.add(_setup(ids["AAA"], rank=0.9, detected=now))
await session.commit()
# Marker is a day old → no scan ran in this pass.
await _mark_scan(session, started=now - timedelta(days=1, minutes=5),
completed=now - timedelta(days=1))
summary = await shadow_book_service.open_shadow_positions(
session, activation_config=_CONFIG
)
assert summary["opened"] == 1
assert summary["opened"] == 0
@pytest.mark.asyncio
async def test_setups_before_this_run_are_excluded(self, session):
"""A qualified row from a previous run (before the current scan start)
must not be traded even though the current scan completed."""
ids = await _seed(session, ["AAA", "BBB"])
now = datetime.now(timezone.utc)
scan_start = now - timedelta(minutes=5)
session.add_all(
[
_setup(ids["AAA"], rank=0.9, detected=now), # this run
_setup(ids["BBB"], rank=0.8, detected=now - timedelta(hours=20)), # prior run
]
)
await session.commit()
await _mark_scan(session, started=scan_start, completed=now)
summary = await shadow_book_service.open_shadow_positions(
session, activation_config=_CONFIG
)
assert summary["symbols"] == [ids["AAA"]]
@pytest.mark.asyncio
async def test_newer_unqualified_row_suppresses_older_qualified(self, session):
"""Dedup happens before qualification: a fresh unqualified row for a
ticker must beat an earlier qualified row, not the other way round."""
ids = await _seed(session, ["AAA"])
now = datetime.now(timezone.utc)
scan_start = now - timedelta(minutes=10)
# Earlier row qualifies; later row fails the R:R floor (rr 1.0 < 2.0).
older = _setup(ids["AAA"], rank=0.9, detected=now - timedelta(minutes=8))
newer = TradeSetup(
ticker_id=ids["AAA"], direction="long", entry_price=100.0,
stop_loss=95.0, target=105.0, rr_ratio=1.0, composite_score=70.0,
confidence_score=70.0, detected_at=now, strategy_rank=0.9,
momentum_percentile=90.0, recommended_action="buy",
targets_json=json.dumps(
[{"price": 105.0, "probability": 45.0, "is_primary": True, "rr": 1.0}]
),
)
session.add_all([older, newer])
await session.commit()
await _mark_scan(session, started=scan_start, completed=now)
summary = await shadow_book_service.open_shadow_positions(
session, activation_config=_CONFIG
)
assert summary["opened"] == 0
class TestLongOnly:
@pytest.mark.asyncio
async def test_shorts_are_never_taken_even_with_gate_disabled(self, session):
"""min_momentum_percentile=0 lets shorts pass the gate; shadow is always
long-only regardless, and its cash accounting assumes longs."""
ids = await _seed(session, ["AAA"])
now = datetime.now(timezone.utc)
session.add(
_setup(ids["AAA"], rank=0.9, detected=now, direction="short",
entry=100.0, stop=105.0)
)
await session.commit()
await _mark_scan(session, started=now - timedelta(minutes=5), completed=now)
gate_off = {**_CONFIG, "min_momentum_percentile": 0.0}
summary = await shadow_book_service.open_shadow_positions(
session, activation_config=gate_off
)
assert summary["opened"] == 0
class TestBookIsolation: