from datetime import date, datetime from sqlalchemy import Date, String, DateTime from sqlalchemy.orm import Mapped, mapped_column, relationship from app.database import Base class Ticker(Base): __tablename__ = "tickers" id: Mapped[int] = mapped_column(primary_key=True) symbol: Mapped[str] = mapped_column(String(10), unique=True, nullable=False) # Company name (e.g. "Biogen Inc."); backfilled from Alpaca, nullable for # symbols Alpaca doesn't know. name: Mapped[str | None] = mapped_column(String(120), nullable=True) # SEC issuer identity, refreshed by the SEC fundamentals import from # company_tickers.json / submissions. The only ticker<->issuer join point; # multi-class tickers (GOOG/GOOGL) share these values. Nullable: not every # symbol resolves to a CIK (e.g. ADRs, foreign issuers not in SEC data). cik: Mapped[str | None] = mapped_column(String(10), nullable=True) sic: Mapped[str | None] = mapped_column(String(4), nullable=True) sic_description: Mapped[str | None] = mapped_column(String(160), nullable=True) # Delisting is recorded, never deleted: the rows carry the price history that # makes a backtest less survivorship-biased, and a delete cascades it away. # NULL == actively traded. The live signal path filters on this (see # ticker_service.active_only); list/admin views keep the row and show it. delisted_on: Mapped[date | None] = mapped_column(Date, nullable=True, index=True) # How we learned: "form_25" (SEC confirmed), "manual" (operator). delisted_reason: Mapped[str | None] = mapped_column(String(32), nullable=True) # The sibling of delisted_on, and the distinction matters: delisted means the # security stopped trading; untracked means it still trades and a prune took # it out of the scanner. Both retain every row — the delete this replaced # cascaded away exactly the history a survivorship-honest backtest needs. # # This records *exclusion by pruning*, NOT current index membership. The # scheduled sync runs prune_missing=False, so a symbol can have left the # index and still be scanned; the two states legitimately differ. Membership # itself lives in universe_memberships. # # active_only filters on it; the backtest's universe query deliberately does # not, so today's scanner state cannot erase yesterday's candidates. untracked_on: Mapped[date | None] = mapped_column(Date, nullable=True, index=True) # The operator's own "stop scanning this", kept apart from the prune flag on # purpose. If one column served both, a prune -> re-entry cycle would clear # an explicit operator "off", because re-entry cannot tell which writer set # it. Only set_tracked writes this; no bootstrap ever clears it. scan_disabled_on: Mapped[date | None] = mapped_column(Date, nullable=True, index=True) # Who owns this row's tracking: "index" (created by a universe bootstrap) or # "manual" (an operator added it). Pruning only ever touches index-owned # rows. Ownership is sticky towards manual — a bootstrap must never convert # manual to index, or a hand-added symbol that happens to join the index # would lose its protection when it later leaves. tracking_source: Mapped[str] = mapped_column( String(16), nullable=False, server_default="manual", default="manual" ) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=datetime.utcnow, nullable=False ) # Relationships (cascade deletes) ohlcv_records = relationship("OHLCVRecord", back_populates="ticker", cascade="all, delete-orphan") sentiment_scores = relationship("SentimentScore", back_populates="ticker", cascade="all, delete-orphan") fundamental_data = relationship("FundamentalData", back_populates="ticker", cascade="all, delete-orphan") sr_levels = relationship("SRLevel", back_populates="ticker", cascade="all, delete-orphan") dimension_scores = relationship("DimensionScore", back_populates="ticker", cascade="all, delete-orphan") composite_scores = relationship("CompositeScore", back_populates="ticker", cascade="all, delete-orphan") trade_setups = relationship("TradeSetup", back_populates="ticker", cascade="all, delete-orphan") watchlist_entries = relationship("WatchlistEntry", back_populates="ticker", cascade="all, delete-orphan") ingestion_progress = relationship("IngestionProgress", back_populates="ticker", cascade="all, delete-orphan", uselist=False) earnings_events = relationship("EarningsEvent", back_populates="ticker", cascade="all, delete-orphan") universe_memberships = relationship("UniverseMembership", back_populates="ticker", cascade="all, delete-orphan")