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>
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
"""Schemas for paper trades."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
class PaperTradeCreate(BaseModel):
|
|
symbol: str = Field(..., min_length=1, max_length=10)
|
|
direction: str = Field(..., pattern=r"^(long|short)$")
|
|
entry_price: float = Field(..., gt=0)
|
|
shares: float = Field(..., gt=0)
|
|
stop_loss: float = Field(..., gt=0)
|
|
target: float = Field(..., gt=0)
|
|
|
|
|
|
class PaperTradeClose(BaseModel):
|
|
close_price: float | None = Field(default=None, gt=0)
|
|
|
|
|
|
class ExitPolicyUpdate(BaseModel):
|
|
"""Auto-exit policy for open paper trades."""
|
|
mode: str | None = Field(default=None, pattern=r"^(trailing|target)$")
|
|
trailing_pct: float | None = Field(default=None, ge=0.5, le=90)
|
|
|
|
|
|
class PaperTradeResponse(BaseModel):
|
|
id: int
|
|
symbol: str
|
|
direction: str
|
|
entry_price: float
|
|
shares: float
|
|
stop_loss: float
|
|
target: float
|
|
status: str
|
|
opened_at: datetime
|
|
close_price: float | None = None
|
|
closed_at: datetime | None = None
|
|
current_price: float | None = None
|
|
# Alpha vs the S&P 500 (SPY) over the trade's holding period. None when the
|
|
# benchmark series doesn't cover the trade's open date yet.
|
|
benchmark_return_pct: float | None = None
|
|
alpha_pct: float | None = None
|
|
alpha_usd: float | None = None
|
|
close_reason: str | None = None
|
|
# Live trailing-stop level + how far price sits above it (% ), for open trades
|
|
# when the trailing exit policy is active.
|
|
trailing_stop: float | None = None
|
|
trailing_distance_pct: float | None = None
|