| from datetime import datetime |
|
|
| from sqlalchemy import Column, DateTime, Float, Integer, String |
| from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine |
| from sqlalchemy.orm import DeclarativeBase, sessionmaker |
|
|
| from app.core.config import settings |
|
|
| async_engine = create_async_engine(settings.DATABASE_URL, echo=False) |
| async_session = sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False) |
|
|
|
|
| class Base(DeclarativeBase): |
| pass |
|
|
|
|
| class OrderModel(Base): |
| __tablename__ = "orders" |
|
|
| order_id = Column(String, primary_key=True) |
| symbol = Column(String, nullable=False, index=True) |
| side = Column(String, nullable=False) |
| order_type = Column(String, nullable=False) |
| quantity = Column(Integer, nullable=False) |
| filled_quantity = Column(Integer, default=0) |
| price = Column(Float, nullable=True) |
| avg_price = Column(Float, default=0.0) |
| status = Column(String, nullable=False) |
| strategy_id = Column(String, nullable=True) |
| created_at = Column(DateTime, default=datetime.utcnow) |
| updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) |
|
|
|
|
| class TradeModel(Base): |
| __tablename__ = "trades" |
|
|
| trade_id = Column(String, primary_key=True) |
| order_id = Column(String, nullable=False, index=True) |
| symbol = Column(String, nullable=False, index=True) |
| side = Column(String, nullable=False) |
| quantity = Column(Integer, nullable=False) |
| price = Column(Float, nullable=False) |
| pnl = Column(Float, default=0.0) |
| strategy_id = Column(String, nullable=True) |
| timestamp = Column(DateTime, default=datetime.utcnow) |
|
|
|
|
| class KlineModel(Base): |
| __tablename__ = "klines" |
|
|
| id = Column(Integer, primary_key=True, autoincrement=True) |
| symbol = Column(String, nullable=False, index=True) |
| interval = Column(String, nullable=False) |
| open = Column(Float, nullable=False) |
| high = Column(Float, nullable=False) |
| low = Column(Float, nullable=False) |
| close = Column(Float, nullable=False) |
| volume = Column(Integer, nullable=False) |
| timestamp = Column(DateTime, nullable=False) |
|
|
|
|
| async def init_db(): |
| async with async_engine.begin() as conn: |
| await conn.run_sync(Base.metadata.create_all) |
|
|
|
|
| async def get_session() -> AsyncSession: |
| async with async_session() as session: |
| yield session |
|
|