Files

277 lines
10 KiB
Python
Raw Permalink Normal View History

2025-09-19 15:30:41 +02:00
from datetime import date, datetime
2025-09-12 15:05:01 +00:00
from enum import Enum
2025-09-25 12:08:07 +02:00
from typing import Optional
2025-09-12 15:05:01 +00:00
2025-09-14 21:01:12 +02:00
from sqlmodel import (
Column,
Date,
DateTime,
Field,
2025-09-25 12:08:07 +02:00
ForeignKey,
2025-09-14 21:01:12 +02:00
Integer,
Relationship,
SQLModel,
Text,
UniqueConstraint,
)
2025-09-12 15:05:01 +00:00
class TradeType(str, Enum):
SELL_PUT = "SELL_PUT"
CLOSE_SELL_PUT = "CLOSE_SELL_PUT"
2025-09-12 15:05:01 +00:00
ASSIGNMENT = "ASSIGNMENT"
SELL_CALL = "SELL_CALL"
CLOSE_SELL_CALL = "CLOSE_SELL_CALL"
2025-09-12 15:05:01 +00:00
EXERCISE_CALL = "EXERCISE_CALL"
2025-09-14 15:40:11 +02:00
LONG_SPOT = "LONG_SPOT"
CLOSE_LONG_SPOT = "CLOSE_LONG_SPOT"
SHORT_SPOT = "SHORT_SPOT"
CLOSE_SHORT_SPOT = "CLOSE_SHORT_SPOT"
LONG_CFD = "LONG_CFD"
CLOSE_LONG_CFD = "CLOSE_LONG_CFD"
SHORT_CFD = "SHORT_CFD"
CLOSE_SHORT_CFD = "CLOSE_SHORT_CFD"
LONG_OTHER = "LONG_OTHER"
CLOSE_LONG_OTHER = "CLOSE_LONG_OTHER"
SHORT_OTHER = "SHORT_OTHER"
CLOSE_SHORT_OTHER = "CLOSE_SHORT_OTHER"
2025-09-12 15:05:01 +00:00
2025-09-13 18:46:16 +02:00
class TradeStrategy(str, Enum):
2025-09-15 20:30:32 +02:00
WHEEL = "WHEEL"
2025-09-13 18:46:16 +02:00
FX = "FX"
SPOT = "SPOT"
OTHER = "OTHER"
2025-09-12 15:05:01 +00:00
class CycleStatus(str, Enum):
OPEN = "OPEN"
CLOSED = "CLOSED"
2025-09-14 21:01:12 +02:00
class UnderlyingCurrency(str, Enum):
EUR = "EUR"
USD = "USD"
GBP = "GBP"
JPY = "JPY"
AUD = "AUD"
CAD = "CAD"
CHF = "CHF"
NZD = "NZD"
CNY = "CNY"
2025-09-12 15:05:01 +00:00
class FundingSource(str, Enum):
CASH = "CASH"
MARGIN = "MARGIN"
MIXED = "MIXED"
class Trades(SQLModel, table=True):
2025-09-23 17:37:14 +02:00
__tablename__ = "trades" # type: ignore[attr-defined]
2025-10-03 11:55:30 +02:00
__table_args__ = (
UniqueConstraint(
"user_id", "friendly_name", name="uq_trades_user_friendly_name"
),
)
2025-09-14 15:40:11 +02:00
id: int | None = Field(default=None, primary_key=True)
user_id: int = Field(foreign_key="users.id", nullable=False, index=True)
# allow null while user may omit friendly_name; uniqueness enforced per-user by constraint
2025-10-03 11:55:30 +02:00
friendly_name: str | None = Field(
default=None, sa_column=Column(Text, nullable=True)
)
2025-09-14 15:40:11 +02:00
symbol: str = Field(sa_column=Column(Text, nullable=False))
2025-09-22 14:33:32 +02:00
exchange_id: int = Field(foreign_key="exchanges.id", nullable=False, index=True)
exchange: "Exchanges" = Relationship(back_populates="trades")
2025-10-03 11:55:30 +02:00
underlying_currency: UnderlyingCurrency = Field(
sa_column=Column(Text, nullable=False)
)
2025-09-14 15:40:11 +02:00
trade_type: TradeType = Field(sa_column=Column(Text, nullable=False))
trade_strategy: TradeStrategy = Field(sa_column=Column(Text, nullable=False))
2025-09-14 21:01:12 +02:00
trade_date: date = Field(sa_column=Column(Date, nullable=False))
2025-10-03 11:55:30 +02:00
trade_time_utc: datetime = Field(
sa_column=Column(DateTime(timezone=True), nullable=False)
)
2025-09-12 15:05:01 +00:00
expiry_date: date | None = Field(default=None, nullable=True)
strike_price_cents: int | None = Field(default=None, nullable=True)
2025-09-14 21:01:12 +02:00
quantity: int = Field(sa_column=Column(Integer, nullable=False))
2025-10-03 11:55:30 +02:00
quantity_multiplier: int = Field(
sa_column=Column(Integer, nullable=False), default=1
)
2025-09-14 21:01:12 +02:00
price_cents: int = Field(sa_column=Column(Integer, nullable=False))
gross_cash_flow_cents: int = Field(sa_column=Column(Integer, nullable=False))
commission_cents: int = Field(sa_column=Column(Integer, nullable=False))
net_cash_flow_cents: int = Field(sa_column=Column(Integer, nullable=False))
2025-09-17 16:36:56 +02:00
is_invalidated: bool = Field(default=False, nullable=False)
2025-10-03 11:55:30 +02:00
invalidated_at: datetime | None = Field(
default=None, sa_column=Column(DateTime(timezone=True), nullable=True)
)
replaced_by_trade_id: int | None = Field(
default=None, foreign_key="trades.id", nullable=True
)
2025-09-17 16:36:56 +02:00
notes: str | None = Field(default=None, sa_column=Column(Text, nullable=True))
2025-10-03 11:55:30 +02:00
cycle_id: int | None = Field(
default=None, foreign_key="cycles.id", nullable=True, index=True
)
2025-09-25 12:08:07 +02:00
2025-09-14 17:03:39 +02:00
cycle: "Cycles" = Relationship(back_populates="trades")
2025-09-12 15:05:01 +00:00
2025-09-25 12:08:07 +02:00
related_loan_change_event: Optional["CycleLoanChangeEvents"] = Relationship(
back_populates="trade",
sa_relationship_kwargs={"uselist": False},
)
2025-09-12 15:05:01 +00:00
class Cycles(SQLModel, table=True):
2025-09-23 17:37:14 +02:00
__tablename__ = "cycles" # type: ignore[attr-defined]
2025-10-03 11:55:30 +02:00
__table_args__ = (
UniqueConstraint(
"user_id", "friendly_name", name="uq_cycles_user_friendly_name"
),
)
2025-09-14 15:40:11 +02:00
id: int | None = Field(default=None, primary_key=True)
user_id: int = Field(foreign_key="users.id", nullable=False, index=True)
2025-10-03 11:55:30 +02:00
friendly_name: str | None = Field(
default=None, sa_column=Column(Text, nullable=True)
)
2025-09-14 15:40:11 +02:00
symbol: str = Field(sa_column=Column(Text, nullable=False))
2025-09-22 14:33:32 +02:00
exchange_id: int = Field(foreign_key="exchanges.id", nullable=False, index=True)
exchange: "Exchanges" = Relationship(back_populates="cycles")
2025-10-03 11:55:30 +02:00
underlying_currency: UnderlyingCurrency = Field(
sa_column=Column(Text, nullable=False)
)
2025-09-14 15:40:11 +02:00
status: CycleStatus = Field(sa_column=Column(Text, nullable=False))
2025-09-14 17:03:39 +02:00
funding_source: FundingSource = Field(sa_column=Column(Text, nullable=True))
capital_exposure_cents: int | None = Field(default=None, nullable=True)
2025-09-14 15:40:11 +02:00
start_date: date = Field(sa_column=Column(Date, nullable=False))
end_date: date | None = Field(default=None, sa_column=Column(Date, nullable=True))
2025-09-25 12:08:07 +02:00
2025-09-14 17:03:39 +02:00
trades: list["Trades"] = Relationship(back_populates="cycle")
2025-09-14 15:40:11 +02:00
loan_amount_cents: int | None = Field(default=None, nullable=True)
loan_interest_rate_tenth_bps: int | None = Field(default=None, nullable=True)
2025-10-03 11:55:30 +02:00
latest_interest_accrued_date: date | None = Field(
default=None, sa_column=Column(Date, nullable=True)
)
total_accrued_amount_cents: int = Field(
default=0, sa_column=Column(Integer, nullable=False)
)
2025-10-03 11:55:30 +02:00
loan_change_events: list["CycleLoanChangeEvents"] = Relationship(
back_populates="cycle"
)
2025-09-25 12:08:07 +02:00
daily_accruals: list["CycleDailyAccrual"] = Relationship(back_populates="cycle")
class CycleLoanChangeEvents(SQLModel, table=True):
__tablename__ = "cycle_loan_change_events" # type: ignore[attr-defined]
2025-10-03 11:55:30 +02:00
__table_args__ = (
UniqueConstraint(
"cycle_id", "effective_date", name="uq_cycle_loan_change_cycle_date"
),
)
2025-09-25 12:08:07 +02:00
id: int | None = Field(default=None, primary_key=True)
2025-10-03 11:55:30 +02:00
cycle_id: int = Field(
sa_column=Column(
Integer,
ForeignKey("cycles.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
)
2025-09-25 12:08:07 +02:00
effective_date: date = Field(sa_column=Column(Date, nullable=False))
2025-10-03 11:55:30 +02:00
loan_amount_cents: int | None = Field(
default=None, sa_column=Column(Integer, nullable=True)
)
loan_interest_rate_tenth_bps: int | None = Field(
default=None, sa_column=Column(Integer, nullable=True)
)
related_trade_id: int | None = Field(
default=None,
sa_column=Column(Integer, ForeignKey("trades.id"), nullable=True, unique=True),
) # Not used for now.
2025-09-25 12:08:07 +02:00
notes: str | None = Field(default=None, sa_column=Column(Text, nullable=True))
2025-10-03 11:55:30 +02:00
created_at: datetime = Field(
sa_column=Column(DateTime(timezone=True), nullable=False)
)
2025-09-25 12:08:07 +02:00
cycle: "Cycles" = Relationship(back_populates="loan_change_events")
trade: Optional["Trades"] = Relationship(back_populates="related_loan_change_event")
class CycleDailyAccrual(SQLModel, table=True):
__tablename__ = "cycle_daily_accrual" # type: ignore[attr-defined]
2025-10-03 11:55:30 +02:00
__table_args__ = (
UniqueConstraint(
"cycle_id", "accrual_date", name="uq_cycle_daily_accruals_cycle_date"
),
)
2025-09-25 12:08:07 +02:00
id: int | None = Field(default=None, primary_key=True)
2025-10-03 11:55:30 +02:00
cycle_id: int = Field(
sa_column=Column(
Integer,
ForeignKey("cycles.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
)
2025-09-25 12:08:07 +02:00
accrual_date: date = Field(sa_column=Column(Date, nullable=False))
accrual_amount_cents: int = Field(sa_column=Column(Integer, nullable=False))
2025-10-03 11:55:30 +02:00
created_at: datetime = Field(
sa_column=Column(DateTime(timezone=True), nullable=False)
)
2025-09-25 12:08:07 +02:00
cycle: "Cycles" = Relationship(back_populates="daily_accruals")
2025-09-14 15:40:11 +02:00
2025-09-22 14:33:32 +02:00
class Exchanges(SQLModel, table=True):
2025-09-23 17:37:14 +02:00
__tablename__ = "exchanges" # type: ignore[attr-defined]
2025-10-03 11:55:30 +02:00
__table_args__ = (
UniqueConstraint("user_id", "name", name="uq_exchanges_user_name"),
)
2025-09-22 14:33:32 +02:00
id: int | None = Field(default=None, primary_key=True)
user_id: int = Field(foreign_key="users.id", nullable=False, index=True)
name: str = Field(sa_column=Column(Text, nullable=False))
2025-09-22 14:33:32 +02:00
notes: str | None = Field(default=None, sa_column=Column(Text, nullable=True))
trades: list["Trades"] = Relationship(back_populates="exchange")
cycles: list["Cycles"] = Relationship(back_populates="exchange")
user: "Users" = Relationship(back_populates="exchanges")
2025-09-22 14:33:32 +02:00
2025-09-14 15:40:11 +02:00
class Users(SQLModel, table=True):
2025-09-23 17:37:14 +02:00
__tablename__ = "users" # type: ignore[attr-defined]
2025-09-14 15:40:11 +02:00
id: int | None = Field(default=None, primary_key=True)
# unique=True already creates an index; no need to also set index=True
username: str = Field(sa_column=Column(Text, nullable=False, unique=True))
password_hash: str = Field(sa_column=Column(Text, nullable=False))
is_active: bool = Field(default=True, nullable=False)
sessions: list["Sessions"] = Relationship(back_populates="user")
exchanges: list["Exchanges"] = Relationship(back_populates="user")
2025-09-19 14:06:32 +02:00
class Sessions(SQLModel, table=True):
2025-09-23 17:37:14 +02:00
__tablename__ = "sessions" # type: ignore[attr-defined]
2025-09-19 14:06:32 +02:00
id: int | None = Field(default=None, primary_key=True)
user_id: int = Field(foreign_key="users.id", nullable=False, index=True)
session_token_hash: str = Field(sa_column=Column(Text, nullable=False, unique=True))
2025-10-03 11:55:30 +02:00
created_at: datetime = Field(
sa_column=Column(DateTime(timezone=True), nullable=False)
)
expires_at: datetime = Field(
sa_column=Column(DateTime(timezone=True), nullable=False, index=True)
)
last_seen_at: datetime | None = Field(
sa_column=Column(DateTime(timezone=True), nullable=True)
)
last_used_ip: str | None = Field(
default=None, sa_column=Column(Text, nullable=True)
)
2025-09-19 14:06:32 +02:00
user_agent: str | None = Field(default=None, sa_column=Column(Text, nullable=True))
device_name: str | None = Field(default=None, sa_column=Column(Text, nullable=True))
user: "Users" = Relationship(back_populates="sessions")