from __future__ import annotations from fastapi import APIRouter, Depends, HTTPException from pydantic import BaseModel from sqlalchemy.orm import Session from app.core.database import get_db from app.models.stock import Trade from app.services.trading import trading_service router = APIRouter(prefix="/api/trading", tags=["trading"]) class OrderRequest(BaseModel): stock_code: str side: str # buy / sell qty: int price: int = 0 order_type: str = "00" class CancelRequest(BaseModel): order_no: str stock_code: str qty: int @router.post("/order") async def place_order(data: OrderRequest, db: Session = Depends(get_db)) -> dict: result = await trading_service.place_order( stock_code=data.stock_code, side=data.side, qty=data.qty, price=data.price, order_type=data.order_type, ) trade = Trade( order_no=result.get("order_no", ""), stock_code=data.stock_code, stock_name="", side=data.side, qty=data.qty, price=data.price, order_type=data.order_type, status="filled" if result.get("rt_cd") == "0" else "rejected", ) db.add(trade) db.commit() return result @router.post("/cancel") async def cancel_order(data: CancelRequest) -> dict: result = await trading_service.cancel_order( order_no=data.order_no, stock_code=data.stock_code, qty=data.qty, ) return result @router.get("/history") def get_trade_history( stock_code: str | None = None, limit: int = 50, db: Session = Depends(get_db), ) -> list[dict]: query = db.query(Trade).order_by(Trade.created_at.desc()) if stock_code: query = query.filter(Trade.stock_code == stock_code) trades = query.limit(limit).all() return [ { "id": t.id, "order_no": t.order_no, "stock_code": t.stock_code, "stock_name": t.stock_name, "side": t.side, "qty": t.qty, "price": t.price, "order_type": t.order_type, "status": t.status, "strategy_id": t.strategy_id, "created_at": t.created_at.isoformat() if t.created_at else None, "filled_at": t.filled_at.isoformat() if t.filled_at else None, } for t in trades ] @router.get("/account") async def get_account() -> dict: from app.services.account import account_service return await account_service.get_balance()