DB - account_strategy

abstract

Junction table binding MT5 accounts to trading strategies — controls which strategies are active on which accounts, with support for enabling/disabling individual bindings.

Table Info

PropertyValue
Table Nameaccount_strategy
SQLAlchemy Modelbackend/db/models.py :: AccountStrategy
Pydantic Schemabackend/api/routes/strategies.py
Migration Filealembic/versions/
TimescaleDB HypertableNo
Partition Column

Columns

ColumnSQLAlchemy TypeNullableDefaultDescription
idIntegerNoautoPrimary key
account_idInteger FKNoFK → accounts.id
strategy_idInteger FKNoFK → strategies.id
is_activeBooleanNoTrueWhether this binding is currently live
created_atDateTime(timezone=True)Nodatetime.now(UTC)Binding creation timestamp

Constraints & Indexes

NameTypeColumnsPurpose
pk_account_strategyPRIMARY KEYidRow uniqueness
uq_account_strategyUNIQUE(account_id, strategy_id)No duplicate bindings
fk_accountFOREIGN KEYaccount_id → accounts.idReferential integrity
fk_strategyFOREIGN KEYstrategy_id → strategies.idReferential integrity

Entity Relationships

SQLAlchemy Model (reference snapshot)

class AccountStrategy(Base):
    __tablename__ = "account_strategy"
 
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    account_id: Mapped[int] = mapped_column(ForeignKey("accounts.id"))
    strategy_id: Mapped[int] = mapped_column(ForeignKey("strategies.id"))
    is_active: Mapped[bool] = mapped_column(Boolean, default=True)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(UTC))
 
    __table_args__ = (UniqueConstraint("account_id", "strategy_id", name="uq_account_strategy"),)
 
    account: Mapped["Account"] = relationship("Account", back_populates="strategy_bindings")
    strategy: Mapped["Strategy"] = relationship("Strategy", back_populates="account_bindings")

Service Layer

LayerFilePurpose
Routerbackend/api/routes/strategies.pyBinding CRUD
Schedulerbackend/services/scheduler.pyadd_binding_jobs() / remove_binding_jobs() reads active bindings
RoleLink
API EndpointAPI-POST-v1-Strategies
Related TableDB - accounts
Related TableDB - strategies