summaryrefslogtreecommitdiff
path: root/services/api/src/trading_api/routers/orders.py
diff options
context:
space:
mode:
Diffstat (limited to 'services/api/src/trading_api/routers/orders.py')
-rw-r--r--services/api/src/trading_api/routers/orders.py90
1 files changed, 51 insertions, 39 deletions
diff --git a/services/api/src/trading_api/routers/orders.py b/services/api/src/trading_api/routers/orders.py
index d0b9fa6..c69dc10 100644
--- a/services/api/src/trading_api/routers/orders.py
+++ b/services/api/src/trading_api/routers/orders.py
@@ -1,55 +1,67 @@
"""Order endpoints."""
-from fastapi import APIRouter, Request
+import logging
+
+from fastapi import APIRouter, HTTPException, Request
from shared.sa_models import OrderRow, SignalRow
from sqlalchemy import select
+logger = logging.getLogger(__name__)
+
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
- ]
+ try:
+ 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
+ ]
+ except Exception as exc:
+ logger.error("Failed to get orders: %s", exc)
+ raise HTTPException(status_code=500, detail="Failed to retrieve orders")
@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
- ]
+ try:
+ 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
+ ]
+ except Exception as exc:
+ logger.error("Failed to get signals: %s", exc)
+ raise HTTPException(status_code=500, detail="Failed to retrieve signals")