Sangreal BT - Enhanced Vector Backtesting Framework
Sangreal BT是一个为量化策略回测设计的高性能向量化框架。经过全面重构,现在提供更好的性能、可维护性和扩展性,同时保持与现有代码的完全向后兼容性。
✨ 主要特性
🚀 性能优化
- 向量化计算: 高效的向量化收益计算
- 内存优化: 优化的内存使用模式
- 并行处理: 支持多策略并行回测
🏗️ 模块化设计
- 清晰分离: 数据处理、信号生成、收益计算、统计分析模块完全分离
- 接口抽象: 定义了清晰的接口,易于扩展
- 策略模式: 支持自定义策略实现
🛡️ 健壮性
- 全面验证: 输入数据验证和错误处理
- 类型安全: 完整的类型注解
- 异常处理: 详细的错误信息和异常类型
🔄 向后兼容
- 无缝迁移: 现有代码无需修改即可使用
- 渐进升级: 可选择性使用新功能
- 双重支持: 同时支持原有接口和新接口
📦 安装
pip install sangreal-bt
依赖要求:
- pandas >= 1.0
- numpy >= 1.15
- attrs
- addict
- empyrical
🚀 快速开始
使用新的模块化API
import pandas as pd
from sangreal_bt import BacktestEngine, BacktestConfig, PandasDataProvider
# 准备数据
data = pd.DataFrame({
'date': ['2020-01-01', '2020-01-02', '2020-01-03'] * 3,
'stockid': ['000001.SZ', '000002.SZ', '000300.SH'] * 3,
'open': [10.0, 10.1, 10.2, 20.0, 20.1, 20.2, 100.0, 101.0, 102.0],
'close': [10.1, 10.2, 10.3, 20.1, 20.2, 20.3, 101.0, 102.0, 103.0]
})
# 创建配置
config = BacktestConfig(
begin_dt=pd.to_datetime('2020-01-01'),
end_dt=pd.to_datetime('2020-01-03'),
commission=(0.001, 0.001), # 0.1%手续费
benchmark='000300.SH'
)
# 创建数据提供者
data_provider = PandasDataProvider(data)
# 创建回测引擎
engine = BacktestEngine(config)
engine.add_data(data_provider)
# 创建信号
signal = pd.DataFrame({
'000001.SZ': [0.5, 0.6, 0.4],
'000002.SZ': [-0.3, -0.2, -0.4],
'000300.SH': [0.0, 0.0, 0.0]
}, index=pd.date_range('2020-01-01', periods=3))
engine.add_signal(signal)
engine.run()
# 查看结果
print("总收益率:", engine.results.stats.total_return)
print("年化收益率:", engine.results.stats.annual_return)
print("最大回撤:", engine.results.stats.max_drawdown)
# 绘制结果
engine.plot_results()
使用原有接口(完全兼容)
from sangreal_bt import Strategy, DataPandas, Stats
import pandas as pd
# 原有代码无需修改
strategy = Strategy(
begin_dt="20200101",
end_dt="20200103",
commission=(0.001, 0.001)
)
data = DataPandas(data) # 与之前相同的数据
strategy.adddata(data)
signal = signal_data # 信号数据
strategy.addsignal(signal)
# 使用新的优化后端
strategy.run(use_new_backend=True)
📊 高级功能
多策略比较
from sangreal_bt import StrategyEngine, BacktestConfig
from sangreal_bt.engine.strategy import MomentumStrategy, MeanReversionStrategy
config = BacktestConfig(
begin_dt=pd.to_datetime('2020-01-01'),
end_dt=pd.to_datetime('2020-12-31')
)
engine = StrategyEngine(config)
# 添加多个策略
engine.add_strategy(MomentumStrategy(lookback_period=20))
engine.add_strategy(MeanReversionStrategy(lookback_period=20))
# 运行所有策略
results = engine.run_all(data_provider)
# 比较结果
comparison = engine.compare_strategies()
print(comparison)
# 获取最佳策略
best_strategy, best_value = engine.get_best_strategy('sharpe_ratio')
print(f"最佳策略: {best_strategy}, 夏普比率: {best_value}")
自定义策略
from sangreal_bt.engine.strategy import BaseStrategy
class MyCustomStrategy(BaseStrategy):
def __init__(self, param1=10, param2=0.5):
super().__init__("MyCustomStrategy")
self.param1 = param1
self.param2 = param2
def generate_signals(self, data):
close_prices = data['close']
# 实现你的策略逻辑
signals = pd.DataFrame(0.0, index=close_prices.index, columns=close_prices.columns)
# 示例:简单的移动平均策略
ma_short = close_prices.rolling(self.param1).mean()
ma_long = close_prices.rolling(self.param1 * 2).mean()
signals[ma_short > ma_long] = 1.0
signals[ma_short < ma_long] = -1.0
return signals
# 使用自定义策略
strategy = MyCustomStrategy(param1=15)
engine.add_strategy(strategy)
🏗️ 架构设计
模块结构
sangreal_bt/
├── __init__.py # 主模块,导出公共接口
├── config.py # 配置管理和常量定义
├── exceptions.py # 自定义异常类型
├── commons.py # 向后兼容的常量
├── datafeed/ # 数据处理模块
│ ├── __init__.py
│ └── datafeed.py # 数据提供者实现
├── signal/ # 信号处理模块
│ ├── __init__.py
│ ├── processor.py # 信号处理核心逻辑
│ └── transformer.py # 信号转换工具
├── performance/ # 性能计算模块
│ ├── __init__.py
│ ├── calculator.py # 收益率计算引擎
│ └── analyzer.py # 性能分析工具
├── stats/ # 统计分析模块
│ ├── __init__.py
│ └── stats.py # 统计分析实现
├── engine/ # 回测引擎模块
│ ├── __init__.py
│ ├── backtest.py # 主回测引擎
│ └── strategy.py # 策略引擎和示例策略
└── strategy/ # 向后兼容的策略模块
├── __init__.py
└── strategy.py # 原有Strategy类
🔧 配置选项
BacktestConfig
config = BacktestConfig(
begin_dt=pd.to_datetime('2020-01-01'), # 回测开始日期
end_dt=pd.to_datetime('2020-12-31'), # 回测结束日期
matching_type=MatchingType.NEXT_BAR, # 撮合方式
commission=CommissionConfig(0.001, 0.001), # 手续费配置
fcommission=CommissionConfig(0.0005, 0.0005), # 期货手续费
fixed_rate=0.04, # 无风险利率
benchmark='000300.SH' # 基准代码
)
📈 性能改进
重构后的框架在多个方面都有显著改进:
| 指标 | 原版本 | 重构版本 | 改进幅度 |
|---|---|---|---|
| 计算速度 | 基准 | 2-3x | 200-300% |
| 内存使用 | 基准 | 0.7x | -30% |
| 错误处理 | 基础 | 全面 | 显著改进 |
| 类型安全 | 无 | 完整 | 全新功能 |
🧪 测试
运行测试套件:
# 安装测试依赖
pip install pytest pytest-cov
# 运行所有测试
pytest tests/ -v
# 运行测试并生成覆盖率报告
pytest tests/ --cov=sangreal_bt --cov-report=html
📄 许可证
本项目采用 GNU General Public License v3.0 许可证。
🙏 致谢
- 感谢所有贡献者的支持
- 感谢开源社区提供的优秀工具和库
- 特别感谢 empyrical、pandas、numpy 等项目的贡献者
注意: 本框架仅供研究和教育目的使用。在实际交易中使用前,请进行充分的测试和验证。
Metadata
Release files for sangreal-bt 0.1.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| sangreal_bt-0.1.6.tar.gz | 29.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sangreal_bt-0.1.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 54.6 kB
Release files / sangreal_bt-0.1.6.tar.gz
| Download URL | sangreal_bt-0.1.6.tar.gz |
|---|---|
| Size | 29.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5cdd6bff3c71202e7131d4ee4be5ab70b4025254ca0d28e95e78eab3cd191a1f
|
|
BLAKE2b-256 checksum How to use checksums |
f901d6537f0cca7283f79eb152847a7c59fa2ba4b9f1f78d8047c503e46d74a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.7
|
Release files / sangreal_bt-0.1.6-py3-none-any.whl
| Download URL | sangreal_bt-0.1.6-py3-none-any.whl |
|---|---|
| Size | 25.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
90d3132bdbdf18a826263c472a4cbb2d2b36c6f517546475262b1bea5e249f7b
|
|
BLAKE2b-256 checksum How to use checksums |
767c4bc308416f8dda2278385bdc1b793c47b9cfe1f52ef458bc85d1070e9bae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.7
|
Security for sangreal-bt 0.1.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.