diff options
| author | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2026-04-01 17:25:54 +0900 |
|---|---|---|
| committer | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2026-04-01 17:25:54 +0900 |
| commit | e10d4a96e062818cb2395add1746c733a053c374 (patch) | |
| tree | bbd222cd840d2c7fc58d3109362fb1b0c00aee26 /services/api/src/trading_api/routers | |
| parent | 21c6b777530b4a027aec9c12bf63092e5a7c006d (diff) | |
feat: add FastAPI REST API service
Diffstat (limited to 'services/api/src/trading_api/routers')
| -rw-r--r-- | services/api/src/trading_api/routers/__init__.py | 0 | ||||
| -rw-r--r-- | services/api/src/trading_api/routers/orders.py | 54 | ||||
| -rw-r--r-- | services/api/src/trading_api/routers/portfolio.py | 41 | ||||
| -rw-r--r-- | services/api/src/trading_api/routers/strategies.py | 28 |
4 files changed, 123 insertions, 0 deletions
diff --git a/services/api/src/trading_api/routers/__init__.py b/services/api/src/trading_api/routers/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/services/api/src/trading_api/routers/__init__.py diff --git a/services/api/src/trading_api/routers/orders.py b/services/api/src/trading_api/routers/orders.py new file mode 100644 index 0000000..989694f --- /dev/null +++ b/services/api/src/trading_api/routers/orders.py @@ -0,0 +1,54 @@ +"""Order endpoints.""" +from fastapi import APIRouter, Request +from shared.sa_models import OrderRow, SignalRow +from sqlalchemy import select + +router = APIRouter() + + +@router.get("/") +async def get_orders(request: Request, limit: int = 50): + """Get recent orders.""" + db = request.app.state.db + async with db.get_session() as session: + stmt = select(OrderRow).order_by(OrderRow.created_at.desc()).limit(limit) + result = await session.execute(stmt) + rows = result.scalars().all() + return [ + { + "id": r.id, + "signal_id": r.signal_id, + "symbol": r.symbol, + "side": r.side, + "type": r.type, + "price": float(r.price), + "quantity": float(r.quantity), + "status": r.status, + "created_at": r.created_at.isoformat() if r.created_at else None, + "filled_at": r.filled_at.isoformat() if r.filled_at else None, + } + for r in rows + ] + + +@router.get("/signals") +async def get_signals(request: Request, limit: int = 50): + """Get recent signals.""" + db = request.app.state.db + async with db.get_session() as session: + stmt = select(SignalRow).order_by(SignalRow.created_at.desc()).limit(limit) + result = await session.execute(stmt) + rows = result.scalars().all() + return [ + { + "id": r.id, + "strategy": r.strategy, + "symbol": r.symbol, + "side": r.side, + "price": float(r.price), + "quantity": float(r.quantity), + "reason": r.reason, + "created_at": r.created_at.isoformat() if r.created_at else None, + } + for r in rows + ] 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 + ] diff --git a/services/api/src/trading_api/routers/strategies.py b/services/api/src/trading_api/routers/strategies.py new file mode 100644 index 0000000..a8d778d --- /dev/null +++ b/services/api/src/trading_api/routers/strategies.py @@ -0,0 +1,28 @@ +"""Strategy endpoints.""" +import sys +from pathlib import Path + +from fastapi import APIRouter + +# Add strategy-engine to path for plugin loading +_STRATEGY_DIR = Path(__file__).resolve().parents[5] / "strategy-engine" +if str(_STRATEGY_DIR) not in sys.path: + sys.path.insert(0, str(_STRATEGY_DIR)) + +router = APIRouter() + + +@router.get("/") +async def list_strategies(): + """List available strategies.""" + from strategy_engine.plugin_loader import load_strategies + strategies_dir = _STRATEGY_DIR / "strategies" + strategies = load_strategies(strategies_dir) + return [ + { + "name": s.name, + "warmup_period": s.warmup_period, + "class": type(s).__name__, + } + for s in strategies + ] |
