feat: database models and alembic migrations — all tables per design
- shared/db.py: async engine + session factory - shared/models/base.py: DeclarativeBase + TimestampMixin - shared/models/trading.py: Strategy, Signal, Trade, Position, StrategyWeightHistory - shared/models/news.py: Article, ArticleSentiment - shared/models/learning.py: TradeOutcome, LearningAdjustment - shared/models/auth.py: User, UserCredential - shared/models/timeseries.py: MarketData, PortfolioSnapshot, StrategyMetric - Alembic async env.py with initial migration including TimescaleDB hypertables - 21 model tests covering enums, instantiation, metadata registration
This commit is contained in:
parent
ae5b3f89d1
commit
72cb1b6fe5
23 changed files with 1283 additions and 0 deletions
51
shared/models/learning.py
Normal file
51
shared/models/learning.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
"""Learning domain models: TradeOutcome, LearningAdjustment."""
|
||||
|
||||
import uuid
|
||||
from datetime import timedelta
|
||||
|
||||
from sqlalchemy import Boolean, Float, ForeignKey, Interval, String, Text
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from shared.models.base import Base, TimestampMixin
|
||||
|
||||
|
||||
class TradeOutcome(TimestampMixin, Base):
|
||||
__tablename__ = "trade_outcomes"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
trade_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("trades.id"), unique=True, nullable=False
|
||||
)
|
||||
hold_duration: Mapped[timedelta | None] = mapped_column(Interval, nullable=True)
|
||||
realized_pnl: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
roi_pct: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
was_profitable: Mapped[bool] = mapped_column(Boolean, nullable=False)
|
||||
|
||||
# Relationships
|
||||
trade: Mapped["Trade"] = relationship("Trade", back_populates="outcome")
|
||||
|
||||
|
||||
class LearningAdjustment(TimestampMixin, Base):
|
||||
__tablename__ = "learning_adjustments"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
strategy_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("strategies.id"), nullable=False
|
||||
)
|
||||
old_weight: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
new_weight: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
reason: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
reward_signal: Mapped[float] = mapped_column(Float, nullable=False)
|
||||
|
||||
# Relationships
|
||||
strategy: Mapped["Strategy"] = relationship("Strategy")
|
||||
|
||||
|
||||
# Avoid circular imports — reference by string in relationship()
|
||||
from shared.models.trading import Trade as Trade # noqa: E402, F401
|
||||
from shared.models.trading import Strategy as Strategy # noqa: E402, F401
|
||||
Loading…
Add table
Add a link
Reference in a new issue