Files
sanguo_quant_live/joinquant_articles/article_01.txt
T

109 lines
3.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
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.
标题: 高效使用聚宽回测平台的技巧
链接: https://www.joinquant.com/view/community/detail/1
分类: 回测
================================================================================
# 高效使用聚宽回测平台的技巧
## 一、回测参数设置策略
### 1.1 分阶段设置回测参数
**策略开发初期**
- 时间范围:1-2年历史数据
- 频率:日频数据
- 股票池:较小范围(如沪深300成分股)
- 目的:快速验证策略逻辑,缩短迭代周期
**策略验证阶段**
- 时间范围:3-5年历史数据
- 频率:分钟级数据(如需要)
- 股票池:较大范围(如全市场)
- 目的:全面验证策略表现和稳健性
**实战演练阶段**
- 时间范围:最近1年
- 频率:Tick级数据(高频策略)
- 股票池:目标交易标的
- 目的:模拟真实交易环境
## 二、数据获取优化
### 2.1 利用数据缓存机制
聚宽平台提供了多种数据缓存方式:
```python
# 示例:将常用数据缓存在全局变量中
from jqdata import *
def initialize(context):
# 初始化时预加载常用数据
g.stock_pool = get_index_stocks('000300.XSHG')
g.historical_data = get_price(g.stock_pool, count=250, end_date=context.previous_date)
```
### 2.2 批量获取数据
避免在循环中重复调用数据API
```python
# 低效方式
def handle_data(context, data):
for stock in context.portfolio.positions:
# 在循环中逐个获取数据
price = data[stock].close
# 高效方式
def before_trading_start(context, data):
# 批量获取所有需要的数据
stocks = list(context.portfolio.positions.keys())
g.prices = get_price(stocks, count=1, end_date=context.previous_date)
def handle_data(context, data):
# 直接使用已获取的数据
for stock in context.portfolio.positions:
price = g.prices[stock]['close'][0]
```
## 三、回测效率提升
### 3.1 使用历史回放功能
在策略逻辑验证阶段,可以使用历史回放功能:
- 快速定位问题时间点
- 查看策略在特定行情下的表现
- 避免每次修改都重新运行完整回测
### 3.2 合理使用history函数
```python
# 推荐用法
def before_trading_start(context, data):
# 一次性获取足够的历史数据
g.hist = history(60, '1d', 'close', g.stock_pool)
def handle_data(context, data):
# 使用已缓存的历史数据计算指标
for stock in g.stock_pool:
ma20 = g.hist[stock][-20:].mean()
ma60 = g.hist[stock].mean()
```
## 四、实践案例分享
某量化团队通过以下优化,将回测速度提升了5倍:
1. 将数据获取从handle_data移到before_trading_start
2. 使用批量数据获取替代循环内的单个获取
3. 合理设置回测频率,开发阶段用日频,验证阶段用分钟级
4. 利用聚宽的数据缓存机制,避免重复计算
**优化前**:完整回测需要30分钟
**优化后**:相同回测仅需6分钟
---
**总结**:高效使用聚宽回测平台的关键是"分层优化"——在不同阶段使用不同的回测策略,结合数据缓存和批量获取,可以显著提升回测效率。