trading/shared/models/learning.py
Viktor Barzin 72cb1b6fe5
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
2026-02-22 15:17:07 +00:00

51 lines
1.9 KiB
Python

"""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