auto_trade_sys/backend/api/routes/stats.py
薇薇安 8a89592cb5 a
2026-01-13 17:30:59 +08:00

64 lines
1.8 KiB
Python

"""
统计分析API
"""
from fastapi import APIRouter, Query
import sys
from pathlib import Path
from datetime import datetime, timedelta
project_root = Path(__file__).parent.parent.parent.parent
sys.path.insert(0, str(project_root))
sys.path.insert(0, str(project_root / 'backend'))
from database.models import AccountSnapshot, Trade, MarketScan, TradingSignal
from fastapi import HTTPException
router = APIRouter()
@router.get("/performance")
async def get_performance_stats(days: int = Query(7, ge=1, le=365)):
"""获取性能统计"""
try:
# 账户快照
snapshots = AccountSnapshot.get_recent(days)
# 交易统计
start_date = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
trades = Trade.get_all(start_date=start_date)
return {
"snapshots": snapshots,
"trades": trades,
"period": f"Last {days} days"
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/dashboard")
async def get_dashboard_data():
"""获取仪表板数据"""
try:
# 最近的账户快照
snapshots = AccountSnapshot.get_recent(1)
latest_snapshot = snapshots[0] if snapshots else None
# 最近的交易
recent_trades = Trade.get_all(status='open')[:10]
# 最近的扫描记录
recent_scans = MarketScan.get_recent(10)
# 最近的信号
recent_signals = TradingSignal.get_recent(20)
return {
"account": latest_snapshot,
"open_trades": recent_trades,
"recent_scans": recent_scans,
"recent_signals": recent_signals
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))