Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 72 additions & 10 deletions database/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
from sqlalchemy import (
TIMESTAMP,
Column,
ForeignKey,
Integer,
Numeric,
String,
Text,
func,
)
from sqlalchemy import TIMESTAMP, Column, ForeignKey, Integer, Numeric, String, Text, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship

Expand Down Expand Up @@ -353,3 +344,74 @@ class GatewayCLMMEvent(Base):
position = relationship("GatewayCLMMPosition", back_populates="events")


class ExecutorRecord(Base):
"""Database model for executor state persistence."""
__tablename__ = "executors"

id = Column(Integer, primary_key=True, index=True)

# Executor identification
executor_id = Column(String, nullable=False, unique=True, index=True)
executor_type = Column(String, nullable=False, index=True)

# Account and connector info
account_name = Column(String, nullable=False, index=True)
connector_name = Column(String, nullable=False, index=True)
trading_pair = Column(String, nullable=False, index=True)

# Timestamps
created_at = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False, index=True)
closed_at = Column(TIMESTAMP(timezone=True), nullable=True, index=True)

# Status
status = Column(String, nullable=False, default="RUNNING", index=True)
close_type = Column(String, nullable=True)

# Performance metrics
net_pnl_quote = Column(Numeric(precision=30, scale=18), nullable=False, default=0)
net_pnl_pct = Column(Numeric(precision=10, scale=6), nullable=False, default=0)
cum_fees_quote = Column(Numeric(precision=30, scale=18), nullable=False, default=0)
filled_amount_quote = Column(Numeric(precision=30, scale=18), nullable=False, default=0)

# Configuration (JSON)
config = Column(Text, nullable=True)

# Final state (JSON)
final_state = Column(Text, nullable=True)

# Relationships
orders = relationship("ExecutorOrder", back_populates="executor", cascade="all, delete-orphan")


class ExecutorOrder(Base):
"""Database model for orders created by executors."""
__tablename__ = "executor_orders"

id = Column(Integer, primary_key=True, index=True)

# Executor reference
executor_id = Column(String, ForeignKey("executors.executor_id"), nullable=False, index=True)

# Order identification
client_order_id = Column(String, nullable=False, index=True)
exchange_order_id = Column(String, nullable=True)

# Order details
order_type = Column(String, nullable=False) # open, close, take_profit, stop_loss
trade_type = Column(String, nullable=False) # BUY, SELL
amount = Column(Numeric(precision=30, scale=18), nullable=False)
price = Column(Numeric(precision=30, scale=18), nullable=True)

# Execution
status = Column(String, nullable=False, default="SUBMITTED")
filled_amount = Column(Numeric(precision=30, scale=18), nullable=False, default=0)
average_fill_price = Column(Numeric(precision=30, scale=18), nullable=True)

# Timestamps
created_at = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False)
updated_at = Column(TIMESTAMP(timezone=True), onupdate=func.now(), nullable=True)

# Relationship
executor = relationship("ExecutorRecord", back_populates="orders")


6 changes: 4 additions & 2 deletions database/repositories/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from .account_repository import AccountRepository
from .bot_run_repository import BotRunRepository
from .executor_repository import ExecutorRepository
from .funding_repository import FundingRepository
from .gateway_clmm_repository import GatewayCLMMRepository
from .gateway_swap_repository import GatewaySwapRepository
from .order_repository import OrderRepository
from .trade_repository import TradeRepository
from .gateway_swap_repository import GatewaySwapRepository
from .gateway_clmm_repository import GatewayCLMMRepository

__all__ = [
"AccountRepository",
"BotRunRepository",
"ExecutorRepository",
"FundingRepository",
"OrderRepository",
"TradeRepository",
Expand Down
Loading