1566b84379
Applies the backtest-validated trailing stop to live paper trading, and surfaces it transparently. Exit (A): - New paper-trade exit policy (paper_exit_mode=trailing, paper_trailing_pct=12), tunable in Admin → Paper-Trade Exit. resolve_open_trades runs a trailing stop (initial stop as floor, ratchets up from the peak; target ignored — the validated rule) and records close_reason (trailing|stop|target|manual; +migration 013). - list_trades enriches open trades with the live trailing-stop level + distance %. Open Trades panel shows the active tactic and a Trail Stop column. Alerts (B): - Daily digest now lists open trades with unrealized gain, trailing stop, and how far away it is. - New "trade closed" alert: one summary per auto-close (trailing/target/stop, not manual) — direction, reason, days held, P&L abs+%/R — covering wins AND stop-loss losses. Deduped by trade id; toggle in Admin alerts. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, Float, ForeignKey, String
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class PaperTrade(Base):
|
|
"""A simulated ('taken') trade for paper trading.
|
|
|
|
Captured from a setup at the moment the user marks it taken: direction,
|
|
entry, size, stop and target. Open trades are marked-to-market against the
|
|
latest close; closing records the exit price and time.
|
|
"""
|
|
|
|
__tablename__ = "paper_trades"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(
|
|
ForeignKey("users.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
ticker_id: Mapped[int] = mapped_column(
|
|
ForeignKey("tickers.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
direction: Mapped[str] = mapped_column(String(10), nullable=False)
|
|
entry_price: Mapped[float] = mapped_column(Float, nullable=False)
|
|
shares: Mapped[float] = mapped_column(Float, nullable=False)
|
|
stop_loss: Mapped[float] = mapped_column(Float, nullable=False)
|
|
target: Mapped[float] = mapped_column(Float, nullable=False)
|
|
status: Mapped[str] = mapped_column(String(10), nullable=False, default="open")
|
|
opened_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), default=datetime.utcnow, nullable=False
|
|
)
|
|
close_price: Mapped[float | None] = mapped_column(Float, nullable=True)
|
|
closed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
# How the trade was closed: "trailing" | "stop" | "target" | "manual".
|
|
close_reason: Mapped[str | None] = mapped_column(String(10), nullable=True)
|