QTrade/src/qmt_adapter/routes/trade.py
cuizhan 085427959c feat: Phase 4-7 - Core Service, Strategy Engine, QMT Adapter, Tests
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
2025-12-09 00:36:27 +08:00

73 lines
1.6 KiB
Python

"""
交易相关路由
"""
from fastapi import APIRouter
from ..services.qmt_client import qmt_client
from ..services.order_executor import order_executor
router = APIRouter()
@router.get("/status")
async def trade_status() -> dict:
"""获取交易状态"""
return {
"connected": qmt_client.is_connected,
"executor_running": order_executor.is_running,
"pending_orders": len(order_executor.pending_orders),
}
@router.post("/order")
async def place_order(
code: str,
direction: str, # buy/sell
quantity: int,
price: float | None = None,
order_type: str = "limit", # limit/market
) -> dict:
"""手动下单(测试用)
Args:
code: 股票代码
direction: 买卖方向 buy/sell
quantity: 数量
price: 价格,市价单可不填
order_type: 订单类型 limit/market
"""
result = await qmt_client.place_order(
code=code,
direction=direction,
quantity=quantity,
price=price,
order_type=order_type,
)
return result
@router.post("/cancel/{order_id}")
async def cancel_order(order_id: str) -> dict:
"""撤单
Args:
order_id: 订单 ID
"""
result = await qmt_client.cancel_order(order_id)
return result
@router.get("/orders")
async def get_orders() -> dict:
"""获取当日委托"""
orders = await qmt_client.get_orders()
return {"count": len(orders), "orders": orders}
@router.get("/trades")
async def get_trades() -> dict:
"""获取当日成交"""
trades = await qmt_client.get_trades()
return {"count": len(trades), "trades": trades}