diff --git a/backend/app/api/__init__.py b/backend/app/api/__init__.py index d1edb96..90d929b 100644 --- a/backend/app/api/__init__.py +++ b/backend/app/api/__init__.py @@ -2,5 +2,6 @@ from app.api.auth import router as auth_router from app.api.admin import router as admin_router from app.api.portfolio import router as portfolio_router from app.api.strategy import router as strategy_router +from app.api.market import router as market_router -__all__ = ["auth_router", "admin_router", "portfolio_router", "strategy_router"] +__all__ = ["auth_router", "admin_router", "portfolio_router", "strategy_router", "market_router"] diff --git a/backend/app/api/market.py b/backend/app/api/market.py new file mode 100644 index 0000000..89ea92e --- /dev/null +++ b/backend/app/api/market.py @@ -0,0 +1,112 @@ +""" +Market data API endpoints. +""" +from typing import List, Optional +from datetime import date, timedelta + +from fastapi import APIRouter, Depends, HTTPException, Query +from sqlalchemy.orm import Session + +from app.core.database import get_db +from app.api.deps import CurrentUser +from app.models.stock import Stock, Valuation, Price, Sector +from app.schemas.strategy import StockInfo, StockSearchResult, PriceData + +router = APIRouter(prefix="/api/market", tags=["market"]) + + +@router.get("/stocks/{ticker}", response_model=StockInfo) +async def get_stock( + ticker: str, + current_user: CurrentUser, + db: Session = Depends(get_db), +): + """Get stock information.""" + stock = db.query(Stock).filter(Stock.ticker == ticker).first() + if not stock: + raise HTTPException(status_code=404, detail="Stock not found") + + valuation = ( + db.query(Valuation) + .filter(Valuation.ticker == ticker) + .order_by(Valuation.base_date.desc()) + .first() + ) + + sector = db.query(Sector).filter(Sector.ticker == ticker).first() + + return StockInfo( + ticker=stock.ticker, + name=stock.name, + market=stock.market, + sector_name=sector.sector_name if sector else None, + stock_type=stock.stock_type.value if stock.stock_type else "common", + close_price=stock.close_price, + market_cap=int(stock.market_cap / 100_000_000) if stock.market_cap else None, + per=valuation.per if valuation else None, + pbr=valuation.pbr if valuation else None, + psr=valuation.psr if valuation else None, + pcr=valuation.pcr if valuation else None, + dividend_yield=valuation.dividend_yield if valuation else None, + eps=stock.eps, + bps=stock.bps, + base_date=stock.base_date, + ) + + +@router.get("/stocks/{ticker}/prices", response_model=List[PriceData]) +async def get_stock_prices( + ticker: str, + current_user: CurrentUser, + db: Session = Depends(get_db), + days: int = Query(default=365, ge=1, le=3650), +): + """Get historical prices for a stock.""" + start_date = date.today() - timedelta(days=days) + + prices = ( + db.query(Price) + .filter(Price.ticker == ticker) + .filter(Price.date >= start_date) + .order_by(Price.date.desc()) + .all() + ) + + return [ + PriceData( + date=p.date, + open=p.open, + high=p.high, + low=p.low, + close=p.close, + volume=p.volume, + ) + for p in prices + ] + + +@router.get("/search", response_model=List[StockSearchResult]) +async def search_stocks( + q: str = Query(..., min_length=1), + current_user: CurrentUser = None, + db: Session = Depends(get_db), + limit: int = Query(default=20, ge=1, le=100), +): + """Search stocks by ticker or name.""" + stocks = ( + db.query(Stock) + .filter( + (Stock.ticker.ilike(f"%{q}%")) | (Stock.name.ilike(f"%{q}%")) + ) + .limit(limit) + .all() + ) + + return [ + StockSearchResult( + ticker=s.ticker, + name=s.name, + market=s.market, + ) + for s in stocks + ] diff --git a/backend/app/main.py b/backend/app/main.py index 44fbc70..3be8cdf 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -4,7 +4,7 @@ Galaxy-PO Backend API from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from app.api import auth_router, admin_router, portfolio_router, strategy_router +from app.api import auth_router, admin_router, portfolio_router, strategy_router, market_router app = FastAPI( title="Galaxy-PO API", @@ -25,6 +25,7 @@ app.include_router(auth_router) app.include_router(admin_router) app.include_router(portfolio_router) app.include_router(strategy_router) +app.include_router(market_router) @app.get("/health")