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

49 lines
1.8 KiB
Python

"""News and sentiment models: Article, ArticleSentiment."""
import uuid
from datetime import datetime
from sqlalchemy import DateTime, Float, ForeignKey, String, Text
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship
from shared.models.base import Base, TimestampMixin
class Article(TimestampMixin, Base):
__tablename__ = "articles"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
source: Mapped[str] = mapped_column(String(100), nullable=False)
url: Mapped[str] = mapped_column(Text, nullable=False)
title: Mapped[str] = mapped_column(Text, nullable=False)
published_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True
)
fetched_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
content_hash: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
# Relationships
sentiments: Mapped[list["ArticleSentiment"]] = relationship(back_populates="article")
class ArticleSentiment(TimestampMixin, Base):
__tablename__ = "article_sentiments"
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
article_id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), ForeignKey("articles.id"), nullable=False
)
ticker: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
score: Mapped[float] = mapped_column(Float, nullable=False)
confidence: Mapped[float] = mapped_column(Float, nullable=False)
model_used: Mapped[str] = mapped_column(String(50), nullable=False)
# Relationships
article: Mapped[Article] = relationship(back_populates="sentiments")