안 B(점진/additive-first)의 1단계. 라이브 영향·cascade 없이 자산 영구 슬롯 토대의 스키마/모델 필드만 additive 추가. - Slot.instance_key(기본 "default")·retired_at 추가 + EXPECTED_SLOT_FIELDS 정합 - 마이그레이션 009: instance_key NOT NULL DEFAULT 'default', retired_at, 부분 UNIQUE(instance_key, portfolio_id) WHERE portfolio_id IS NOT NULL (멱등) - postgresql _SLOT_COLUMNS/_row_to_slot/update_slot 매핑(row[19]/[20]) - entry_count/trading_date 컬럼은 유지(drop 은 cutover BAT-δ) 검증: 전체 375 passed / 35 skipped, ruff clean (additive — cascade 무파손). M2(상태머신 inactive 별칭+인스턴스 lock)/M3(소비처 cascade)는 후속. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
109 lines
3.9 KiB
Python
109 lines
3.9 KiB
Python
"""Slot domain data classes (REQ-001, REQ-002)."""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from datetime import date, datetime
|
|
from decimal import Decimal
|
|
from typing import Optional
|
|
from uuid import UUID
|
|
|
|
from trex_core.domain.slot.state import SlotState
|
|
|
|
|
|
@dataclass
|
|
class Slot:
|
|
"""Single daily_slot row.
|
|
|
|
Matches the slot column contract recorded in dcy-001 부록 A and
|
|
docs/data-model.md (trex-core stores this in-memory for now; a future
|
|
persistence layer must satisfy the same contract).
|
|
"""
|
|
|
|
slot_id: UUID
|
|
trading_date: date
|
|
status: SlotState
|
|
symbol: Optional[str] = None
|
|
qty: Optional[float] = None
|
|
avg_price: Optional[float] = None
|
|
# dcy-012 / REQ-002: side-agnostic "current pending order limit price"
|
|
# (renamed from entry_price). Set on pending entry (buy/sell limit), cleared
|
|
# on pending exit (→HOLDING, →CLOSED) so a buy limit never leaks into a sell
|
|
# order (issue #10). None => market order at dispatch.
|
|
limit_price: Optional[float] = None
|
|
allocated_capital: Optional[float] = None
|
|
order_id: Optional[str] = None
|
|
strategy_id: Optional[str] = None
|
|
broker_order_id: Optional[str] = None # BAT-003 / REQ-011 (trex-orbit lineage)
|
|
portfolio_id: Optional[str] = None # BAT-006 / REQ-025 (trex-orbit lineage)
|
|
sell_strategy_id: Optional[str] = None # BAT-008 / REQ-047 (trex-orbit lineage)
|
|
forced_close_at: Optional[datetime] = None # BAT-014 / REQ-066 (trex-orbit lineage)
|
|
stop_loss_price: Optional[Decimal] = None # REQ-035 / BAT-010
|
|
take_profit_price: Optional[Decimal] = None # REQ-035 / BAT-010
|
|
# dcy-019 / REQ-002 (BAT-025): slot-scoped cumulative entry count. +1 on each
|
|
# allocation transition (idle/closed → pending_buy); drives admission slot
|
|
# selection (lowest entry_count first). Distinct from the symbol-scoped
|
|
# daily_symbol_entries counter (REQ-002 AC-3) — different key/purpose.
|
|
entry_count: int = 0
|
|
created_at: Optional[datetime] = None
|
|
updated_at: Optional[datetime] = None
|
|
# dcy-020 / REQ-056 (BAT-027): asset-centric permanent slot foundation (안 B).
|
|
# instance_key scopes slots per daemon instance in a shared DB
|
|
# (UNIQUE(instance_key, portfolio_id)); advisory lock is also instance-scoped.
|
|
# retired_at marks a soft-retired / universe-detached slot (OQ-2). Both added
|
|
# additively here; entry_count/trading_date removal is deferred to cutover (BAT-δ).
|
|
instance_key: str = "default"
|
|
retired_at: Optional[datetime] = None
|
|
|
|
|
|
# Canonical column contract for `Slot` (dcy-001 OQ-2 / FR-5).
|
|
# test_contract.py asserts this matches `dataclasses.fields(Slot)` so any
|
|
# field add/remove fails loudly. Type mapping lives in the docs (dcy-001 부록 A
|
|
# / docs/data-model.md), not here — names/presence only.
|
|
EXPECTED_SLOT_FIELDS: frozenset[str] = frozenset({
|
|
"slot_id",
|
|
"trading_date",
|
|
"status",
|
|
"symbol",
|
|
"qty",
|
|
"avg_price",
|
|
"limit_price",
|
|
"allocated_capital",
|
|
"order_id",
|
|
"strategy_id",
|
|
"broker_order_id",
|
|
"portfolio_id",
|
|
"sell_strategy_id",
|
|
"forced_close_at",
|
|
"stop_loss_price", # REQ-035
|
|
"take_profit_price", # REQ-035
|
|
"entry_count", # dcy-019 / REQ-002 (BAT-025)
|
|
"created_at",
|
|
"updated_at",
|
|
"instance_key", # dcy-020 / REQ-056 (BAT-027)
|
|
"retired_at", # dcy-020 / REQ-056 (BAT-027)
|
|
})
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class SlotCapacity:
|
|
"""Snapshot of slot capacity returned by compute_slot_capacity (REQ-002 AC-6)."""
|
|
|
|
idle_count: int
|
|
holding_count: int
|
|
pending_count: int
|
|
total_slots: int
|
|
scarcity_ratio: float
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class SlotTransitionRecord:
|
|
"""One append-only row in slot_transition_log (REQ-001, REQ-002)."""
|
|
|
|
slot_id: UUID
|
|
from_state: SlotState
|
|
to_state: SlotState
|
|
ts: datetime
|
|
reason: str
|
|
actor: str
|
|
extra: dict = field(default_factory=dict)
|