Refactor: restructure market module with services, stores, and utils

This commit is contained in:
guaiwoluo2020
2026-03-19 09:47:41 +08:00
parent ef15b92342
commit b7ea37ad09
78 changed files with 10594 additions and 7645 deletions
+79 -19
View File
@@ -6,18 +6,18 @@
from fastapi import APIRouter, Request
from typing import Dict, Optional
import json
from market.position_store import get_position_store
from market.system_log import get_system_log
def create_position_routes() -> APIRouter:
def create_position_routes(trading_server=None) -> APIRouter:
"""
创建仓位管理路由
Args:
trading_server: TradingServer 实例
"""
router = APIRouter()
position_store = get_position_store()
@router.post("/ea/positions")
async def receive_positions(request: Request) -> Dict:
@@ -50,7 +50,8 @@ def create_position_routes() -> APIRouter:
if not symbol:
return {"status": "error", "message": "缺少品种信息"}
result = position_store.update_positions(symbol, positions)
# 使用新的持仓服务
result = trading_server.position_service.update_positions(symbol, positions)
# 记录日志
if positions:
@@ -79,7 +80,7 @@ def create_position_routes() -> APIRouter:
参数:
- symbol: 可选,指定品种;不提供则返回所有
"""
positions = position_store.get_positions(symbol)
positions = trading_server.position_service.get_positions(symbol)
return {
"status": "ok",
"count": len(positions),
@@ -94,7 +95,7 @@ def create_position_routes() -> APIRouter:
参数:
- symbol: 可选,指定品种;不提供则返回所有
"""
summary = position_store.get_summary(symbol)
summary = trading_server.position_service.get_summary(symbol)
return {
"status": "ok",
**summary
@@ -105,7 +106,7 @@ def create_position_routes() -> APIRouter:
"""
获取单个持仓详情
"""
position = position_store.get_position(symbol, ticket)
position = trading_server.position_service.get_position(symbol, ticket)
if not position:
return {"status": "error", "message": "持仓不存在"}
return {
@@ -115,16 +116,75 @@ def create_position_routes() -> APIRouter:
# ==================== 交易历史接口 ====================
@router.post("/ea/trade_history")
async def receive_trade_history(request: Request) -> Dict:
"""
EA推送交易历史数据
请求体:
```json
{
"deals": [
{
"ticket": 123456,
"order": 789012,
"symbol": "GOLD#",
"type": 0,
"entry": 0,
"volume": 0.1,
"price": 2050.50,
"profit": 0,
"swap": 0,
"commission": -5.0,
"time": "2026.03.16 15:30:00",
"comment": ""
}
]
}
```
"""
try:
data = await request.json()
deals = data.get('deals', [])
if not deals:
return {"status": "ok", "message": "无数据", "count": 0}
# 使用新的交易历史服务
new_count = trading_server.trade_history_service.process_deals(deals)
# 记录日志
system_log = get_system_log()
system_log.add_log(
"trade_history_update",
{
"deals_received": len(deals),
"deals_new": new_count,
"total_deals": len(trading_server.trade_history_store.get())
},
message=f"交易历史上报: 收到{len(deals)}条, 新增{new_count}"
)
return {
"status": "ok",
"message": "交易历史已更新",
"count": new_count
}
except Exception as e:
print(f"[PositionAPI] 接收交易历史异常: {e}")
return {"status": "error", "message": str(e)}
@router.get("/trade_history")
async def get_trade_history() -> Dict:
async def get_trade_history(symbol: Optional[str] = None) -> Dict:
"""
获取交易历史数据
"""
from market.trade_history_store import get_trade_history_store
store = get_trade_history_store()
deals = store.get_all_deals()
statistics = store.get_statistics()
参数:
- symbol: 可选,指定品种
"""
deals = trading_server.trade_history_service.get_deals(symbol)
statistics = trading_server.trade_history_service.get_statistics(symbol)
return {
"status": "ok",
@@ -133,14 +193,14 @@ def create_position_routes() -> APIRouter:
}
@router.get("/trade_history/statistics")
async def get_trade_history_statistics() -> Dict:
async def get_trade_history_statistics(symbol: Optional[str] = None) -> Dict:
"""
获取交易历史统计
"""
from market.trade_history_store import get_trade_history_store
store = get_trade_history_store()
statistics = store.get_statistics()
参数:
- symbol: 可选,指定品种
"""
statistics = trading_server.trade_history_service.get_statistics(symbol)
return {
"status": "ok",