Phase 4: Core Service (port 8010) - Pairing management (symbol-strategy pairs) - Order service with Redis pub/sub - Position and balance tracking Phase 5: Strategy Engine (port 8030) - BaseStrategy abstract class - Strategy registry and executor - Built-in strategies: Grid, MA Cross, Dual MA - Signal manager with Redis integration Phase 6: QMT Adapter (port 8040) - XtQuant SDK wrapper with simulation mode - Order executor listening to Redis channels - Quote pusher and account syncer Phase 7: Testing & Documentation - Pytest fixtures for all services - Unit tests for models and strategies - Windows startup scripts
121 lines
2.9 KiB
Python
121 lines
2.9 KiB
Python
"""
|
|
资金管理路由
|
|
|
|
资金查询与管理
|
|
"""
|
|
|
|
from typing import Any, Optional
|
|
|
|
from fastapi import APIRouter, Query, HTTPException
|
|
from pydantic import BaseModel
|
|
|
|
from src.core.logger import get_logger
|
|
from ..services.balance_service import balance_service
|
|
|
|
logger = get_logger("core_service.balance")
|
|
router = APIRouter()
|
|
|
|
|
|
class BalanceAdjust(BaseModel):
|
|
"""资金调整请求"""
|
|
|
|
amount: float
|
|
reason: str
|
|
|
|
|
|
@router.get("/{pairing_id}")
|
|
async def get_balance(pairing_id: str) -> dict[str, Any]:
|
|
"""
|
|
查询配对资金
|
|
|
|
Args:
|
|
pairing_id: 配对ID
|
|
"""
|
|
try:
|
|
result = await balance_service.get_balance(pairing_id)
|
|
|
|
if not result:
|
|
raise HTTPException(status_code=404, detail=f"配对不存在: {pairing_id}")
|
|
|
|
return {
|
|
"code": 0,
|
|
"message": "success",
|
|
"data": result,
|
|
}
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"查询资金失败: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.get("")
|
|
async def list_balances(
|
|
strategy_id: Optional[str] = Query(default=None, description="策略ID"),
|
|
) -> dict[str, Any]:
|
|
"""
|
|
查询资金列表
|
|
"""
|
|
try:
|
|
result = await balance_service.list_balances(strategy_id=strategy_id)
|
|
|
|
return {
|
|
"code": 0,
|
|
"message": "success",
|
|
"data": result,
|
|
}
|
|
except Exception as e:
|
|
logger.error(f"查询资金列表失败: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.get("/summary/all")
|
|
async def get_balance_summary() -> dict[str, Any]:
|
|
"""
|
|
获取资金汇总
|
|
"""
|
|
try:
|
|
result = await balance_service.get_summary()
|
|
|
|
return {
|
|
"code": 0,
|
|
"message": "success",
|
|
"data": result,
|
|
}
|
|
except Exception as e:
|
|
logger.error(f"获取资金汇总失败: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.post("/{pairing_id}/adjust")
|
|
async def adjust_balance(pairing_id: str, adjust: BalanceAdjust) -> dict[str, Any]:
|
|
"""
|
|
调整资金(增加或减少初始资金)
|
|
|
|
Args:
|
|
pairing_id: 配对ID
|
|
adjust: 调整请求
|
|
"""
|
|
try:
|
|
result = await balance_service.adjust_balance(
|
|
pairing_id=pairing_id,
|
|
amount=adjust.amount,
|
|
reason=adjust.reason,
|
|
)
|
|
|
|
if not result:
|
|
raise HTTPException(status_code=404, detail=f"配对不存在: {pairing_id}")
|
|
|
|
logger.info(f"调整资金: {pairing_id}, {adjust.amount}, {adjust.reason}")
|
|
|
|
return {
|
|
"code": 0,
|
|
"message": "success",
|
|
"data": result,
|
|
}
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"调整资金失败: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|