Created strategy_service module with complete architecture: - ConfigLoader: Load and validate strategy configurations - StrategyManager: Manage all strategy instances - PositionManager: Track positions and capital for each instance - RiskManager: Multi-level risk control (global/instance/signal) - TradeRecorder: Record all signals and trades - StrategyExecutor: Core coordinator for strategy execution Key features: - One symbol can have multiple independent strategy instances - Each instance has isolated capital and positions - No signal aggregation - each instance returns independent signals - Multi-level risk management - Configuration-driven strategy deployment - Clean separation: ws_server (communication) vs strategy_service (business) Files added: - src/strategy_service/*.py (6 core modules) - config/strategy_config.yaml (example configuration) - src/networks/server/ws_server_refactored.py (new server) - docs/backend_refactor.md (architecture documentation) Protocol changes: - Response now returns multiple signals (TRADE_SIGNALS) - Each signal includes instance_id for tracking - Added TRADE_RESULT message for trade feedback
247 lines
6.2 KiB
Markdown
247 lines
6.2 KiB
Markdown
# 后端架构重构说明
|
||
|
||
## 架构概览
|
||
|
||
新的后端架构将业务逻辑从通信层完全分离,采用分层设计:
|
||
|
||
```
|
||
┌─────────────────────────────────────┐
|
||
│ WebSocket Server (通信层) │
|
||
│ ws_server_refactored.py │
|
||
│ - 接收/发送WebSocket消息 │
|
||
│ - 协议解析 │
|
||
│ - 不包含业务逻辑 │
|
||
└────────────┬────────────────────────┘
|
||
│ 委托
|
||
↓
|
||
┌─────────────────────────────────────┐
|
||
│ Strategy Service (业务层) │
|
||
│ │
|
||
│ ├─ StrategyExecutor (执行器) │
|
||
│ ├─ StrategyManager (策略管理) │
|
||
│ ├─ PositionManager (持仓管理) │
|
||
│ ├─ RiskManager (风控管理) │
|
||
│ ├─ TradeRecorder (交易记录) │
|
||
│ └─ ConfigLoader (配置加载) │
|
||
└─────────────────────────────────────┘
|
||
```
|
||
|
||
## 核心特性
|
||
|
||
### 1. 一个股票多个策略
|
||
- 每个策略实例完全独立
|
||
- 独立的资金分配
|
||
- 独立的持仓管理
|
||
- 独立的风控参数
|
||
|
||
### 2. 多层级风控
|
||
- **全局风控**: 总资金、总仓位限制
|
||
- **实例风控**: 单个策略的资金、仓位、止损止盈
|
||
- **信号风控**: 单次交易的合法性检查
|
||
|
||
### 3. 无信号聚合
|
||
- 每个策略实例独立计算和返回信号
|
||
- 后端返回所有策略的信号列表
|
||
- 前端/回测引擎决定如何执行
|
||
|
||
## 目录结构
|
||
|
||
```
|
||
src/
|
||
├── strategy_service/ # 业务层模块
|
||
│ ├── __init__.py
|
||
│ ├── config_loader.py # 配置加载器
|
||
│ ├── strategy_manager.py # 策略管理器
|
||
│ ├── position_manager.py # 持仓管理器
|
||
│ ├── risk_manager.py # 风控管理器
|
||
│ ├── trade_recorder.py # 交易记录器
|
||
│ └── strategy_executor.py # 策略执行器(核心)
|
||
│
|
||
├── networks/server/
|
||
│ ├── ws_server.py # 旧版本(保留)
|
||
│ └── ws_server_refactored.py # 新版本(重构后)
|
||
│
|
||
└── config/
|
||
└── strategy_config.yaml # 策略配置文件
|
||
|
||
strategies/ # 策略实现目录
|
||
├── ma_strategy.py
|
||
├── macd_strategy.py
|
||
└── ...
|
||
```
|
||
|
||
## 配置文件
|
||
|
||
```yaml
|
||
global:
|
||
total_capital: 1000000
|
||
max_position_ratio: 0.8
|
||
|
||
strategy_instances:
|
||
- id: "ma_short_000001" # 唯一ID
|
||
symbol: "000001.SZ" # 股票代码
|
||
strategy_class: "MAStrategy" # 策略类名
|
||
capital: 100000 # 分配资金
|
||
params: # 策略参数
|
||
short_period: 5
|
||
long_period: 20
|
||
risk: # 风控参数
|
||
max_position_ratio: 0.8
|
||
stop_loss: 0.05
|
||
```
|
||
|
||
## 核心模块
|
||
|
||
### ConfigLoader
|
||
- 加载并验证配置文件
|
||
- 提供配置查询接口
|
||
- 支持热更新
|
||
|
||
### StrategyManager
|
||
- 创建和管理所有策略实例
|
||
- 动态加载策略类
|
||
- 提供策略查询接口
|
||
|
||
### PositionManager
|
||
- 管理每个策略实例的持仓
|
||
- 管理每个策略实例的资金
|
||
- 计算盈亏
|
||
- 策略间完全隔离
|
||
|
||
### RiskManager
|
||
- 全局风控检查
|
||
- 策略实例风控检查
|
||
- 信号合法性检查
|
||
|
||
### TradeRecorder
|
||
- 记录所有信号
|
||
- 记录所有成交
|
||
- 提供历史查询
|
||
- 计算策略绩效
|
||
|
||
### StrategyExecutor
|
||
- 核心协调器
|
||
- 接收K线数据
|
||
- 调用策略计算
|
||
- 执行风控检查
|
||
- 记录信号
|
||
- 返回结果
|
||
|
||
## 数据流
|
||
|
||
```
|
||
1. K线数据进入
|
||
↓
|
||
2. WebSocket Server 接收并解析
|
||
↓
|
||
3. StrategyExecutor 处理
|
||
├─ 获取该股票的所有策略实例
|
||
├─ 并行调用每个策略计算
|
||
├─ 对每个信号进行风控检查
|
||
└─ 记录信号
|
||
↓
|
||
4. 返回所有通过风控的信号
|
||
↓
|
||
5. 前端/回测引擎执行交易
|
||
↓
|
||
6. 成交结果返回
|
||
↓
|
||
7. StrategyExecutor 更新持仓和资金
|
||
```
|
||
|
||
## 协议更新
|
||
|
||
### 请求: KLINE_DATA
|
||
```json
|
||
{
|
||
"header": {...},
|
||
"msg_type": "KLINE_DATA",
|
||
"symbol": "000001.SZ",
|
||
"data": {...}
|
||
}
|
||
```
|
||
|
||
### 响应: TRADE_SIGNALS (复数)
|
||
```json
|
||
{
|
||
"header": {...},
|
||
"msg_type": "TRADE_SIGNALS",
|
||
"signals": [
|
||
{
|
||
"instance_id": "ma_short_000001",
|
||
"symbol": "000001.SZ",
|
||
"action": "BUY",
|
||
"price": 10.5,
|
||
"quantity": 1000,
|
||
"message": "Golden cross"
|
||
},
|
||
{
|
||
"instance_id": "macd_000001",
|
||
"symbol": "000001.SZ",
|
||
"action": "HOLD",
|
||
"price": 10.5,
|
||
"quantity": 0
|
||
}
|
||
]
|
||
}
|
||
```
|
||
|
||
### 请求: TRADE_RESULT (成交反馈)
|
||
```json
|
||
{
|
||
"header": {...},
|
||
"msg_type": "TRADE_RESULT",
|
||
"instance_id": "ma_short_000001",
|
||
"symbol": "000001.SZ",
|
||
"action": "BUY",
|
||
"price": 10.5,
|
||
"quantity": 1000,
|
||
"trade_id": "...",
|
||
"signal_id": "..."
|
||
}
|
||
```
|
||
|
||
## 使用方法
|
||
|
||
### 启动服务器
|
||
```python
|
||
from src.networks.server.ws_server_refactored import start_server
|
||
|
||
start_server(
|
||
host="0.0.0.0",
|
||
port=8000,
|
||
config_path="config/strategy_config.yaml"
|
||
)
|
||
```
|
||
|
||
### 添加新策略
|
||
1. 在 `strategies/` 目录创建策略类
|
||
2. 在配置文件中添加策略实例
|
||
3. 重启服务器
|
||
|
||
### 热更新配置
|
||
```python
|
||
# ConfigLoader 支持 reload() 方法
|
||
# 未来可以添加配置文件监听
|
||
```
|
||
|
||
## 优势
|
||
|
||
1. **清晰的职责分离**: 通信层和业务层完全解耦
|
||
2. **高度模块化**: 每个模块职责单一,易于测试和维护
|
||
3. **灵活的配置**: 通过配置文件管理所有策略实例
|
||
4. **策略隔离**: 每个策略实例资金和持仓完全隔离
|
||
5. **多层风控**: 全局、实例、信号三层风控保护
|
||
6. **易于扩展**: 添加新策略无需修改核心代码
|
||
7. **完整记录**: 所有信号和成交都有详细记录
|
||
|
||
## 后续工作
|
||
|
||
- [ ] 实现具体的策略类 (MAStrategy, MACDStrategy等)
|
||
- [ ] 添加策略基类定义标准接口
|
||
- [ ] 实现配置文件热更新
|
||
- [ ] 添加数据库持久化(使用 TradeRecorder)
|
||
- [ ] 添加性能监控和日志
|
||
- [ ] 编写单元测试
|
||
- [ ] 添加WebSocket断线重连机制
|