deltafq 0.0.1__tar.gz → 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of deltafq might be problematic. Click here for more details.
- deltafq-0.1.0/LICENSE +22 -0
- deltafq-0.1.0/MANIFEST.in +7 -0
- deltafq-0.1.0/PKG-INFO +195 -0
- deltafq-0.1.0/README.md +156 -0
- deltafq-0.1.0/deltafq/__init__.py +31 -0
- deltafq-0.1.0/deltafq/backtest/__init__.py +7 -0
- deltafq-0.1.0/deltafq/backtest/engine.py +52 -0
- deltafq-0.1.0/deltafq/backtest/result.py +45 -0
- deltafq-0.1.0/deltafq/data/__init__.py +7 -0
- deltafq-0.1.0/deltafq/data/base.py +30 -0
- deltafq-0.1.0/deltafq/data/loader.py +63 -0
- deltafq-0.1.0/deltafq/indicators/__init__.py +8 -0
- deltafq-0.1.0/deltafq/indicators/momentum.py +23 -0
- deltafq-0.1.0/deltafq/indicators/trend.py +61 -0
- deltafq-0.1.0/deltafq/indicators/volatility.py +27 -0
- deltafq-0.1.0/deltafq/optimization/__init__.py +6 -0
- deltafq-0.1.0/deltafq/optimization/grid_search.py +41 -0
- deltafq-0.1.0/deltafq/performance/__init__.py +6 -0
- deltafq-0.1.0/deltafq/performance/metrics.py +37 -0
- deltafq-0.1.0/deltafq/risk/__init__.py +7 -0
- deltafq-0.1.0/deltafq/risk/metrics.py +33 -0
- deltafq-0.1.0/deltafq/risk/position.py +39 -0
- deltafq-0.1.0/deltafq/strategy/__init__.py +6 -0
- deltafq-0.1.0/deltafq/strategy/base.py +44 -0
- deltafq-0.1.0/deltafq/trade/__init__.py +6 -0
- deltafq-0.1.0/deltafq/trade/broker.py +40 -0
- deltafq-0.1.0/deltafq/utils/__init__.py +6 -0
- deltafq-0.1.0/deltafq/utils/time.py +32 -0
- deltafq-0.1.0/deltafq.egg-info/PKG-INFO +195 -0
- deltafq-0.1.0/deltafq.egg-info/SOURCES.txt +39 -0
- deltafq-0.1.0/deltafq.egg-info/requires.txt +15 -0
- deltafq-0.1.0/pyproject.toml +74 -0
- deltafq-0.1.0/setup.py +12 -0
- deltafq-0.1.0/tests/test_backtest.py +54 -0
- deltafq-0.1.0/tests/test_data.py +27 -0
- deltafq-0.1.0/tests/test_full_workflow.py +158 -0
- deltafq-0.1.0/tests/test_indicators.py +55 -0
- deltafq-0.1.0/tests/test_strategy.py +49 -0
- deltafq-0.0.1/LICENSE +0 -7
- deltafq-0.0.1/PKG-INFO +0 -24
- deltafq-0.0.1/README.md +0 -1
- deltafq-0.0.1/deltafq/__init__.py +0 -0
- deltafq-0.0.1/deltafq.egg-info/PKG-INFO +0 -24
- deltafq-0.0.1/deltafq.egg-info/SOURCES.txt +0 -8
- deltafq-0.0.1/setup.py +0 -19
- {deltafq-0.0.1 → deltafq-0.1.0}/deltafq.egg-info/dependency_links.txt +0 -0
- {deltafq-0.0.1 → deltafq-0.1.0}/deltafq.egg-info/top_level.txt +0 -0
- {deltafq-0.0.1 → deltafq-0.1.0}/setup.cfg +0 -0
deltafq-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 DeltaFQ Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
deltafq-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: deltafq
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 专业的Python量化交易库
|
|
5
|
+
Author-email: DeltaFQ Team <your.email@example.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Delta-F/deltafq
|
|
8
|
+
Project-URL: Documentation, https://deltafq.readthedocs.io
|
|
9
|
+
Project-URL: Repository, https://github.com/Delta-F/deltafq
|
|
10
|
+
Project-URL: PyPI, https://pypi.org/project/deltafq/
|
|
11
|
+
Project-URL: Bug Tracker, https://github.com/Delta-F/deltafq/issues
|
|
12
|
+
Keywords: quantitative,trading,finance,backtest,strategy
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
23
|
+
Requires-Python: >=3.8
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: pandas>=1.3.0
|
|
27
|
+
Requires-Dist: numpy>=1.21.0
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
31
|
+
Requires-Dist: black>=22.0.0; extra == "dev"
|
|
32
|
+
Requires-Dist: flake8>=5.0.0; extra == "dev"
|
|
33
|
+
Requires-Dist: mypy>=0.990; extra == "dev"
|
|
34
|
+
Provides-Extra: plot
|
|
35
|
+
Requires-Dist: matplotlib>=3.5.0; extra == "plot"
|
|
36
|
+
Provides-Extra: all
|
|
37
|
+
Requires-Dist: matplotlib>=3.5.0; extra == "all"
|
|
38
|
+
Dynamic: license-file
|
|
39
|
+
|
|
40
|
+
# DeltaFQ
|
|
41
|
+
|
|
42
|
+
[](https://badge.fury.io/py/deltafq)
|
|
43
|
+
[](https://www.python.org/downloads/)
|
|
44
|
+
[](https://opensource.org/licenses/MIT)
|
|
45
|
+
|
|
46
|
+
专业的Python量化交易库,为量化策略开发者和研究人员提供从数据获取到策略回测的完整工具链。
|
|
47
|
+
|
|
48
|
+
## 特性
|
|
49
|
+
|
|
50
|
+
- 📊 **多源数据支持** - 统一的数据接口,支持多种数据源
|
|
51
|
+
- 📈 **丰富的技术指标** - 内置常用技术指标,支持自定义扩展
|
|
52
|
+
- 🎯 **灵活的策略框架** - 简洁的API,快速构建交易策略
|
|
53
|
+
- ⚡ **高效的回测引擎** - 向量化计算,快速验证策略效果
|
|
54
|
+
- 📉 **全面的风险管理** - 仓位管理、风险控制、绩效分析
|
|
55
|
+
- 🔧 **参数优化工具** - 多种优化算法,寻找最佳参数
|
|
56
|
+
- 📱 **实盘交易接口** - 统一的交易接口,无缝切换模拟与实盘
|
|
57
|
+
|
|
58
|
+
## 安装
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install deltafq
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
或者安装开发版本:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
git clone https://github.com/Delta-F/deltafq.git
|
|
68
|
+
cd deltafq
|
|
69
|
+
pip install -e .
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 快速开始
|
|
73
|
+
|
|
74
|
+
### 获取数据
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
import deltafq as dfq
|
|
78
|
+
|
|
79
|
+
# 获取股票数据
|
|
80
|
+
data = dfq.data.get_stock_daily('000001.SZ', start='2020-01-01', end='2023-12-31')
|
|
81
|
+
print(data.head())
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 计算技术指标
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
# 计算移动平均线
|
|
88
|
+
data['ma5'] = dfq.indicators.SMA(data['close'], 5)
|
|
89
|
+
data['ma20'] = dfq.indicators.SMA(data['close'], 20)
|
|
90
|
+
|
|
91
|
+
# 计算MACD
|
|
92
|
+
macd = dfq.indicators.MACD(data['close'])
|
|
93
|
+
data = data.join(macd)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 构建交易策略
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
class MAStrategy(dfq.strategy.Strategy):
|
|
100
|
+
"""双均线策略"""
|
|
101
|
+
|
|
102
|
+
def on_bar(self, bar):
|
|
103
|
+
if bar.ma5 > bar.ma20:
|
|
104
|
+
self.buy()
|
|
105
|
+
elif bar.ma5 < bar.ma20:
|
|
106
|
+
self.sell()
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### 运行回测
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
# 创建回测引擎
|
|
113
|
+
engine = dfq.backtest.BacktestEngine(
|
|
114
|
+
initial_cash=100000,
|
|
115
|
+
commission=0.0003
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
# 运行回测
|
|
119
|
+
result = engine.run(data, MAStrategy())
|
|
120
|
+
|
|
121
|
+
# 查看结果
|
|
122
|
+
print(result.summary())
|
|
123
|
+
result.plot()
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## 模块说明
|
|
127
|
+
|
|
128
|
+
- **data** - 数据获取和管理
|
|
129
|
+
- **indicators** - 技术指标计算
|
|
130
|
+
- **strategy** - 策略开发框架
|
|
131
|
+
- **backtest** - 回测引擎
|
|
132
|
+
- **risk** - 风险管理
|
|
133
|
+
- **performance** - 绩效分析
|
|
134
|
+
- **optimization** - 参数优化
|
|
135
|
+
- **trade** - 实盘交易接口
|
|
136
|
+
- **utils** - 工具函数
|
|
137
|
+
|
|
138
|
+
## 示例
|
|
139
|
+
|
|
140
|
+
查看 `examples/` 目录获取更多示例代码:
|
|
141
|
+
|
|
142
|
+
- `ma_strategy.py` - 双均线策略
|
|
143
|
+
- `macd_strategy.py` - MACD策略
|
|
144
|
+
- `optimization_example.py` - 参数优化示例
|
|
145
|
+
|
|
146
|
+
## 文档
|
|
147
|
+
|
|
148
|
+
- **使用指南**: [docs/GUIDE.md](docs/GUIDE.md)
|
|
149
|
+
- **API参考**: [docs/API.md](docs/API.md)
|
|
150
|
+
- **开发指南**: [docs/CONTRIBUTING.md](docs/CONTRIBUTING.md)
|
|
151
|
+
- **更新日志**: [docs/CHANGELOG.md](docs/CHANGELOG.md)
|
|
152
|
+
|
|
153
|
+
## 依赖
|
|
154
|
+
|
|
155
|
+
- Python >= 3.8
|
|
156
|
+
- pandas >= 1.3.0
|
|
157
|
+
- numpy >= 1.21.0
|
|
158
|
+
|
|
159
|
+
## 开发
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
# 克隆仓库
|
|
163
|
+
git clone https://github.com/Delta-F/deltafq.git
|
|
164
|
+
cd deltafq
|
|
165
|
+
|
|
166
|
+
# 安装开发依赖
|
|
167
|
+
pip install -e ".[dev]"
|
|
168
|
+
|
|
169
|
+
# 运行测试
|
|
170
|
+
pytest
|
|
171
|
+
|
|
172
|
+
# 代码格式化
|
|
173
|
+
black deltafq/
|
|
174
|
+
|
|
175
|
+
# 类型检查
|
|
176
|
+
mypy deltafq/
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## 许可证
|
|
180
|
+
|
|
181
|
+
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件
|
|
182
|
+
|
|
183
|
+
## 贡献
|
|
184
|
+
|
|
185
|
+
欢迎提交 Issue 和 Pull Request!
|
|
186
|
+
|
|
187
|
+
## 联系方式
|
|
188
|
+
|
|
189
|
+
- 项目主页:[https://github.com/Delta-F/deltafq](https://github.com/Delta-F/deltafq)
|
|
190
|
+
- PyPI 主页:[https://pypi.org/project/deltafq/](https://pypi.org/project/deltafq/)
|
|
191
|
+
- 问题反馈:[https://github.com/Delta-F/deltafq/issues](https://github.com/Delta-F/deltafq/issues)
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
⚠️ **风险提示**:量化交易存在风险,本库仅供学习研究使用,不构成投资建议。实盘交易需谨慎,风险自担。
|
deltafq-0.1.0/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# DeltaFQ
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/py/deltafq)
|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
专业的Python量化交易库,为量化策略开发者和研究人员提供从数据获取到策略回测的完整工具链。
|
|
8
|
+
|
|
9
|
+
## 特性
|
|
10
|
+
|
|
11
|
+
- 📊 **多源数据支持** - 统一的数据接口,支持多种数据源
|
|
12
|
+
- 📈 **丰富的技术指标** - 内置常用技术指标,支持自定义扩展
|
|
13
|
+
- 🎯 **灵活的策略框架** - 简洁的API,快速构建交易策略
|
|
14
|
+
- ⚡ **高效的回测引擎** - 向量化计算,快速验证策略效果
|
|
15
|
+
- 📉 **全面的风险管理** - 仓位管理、风险控制、绩效分析
|
|
16
|
+
- 🔧 **参数优化工具** - 多种优化算法,寻找最佳参数
|
|
17
|
+
- 📱 **实盘交易接口** - 统一的交易接口,无缝切换模拟与实盘
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install deltafq
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
或者安装开发版本:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
git clone https://github.com/Delta-F/deltafq.git
|
|
29
|
+
cd deltafq
|
|
30
|
+
pip install -e .
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## 快速开始
|
|
34
|
+
|
|
35
|
+
### 获取数据
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
import deltafq as dfq
|
|
39
|
+
|
|
40
|
+
# 获取股票数据
|
|
41
|
+
data = dfq.data.get_stock_daily('000001.SZ', start='2020-01-01', end='2023-12-31')
|
|
42
|
+
print(data.head())
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 计算技术指标
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
# 计算移动平均线
|
|
49
|
+
data['ma5'] = dfq.indicators.SMA(data['close'], 5)
|
|
50
|
+
data['ma20'] = dfq.indicators.SMA(data['close'], 20)
|
|
51
|
+
|
|
52
|
+
# 计算MACD
|
|
53
|
+
macd = dfq.indicators.MACD(data['close'])
|
|
54
|
+
data = data.join(macd)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 构建交易策略
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
class MAStrategy(dfq.strategy.Strategy):
|
|
61
|
+
"""双均线策略"""
|
|
62
|
+
|
|
63
|
+
def on_bar(self, bar):
|
|
64
|
+
if bar.ma5 > bar.ma20:
|
|
65
|
+
self.buy()
|
|
66
|
+
elif bar.ma5 < bar.ma20:
|
|
67
|
+
self.sell()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### 运行回测
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
# 创建回测引擎
|
|
74
|
+
engine = dfq.backtest.BacktestEngine(
|
|
75
|
+
initial_cash=100000,
|
|
76
|
+
commission=0.0003
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
# 运行回测
|
|
80
|
+
result = engine.run(data, MAStrategy())
|
|
81
|
+
|
|
82
|
+
# 查看结果
|
|
83
|
+
print(result.summary())
|
|
84
|
+
result.plot()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## 模块说明
|
|
88
|
+
|
|
89
|
+
- **data** - 数据获取和管理
|
|
90
|
+
- **indicators** - 技术指标计算
|
|
91
|
+
- **strategy** - 策略开发框架
|
|
92
|
+
- **backtest** - 回测引擎
|
|
93
|
+
- **risk** - 风险管理
|
|
94
|
+
- **performance** - 绩效分析
|
|
95
|
+
- **optimization** - 参数优化
|
|
96
|
+
- **trade** - 实盘交易接口
|
|
97
|
+
- **utils** - 工具函数
|
|
98
|
+
|
|
99
|
+
## 示例
|
|
100
|
+
|
|
101
|
+
查看 `examples/` 目录获取更多示例代码:
|
|
102
|
+
|
|
103
|
+
- `ma_strategy.py` - 双均线策略
|
|
104
|
+
- `macd_strategy.py` - MACD策略
|
|
105
|
+
- `optimization_example.py` - 参数优化示例
|
|
106
|
+
|
|
107
|
+
## 文档
|
|
108
|
+
|
|
109
|
+
- **使用指南**: [docs/GUIDE.md](docs/GUIDE.md)
|
|
110
|
+
- **API参考**: [docs/API.md](docs/API.md)
|
|
111
|
+
- **开发指南**: [docs/CONTRIBUTING.md](docs/CONTRIBUTING.md)
|
|
112
|
+
- **更新日志**: [docs/CHANGELOG.md](docs/CHANGELOG.md)
|
|
113
|
+
|
|
114
|
+
## 依赖
|
|
115
|
+
|
|
116
|
+
- Python >= 3.8
|
|
117
|
+
- pandas >= 1.3.0
|
|
118
|
+
- numpy >= 1.21.0
|
|
119
|
+
|
|
120
|
+
## 开发
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
# 克隆仓库
|
|
124
|
+
git clone https://github.com/Delta-F/deltafq.git
|
|
125
|
+
cd deltafq
|
|
126
|
+
|
|
127
|
+
# 安装开发依赖
|
|
128
|
+
pip install -e ".[dev]"
|
|
129
|
+
|
|
130
|
+
# 运行测试
|
|
131
|
+
pytest
|
|
132
|
+
|
|
133
|
+
# 代码格式化
|
|
134
|
+
black deltafq/
|
|
135
|
+
|
|
136
|
+
# 类型检查
|
|
137
|
+
mypy deltafq/
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## 许可证
|
|
141
|
+
|
|
142
|
+
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件
|
|
143
|
+
|
|
144
|
+
## 贡献
|
|
145
|
+
|
|
146
|
+
欢迎提交 Issue 和 Pull Request!
|
|
147
|
+
|
|
148
|
+
## 联系方式
|
|
149
|
+
|
|
150
|
+
- 项目主页:[https://github.com/Delta-F/deltafq](https://github.com/Delta-F/deltafq)
|
|
151
|
+
- PyPI 主页:[https://pypi.org/project/deltafq/](https://pypi.org/project/deltafq/)
|
|
152
|
+
- 问题反馈:[https://github.com/Delta-F/deltafq/issues](https://github.com/Delta-F/deltafq/issues)
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
⚠️ **风险提示**:量化交易存在风险,本库仅供学习研究使用,不构成投资建议。实盘交易需谨慎,风险自担。
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""DeltaFQ - 专业的Python量化交易库
|
|
2
|
+
|
|
3
|
+
一个面向量化策略开发者和量化研究人员的完整工具链。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
__version__ = "0.1.0"
|
|
7
|
+
__author__ = "DeltaFQ Team"
|
|
8
|
+
|
|
9
|
+
# 导入主要模块
|
|
10
|
+
from deltafq import data
|
|
11
|
+
from deltafq import indicators
|
|
12
|
+
from deltafq import strategy
|
|
13
|
+
from deltafq import backtest
|
|
14
|
+
from deltafq import risk
|
|
15
|
+
from deltafq import performance
|
|
16
|
+
from deltafq import optimization
|
|
17
|
+
from deltafq import trade
|
|
18
|
+
from deltafq import utils
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"data",
|
|
22
|
+
"indicators",
|
|
23
|
+
"strategy",
|
|
24
|
+
"backtest",
|
|
25
|
+
"risk",
|
|
26
|
+
"performance",
|
|
27
|
+
"optimization",
|
|
28
|
+
"trade",
|
|
29
|
+
"utils",
|
|
30
|
+
]
|
|
31
|
+
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""回测引擎"""
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from typing import Optional
|
|
5
|
+
from deltafq.strategy.base import Strategy
|
|
6
|
+
from deltafq.backtest.result import BacktestResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class BacktestEngine:
|
|
10
|
+
"""回测引擎"""
|
|
11
|
+
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
initial_cash: float = 100000,
|
|
15
|
+
commission: float = 0.0003,
|
|
16
|
+
slippage: float = 0.0
|
|
17
|
+
):
|
|
18
|
+
"""初始化回测引擎
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
initial_cash: 初始资金
|
|
22
|
+
commission: 手续费率
|
|
23
|
+
slippage: 滑点
|
|
24
|
+
"""
|
|
25
|
+
self.initial_cash = initial_cash
|
|
26
|
+
self.commission = commission
|
|
27
|
+
self.slippage = slippage
|
|
28
|
+
|
|
29
|
+
def run(
|
|
30
|
+
self,
|
|
31
|
+
data: pd.DataFrame,
|
|
32
|
+
strategy: Strategy
|
|
33
|
+
) -> BacktestResult:
|
|
34
|
+
"""运行回测
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
data: 历史数据
|
|
38
|
+
strategy: 策略实例
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
回测结果
|
|
42
|
+
"""
|
|
43
|
+
for idx, bar in data.iterrows():
|
|
44
|
+
strategy.on_bar(bar)
|
|
45
|
+
|
|
46
|
+
result = BacktestResult(
|
|
47
|
+
initial_cash=self.initial_cash,
|
|
48
|
+
data=data,
|
|
49
|
+
signals=strategy.get_signals()
|
|
50
|
+
)
|
|
51
|
+
return result
|
|
52
|
+
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""回测结果"""
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from typing import Dict, Any, List
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BacktestResult:
|
|
8
|
+
"""回测结果类"""
|
|
9
|
+
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
initial_cash: float,
|
|
13
|
+
data: pd.DataFrame,
|
|
14
|
+
signals: List[Dict]
|
|
15
|
+
):
|
|
16
|
+
self.initial_cash = initial_cash
|
|
17
|
+
self.data = data
|
|
18
|
+
self.signals = signals
|
|
19
|
+
|
|
20
|
+
def summary(self) -> Dict[str, Any]:
|
|
21
|
+
"""生成回测摘要
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
包含各项指标的字典
|
|
25
|
+
"""
|
|
26
|
+
return {
|
|
27
|
+
'initial_cash': self.initial_cash,
|
|
28
|
+
'total_signals': len(self.signals),
|
|
29
|
+
'data_length': len(self.data),
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
def plot(self) -> None:
|
|
33
|
+
"""绘制回测结果"""
|
|
34
|
+
try:
|
|
35
|
+
import matplotlib.pyplot as plt
|
|
36
|
+
plt.figure(figsize=(12, 6))
|
|
37
|
+
plt.plot(self.data['close'])
|
|
38
|
+
plt.title('Price Chart')
|
|
39
|
+
plt.xlabel('Date')
|
|
40
|
+
plt.ylabel('Price')
|
|
41
|
+
plt.grid(True)
|
|
42
|
+
plt.show()
|
|
43
|
+
except ImportError:
|
|
44
|
+
print("需要matplotlib: pip install matplotlib")
|
|
45
|
+
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""数据源基类"""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import Optional
|
|
5
|
+
import pandas as pd
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class DataSource(ABC):
|
|
9
|
+
"""数据源抽象基类"""
|
|
10
|
+
|
|
11
|
+
@abstractmethod
|
|
12
|
+
def get_data(
|
|
13
|
+
self,
|
|
14
|
+
symbol: str,
|
|
15
|
+
start_date: str,
|
|
16
|
+
end_date: str,
|
|
17
|
+
**kwargs
|
|
18
|
+
) -> pd.DataFrame:
|
|
19
|
+
"""获取数据
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
symbol: 股票代码
|
|
23
|
+
start_date: 开始日期
|
|
24
|
+
end_date: 结束日期
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
包含OHLCV数据的DataFrame
|
|
28
|
+
"""
|
|
29
|
+
pass
|
|
30
|
+
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""数据加载器"""
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
import numpy as np
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_stock_daily(
|
|
9
|
+
symbol: str,
|
|
10
|
+
start: str,
|
|
11
|
+
end: str,
|
|
12
|
+
source: str = "mock"
|
|
13
|
+
) -> pd.DataFrame:
|
|
14
|
+
"""获取股票日线数据
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
symbol: 股票代码
|
|
18
|
+
start: 开始日期
|
|
19
|
+
end: 结束日期
|
|
20
|
+
source: 数据源,目前支持 'mock'
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
包含OHLCV数据的DataFrame
|
|
24
|
+
"""
|
|
25
|
+
dates = pd.date_range(start=start, end=end, freq='B')
|
|
26
|
+
n = len(dates)
|
|
27
|
+
base_price = 100 + np.random.randn(n).cumsum()
|
|
28
|
+
open_price = base_price + np.random.randn(n) * 0.5
|
|
29
|
+
close_price = base_price + np.random.randn(n) * 0.5
|
|
30
|
+
high_price = np.maximum(open_price, close_price) + np.abs(np.random.randn(n) * 0.5)
|
|
31
|
+
low_price = np.minimum(open_price, close_price) - np.abs(np.random.randn(n) * 0.5)
|
|
32
|
+
|
|
33
|
+
data = pd.DataFrame({
|
|
34
|
+
'open': open_price,
|
|
35
|
+
'high': high_price,
|
|
36
|
+
'low': low_price,
|
|
37
|
+
'close': close_price,
|
|
38
|
+
'volume': np.random.randint(1000000, 10000000, n),
|
|
39
|
+
}, index=dates)
|
|
40
|
+
|
|
41
|
+
data.index.name = 'date'
|
|
42
|
+
return data
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def get_stock_minute(
|
|
46
|
+
symbol: str,
|
|
47
|
+
start: str,
|
|
48
|
+
end: str,
|
|
49
|
+
freq: str = '1min'
|
|
50
|
+
) -> pd.DataFrame:
|
|
51
|
+
"""获取股票分钟数据
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
symbol: 股票代码
|
|
55
|
+
start: 开始日期
|
|
56
|
+
end: 结束日期
|
|
57
|
+
freq: 频率,如 '1min', '5min'
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
包含OHLCV数据的DataFrame
|
|
61
|
+
"""
|
|
62
|
+
raise NotImplementedError("分钟数据功能待实现")
|
|
63
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""动量类指标"""
|
|
2
|
+
|
|
3
|
+
import pandas as pd
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def RSI(data: pd.Series, period: int = 14) -> pd.Series:
|
|
7
|
+
"""相对强弱指标
|
|
8
|
+
|
|
9
|
+
Args:
|
|
10
|
+
data: 价格序列
|
|
11
|
+
period: 周期
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
RSI序列
|
|
15
|
+
"""
|
|
16
|
+
delta = data.diff()
|
|
17
|
+
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
|
|
18
|
+
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
|
|
19
|
+
|
|
20
|
+
rs = gain / loss
|
|
21
|
+
rsi = 100 - (100 / (1 + rs))
|
|
22
|
+
return rsi
|
|
23
|
+
|