quanthedge / backend /app /models /holding.py
jashdoshi77's picture
QuantHedge: Full deployment with Docker + nginx + uvicorn
9d29748
"""
Holding ORM Model.
Tracks user investment positions — ticker, quantity, average price,
purchase date, and position type (long/short).
"""
from __future__ import annotations
from datetime import date, datetime
from sqlalchemy import (
Date,
DateTime,
Float,
ForeignKey,
Integer,
String,
Text,
func,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class Holding(Base):
"""A single investment position in a user's portfolio."""
__tablename__ = "holdings"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(
Integer, ForeignKey("users.id"), nullable=False, index=True
)
ticker: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
name: Mapped[str] = mapped_column(String(300), nullable=True)
quantity: Mapped[float] = mapped_column(Float, nullable=False)
avg_price: Mapped[float] = mapped_column(Float, nullable=False)
purchase_date: Mapped[date] = mapped_column(Date, nullable=True)
position_type: Mapped[str] = mapped_column(
String(10), nullable=False, default="long"
) # long / short
asset_class: Mapped[str] = mapped_column(
String(30), nullable=True, default="equity"
) # equity, etf, option, crypto, forex, commodity
exchange: Mapped[str] = mapped_column(String(20), nullable=True)
currency: Mapped[str] = mapped_column(String(10), nullable=True, default="USD")
notes: Mapped[str] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
)
user = relationship("User", back_populates="holdings")
def __repr__(self) -> str:
return f"<Holding(id={self.id}, ticker='{self.ticker}', qty={self.quantity})>"