77 lines
3.6 KiB
Python
77 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import Boolean, DateTime, Float, Integer, String, func
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.core.database import Base
|
|
|
|
|
|
class Stock(Base):
|
|
__tablename__ = "stocks"
|
|
|
|
code: Mapped[str] = mapped_column(String(10), primary_key=True)
|
|
name: Mapped[str] = mapped_column(String(50), nullable=False)
|
|
market: Mapped[str] = mapped_column(String(10), nullable=False, default="KRX")
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
|
|
|
|
|
class PriceHistory(Base):
|
|
__tablename__ = "price_history"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
stock_code: Mapped[str] = mapped_column(String(10), nullable=False, index=True)
|
|
datetime: Mapped[datetime] = mapped_column(DateTime, nullable=False, index=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[int] = mapped_column(Integer, default=0)
|
|
|
|
|
|
class Trade(Base):
|
|
__tablename__ = "trades"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
order_no: Mapped[str] = mapped_column(String(20), default="")
|
|
stock_code: Mapped[str] = mapped_column(String(10), nullable=False, index=True)
|
|
stock_name: Mapped[str] = mapped_column(String(50), default="")
|
|
side: Mapped[str] = mapped_column(String(4), nullable=False) # buy / sell
|
|
qty: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
price: Mapped[float] = mapped_column(Float, nullable=False)
|
|
order_type: Mapped[str] = mapped_column(String(4), default="00") # 지정가
|
|
status: Mapped[str] = mapped_column(String(10), default="pending") # pending, filled, cancelled, rejected
|
|
strategy_id: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
|
filled_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
|
|
|
|
|
class Strategy(Base):
|
|
__tablename__ = "strategies"
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
name: Mapped[str] = mapped_column(String(100), nullable=False)
|
|
strategy_type: Mapped[str] = mapped_column(String(20), nullable=False)
|
|
stock_code: Mapped[str] = mapped_column(String(10), nullable=False, index=True)
|
|
params_json: Mapped[str] = mapped_column(String(500), default="{}")
|
|
order_type: Mapped[str] = mapped_column(String(4), default="00")
|
|
qty: Mapped[int] = mapped_column(Integer, default=1)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
|
|
|
|
|
|
class Holding(Base):
|
|
__tablename__ = "holdings"
|
|
|
|
stock_code: Mapped[str] = mapped_column(String(10), primary_key=True)
|
|
stock_name: Mapped[str] = mapped_column(String(50), default="")
|
|
qty: Mapped[int] = mapped_column(Integer, default=0)
|
|
avg_price: Mapped[float] = mapped_column(Float, default=0.0)
|
|
current_price: Mapped[float] = mapped_column(Float, default=0.0)
|
|
profit: Mapped[float] = mapped_column(Float, default=0.0)
|
|
profit_rate: Mapped[float] = mapped_column(Float, default=0.0)
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
|