132 lines
3.1 KiB
Python
132 lines
3.1 KiB
Python
"""Backtest API endpoints."""
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from uuid import UUID
|
|
|
|
from app.database import get_db
|
|
from app.schemas.backtest import (
|
|
BacktestConfig,
|
|
BacktestRunResponse,
|
|
BacktestListResponse
|
|
)
|
|
from app.services.backtest_service import BacktestService
|
|
from app.strategies import list_strategies
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/run", response_model=BacktestRunResponse, status_code=status.HTTP_201_CREATED)
|
|
async def run_backtest(
|
|
config: BacktestConfig,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
백테스트 실행.
|
|
|
|
Args:
|
|
config: 백테스트 설정
|
|
db: 데이터베이스 세션
|
|
|
|
Returns:
|
|
백테스트 실행 결과
|
|
"""
|
|
try:
|
|
backtest_run = BacktestService.run_backtest(config, db)
|
|
return backtest_run
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=str(e)
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"백테스트 실행 오류: {str(e)}"
|
|
)
|
|
|
|
|
|
@router.get("/{backtest_id}", response_model=BacktestRunResponse)
|
|
async def get_backtest(
|
|
backtest_id: UUID,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
백테스트 조회.
|
|
|
|
Args:
|
|
backtest_id: 백테스트 ID
|
|
db: 데이터베이스 세션
|
|
|
|
Returns:
|
|
백테스트 실행 결과
|
|
"""
|
|
backtest_run = BacktestService.get_backtest(backtest_id, db)
|
|
|
|
if not backtest_run:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="백테스트를 찾을 수 없습니다"
|
|
)
|
|
|
|
return backtest_run
|
|
|
|
|
|
@router.get("/", response_model=BacktestListResponse)
|
|
async def list_backtests(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
백테스트 목록 조회.
|
|
|
|
Args:
|
|
skip: 건너뛸 레코드 수
|
|
limit: 최대 레코드 수
|
|
db: 데이터베이스 세션
|
|
|
|
Returns:
|
|
백테스트 목록
|
|
"""
|
|
result = BacktestService.list_backtests(db, skip, limit)
|
|
return result
|
|
|
|
|
|
@router.delete("/{backtest_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_backtest(
|
|
backtest_id: UUID,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
백테스트 삭제.
|
|
|
|
Args:
|
|
backtest_id: 백테스트 ID
|
|
db: 데이터베이스 세션
|
|
"""
|
|
success = BacktestService.delete_backtest(backtest_id, db)
|
|
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="백테스트를 찾을 수 없습니다"
|
|
)
|
|
|
|
|
|
@router.get("/strategies/list")
|
|
async def get_strategies():
|
|
"""
|
|
사용 가능한 전략 목록 조회.
|
|
|
|
Returns:
|
|
전략 목록
|
|
"""
|
|
strategies = list_strategies()
|
|
return {
|
|
"strategies": [
|
|
{"name": name, "description": desc}
|
|
for name, desc in strategies.items()
|
|
]
|
|
}
|