trading/shared/models/timeseries.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

57 lines
2.1 KiB
Python

"""TimescaleDB hypertable models: MarketData, PortfolioSnapshot, StrategyMetric."""
import uuid
from datetime import datetime
from sqlalchemy import DateTime, Float, ForeignKey, Integer, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column
from shared.models.base import Base
class MarketData(Base):
"""OHLCV bars — intended as a TimescaleDB hypertable partitioned by timestamp."""
__tablename__ = "market_data"
timestamp: Mapped[datetime] = mapped_column(
DateTime(timezone=True), primary_key=True
)
ticker: Mapped[str] = mapped_column(String(20), primary_key=True)
open: Mapped[float] = mapped_column(Float, nullable=False)
high: Mapped[float] = mapped_column(Float, nullable=False)
low: Mapped[float] = mapped_column(Float, nullable=False)
close: Mapped[float] = mapped_column(Float, nullable=False)
volume: Mapped[float] = mapped_column(Float, nullable=False)
class PortfolioSnapshot(Base):
"""Periodic portfolio value snapshots — TimescaleDB hypertable."""
__tablename__ = "portfolio_snapshots"
timestamp: Mapped[datetime] = mapped_column(
DateTime(timezone=True), primary_key=True
)
total_value: Mapped[float] = mapped_column(Float, nullable=False)
cash: Mapped[float] = mapped_column(Float, nullable=False)
positions_value: Mapped[float] = mapped_column(Float, nullable=False)
daily_pnl: Mapped[float] = mapped_column(Float, nullable=False)
class StrategyMetric(Base):
"""Per-strategy performance over time — TimescaleDB hypertable."""
__tablename__ = "strategy_metrics"
timestamp: Mapped[datetime] = mapped_column(
DateTime(timezone=True), primary_key=True
)
strategy_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("strategies.id"), primary_key=True
)
win_rate: Mapped[float] = mapped_column(Float, nullable=False)
total_pnl: Mapped[float] = mapped_column(Float, nullable=False)
trade_count: Mapped[int] = mapped_column(Integer, nullable=False)
sharpe_ratio: Mapped[float | None] = mapped_column(Float, nullable=True)