feat(trade-executor): defer outside_market_hours signals to next open
Kevin's signals are mid-long term (weeks/months) and he uploads almost
exclusively pre-market or evenings. Before this change, every such
signal hit RiskManager.outside_market_hours, got consumed off the
Redis stream, and was lost. End result: 71 emitted signals, 0 trades.
New behaviour: when RiskManager rejects with outside_market_hours,
push the signal into a Redis sorted-set keyed by next_market_open
(via Alpaca's clock API — handles weekends + holidays). A background
drain task polls the set every kevin_defer_drain_interval_s (60s);
any signal whose target <= now gets re-run through process_signal.
Safety:
- kevin_max_defer_hours (default 72h) caps signal staleness so we
don't trade on week-old views.
- Other RiskManager rejections (cooldown, kill-switch, drawdown
halt) fall through to the existing drop path.
- kevin_defer_outside_market_hours toggle defaults True; flip to
false for legacy behaviour.
Slack: new notify_deferred() emits "🕒 Meet Kevin: DEFERRED
NVDA until Mon 13:30 UTC (market closed; conviction 0.85)" instead
of the noisy outside_market_hours rejection spam.
Tests: 5 queue + 4 integration = 9 new, all 32 trade-executor tests
GREEN.
This commit is contained in:
parent
00a40c9d2f
commit
2855e79af4
6 changed files with 440 additions and 0 deletions
76
services/trade_executor/deferred_queue.py
Normal file
76
services/trade_executor/deferred_queue.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
"""Defer signals that arrive outside US market hours, drain at next market open.
|
||||
|
||||
Kevin's signals are mid/long-term (weeks/months). A signal that lands at
|
||||
Sunday 19:00 UTC should turn into a Monday-morning paper trade, not be
|
||||
dropped on the floor. This queue holds (signal, target_submission_ts)
|
||||
pairs in a Redis sorted-set keyed by target_submission_ts.
|
||||
|
||||
A background drain task in trade-executor's run() loop polls pop_due()
|
||||
every ~60 s; signals whose target_submission_ts <= now are reprocessed
|
||||
through the normal process_signal path.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from redis.asyncio import Redis
|
||||
|
||||
from shared.schemas.trading import TradeSignal
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DEFERRED_KEY = "kevin:deferred_signals"
|
||||
|
||||
|
||||
class DeferredSignalQueue:
|
||||
"""Redis-sorted-set wrapper. Score = target_submission_ts (epoch seconds).
|
||||
Member = JSON of {"signal": <TradeSignal.model_dump>, "queued_at": <ISO>}.
|
||||
"""
|
||||
|
||||
def __init__(self, redis: Redis) -> None:
|
||||
self.redis = redis
|
||||
|
||||
async def defer(self, signal: TradeSignal, target_ts: datetime) -> None:
|
||||
"""Add a signal to the queue, scheduled for re-submission at
|
||||
``target_ts`` (usually the next market-open timestamp).
|
||||
"""
|
||||
member = json.dumps(
|
||||
{
|
||||
"signal": signal.model_dump(mode="json"),
|
||||
"queued_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
)
|
||||
await self.redis.zadd(DEFERRED_KEY, {member: target_ts.timestamp()})
|
||||
|
||||
async def pop_due(
|
||||
self, now: datetime | None = None
|
||||
) -> list[tuple[TradeSignal, datetime]]:
|
||||
"""Atomically pop and return every signal whose target_ts <= now.
|
||||
|
||||
Returns: list of (TradeSignal, queued_at) pairs.
|
||||
"""
|
||||
now = now or datetime.now(timezone.utc)
|
||||
cutoff = now.timestamp()
|
||||
async with self.redis.pipeline(transaction=True) as pipe:
|
||||
pipe.zrangebyscore(DEFERRED_KEY, min="-inf", max=cutoff)
|
||||
pipe.zremrangebyscore(DEFERRED_KEY, min="-inf", max=cutoff)
|
||||
members, _removed = await pipe.execute()
|
||||
|
||||
result: list[tuple[TradeSignal, datetime]] = []
|
||||
for raw in members:
|
||||
try:
|
||||
if isinstance(raw, bytes):
|
||||
raw = raw.decode("utf-8")
|
||||
payload = json.loads(raw)
|
||||
signal = TradeSignal.model_validate(payload["signal"])
|
||||
queued_at = datetime.fromisoformat(payload["queued_at"])
|
||||
result.append((signal, queued_at))
|
||||
except Exception:
|
||||
logger.exception("Failed to deserialize deferred signal: %s", raw)
|
||||
return result
|
||||
|
||||
async def size(self) -> int:
|
||||
return await self.redis.zcard(DEFERRED_KEY)
|
||||
Loading…
Add table
Add a link
Reference in a new issue