summaryrefslogtreecommitdiff
path: root/shared/src
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src')
-rw-r--r--shared/src/shared/sa_models.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/shared/src/shared/sa_models.py b/shared/src/shared/sa_models.py
new file mode 100644
index 0000000..0537846
--- /dev/null
+++ b/shared/src/shared/sa_models.py
@@ -0,0 +1,93 @@
+"""SQLAlchemy 2.0 ORM models mirroring the existing asyncpg tables."""
+
+from datetime import datetime
+from decimal import Decimal
+
+from sqlalchemy import DateTime, ForeignKey, Numeric, Text
+from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
+
+
+class Base(DeclarativeBase):
+ pass
+
+
+class CandleRow(Base):
+ __tablename__ = "candles"
+
+ symbol: Mapped[str] = mapped_column(Text, primary_key=True)
+ timeframe: Mapped[str] = mapped_column(Text, primary_key=True)
+ open_time: Mapped[datetime] = mapped_column(DateTime(timezone=True), primary_key=True)
+ open: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ high: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ low: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ close: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ volume: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+
+
+class SignalRow(Base):
+ __tablename__ = "signals"
+
+ id: Mapped[str] = mapped_column(Text, primary_key=True)
+ strategy: Mapped[str] = mapped_column(Text, nullable=False)
+ symbol: Mapped[str] = mapped_column(Text, nullable=False)
+ side: Mapped[str] = mapped_column(Text, nullable=False)
+ price: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ quantity: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ reason: Mapped[str | None] = mapped_column(Text)
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
+
+
+class OrderRow(Base):
+ __tablename__ = "orders"
+
+ id: Mapped[str] = mapped_column(Text, primary_key=True)
+ signal_id: Mapped[str | None] = mapped_column(
+ Text, ForeignKey("signals.id")
+ )
+ symbol: Mapped[str] = mapped_column(Text, nullable=False)
+ side: Mapped[str] = mapped_column(Text, nullable=False)
+ type: Mapped[str] = mapped_column(Text, nullable=False)
+ price: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ quantity: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ status: Mapped[str] = mapped_column(
+ Text, nullable=False, server_default="PENDING"
+ )
+ created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
+ filled_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
+
+
+class TradeRow(Base):
+ __tablename__ = "trades"
+
+ id: Mapped[str] = mapped_column(Text, primary_key=True)
+ order_id: Mapped[str | None] = mapped_column(
+ Text, ForeignKey("orders.id")
+ )
+ symbol: Mapped[str] = mapped_column(Text, nullable=False)
+ side: Mapped[str] = mapped_column(Text, nullable=False)
+ price: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ quantity: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ fee: Mapped[Decimal] = mapped_column(
+ Numeric, nullable=False, server_default="0"
+ )
+ traded_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
+
+
+class PositionRow(Base):
+ __tablename__ = "positions"
+
+ symbol: Mapped[str] = mapped_column(Text, primary_key=True)
+ quantity: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ avg_entry_price: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ current_price: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
+
+
+class PortfolioSnapshotRow(Base):
+ __tablename__ = "portfolio_snapshots"
+
+ id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
+ total_value: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ realized_pnl: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ unrealized_pnl: Mapped[Decimal] = mapped_column(Numeric, nullable=False)
+ snapshot_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)