fix: harden shadow book against book leakage (review of ba2df8b)
Review of the shadow book found seven ways the two books could leak into
each other; all are fixed here. The most serious silently invalidated the
comparison the shadow book exists to make.
- Shadow holdings no longer suppress the manual candidate list. The
open-trade exclusion filtered on any book, so shadow taking the
top-ranked names removed exactly those from the user's list and alerts,
confining the discretionary book to leftovers. Scoped to the manual
book. Closed-trade alerts and paper-book equity were leaking the same
way and are likewise scoped.
- Shadow sizing now matches _simulate_portfolio: min(1% risk, 20% notional
cap, available cash) from marked equity, plus the sub- dust guard.
Previously risk-only from realized equity, so a tight stop produced a
multiples-of-equity leveraged position the strategy would never take.
- Shadow only trades setups from the scan that just ran (<6h old) with one
setup per ticker. A failed or disabled scan step could otherwise open
positions from a prior session at stale prices.
- Gate-reset transitions are observed for both books, so a shadow stop-out
completes fail -> requalify instead of staying locked forever.
- Manual list/close endpoints default to the manual book and reject
hand-closing shadow trades; the performance endpoint is scoped to the
caller so 'your picks' is not every user's book.
- run_shadow_book is registered as a paused job so Admin can trigger it.
Also anchors three pre-existing paper-trade tests (and the new alpaca
window test) on the UTC date. They build fixtures from the local date but
the service stamps opened_at in UTC, so they failed only between 00:00 and
02:00 in a UTC+hh timezone -- latent on ba2df8b, exposed by the clock.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -402,7 +402,15 @@ async def list_trades(
|
||||
db: AsyncSession,
|
||||
user_id: int | None = None,
|
||||
status: str | None = None,
|
||||
book: str | None = MANUAL_BOOK,
|
||||
) -> list[dict]:
|
||||
"""Trades for the UI. Defaults to the discretionary book.
|
||||
|
||||
Shadow trades are attached to a user row for FK reasons only — they are not
|
||||
that person's decisions. Listing them alongside manual trades would mix two
|
||||
different books in one P&L and let the autonomous record be edited by hand.
|
||||
Pass ``book=None`` to deliberately span both.
|
||||
"""
|
||||
stmt = (
|
||||
select(PaperTrade, Ticker.symbol)
|
||||
.join(Ticker, PaperTrade.ticker_id == Ticker.id)
|
||||
@@ -411,6 +419,8 @@ async def list_trades(
|
||||
stmt = stmt.where(PaperTrade.user_id == user_id)
|
||||
if status is not None:
|
||||
stmt = stmt.where(PaperTrade.status == status)
|
||||
if book is not None:
|
||||
stmt = stmt.where(PaperTrade.book == book)
|
||||
stmt = stmt.order_by(PaperTrade.opened_at.desc())
|
||||
|
||||
rows = (await db.execute(stmt)).all()
|
||||
@@ -493,6 +503,13 @@ async def close_trade(
|
||||
trade = result.scalar_one_or_none()
|
||||
if trade is None:
|
||||
raise NotFoundError(f"Paper trade not found: {trade_id}")
|
||||
if trade.book == SHADOW_BOOK:
|
||||
# The shadow book's value is that no human touched it. A hand-closed
|
||||
# position would make its record something other than what the strategy
|
||||
# would have done; it exits only via the automatic exit policy.
|
||||
raise ValidationError(
|
||||
"Shadow book trades are closed by the exit policy, not by hand"
|
||||
)
|
||||
if trade.status == "closed":
|
||||
raise ValidationError("Trade is already closed")
|
||||
|
||||
@@ -811,7 +828,7 @@ def _cumulative_pnl(trades: list, ticker_closes: dict, days: list[date]) -> list
|
||||
return out
|
||||
|
||||
|
||||
async def performance_summary(db: AsyncSession) -> dict:
|
||||
async def performance_summary(db: AsyncSession, user_id: int | None = None) -> dict:
|
||||
"""Shadow book vs discretionary book vs SPY, from the configured start date.
|
||||
|
||||
Currency P&L is reported per book but is *not* the comparison — the books
|
||||
@@ -823,6 +840,12 @@ async def performance_summary(db: AsyncSession) -> dict:
|
||||
stmt = select(PaperTrade)
|
||||
if start is not None:
|
||||
stmt = stmt.where(func.date(PaperTrade.opened_at) >= start)
|
||||
if user_id is not None:
|
||||
# "Your picks" must be *yours*. The shadow book is a single autonomous
|
||||
# book with no owner, so it is never scoped to a user.
|
||||
stmt = stmt.where(
|
||||
(PaperTrade.book == SHADOW_BOOK) | (PaperTrade.user_id == user_id)
|
||||
)
|
||||
trades = list((await db.execute(stmt)).scalars().all())
|
||||
|
||||
benchmark_closes = await benchmark_service.load_benchmark_closes(db)
|
||||
|
||||
Reference in New Issue
Block a user