Files
backtrader/全市场回测说明.md
2026-01-17 21:21:30 +08:00

200 lines
5.5 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 全市场股票池回测说明
## 📊 功能概述
系统已支持**全市场股票池回测**可从本地数据目录或Tushare自动获取全市场股票代码并根据配置条件筛选。
---
## ⚙️ 配置方式
### 1. 启用全市场模式
`config.py` 中设置 `STOCK_POOL = None`
```python
# ========== 股票池配置 ==========
# 设置为 None 则使用全市场股票池(根据 STOCK_FILTER_CONFIG 筛选)
# 设置为列表则使用指定股票
STOCK_POOL = None # 全市场模式,根据筛选条件自动获取
# STOCK_POOL = ['002402.SZ','600362.SH','002863.SZ','300086.SZ'] # 自定义股票池(取消注释以启用)
```
### 2. 配置筛选条件
`config.py` 中配置 `STOCK_FILTER_CONFIG`
```python
STOCK_FILTER_CONFIG = {
'include_st': False, # 是否包含ST/*ST股票
'include_bse': False, # 是否包含北交所股票代8开头
'include_sse_star': False, # 是否包含科创板股票代码688开头
'include_gem': True, # 是否包含创业板股票代码3开头
'include_main': True, # 是否包含主板股票代码0/6开头
'custom_stocks': STOCK_POOL # 自定义股票列表,优先级最高,设置 STOCK_POOL 为 None 即启用全市场筛选
}
```
---
## 🔍 筛选规则说明
### 股票代码规则
| 市场板块 | 代码规则 | 示例 | 配置项 |
|---------|---------|------|--------|
| 主板沪市 | 6开头 | 600000.SH | `include_main` |
| 主板深市 | 0开头 | 000001.SZ | `include_main` |
| 创业板 | 3开头 | 300001.SZ | `include_gem` |
| 科创板 | 688开头 | 688001.SH | `include_sse_star` |
| 北交所 | 8开头 | 830001.BJ | `include_bse` |
| ST股票 | 名称包含ST | ST大地 | `include_st` |
### 常用筛选组合
**1⃣ 全市场排除ST和高风险板块**
```python
STOCK_FILTER_CONFIG = {
'include_st': False,
'include_bse': False,
'include_sse_star': False,
'include_gem': True,
'include_main': True,
'custom_stocks': None
}
```
**2⃣ 仅主板**
```python
STOCK_FILTER_CONFIG = {
'include_st': False,
'include_bse': False,
'include_sse_star': False,
'include_gem': False, # 关闭创业板
'include_main': True,
'custom_stocks': None
}
```
**3⃣ 全市场(包含所有板块)**
```python
STOCK_FILTER_CONFIG = {
'include_st': True,
'include_bse': True,
'include_sse_star': True,
'include_gem': True,
'include_main': True,
'custom_stocks': None
}
```
---
## 📂 数据来源
### 本地模式(推荐)
系统会自动扫描 `DATA_DIR` 目录下的所有股票文件:
- **文件格式**: `{股票代码}_daily_data.txt`
- **示例**: `000002.SZ_daily_data.txt`, `600000.SH_daily_data.txt`
- **优点**: 速度快无API限制
### Tushare在线模式
系统会调用 `pro.stock_basic()` 获取全市场股票列表:
```python
LOCAL_HQ = False # 切换到在线模式
TUSHARE_TOKEN = '你的token'
```
---
## 🚀 运行流程
1. **筛选股票池**: 根据 `STOCK_FILTER_CONFIG` 从全市场筛选
2. **加载数据**: 批量加载所有筛选股票的历史数据
3. **统一回测**: 使用统一资金池,按信号强度选股
4. **结果输出**: 生成图表、交易记录CSV
### 示例输出
```
[2/4] 筛选股票池...
全市场模式: 筛选到 2289 只股票
筛选条件: ST=False, 北交所=False, 科创板=False, 创业板=True, 主板=True
[3/4] 获取股票数据...
开始加载 2289 只股票的数据...
进度: 100/2289 (4.4%)
进度: 200/2289 (8.7%)
...
数据加载完成: 成功 2288 只, 失败 1 只
```
---
## ⚠️ 注意事项
1. **性能**:
- 全市场回测需要较长时间2000+股票约5-10分钟
- 建议先用小股票池测试策略,确认无误后再全市场回测
2. **内存**:
- 2000+股票数据会占用约1-2GB内存
- 确保系统内存充足
3. **数据完整性**:
- 确保本地数据文件完整从20230101到当前日期
- 缺失数据的股票会被自动跳过
4. **回测逻辑**:
- 所有股票共享一个资金池如10万
- 每天从全市场选择最强信号买入
- 最多持仓 `max_positions`配置为2或5
---
## 📝 切换模式
### 从全市场切换到自定义股票池
取消注释并修改:
```python
# STOCK_POOL = None # 注释掉这行
STOCK_POOL = ['002402.SZ', '600362.SH', '002863.SZ', '300086.SZ'] # 启用这行
```
### 从自定义切换到全市场
```python
STOCK_POOL = None # 启用这行
# STOCK_POOL = ['002402.SZ', '600362.SH', '002863.SZ', '300086.SZ'] # 注释掉这行
```
---
## ✅ 验证测试
运行以下命令验证配置:
```bash
# 查看当前筛选到多少只股票
python -c "from config import Config; from data.local_data import LocalDataFetcher; from data.stock_filter import StockFilter; config = Config(); fetcher = LocalDataFetcher(config.DATA_DIR); filter = StockFilter(fetcher); stocks = filter.get_stock_pool(config.STOCK_FILTER_CONFIG); print(f'筛选到 {len(stocks)} 只股票')"
```
---
## 🎯 最佳实践
1. **开发阶段**: 使用自定义股票池4-10只快速迭代策略
2. **测试阶段**: 使用100-200只股票验证策略稳定性
3. **生产阶段**: 使用全市场回测,获取完整绩效数据
---
**更新时间**: 2025-12-11
**版本**: v1.0