cyqnt-trd 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.
- cyqnt_trd-0.1.0/LICENSE +21 -0
- cyqnt_trd-0.1.0/MANIFEST.in +20 -0
- cyqnt_trd-0.1.0/PKG-INFO +148 -0
- cyqnt_trd-0.1.0/README.md +116 -0
- cyqnt_trd-0.1.0/cyqnt_trd/__init__.py +26 -0
- cyqnt_trd-0.1.0/cyqnt_trd/backtesting/README.md +264 -0
- cyqnt_trd-0.1.0/cyqnt_trd/backtesting/__init__.py +12 -0
- cyqnt_trd-0.1.0/cyqnt_trd/backtesting/factor_test.py +332 -0
- cyqnt_trd-0.1.0/cyqnt_trd/backtesting/framework.py +311 -0
- cyqnt_trd-0.1.0/cyqnt_trd/backtesting/strategy_backtest.py +500 -0
- cyqnt_trd-0.1.0/cyqnt_trd/diagnose_api.py +28 -0
- cyqnt_trd-0.1.0/cyqnt_trd/get_data/__init__.py +15 -0
- cyqnt_trd-0.1.0/cyqnt_trd/get_data/get_futures_data.py +472 -0
- cyqnt_trd-0.1.0/cyqnt_trd/get_data/get_trending_data.py +771 -0
- cyqnt_trd-0.1.0/cyqnt_trd/online_trading/__init__.py +12 -0
- cyqnt_trd-0.1.0/cyqnt_trd/online_trading/realtime_price_tracker.py +1001 -0
- cyqnt_trd-0.1.0/cyqnt_trd/test.py +119 -0
- cyqnt_trd-0.1.0/cyqnt_trd/test_script/README.md +411 -0
- cyqnt_trd-0.1.0/cyqnt_trd/test_script/get_network_info.py +192 -0
- cyqnt_trd-0.1.0/cyqnt_trd/test_script/get_symbols_by_volume.py +227 -0
- cyqnt_trd-0.1.0/cyqnt_trd/test_script/test_alpha.py +261 -0
- cyqnt_trd-0.1.0/cyqnt_trd/test_script/test_kline_data.py +479 -0
- cyqnt_trd-0.1.0/cyqnt_trd/test_script/test_order.py +1360 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/README.md +276 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/__init__.py +17 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/factor/__init__.py +18 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/factor/ma_factor.py +75 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/factor/rsi_factor.py +56 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/selected_alpha/__init__.py +10 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/selected_alpha/alpha1.py +179 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/selected_alpha/test_alpha.py +261 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/signal/__init__.py +19 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/signal/factor_based_signal.py +244 -0
- cyqnt_trd-0.1.0/cyqnt_trd/trading_signal/signal/ma_signal.py +163 -0
- cyqnt_trd-0.1.0/cyqnt_trd/utils/__init__.py +3 -0
- cyqnt_trd-0.1.0/cyqnt_trd/utils/set_user.py +33 -0
- cyqnt_trd-0.1.0/cyqnt_trd.egg-info/PKG-INFO +148 -0
- cyqnt_trd-0.1.0/cyqnt_trd.egg-info/SOURCES.txt +43 -0
- cyqnt_trd-0.1.0/cyqnt_trd.egg-info/dependency_links.txt +1 -0
- cyqnt_trd-0.1.0/cyqnt_trd.egg-info/requires.txt +10 -0
- cyqnt_trd-0.1.0/cyqnt_trd.egg-info/top_level.txt +2 -0
- cyqnt_trd-0.1.0/pyproject.toml +64 -0
- cyqnt_trd-0.1.0/requirements.txt +19 -0
- cyqnt_trd-0.1.0/setup.cfg +4 -0
- cyqnt_trd-0.1.0/setup.py +12 -0
cyqnt_trd-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Haowen Wang
|
|
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.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# 包含必要的文件
|
|
2
|
+
include README.md
|
|
3
|
+
include LICENSE
|
|
4
|
+
include requirements.txt
|
|
5
|
+
include pyproject.toml
|
|
6
|
+
include setup.py
|
|
7
|
+
|
|
8
|
+
# 包含所有 Python 文件
|
|
9
|
+
recursive-include cyqnt_trd *.py
|
|
10
|
+
recursive-include cyqnt_trd *.md
|
|
11
|
+
recursive-include cyqnt_trd *.txt
|
|
12
|
+
|
|
13
|
+
# 排除不需要的文件
|
|
14
|
+
global-exclude __pycache__
|
|
15
|
+
global-exclude *.py[cod]
|
|
16
|
+
global-exclude *$py.class
|
|
17
|
+
global-exclude *.so
|
|
18
|
+
global-exclude .DS_Store
|
|
19
|
+
global-exclude *.egg-info
|
|
20
|
+
|
cyqnt_trd-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cyqnt-trd
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A cryptocurrency trading toolkit with data fetching, signal generation, and backtesting capabilities
|
|
5
|
+
Author-email: Haowen Wang <wanghw@zju.edu.cn>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/whw199833/cyqnt_trd
|
|
8
|
+
Project-URL: Repository, https://github.com/whw199833/cyqnt_trd
|
|
9
|
+
Project-URL: Issues, https://github.com/whw199833/cyqnt_trd/issues
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: pandas>=1.5.0
|
|
23
|
+
Requires-Dist: numpy>=1.23.0
|
|
24
|
+
Requires-Dist: matplotlib>=3.5.0
|
|
25
|
+
Requires-Dist: requests>=2.28.0
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
29
|
+
Requires-Dist: black>=22.0.0; extra == "dev"
|
|
30
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# cyqnt_trd
|
|
34
|
+
|
|
35
|
+
一个用于加密货币交易的工具包,包含数据获取、交易信号生成和回测功能。
|
|
36
|
+
|
|
37
|
+
## 功能特性
|
|
38
|
+
|
|
39
|
+
- **数据获取**: 从 Binance 获取期货和现货K线数据
|
|
40
|
+
- **交易信号**: 提供多种技术指标因子和信号策略
|
|
41
|
+
- **回测框架**: 支持因子测试和策略回测
|
|
42
|
+
|
|
43
|
+
## 安装
|
|
44
|
+
|
|
45
|
+
### 方式1: 作为可编辑包安装(推荐用于开发)
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
cd /path/to/cyqnt_trd
|
|
49
|
+
pip install -e .
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 方式2: 直接安装
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
cd /path/to/cyqnt_trd
|
|
56
|
+
pip install .
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 方式3: 从源码安装
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
cd /path/to/cyqnt_trd
|
|
63
|
+
python setup.py install
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## 依赖
|
|
67
|
+
|
|
68
|
+
主要依赖包:
|
|
69
|
+
- pandas >= 1.5.0
|
|
70
|
+
- numpy >= 1.23.0
|
|
71
|
+
- matplotlib >= 3.5.0
|
|
72
|
+
- requests >= 2.28.0
|
|
73
|
+
|
|
74
|
+
Binance SDK 依赖(需要单独安装):
|
|
75
|
+
- binance-sdk-spot
|
|
76
|
+
- binance-sdk-derivatives-trading-usds-futures
|
|
77
|
+
- binance-sdk-algo
|
|
78
|
+
- binance-common
|
|
79
|
+
|
|
80
|
+
安装依赖:
|
|
81
|
+
```bash
|
|
82
|
+
pip install -r requirements.txt
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## 使用方法
|
|
86
|
+
|
|
87
|
+
### 作为 Python Package 使用
|
|
88
|
+
|
|
89
|
+
安装后,可以直接导入使用:
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
# 导入数据获取模块
|
|
93
|
+
from cyqnt_trd.get_data import get_and_save_futures_klines, get_and_save_klines
|
|
94
|
+
|
|
95
|
+
# 导入交易信号模块
|
|
96
|
+
from cyqnt_trd.trading_signal.factor import ma_factor, rsi_factor
|
|
97
|
+
from cyqnt_trd.trading_signal.signal import ma_signal, factor_based_signal
|
|
98
|
+
|
|
99
|
+
# 导入回测框架
|
|
100
|
+
from cyqnt_trd.backtesting import BacktestFramework, FactorTester, StrategyBacktester
|
|
101
|
+
|
|
102
|
+
# 使用示例
|
|
103
|
+
data = get_and_save_futures_klines("BTCUSDT", interval="1h", limit=100)
|
|
104
|
+
framework = BacktestFramework(data_path="data/BTCUSDT_1h.json")
|
|
105
|
+
result = framework.test_factor(ma_factor, short_window=5, long_window=20)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 运行示例脚本
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
# 作为模块运行
|
|
112
|
+
python -m cyqnt_trd.trading_signal.example_usage
|
|
113
|
+
|
|
114
|
+
# 运行测试脚本
|
|
115
|
+
python -m cyqnt_trd.test_script.realtime_price_tracker
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 项目结构
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
cyqnt_trd/
|
|
122
|
+
├── cyqnt_trd/ # 主包目录
|
|
123
|
+
│ ├── __init__.py # 包初始化文件
|
|
124
|
+
│ ├── get_data/ # 数据获取模块
|
|
125
|
+
│ │ ├── __init__.py
|
|
126
|
+
│ │ ├── get_futures_data.py
|
|
127
|
+
│ │ └── get_trending_data.py
|
|
128
|
+
│ ├── trading_signal/ # 交易信号模块
|
|
129
|
+
│ │ ├── __init__.py
|
|
130
|
+
│ │ ├── factor/ # 因子模块
|
|
131
|
+
│ │ ├── signal/ # 信号策略模块
|
|
132
|
+
│ │ └── selected_alpha/ # Alpha因子模块
|
|
133
|
+
│ ├── backtesting/ # 回测框架
|
|
134
|
+
│ │ ├── __init__.py
|
|
135
|
+
│ │ ├── framework.py
|
|
136
|
+
│ │ ├── factor_test.py
|
|
137
|
+
│ │ └── strategy_backtest.py
|
|
138
|
+
│ └── test_script/ # 测试脚本
|
|
139
|
+
├── pyproject.toml # 包配置文件
|
|
140
|
+
├── requirements.txt # 依赖列表
|
|
141
|
+
└── README.md # 说明文档
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## 许可证
|
|
145
|
+
|
|
146
|
+
MIT License
|
|
147
|
+
|
|
148
|
+
Copyright (c) 2025 Haowen Wang
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# cyqnt_trd
|
|
2
|
+
|
|
3
|
+
一个用于加密货币交易的工具包,包含数据获取、交易信号生成和回测功能。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- **数据获取**: 从 Binance 获取期货和现货K线数据
|
|
8
|
+
- **交易信号**: 提供多种技术指标因子和信号策略
|
|
9
|
+
- **回测框架**: 支持因子测试和策略回测
|
|
10
|
+
|
|
11
|
+
## 安装
|
|
12
|
+
|
|
13
|
+
### 方式1: 作为可编辑包安装(推荐用于开发)
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cd /path/to/cyqnt_trd
|
|
17
|
+
pip install -e .
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 方式2: 直接安装
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
cd /path/to/cyqnt_trd
|
|
24
|
+
pip install .
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### 方式3: 从源码安装
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
cd /path/to/cyqnt_trd
|
|
31
|
+
python setup.py install
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 依赖
|
|
35
|
+
|
|
36
|
+
主要依赖包:
|
|
37
|
+
- pandas >= 1.5.0
|
|
38
|
+
- numpy >= 1.23.0
|
|
39
|
+
- matplotlib >= 3.5.0
|
|
40
|
+
- requests >= 2.28.0
|
|
41
|
+
|
|
42
|
+
Binance SDK 依赖(需要单独安装):
|
|
43
|
+
- binance-sdk-spot
|
|
44
|
+
- binance-sdk-derivatives-trading-usds-futures
|
|
45
|
+
- binance-sdk-algo
|
|
46
|
+
- binance-common
|
|
47
|
+
|
|
48
|
+
安装依赖:
|
|
49
|
+
```bash
|
|
50
|
+
pip install -r requirements.txt
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## 使用方法
|
|
54
|
+
|
|
55
|
+
### 作为 Python Package 使用
|
|
56
|
+
|
|
57
|
+
安装后,可以直接导入使用:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
# 导入数据获取模块
|
|
61
|
+
from cyqnt_trd.get_data import get_and_save_futures_klines, get_and_save_klines
|
|
62
|
+
|
|
63
|
+
# 导入交易信号模块
|
|
64
|
+
from cyqnt_trd.trading_signal.factor import ma_factor, rsi_factor
|
|
65
|
+
from cyqnt_trd.trading_signal.signal import ma_signal, factor_based_signal
|
|
66
|
+
|
|
67
|
+
# 导入回测框架
|
|
68
|
+
from cyqnt_trd.backtesting import BacktestFramework, FactorTester, StrategyBacktester
|
|
69
|
+
|
|
70
|
+
# 使用示例
|
|
71
|
+
data = get_and_save_futures_klines("BTCUSDT", interval="1h", limit=100)
|
|
72
|
+
framework = BacktestFramework(data_path="data/BTCUSDT_1h.json")
|
|
73
|
+
result = framework.test_factor(ma_factor, short_window=5, long_window=20)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 运行示例脚本
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
# 作为模块运行
|
|
80
|
+
python -m cyqnt_trd.trading_signal.example_usage
|
|
81
|
+
|
|
82
|
+
# 运行测试脚本
|
|
83
|
+
python -m cyqnt_trd.test_script.realtime_price_tracker
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## 项目结构
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
cyqnt_trd/
|
|
90
|
+
├── cyqnt_trd/ # 主包目录
|
|
91
|
+
│ ├── __init__.py # 包初始化文件
|
|
92
|
+
│ ├── get_data/ # 数据获取模块
|
|
93
|
+
│ │ ├── __init__.py
|
|
94
|
+
│ │ ├── get_futures_data.py
|
|
95
|
+
│ │ └── get_trending_data.py
|
|
96
|
+
│ ├── trading_signal/ # 交易信号模块
|
|
97
|
+
│ │ ├── __init__.py
|
|
98
|
+
│ │ ├── factor/ # 因子模块
|
|
99
|
+
│ │ ├── signal/ # 信号策略模块
|
|
100
|
+
│ │ └── selected_alpha/ # Alpha因子模块
|
|
101
|
+
│ ├── backtesting/ # 回测框架
|
|
102
|
+
│ │ ├── __init__.py
|
|
103
|
+
│ │ ├── framework.py
|
|
104
|
+
│ │ ├── factor_test.py
|
|
105
|
+
│ │ └── strategy_backtest.py
|
|
106
|
+
│ └── test_script/ # 测试脚本
|
|
107
|
+
├── pyproject.toml # 包配置文件
|
|
108
|
+
├── requirements.txt # 依赖列表
|
|
109
|
+
└── README.md # 说明文档
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## 许可证
|
|
113
|
+
|
|
114
|
+
MIT License
|
|
115
|
+
|
|
116
|
+
Copyright (c) 2025 Haowen Wang
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Cyqnt Trading Package
|
|
3
|
+
|
|
4
|
+
一个用于加密货币交易的工具包,包含数据获取、交易信号生成和回测功能。
|
|
5
|
+
|
|
6
|
+
主要模块:
|
|
7
|
+
- get_data: 数据获取模块,支持从 Binance 获取期货和现货数据
|
|
8
|
+
- trading_signal: 交易信号模块,包含因子计算和信号策略
|
|
9
|
+
- backtesting: 回测框架,支持因子测试和策略回测
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
__version__ = "0.1.0"
|
|
13
|
+
|
|
14
|
+
# 导入主要模块
|
|
15
|
+
from . import get_data
|
|
16
|
+
from . import trading_signal
|
|
17
|
+
from . import backtesting
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
'get_data',
|
|
21
|
+
'trading_signal',
|
|
22
|
+
'backtesting',
|
|
23
|
+
'utils',
|
|
24
|
+
'__version__',
|
|
25
|
+
]
|
|
26
|
+
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# 回测框架使用说明
|
|
2
|
+
|
|
3
|
+
本回测框架提供了两个主要功能:
|
|
4
|
+
1. **单因子胜率测试** - 测试某个因子在预测未来价格方向上的胜率
|
|
5
|
+
2. **策略回测** - 根据买卖信号进行回测,计算收益率和收益曲线
|
|
6
|
+
|
|
7
|
+
## 功能概述
|
|
8
|
+
|
|
9
|
+
### 1. 单因子胜率测试 (FactorTester)
|
|
10
|
+
|
|
11
|
+
用于测试某个因子在预测未来一段时间内价格方向(多/空)的胜率。
|
|
12
|
+
|
|
13
|
+
**主要功能:**
|
|
14
|
+
- 计算因子值(正数=看多,负数=看空,0=中性)
|
|
15
|
+
- 统计看多/看空信号的胜率
|
|
16
|
+
- 计算平均收益率
|
|
17
|
+
- 输出详细的测试结果
|
|
18
|
+
|
|
19
|
+
### 2. 策略回测 (StrategyBacktester)
|
|
20
|
+
|
|
21
|
+
根据买卖信号进行回测,计算收益率和收益曲线。
|
|
22
|
+
|
|
23
|
+
**主要功能:**
|
|
24
|
+
- 根据买卖信号模拟交易
|
|
25
|
+
- 计算总收益率、胜率、最大回撤、夏普比率等指标
|
|
26
|
+
- 绘制资金曲线、价格曲线和回撤曲线
|
|
27
|
+
- 记录所有交易详情
|
|
28
|
+
|
|
29
|
+
## 快速开始
|
|
30
|
+
|
|
31
|
+
### 基本使用
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import pandas as pd
|
|
35
|
+
import json
|
|
36
|
+
from cyqnt_trd.backtesting import BacktestFramework
|
|
37
|
+
|
|
38
|
+
# 加载数据
|
|
39
|
+
data_path = 'path/to/your/data.json'
|
|
40
|
+
framework = BacktestFramework(data_path=data_path)
|
|
41
|
+
|
|
42
|
+
# 定义因子函数
|
|
43
|
+
def my_factor(data: pd.DataFrame, index: int) -> float:
|
|
44
|
+
"""因子函数:返回因子值(正数=看多,负数=看空)"""
|
|
45
|
+
current_price = data.iloc[index]['close_price']
|
|
46
|
+
# 你的因子计算逻辑
|
|
47
|
+
# ...
|
|
48
|
+
return 1.0 # 或 -1.0, 0.0
|
|
49
|
+
|
|
50
|
+
# 测试因子
|
|
51
|
+
results = framework.test_factor(
|
|
52
|
+
factor_func=my_factor,
|
|
53
|
+
forward_periods=7, # 未来7个周期
|
|
54
|
+
min_periods=0,
|
|
55
|
+
factor_name="我的因子"
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
# 打印结果
|
|
59
|
+
framework.print_factor_results(results)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 策略回测
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
# 定义信号函数
|
|
66
|
+
def my_signal(data: pd.DataFrame, index: int) -> str:
|
|
67
|
+
"""信号函数:返回 'buy', 'sell', 'hold' 或 None"""
|
|
68
|
+
# 你的信号生成逻辑
|
|
69
|
+
# ...
|
|
70
|
+
return 'buy' # 或 'sell', 'hold'
|
|
71
|
+
|
|
72
|
+
# 回测策略
|
|
73
|
+
results = framework.backtest_strategy(
|
|
74
|
+
signal_func=my_signal,
|
|
75
|
+
min_periods=0,
|
|
76
|
+
position_size=0.5, # 每次使用50%的资金
|
|
77
|
+
initial_capital=10000.0,
|
|
78
|
+
commission_rate=0.001 # 0.1%手续费
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
# 打印结果
|
|
82
|
+
framework.print_backtest_results(results)
|
|
83
|
+
|
|
84
|
+
# 绘制结果
|
|
85
|
+
framework.plot_backtest_results(results)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## 数据格式要求
|
|
89
|
+
|
|
90
|
+
数据可以是以下两种格式之一:
|
|
91
|
+
|
|
92
|
+
### 1. JSON文件格式
|
|
93
|
+
|
|
94
|
+
```json
|
|
95
|
+
{
|
|
96
|
+
"symbol": "BTCUSDT",
|
|
97
|
+
"interval": "1m",
|
|
98
|
+
"data": [
|
|
99
|
+
{
|
|
100
|
+
"open_time": 1234567890000,
|
|
101
|
+
"open_time_str": "2023-01-01 00:00:00",
|
|
102
|
+
"open_price": 100.0,
|
|
103
|
+
"high_price": 105.0,
|
|
104
|
+
"low_price": 95.0,
|
|
105
|
+
"close_price": 102.0,
|
|
106
|
+
"volume": 1000.0,
|
|
107
|
+
...
|
|
108
|
+
},
|
|
109
|
+
...
|
|
110
|
+
]
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 2. DataFrame格式
|
|
115
|
+
|
|
116
|
+
DataFrame必须包含以下列:
|
|
117
|
+
- `datetime` 或 `open_time_str` 或 `open_time`: 时间
|
|
118
|
+
- `close_price`: 收盘价
|
|
119
|
+
- 其他因子计算所需的列(如 `open_price`, `high_price`, `low_price`, `volume` 等)
|
|
120
|
+
|
|
121
|
+
## 详细示例
|
|
122
|
+
|
|
123
|
+
### 示例1: 移动平均线因子测试
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
def ma_factor(data: pd.DataFrame, index: int) -> float:
|
|
127
|
+
"""MA5因子:价格高于MA5看多,低于MA5看空"""
|
|
128
|
+
if index < 5:
|
|
129
|
+
return 0
|
|
130
|
+
|
|
131
|
+
current_price = data.iloc[index]['close_price']
|
|
132
|
+
ma5 = data.iloc[index-5:index]['close_price'].mean()
|
|
133
|
+
|
|
134
|
+
if current_price > ma5:
|
|
135
|
+
return 1.0 # 看多
|
|
136
|
+
else:
|
|
137
|
+
return -1.0 # 看空
|
|
138
|
+
|
|
139
|
+
# 测试因子
|
|
140
|
+
framework = BacktestFramework(data_path='data.json')
|
|
141
|
+
results = framework.test_factor(
|
|
142
|
+
factor_func=ma_factor,
|
|
143
|
+
forward_periods=7,
|
|
144
|
+
min_periods=5,
|
|
145
|
+
factor_name="MA5因子"
|
|
146
|
+
)
|
|
147
|
+
framework.print_factor_results(results)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### 示例2: 移动平均线交叉策略
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
def ma_cross_signal(data: pd.DataFrame, index: int) -> str:
|
|
154
|
+
"""MA交叉策略:价格上穿MA5买入,下穿MA5卖出"""
|
|
155
|
+
if index < 5:
|
|
156
|
+
return 'hold'
|
|
157
|
+
|
|
158
|
+
current_price = data.iloc[index]['close_price']
|
|
159
|
+
ma5 = data.iloc[index-5:index]['close_price'].mean()
|
|
160
|
+
prev_price = data.iloc[index-1]['close_price']
|
|
161
|
+
prev_ma5 = data.iloc[index-6:index-1]['close_price'].mean()
|
|
162
|
+
|
|
163
|
+
# 上穿
|
|
164
|
+
if prev_price <= prev_ma5 and current_price > ma5:
|
|
165
|
+
return 'buy'
|
|
166
|
+
# 下穿
|
|
167
|
+
elif prev_price >= prev_ma5 and current_price < ma5:
|
|
168
|
+
return 'sell'
|
|
169
|
+
else:
|
|
170
|
+
return 'hold'
|
|
171
|
+
|
|
172
|
+
# 回测策略
|
|
173
|
+
results = framework.backtest_strategy(
|
|
174
|
+
signal_func=ma_cross_signal,
|
|
175
|
+
min_periods=5,
|
|
176
|
+
position_size=0.5,
|
|
177
|
+
initial_capital=10000.0,
|
|
178
|
+
commission_rate=0.001
|
|
179
|
+
)
|
|
180
|
+
framework.plot_backtest_results(results)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## API参考
|
|
184
|
+
|
|
185
|
+
### BacktestFramework
|
|
186
|
+
|
|
187
|
+
主回测框架类,提供统一的接口。
|
|
188
|
+
|
|
189
|
+
#### 方法
|
|
190
|
+
|
|
191
|
+
- `test_factor(factor_func, forward_periods=7, min_periods=0, factor_name="factor")` - 测试因子胜率
|
|
192
|
+
- `backtest_strategy(signal_func, min_periods=0, position_size=1.0, initial_capital=10000.0, commission_rate=0.001)` - 回测策略
|
|
193
|
+
- `plot_backtest_results(results, figsize=(14, 10))` - 绘制回测结果
|
|
194
|
+
- `print_factor_results(results)` - 打印因子测试结果
|
|
195
|
+
- `print_backtest_results(results)` - 打印回测结果
|
|
196
|
+
|
|
197
|
+
### FactorTester
|
|
198
|
+
|
|
199
|
+
单因子胜率测试器。
|
|
200
|
+
|
|
201
|
+
#### 方法
|
|
202
|
+
|
|
203
|
+
- `test_factor(factor_func, forward_periods=7, min_periods=0, factor_name="factor")` - 测试因子
|
|
204
|
+
- `print_results(results)` - 打印结果
|
|
205
|
+
- `save_results(results, filepath)` - 保存结果到JSON文件
|
|
206
|
+
|
|
207
|
+
### StrategyBacktester
|
|
208
|
+
|
|
209
|
+
策略回测器。
|
|
210
|
+
|
|
211
|
+
#### 方法
|
|
212
|
+
|
|
213
|
+
- `backtest(signal_func, min_periods=0, position_size=1.0)` - 执行回测
|
|
214
|
+
- `plot_results(results, figsize=(14, 10))` - 绘制结果
|
|
215
|
+
- `print_results(results)` - 打印结果
|
|
216
|
+
- `save_results(results, filepath)` - 保存结果到JSON文件
|
|
217
|
+
|
|
218
|
+
## 注意事项
|
|
219
|
+
|
|
220
|
+
1. **因子函数** (`factor_func`):
|
|
221
|
+
- 接受 `(data: pd.DataFrame, index: int)` 作为参数
|
|
222
|
+
- 返回 `float`: 正数表示看多,负数表示看空,0表示中性
|
|
223
|
+
|
|
224
|
+
2. **信号函数** (`signal_func`):
|
|
225
|
+
- 接受 `(data: pd.DataFrame, index: int)` 作为参数
|
|
226
|
+
- 返回 `str`: `'buy'`(买入)、`'sell'`(卖出)、`'hold'`(持有)或 `None`
|
|
227
|
+
|
|
228
|
+
3. **数据要求**:
|
|
229
|
+
- 数据必须按时间排序
|
|
230
|
+
- 必须包含 `close_price` 列
|
|
231
|
+
- 必须包含时间列(`datetime`, `open_time_str` 或 `open_time`)
|
|
232
|
+
|
|
233
|
+
4. **性能考虑**:
|
|
234
|
+
- 对于大量数据,建议使用适当的数据切片
|
|
235
|
+
- 因子和信号函数应该尽可能高效
|
|
236
|
+
|
|
237
|
+
## 输出结果说明
|
|
238
|
+
|
|
239
|
+
### 因子测试结果
|
|
240
|
+
|
|
241
|
+
- `total_samples`: 总样本数
|
|
242
|
+
- `long_signals`: 看多信号数量
|
|
243
|
+
- `short_signals`: 看空信号数量
|
|
244
|
+
- `long_win_rate`: 看多信号胜率
|
|
245
|
+
- `short_win_rate`: 看空信号胜率
|
|
246
|
+
- `overall_win_rate`: 总体胜率
|
|
247
|
+
- `long_avg_return`: 看多信号平均收益率
|
|
248
|
+
- `short_avg_return`: 看空信号平均收益率
|
|
249
|
+
- `details`: 详细结果列表
|
|
250
|
+
|
|
251
|
+
### 策略回测结果
|
|
252
|
+
|
|
253
|
+
- `initial_capital`: 初始资金
|
|
254
|
+
- `final_capital`: 最终资金
|
|
255
|
+
- `total_return`: 总收益率
|
|
256
|
+
- `total_trades`: 总交易次数
|
|
257
|
+
- `win_trades`: 盈利交易次数
|
|
258
|
+
- `loss_trades`: 亏损交易次数
|
|
259
|
+
- `win_rate`: 胜率
|
|
260
|
+
- `max_drawdown`: 最大回撤
|
|
261
|
+
- `sharpe_ratio`: 夏普比率
|
|
262
|
+
- `equity_curve`: 资金曲线(DataFrame)
|
|
263
|
+
- `trades`: 交易记录列表
|
|
264
|
+
|