65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
"""Storage: Data models for trades, orders, etc."""
|
|
|
|
from sqlalchemy import create_engine, Column, Integer, String, Float, DateTime, Enum
|
|
from sqlalchemy.orm import declarative_base
|
|
from sqlalchemy.sql import func
|
|
import enum
|
|
|
|
Base = declarative_base()
|
|
|
|
class OrderStatus(enum.Enum):
|
|
PENDING = "PENDING"
|
|
OPEN = "OPEN"
|
|
EXECUTED = "EXECUTED"
|
|
CANCELED = "CANCELED"
|
|
REJECTED = "REJECTED"
|
|
|
|
class OrderType(enum.Enum):
|
|
MARKET = "MARKET"
|
|
LIMIT = "LIMIT"
|
|
|
|
class TransactionType(enum.Enum):
|
|
BUY = "BUY"
|
|
SELL = "SELL"
|
|
|
|
class Order(Base):
|
|
__tablename__ = 'orders'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
symbol = Column(String, nullable=False)
|
|
quantity = Column(Integer, nullable=False)
|
|
price = Column(Float)
|
|
order_type = Column(Enum(OrderType), nullable=False)
|
|
transaction_type = Column(Enum(TransactionType), nullable=False)
|
|
status = Column(Enum(OrderStatus), default=OrderStatus.PENDING)
|
|
created_at = Column(DateTime, default=func.now())
|
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
|
|
|
def __repr__(self):
|
|
return f"<Order(id={self.id}, symbol='{self.symbol}', status='{self.status}')>"
|
|
|
|
class Trade(Base):
|
|
__tablename__ = 'trades'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
order_id = Column(Integer, nullable=False) # FK to Order table
|
|
symbol = Column(String, nullable=False)
|
|
quantity = Column(Integer, nullable=False)
|
|
price = Column(Float, nullable=False)
|
|
transaction_type = Column(Enum(TransactionType), nullable=False)
|
|
trade_time = Column(DateTime, default=func.now())
|
|
|
|
def __repr__(self):
|
|
return f"<Trade(id={self.id}, symbol='{self.symbol}', quantity={self.quantity})>"
|
|
|
|
class Position(Base):
|
|
__tablename__ = 'positions'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
symbol = Column(String, unique=True, nullable=False)
|
|
quantity = Column(Integer, nullable=False)
|
|
average_price = Column(Float, nullable=False)
|
|
last_updated = Column(DateTime, default=func.now(), onupdate=func.now())
|
|
|
|
def __repr__(self):
|
|
return f"<Position(symbol='{self.symbol}', quantity={self.quantity})>" |