Files
signal-platform/app/routers/paper_trades.py
T
dennisthiessen a69557f5d8
Deploy / lint (push) Successful in 5s
Deploy / test (push) Successful in 35s
Deploy / deploy (push) Successful in 24s
add paper trading: mark a setup as taken, track open P&L, sell
New paper_trades table (migration 007) + service/router. "Mark as taken" on each
setup card (shares prefilled from position sizing, entry from current price, both
editable) records a simulated trade. Overview gains an Open Trades table that
marks each position to the latest close — P&L in $, %, and R-multiples — with a
total unrealized P&L footer and a Sell button to close at the current price.
Closed trades are retained for future realized-P&L reporting.

Deploy: alembic upgrade (new paper_trades table).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 06:33:56 +02:00

70 lines
2.4 KiB
Python

"""Paper trades router — take, list, and close simulated trades."""
from fastapi import APIRouter, Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession
from app.dependencies import get_db, require_access
from app.models.user import User
from app.schemas.common import APIEnvelope
from app.schemas.paper_trade import PaperTradeClose, PaperTradeCreate, PaperTradeResponse
from app.services import paper_trade_service
router = APIRouter(tags=["paper-trades"])
def _resp(trade, symbol: str, current_price=None) -> dict:
return PaperTradeResponse(
id=trade.id,
symbol=symbol,
direction=trade.direction,
entry_price=trade.entry_price,
shares=trade.shares,
stop_loss=trade.stop_loss,
target=trade.target,
status=trade.status,
opened_at=trade.opened_at,
close_price=trade.close_price,
closed_at=trade.closed_at,
current_price=current_price,
).model_dump(mode="json")
@router.get("/paper-trades", response_model=APIEnvelope)
async def list_paper_trades(
status: str | None = Query(default=None, pattern=r"^(open|closed)$"),
user: User = Depends(require_access),
db: AsyncSession = Depends(get_db),
) -> APIEnvelope:
rows = await paper_trade_service.list_trades(db, user.id, status=status)
data = [PaperTradeResponse(**r).model_dump(mode="json") for r in rows]
return APIEnvelope(status="success", data=data)
@router.post("/paper-trades", response_model=APIEnvelope, status_code=201)
async def create_paper_trade(
body: PaperTradeCreate,
user: User = Depends(require_access),
db: AsyncSession = Depends(get_db),
) -> APIEnvelope:
trade = await paper_trade_service.create_trade(
db, user.id,
symbol=body.symbol,
direction=body.direction,
entry_price=body.entry_price,
shares=body.shares,
stop_loss=body.stop_loss,
target=body.target,
)
return APIEnvelope(status="success", data=_resp(trade, body.symbol.strip().upper()))
@router.post("/paper-trades/{trade_id}/close", response_model=APIEnvelope)
async def close_paper_trade(
trade_id: int,
body: PaperTradeClose,
user: User = Depends(require_access),
db: AsyncSession = Depends(get_db),
) -> APIEnvelope:
trade = await paper_trade_service.close_trade(db, user.id, trade_id, body.close_price)
return APIEnvelope(status="success", data={"id": trade.id, "status": trade.status})