feat: trailing-stop auto-exit for paper trades + close/digest alerts
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>
This commit is contained in:
@@ -3,10 +3,15 @@
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.dependencies import get_db, require_access
|
||||
from app.dependencies import get_db, require_access, require_admin
|
||||
from app.models.user import User
|
||||
from app.schemas.common import APIEnvelope
|
||||
from app.schemas.paper_trade import PaperTradeClose, PaperTradeCreate, PaperTradeResponse
|
||||
from app.schemas.paper_trade import (
|
||||
ExitPolicyUpdate,
|
||||
PaperTradeClose,
|
||||
PaperTradeCreate,
|
||||
PaperTradeResponse,
|
||||
)
|
||||
from app.services import paper_trade_service
|
||||
|
||||
router = APIRouter(tags=["paper-trades"])
|
||||
@@ -40,6 +45,26 @@ async def list_paper_trades(
|
||||
return APIEnvelope(status="success", data=data)
|
||||
|
||||
|
||||
@router.get("/paper-trades/exit-policy", response_model=APIEnvelope)
|
||||
async def read_exit_policy(
|
||||
_user: User = Depends(require_access),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> APIEnvelope:
|
||||
"""The active auto-exit policy for open paper trades (shown in the UI)."""
|
||||
return APIEnvelope(status="success", data=await paper_trade_service.get_exit_policy(db))
|
||||
|
||||
|
||||
@router.put("/paper-trades/exit-policy", response_model=APIEnvelope)
|
||||
async def write_exit_policy(
|
||||
body: ExitPolicyUpdate,
|
||||
_user: User = Depends(require_admin),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
) -> APIEnvelope:
|
||||
"""Change the auto-exit policy (admin)."""
|
||||
data = await paper_trade_service.set_exit_policy(db, mode=body.mode, trailing_pct=body.trailing_pct)
|
||||
return APIEnvelope(status="success", data=data)
|
||||
|
||||
|
||||
@router.post("/paper-trades", response_model=APIEnvelope, status_code=201)
|
||||
async def create_paper_trade(
|
||||
body: PaperTradeCreate,
|
||||
|
||||
Reference in New Issue
Block a user