Rebuild position history from Binance fills

Sync account fills into a new trading_fills table every 60s and
reconstruct closed positions from them, so positions opened outside
the program are included and external closes get an exit price.
Positions link to plans by order id. The history drawer gains symbol,
close-date, outcome and source filters, a summary bar and pagination.
This commit is contained in:
eezd committed 2026-10-16 11:57:45 +08:00
1 parent a2e51a3011
commit 010d89e0f5
10 files changed
+589 -140

No files matched your search

+19
View File
@@ -125,3 +125,22 @@ class TradingEvent(Base):
level: Mapped[str] = mapped_column(String(8), default="info")
message: Mapped[str] = mapped_column(Text)
data: Mapped[dict | None] = mapped_column(JSONB)
class TradingFill(Base):
"""币安成交记录(userTrades),历史仓位由它还原;包括程序外的手动成交。trade id 只在单个 symbol 内唯一"""
__tablename__ = "trading_fills"
__table_args__ = (Index("ix_trading_fills_symbol_time", "symbol", "time"),)
symbol: Mapped[str] = mapped_column(String(32), primary_key=True)
trade_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
order_id: Mapped[int] = mapped_column(BigInteger)
# BUY / SELL;账户为单向持仓,买为正、卖为负即可还原仓位
side: Mapped[str] = mapped_column(String(4))
price: Mapped[Decimal] = mapped_column(Numeric)
qty: Mapped[Decimal] = mapped_column(Numeric)
realized_pnl: Mapped[Decimal] = mapped_column(Numeric)
commission: Mapped[Decimal] = mapped_column(Numeric)
commission_asset: Mapped[str] = mapped_column(String(16))
time: Mapped[datetime] = mapped_column(DateTime(timezone=True))