From e10d4a96e062818cb2395add1746c733a053c374 Mon Sep 17 00:00:00 2001 From: TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> Date: Wed, 1 Apr 2026 17:25:54 +0900 Subject: feat: add FastAPI REST API service --- services/api/src/trading_api/routers/portfolio.py | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 services/api/src/trading_api/routers/portfolio.py (limited to 'services/api/src/trading_api/routers/portfolio.py') diff --git a/services/api/src/trading_api/routers/portfolio.py b/services/api/src/trading_api/routers/portfolio.py new file mode 100644 index 0000000..f4169cb --- /dev/null +++ b/services/api/src/trading_api/routers/portfolio.py @@ -0,0 +1,41 @@ +"""Portfolio endpoints.""" +from fastapi import APIRouter, Request +from shared.sa_models import PositionRow, PortfolioSnapshotRow +from sqlalchemy import select + +router = APIRouter() + + +@router.get("/positions") +async def get_positions(request: Request): + """Get all current positions.""" + db = request.app.state.db + async with db.get_session() as session: + result = await session.execute(select(PositionRow)) + rows = result.scalars().all() + return [ + { + "symbol": r.symbol, + "quantity": float(r.quantity), + "avg_entry_price": float(r.avg_entry_price), + "current_price": float(r.current_price), + "unrealized_pnl": float(r.quantity * (r.current_price - r.avg_entry_price)), + } + for r in rows + ] + + +@router.get("/snapshots") +async def get_snapshots(request: Request, days: int = 30): + """Get portfolio snapshots for the last N days.""" + db = request.app.state.db + snapshots = await db.get_portfolio_snapshots(days=days) + return [ + { + "total_value": float(s["total_value"]), + "realized_pnl": float(s["realized_pnl"]), + "unrealized_pnl": float(s["unrealized_pnl"]), + "snapshot_at": s["snapshot_at"].isoformat(), + } + for s in snapshots + ] -- cgit v1.2.3