minibt 1.2.9__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.
- minibt-1.2.9/LICENSE +21 -0
- minibt-1.2.9/MANIFEST.in +32 -0
- minibt-1.2.9/PKG-INFO +398 -0
- minibt-1.2.9/README.md +327 -0
- minibt-1.2.9/minibt/SingleAssetFactorOptimizer.py +533 -0
- minibt-1.2.9/minibt/__init__.py +174 -0
- minibt-1.2.9/minibt/bt.py +2194 -0
- minibt-1.2.9/minibt/btplot.py +2659 -0
- minibt-1.2.9/minibt/callback.py +97 -0
- minibt-1.2.9/minibt/constant.py +445 -0
- minibt-1.2.9/minibt/core.py +4434 -0
- minibt-1.2.9/minibt/cython_functions/__init__.py +1 -0
- minibt-1.2.9/minibt/cython_functions/backtest_engine.pyx +709 -0
- minibt-1.2.9/minibt/cython_functions/backtrader_from_signals.pyx +589 -0
- minibt-1.2.9/minibt/cython_functions/backtrader_pair_from_signals.pyx +648 -0
- minibt-1.2.9/minibt/cython_functions/setup.py +13 -0
- minibt-1.2.9/minibt/cython_functions/signal_profit.pyx +204 -0
- minibt-1.2.9/minibt/data/__init__.py +1 -0
- minibt-1.2.9/minibt/data/contract.csv +62 -0
- minibt-1.2.9/minibt/data/minibt_dark.png +0 -0
- minibt-1.2.9/minibt/data/minibt_light.png +0 -0
- minibt-1.2.9/minibt/data/settings.ini +2 -0
- minibt-1.2.9/minibt/data/test/l2609_60.csv +10001 -0
- minibt-1.2.9/minibt/data/test/pp2609_60.csv +10001 -0
- minibt-1.2.9/minibt/data/test/test.csv +8965 -0
- minibt-1.2.9/minibt/data/tools.py +307 -0
- minibt-1.2.9/minibt/data/utils.py +27 -0
- minibt-1.2.9/minibt/data_enhancement.py +264 -0
- minibt-1.2.9/minibt/elegantrl/__init__.py +7 -0
- minibt-1.2.9/minibt/elegantrl/agents/AgentBase.py +733 -0
- minibt-1.2.9/minibt/elegantrl/agents/AgentDQN.py +659 -0
- minibt-1.2.9/minibt/elegantrl/agents/AgentEmbedDQN.py +346 -0
- minibt-1.2.9/minibt/elegantrl/agents/AgentPPO.py +667 -0
- minibt-1.2.9/minibt/elegantrl/agents/AgentPPO_.py +716 -0
- minibt-1.2.9/minibt/elegantrl/agents/AgentSAC.py +472 -0
- minibt-1.2.9/minibt/elegantrl/agents/AgentTD3.py +461 -0
- minibt-1.2.9/minibt/elegantrl/agents/MAgentMADDPG.py +206 -0
- minibt-1.2.9/minibt/elegantrl/agents/MAgentMAPPO.py +404 -0
- minibt-1.2.9/minibt/elegantrl/agents/MAgentQMix.py +238 -0
- minibt-1.2.9/minibt/elegantrl/agents/MAgentVDN.py +157 -0
- minibt-1.2.9/minibt/elegantrl/agents/__init__.py +67 -0
- minibt-1.2.9/minibt/elegantrl/agents/embedDQN.py +337 -0
- minibt-1.2.9/minibt/elegantrl/envs/CustomGymEnv.py +544 -0
- minibt-1.2.9/minibt/elegantrl/envs/PlanIsaacGymEnv.py +299 -0
- minibt-1.2.9/minibt/elegantrl/envs/PointChasingEnv.py +268 -0
- minibt-1.2.9/minibt/elegantrl/envs/StockTradingEnv.py +387 -0
- minibt-1.2.9/minibt/elegantrl/envs/__init__.py +0 -0
- minibt-1.2.9/minibt/elegantrl/train/__init__.py +9 -0
- minibt-1.2.9/minibt/elegantrl/train/config.py +482 -0
- minibt-1.2.9/minibt/elegantrl/train/evaluator.py +678 -0
- minibt-1.2.9/minibt/elegantrl/train/replay_buffer.py +323 -0
- minibt-1.2.9/minibt/elegantrl/train/run.py +563 -0
- minibt-1.2.9/minibt/extendoptimizer.py +187 -0
- minibt-1.2.9/minibt/indicators/__init__.py +54 -0
- minibt-1.2.9/minibt/indicators/_acp.py +248 -0
- minibt-1.2.9/minibt/indicators/_typing.py +133 -0
- minibt-1.2.9/minibt/indicators/base.py +6756 -0
- minibt-1.2.9/minibt/indicators/btind.py +2062 -0
- minibt-1.2.9/minibt/indicators/core.py +7678 -0
- minibt-1.2.9/minibt/indicators/factors.py +369 -0
- minibt-1.2.9/minibt/indicators/finta.py +1308 -0
- minibt-1.2.9/minibt/indicators/pair.py +889 -0
- minibt-1.2.9/minibt/indicators/pandas_ta.py +9297 -0
- minibt-1.2.9/minibt/indicators/pine.py +319 -0
- minibt-1.2.9/minibt/indicators/talib.py +1757 -0
- minibt-1.2.9/minibt/indicators/tqfunc.py +1177 -0
- minibt-1.2.9/minibt/indicators/tqta.py +3233 -0
- minibt-1.2.9/minibt/indicators/tradingview.py +29114 -0
- minibt-1.2.9/minibt/indicators/tulip.py +1785 -0
- minibt-1.2.9/minibt/logger.py +1079 -0
- minibt-1.2.9/minibt/loss.py +220 -0
- minibt-1.2.9/minibt/muon.py +1944 -0
- minibt-1.2.9/minibt/order.py +264 -0
- minibt-1.2.9/minibt/other.py +1290 -0
- minibt-1.2.9/minibt/rl_utils.py +918 -0
- minibt-1.2.9/minibt/stop/__init__.py +78 -0
- minibt-1.2.9/minibt/stop/stop.py +609 -0
- minibt-1.2.9/minibt/strategy/__init__.py +1 -0
- minibt-1.2.9/minibt/strategy/autoscale_cb.js +78 -0
- minibt-1.2.9/minibt/strategy/autoscale_cb_candles.js +33 -0
- minibt-1.2.9/minibt/strategy/base.py +4690 -0
- minibt-1.2.9/minibt/strategy/bokeh_plot.py +2975 -0
- minibt-1.2.9/minibt/strategy/bokeh_realtime.py +420 -0
- minibt-1.2.9/minibt/strategy/bokeh_replay.py +414 -0
- minibt-1.2.9/minibt/strategy/factors_plot.py +905 -0
- minibt-1.2.9/minibt/strategy/light_chart.py +3890 -0
- minibt-1.2.9/minibt/strategy/light_chart_replay.py +3071 -0
- minibt-1.2.9/minibt/strategy/live_window.py +242 -0
- minibt-1.2.9/minibt/strategy/optimize.py +409 -0
- minibt-1.2.9/minibt/strategy/pages.py +657 -0
- minibt-1.2.9/minibt/strategy/qs_plots.py +162 -0
- minibt-1.2.9/minibt/strategy/realtime.py +2604 -0
- minibt-1.2.9/minibt/strategy/replay_window.py +888 -0
- minibt-1.2.9/minibt/strategy/stats.py +1158 -0
- minibt-1.2.9/minibt/strategy/strategy.py +2063 -0
- minibt-1.2.9/minibt/strategy/strategy_window.py +1739 -0
- minibt-1.2.9/minibt/strategy/tq_live.py +217 -0
- minibt-1.2.9/minibt/ta.py +3587 -0
- minibt-1.2.9/minibt/ta_.py +1273 -0
- minibt-1.2.9/minibt/tools.py +1285 -0
- minibt-1.2.9/minibt/tradingsymtem/__init__.py +1 -0
- minibt-1.2.9/minibt/tradingsymtem/candle.py +6 -0
- minibt-1.2.9/minibt/tradingsymtem/cycle.py +6 -0
- minibt-1.2.9/minibt/tradingsymtem/momentum.py +7 -0
- minibt-1.2.9/minibt/tradingsymtem/overlap.py +5 -0
- minibt-1.2.9/minibt/tradingsymtem/pair.py +847 -0
- minibt-1.2.9/minibt/tradingsymtem/performance.py +5 -0
- minibt-1.2.9/minibt/tradingsymtem/statistics.py +6 -0
- minibt-1.2.9/minibt/tradingsymtem/support_resistance.py +7 -0
- minibt-1.2.9/minibt/tradingsymtem/tradingsystem.py +82 -0
- minibt-1.2.9/minibt/tradingsymtem/trend.py +7 -0
- minibt-1.2.9/minibt/tradingsymtem/volatility.py +7 -0
- minibt-1.2.9/minibt/tradingsymtem/volume.py +7 -0
- minibt-1.2.9/minibt/tradingview_.py +5160 -0
- minibt-1.2.9/minibt/typing_.py +160 -0
- minibt-1.2.9/minibt/utils.py +9205 -0
- minibt-1.2.9/minibt/zigzag/__init__.py +148 -0
- minibt-1.2.9/minibt/zigzag/build.py +56 -0
- minibt-1.2.9/minibt/zigzag/core.py +18 -0
- minibt-1.2.9/minibt/zigzag/core.pyx +234 -0
- minibt-1.2.9/minibt/zigzag/demo.py +30 -0
- minibt-1.2.9/minibt.egg-info/PKG-INFO +398 -0
- minibt-1.2.9/minibt.egg-info/SOURCES.txt +129 -0
- minibt-1.2.9/minibt.egg-info/dependency_links.txt +1 -0
- minibt-1.2.9/minibt.egg-info/requires.txt +48 -0
- minibt-1.2.9/minibt.egg-info/top_level.txt +1 -0
- minibt-1.2.9/pyproject.toml +139 -0
- minibt-1.2.9/requirements-lock.txt +77 -0
- minibt-1.2.9/requirements.txt +93 -0
- minibt-1.2.9/setup.cfg +4 -0
- minibt-1.2.9/setup.py +153 -0
minibt-1.2.9/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 owen9527
|
|
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.
|
minibt-1.2.9/MANIFEST.in
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# sdist 清单:只收录构建/安装真正需要的文件
|
|
2
|
+
include LICENSE
|
|
3
|
+
include README.md
|
|
4
|
+
include pyproject.toml
|
|
5
|
+
include setup.py
|
|
6
|
+
include requirements.txt
|
|
7
|
+
include requirements-lock.txt
|
|
8
|
+
|
|
9
|
+
# ---- 随包分发的资源 ----
|
|
10
|
+
recursive-include minibt/data *.csv *.ini *.png
|
|
11
|
+
recursive-include minibt/strategy *.js
|
|
12
|
+
|
|
13
|
+
# ---- Cython 源码(从 sdist 安装时需要用它现场编译)----
|
|
14
|
+
recursive-include minibt *.pyx
|
|
15
|
+
|
|
16
|
+
# ---- 排除运行时 / 生成 / 中间产物 ----
|
|
17
|
+
prune minibt/build
|
|
18
|
+
prune minibt/cython_functions/build
|
|
19
|
+
recursive-exclude minibt/logs *
|
|
20
|
+
recursive-exclude minibt/strategy/plots *
|
|
21
|
+
recursive-exclude minibt/strategy/analysis_reports *
|
|
22
|
+
recursive-exclude minibt/op_params *
|
|
23
|
+
exclude minibt/strategy/setting.json
|
|
24
|
+
exclude minibt/strategy/backtest_result.json
|
|
25
|
+
|
|
26
|
+
# ---- 全局排除垃圾文件 ----
|
|
27
|
+
global-exclude *.py[cod]
|
|
28
|
+
global-exclude *.pyd *.so *.dylib
|
|
29
|
+
global-exclude *.c
|
|
30
|
+
global-exclude *.bak *.old *.orig *.rej
|
|
31
|
+
global-exclude *~
|
|
32
|
+
global-exclude "*.pyx.bak"
|
minibt-1.2.9/PKG-INFO
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: minibt
|
|
3
|
+
Version: 1.2.9
|
|
4
|
+
Summary: 一站式量化交易策略开发库,简化从策略搭建到实盘交易的全流程
|
|
5
|
+
Author-email: owen <407841129@qq.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/MiniBtMaster/minibt
|
|
8
|
+
Project-URL: Documentation, https://www.minibt.cn/
|
|
9
|
+
Project-URL: Repository, https://github.com/MiniBtMaster/minibt
|
|
10
|
+
Project-URL: Issues, https://github.com/MiniBtMaster/minibt/issues
|
|
11
|
+
Keywords: quant,trading,backtest,strategy,indicator,futures,tqsdk
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
20
|
+
Requires-Python: >=3.12
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: numpy>=2.2.6
|
|
24
|
+
Requires-Dist: pandas<=2.3.3,>=2.2.0
|
|
25
|
+
Requires-Dist: pandas-ta>=0.3.14b0
|
|
26
|
+
Requires-Dist: numba>=0.60
|
|
27
|
+
Requires-Dist: TA-Lib>=0.4.24
|
|
28
|
+
Requires-Dist: finta>=1.3
|
|
29
|
+
Requires-Dist: scipy>=1.7.0
|
|
30
|
+
Requires-Dist: tulipy>=0.4.0
|
|
31
|
+
Requires-Dist: quantstats>=0.0.62
|
|
32
|
+
Requires-Dist: statsmodels>=0.13.5
|
|
33
|
+
Requires-Dist: arch>=7.2.0
|
|
34
|
+
Requires-Dist: pykalman>=0.10.1
|
|
35
|
+
Requires-Dist: matplotlib>=3.7
|
|
36
|
+
Requires-Dist: bokeh>=3.3.0
|
|
37
|
+
Requires-Dist: plotly>=6.4.0
|
|
38
|
+
Requires-Dist: PyQt6>=6.11.0
|
|
39
|
+
Requires-Dist: PyQt6-WebEngine>=6.11.0
|
|
40
|
+
Requires-Dist: PyQt6-Fluent-Widgets>=1.11.1
|
|
41
|
+
Requires-Dist: pylightcharts>=0.1.2
|
|
42
|
+
Requires-Dist: tqsdk>=3.4.10
|
|
43
|
+
Requires-Dist: tqsdk-ctpse>=1.0.2
|
|
44
|
+
Requires-Dist: tqsdk-sm>=1.0.5
|
|
45
|
+
Requires-Dist: scikit-learn>=1.3
|
|
46
|
+
Requires-Dist: holoviews>=1.18
|
|
47
|
+
Requires-Dist: tqdm>=4.65
|
|
48
|
+
Requires-Dist: torch>=1.13.1
|
|
49
|
+
Requires-Dist: torchcontrib>=0.0.2
|
|
50
|
+
Requires-Dist: gymnasium>=0.29
|
|
51
|
+
Requires-Dist: rich>=13.0.0
|
|
52
|
+
Requires-Dist: addict>=2.4.0
|
|
53
|
+
Requires-Dist: cachetools>=5.3.0
|
|
54
|
+
Requires-Dist: retrying>=1.3.4
|
|
55
|
+
Requires-Dist: psutil>=5.9.4
|
|
56
|
+
Requires-Dist: iteration-utilities>=0.11.0
|
|
57
|
+
Requires-Dist: joblib>=1.2.0
|
|
58
|
+
Requires-Dist: colorlog>=6.7.0
|
|
59
|
+
Requires-Dist: typing-extensions>=4.12.2
|
|
60
|
+
Requires-Dist: optuna>=3.4.0
|
|
61
|
+
Requires-Dist: deap>=1.4.1
|
|
62
|
+
Provides-Extra: dev
|
|
63
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
64
|
+
Requires-Dist: setuptools>=77; extra == "dev"
|
|
65
|
+
Requires-Dist: wheel; extra == "dev"
|
|
66
|
+
Requires-Dist: Cython>=3.0; extra == "dev"
|
|
67
|
+
Requires-Dist: autopep8>=1.6.0; extra == "dev"
|
|
68
|
+
Requires-Dist: pylint>=2.17.1; extra == "dev"
|
|
69
|
+
Requires-Dist: pipdeptree>=2.28.0; extra == "dev"
|
|
70
|
+
Dynamic: license-file
|
|
71
|
+
|
|
72
|
+
# MiniBT — 一站式量化交易策略开发库
|
|
73
|
+
|
|
74
|
+
[](https://www.python.org/)
|
|
75
|
+
[](https://opensource.org/licenses/MIT)
|
|
76
|
+
[](https://pypi.org/project/minibt/)
|
|
77
|
+
[](https://github.com/MiniBtMaster/minibt)
|
|
78
|
+
|
|
79
|
+
MiniBT 是一个覆盖量化交易**全流程**的 Python 开发库:从指标计算、策略编写、回测分析,到参数优化、K线回放、实盘交易,再到机器学习与强化学习,全部通过一套极简 API 打通。基于 Python 3.12+,MIT 许可证。
|
|
80
|
+
|
|
81
|
+
## ✨ 核心优势
|
|
82
|
+
|
|
83
|
+
- **🚀 极简开发**:约 20 行代码即可完成一个完整策略,无需关心底层细节,精力聚焦策略思想。
|
|
84
|
+
- **📊 指标即插即用**:内置 10+ 指标库(Pandas-TA、TA-Lib、TradingView、Tulip、FinTA、BtInd、TqTa、TqFunc、Factors、Pair 等),数千个指标一行调用。
|
|
85
|
+
- **⚡ 高性能回测**:信号级 Cython 快速回测 + 完整策略回测双引擎,回测/实盘同一套策略代码,零改写切换。
|
|
86
|
+
- **🎯 内置优化器**:网格搜索 / Optuna 贝叶斯优化,单目标 / 多目标 / 加权,收益、胜率、夏普、回撤等指标一键寻优。
|
|
87
|
+
- **🖥️ 可视化 & 回放**:**三个引擎 × 三种场景 = 九种模式** —— 引擎为 **Bokeh**(浏览器,纯 bokeh server)、**纯 pylightcharts**(原生窗口)、**pylightcharts_web**(浏览器 + SSE);场景为**回测 / 回放 / 实时**,支持逐 K 线回放,直观观察信号与交易过程。
|
|
88
|
+
- **🤖 智能交易**:集成 ElegantRL 强化学习,让策略具备自适应能力。
|
|
89
|
+
- **🔗 实盘对接**:基于 TQSDK(天勤)无缝连接期货实盘,支持图表 / 后台串行 / 多进程并行三种部署模式。
|
|
90
|
+
|
|
91
|
+
## 📦 安装
|
|
92
|
+
|
|
93
|
+
默认安装即为**全功能**(指标 / 回测 / 图表 / 参数优化 / 统计报告 / 实盘 / 强化学习):
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
pip install minibt
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
> 全部重依赖(PyQt6、torch、tqsdk、quantstats 等)都采用**按需惰性 import**:
|
|
100
|
+
> 依赖会一并安装,但 `import minibt` 不会加载它们,启动时间仍保持在 **~1.2s**。
|
|
101
|
+
|
|
102
|
+
开发 / 打包额外工具:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
pip install "minibt[dev]"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## 🚀 快速开始
|
|
109
|
+
|
|
110
|
+
一个完整的双均线交叉策略只需约 20 行:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
from minibt import *
|
|
114
|
+
|
|
115
|
+
class MA(Strategy):
|
|
116
|
+
params = dict(length1=10, length2=20)
|
|
117
|
+
|
|
118
|
+
def __init__(self):
|
|
119
|
+
self.kline = self.get_kline(LocalDatas.test) # 1. 加载数据(内置测试数据)
|
|
120
|
+
self.ma1 = self.kline.close.sma(self.params.length1) # 2. 计算指标
|
|
121
|
+
self.ma2 = self.kline.close.sma(self.params.length2)
|
|
122
|
+
self.long_signal = self.ma1.cross_up(self.ma2) # 3. 定义信号:金叉/死叉
|
|
123
|
+
self.short_signal = self.ma1.cross_down(self.ma2)
|
|
124
|
+
|
|
125
|
+
def next(self): # 4. 逐 K 线交易逻辑
|
|
126
|
+
if not self.kline.position:
|
|
127
|
+
if self.long_signal.new:
|
|
128
|
+
self.kline.buy()
|
|
129
|
+
elif self.short_signal.new:
|
|
130
|
+
self.kline.sell()
|
|
131
|
+
elif self.kline.position > 0 and self.short_signal.new:
|
|
132
|
+
self.kline.sell()
|
|
133
|
+
elif self.kline.position < 0 and self.long_signal.new:
|
|
134
|
+
self.kline.buy()
|
|
135
|
+
|
|
136
|
+
if __name__ == "__main__":
|
|
137
|
+
Bt().run() # 5. 一键回测并出图(默认 pylightcharts 原生窗口)
|
|
138
|
+
# 想用 bokeh:Bt().run(gui=Gui.Bokeh);网页版:gui=Gui.PylightchartsWeb
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## 📖 重要功能
|
|
142
|
+
|
|
143
|
+
### 1. 指标即插即用
|
|
144
|
+
|
|
145
|
+
K线 / IndSeries / IndFrame 直接调用全部内置指标库,无需自写基础指标:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
rsi = self.kline.close.rsi(14) # Pandas-TA 指标
|
|
149
|
+
macd = self.kline.close.MACD() # TA-Lib 指标
|
|
150
|
+
pmax = self.kline.close.btind.pmax3() # BtInd 指标
|
|
151
|
+
sma = self.kline.close.tqfunc.sma(75, 0.02) # TqFunc 指标
|
|
152
|
+
ut = self.kline.tradingview.UT_Bot_Alerts() # TradingView 指标(含信号)
|
|
153
|
+
|
|
154
|
+
rsi = self.kline.rsi(14,close = self.kline.open) # 使用开盘价计算rsi指标(close 为默认)
|
|
155
|
+
atr = self.kline.atr(14,high=_high,low=_low,close=_close) # 使用其它最高最低价、收盘价计算atr指标
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 2. 自定义指标 `BtIndicator`
|
|
159
|
+
|
|
160
|
+
继承 `BtIndicator`,在 `next()` 中计算并返回输出线,自动生成交易信号:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
class CCI(BtIndicator):
|
|
164
|
+
params = dict(CCI_PERIOD=10, CCI_UPPER=100, CCI_LOWER=-100)
|
|
165
|
+
isplot = dict(long_signal=False, short_signal=False) # 控制显示
|
|
166
|
+
|
|
167
|
+
def next(self):
|
|
168
|
+
cci = self.close.cci(self.params.CCI_PERIOD)
|
|
169
|
+
long_signal = cci.cross_up(self.params.CCI_UPPER)
|
|
170
|
+
short_signal = cci.cross_down(self.params.CCI_LOWER)
|
|
171
|
+
# 方式一:将指标值赋值给 `lines`
|
|
172
|
+
self.lines.cci = cci
|
|
173
|
+
self.lines.long_signal = long_signal
|
|
174
|
+
self.lines.short_signal = short_signal
|
|
175
|
+
# 方式二:直接返回指标值
|
|
176
|
+
# return cci, long_signal, short_signal
|
|
177
|
+
|
|
178
|
+
class CCIStrategy(Strategy):
|
|
179
|
+
def __init__(self):
|
|
180
|
+
self.kline = self.get_kline(LocalDatas.pp2609_60)
|
|
181
|
+
self.cci = CCI(self.kline)
|
|
182
|
+
|
|
183
|
+
def next(self):
|
|
184
|
+
if not self.kline.position:
|
|
185
|
+
if self.cci.long_signal.new:
|
|
186
|
+
self.kline.buy(stop=BtStop.SegmentationTracking)
|
|
187
|
+
elif self.cci.short_signal.new:
|
|
188
|
+
self.kline.sell(stop=BtStop.SegmentationTracking)
|
|
189
|
+
|
|
190
|
+
if __name__ == "__main__":
|
|
191
|
+
Bt().run()
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### 3. 信号快速回测 `signal_backtest`
|
|
195
|
+
|
|
196
|
+
指标内置信号回测引擎,无需编写策略即可评估,支持完整止损/止盈体系:
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
ind = CCI(kline) # 任意含 long_signal/short_signal 的指标
|
|
200
|
+
|
|
201
|
+
result = ind.signal_backtest(
|
|
202
|
+
commission=1.0, com_type=1,
|
|
203
|
+
sl_stop=0.01, tp_stop=0.03, stop_mode=2, # 止损止盈(tick/金额/百分比三模式)
|
|
204
|
+
sl_trail=1, # 移动止损
|
|
205
|
+
max_hold_bars=5, # 持仓超时强平
|
|
206
|
+
# sl_callback=my_sl, sl_callback_args=(...), # 动态止损/止盈回调
|
|
207
|
+
isplot=True, isreport=True,
|
|
208
|
+
)
|
|
209
|
+
result.pprint() # 统计报告 + 资金曲线
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### 4. 参数优化
|
|
213
|
+
|
|
214
|
+
策略级优化有两种方式:
|
|
215
|
+
|
|
216
|
+
**方式一:策略类属性 `op_config`(推荐,最简洁)**
|
|
217
|
+
|
|
218
|
+
在策略类上定义 `op_config`,`Bt.run()` 会自动检测并执行优化,无需手动调用 `optstrategy()`:
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
class MyStrategy(Strategy):
|
|
222
|
+
params = dict(len1=10, len2=20, a=1.0)
|
|
223
|
+
op_config = OpConfig(
|
|
224
|
+
target=['profit', 'sharpe', 'max_drawdown'],
|
|
225
|
+
weights=(1., 1., 1.),
|
|
226
|
+
opconfig=OptunaConfig(n_trials=10),
|
|
227
|
+
op_method='optuna',
|
|
228
|
+
params={'len1': (5, 21, 1), 'len2': (10, 31, 1), 'a': (0.5, 3., 0.1)},
|
|
229
|
+
)
|
|
230
|
+
...
|
|
231
|
+
|
|
232
|
+
if __name__ == "__main__":
|
|
233
|
+
Bt().run() # 自动识别 op_config,执行参数优化
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**方式二:手动调用 `optstrategy()`**
|
|
237
|
+
|
|
238
|
+
```python
|
|
239
|
+
if __name__ == "__main__":
|
|
240
|
+
bt = Bt()
|
|
241
|
+
bt.optstrategy(['profit', 'sharpe', 'max_drawdown'], (1., 1., 1.),
|
|
242
|
+
opconfig=OptunaConfig(n_trials=10), op_method='optuna',
|
|
243
|
+
len1=(5, 21, 1), len2=(10, 31, 1), a=(0.5, 3., 0.1))
|
|
244
|
+
bt.run()
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
信号级优化(`OptimizeConfig`:单/多目标、网格/Optuna、加权):
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
config = OptimizeConfig(
|
|
251
|
+
params={'length1': (5, 30, 2), 'length2': (30, 60, 2)},
|
|
252
|
+
target=['max_drawdown', 'sharpe', 'win_rate'],
|
|
253
|
+
weights=(-1.0, 3.0, 1.5), # 回撤最小化、夏普与胜率最大化
|
|
254
|
+
method='optuna', config={'n_trials': 100, 'sampler': 'TPESampler'},
|
|
255
|
+
)
|
|
256
|
+
result = ind.signal_backtest(optimize=config)
|
|
257
|
+
result.plot_optuna()
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### 5. 自定义止损 `Stop`
|
|
261
|
+
|
|
262
|
+
继承 `Stop`,实现 `long()/short()` 即可打造 ATR 动态止损等任意止损逻辑:
|
|
263
|
+
|
|
264
|
+
```python
|
|
265
|
+
class ATRDynamicStop(Stop):
|
|
266
|
+
|
|
267
|
+
def __init__(self, atr_length: int = 14, atr_mult: float = 1.0):
|
|
268
|
+
self.atr_length = atr_length
|
|
269
|
+
self.atr_mult = atr_mult
|
|
270
|
+
self.atr = self.kline.atr(self.atr_length)
|
|
271
|
+
|
|
272
|
+
def long(self):
|
|
273
|
+
stop = self.low[-1] - self.atr_mult * self.atr[-1]
|
|
274
|
+
# 写入多头止损价
|
|
275
|
+
self.stop_price.new = stop
|
|
276
|
+
# 写入多头止盈价
|
|
277
|
+
self.target_price.new = self.kline.open_price + (self.kline.open_price - stop) * 2
|
|
278
|
+
def short(self):
|
|
279
|
+
...
|
|
280
|
+
|
|
281
|
+
BtStop.ATRDynamicStop = ATRDynamicStop # 注册后即可在策略中使用
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### 6. 多周期 K 线
|
|
285
|
+
|
|
286
|
+
`resample()` 一键切换周期,指标自动对齐(只适用于回测):
|
|
287
|
+
|
|
288
|
+
```python
|
|
289
|
+
self.kline_1m = self.get_kline(LocalDatas.v2509_60)
|
|
290
|
+
self.kline_5m = self.kline_1m.resample(300) # 1分钟 → 5分钟
|
|
291
|
+
self.ma_1m = self.kline_1m.close.sma(20)
|
|
292
|
+
self.ma_5m = self.kline_5m.close.sma(20)() # 上采样回主周期
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### 7. 多策略加载 `addstrategy`
|
|
296
|
+
|
|
297
|
+
支持策略类 / 模块名 / .py 路径 / 目录 / 包路径混合加载,并可传参:
|
|
298
|
+
|
|
299
|
+
```python
|
|
300
|
+
bt = Bt()
|
|
301
|
+
bt.addstrategy('cci', 'cmo') # 模块名自动发现
|
|
302
|
+
bt.addstrategy('./strategy/cci.py') # 文件路径
|
|
303
|
+
bt.addstrategy('strategy') # 目录批量加载
|
|
304
|
+
mystrategy= MyStrategy.copy(params=dict(symbol="l2609_60"))
|
|
305
|
+
bt.addstrategy(mystrategy) # 副本+参数
|
|
306
|
+
bt.run(gui=Gui.Pylightcharts)
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### 8. K 线回放 `replay`
|
|
310
|
+
|
|
311
|
+
逐 K 线推进回测,直观复现实时交易过程:
|
|
312
|
+
|
|
313
|
+
```python
|
|
314
|
+
class ReplayStrategy(Strategy):
|
|
315
|
+
def __init__(self):
|
|
316
|
+
self.kline = self.get_kline(LocalDatas.test)
|
|
317
|
+
self.test = self.kline.tradingview.UT_Bot_Alerts()
|
|
318
|
+
|
|
319
|
+
def next(self):
|
|
320
|
+
if not self.kline.position:
|
|
321
|
+
if self.test.long_signal.new:
|
|
322
|
+
self.kline.buy(stop=BtStop.SegmentationTracking)
|
|
323
|
+
elif self.test.short_signal.new:
|
|
324
|
+
self.kline.sell(stop=BtStop.SegmentationTracking)
|
|
325
|
+
|
|
326
|
+
if __name__ == "__main__":
|
|
327
|
+
Bt(replay=True).run(period_milliseconds=1000, gui=Gui.Pylightcharts)
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### 9. 实盘交易(TQSDK)
|
|
331
|
+
|
|
332
|
+
回测通过的策略,切换 `Bt(live=True)` 即可实盘,支持三种部署模式:
|
|
333
|
+
|
|
334
|
+
```python
|
|
335
|
+
bt = Bt(live=True)
|
|
336
|
+
bt.addstrategy(CCIStrategy, CMOStrategy)
|
|
337
|
+
bt.addTqapi(tq_auth=tq_auth(user_name="账号", password="密码"))
|
|
338
|
+
|
|
339
|
+
bt.run() # 模式一:pylightcharts 实时图表(原生窗口)
|
|
340
|
+
# bt.run(isplot=False) # 模式二:后台串行(低资源)
|
|
341
|
+
# bt.run(isplot=False, run_parallel=True) # 模式三:多进程并行(互相隔离)
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### 10. 机器学习 & 强化学习
|
|
345
|
+
|
|
346
|
+
集成 ElegantRL 强化学习,策略一行配置即可训练:
|
|
347
|
+
|
|
348
|
+
```python
|
|
349
|
+
class RLStrategy(Strategy):
|
|
350
|
+
rl = True # 开启强化学习
|
|
351
|
+
def __init__(self):
|
|
352
|
+
# 初始化指标特征
|
|
353
|
+
...
|
|
354
|
+
# 配置强化学习模型
|
|
355
|
+
self.set_model_params(
|
|
356
|
+
agent=Agents.AgentDiscretePPO, if_discrete=True,
|
|
357
|
+
train=True, break_step=1e6, state_dim=..., action_dim=...,
|
|
358
|
+
auto_process_features=True, # 自动处理指标特征
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
def reset(self):
|
|
362
|
+
# 重置强化学习模型
|
|
363
|
+
...
|
|
364
|
+
|
|
365
|
+
def step(self):
|
|
366
|
+
# 训练和测试强化学习模型
|
|
367
|
+
...
|
|
368
|
+
# 训练 / 回测 / 实盘统一由 Bt 调度
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## 🔬 更多示例
|
|
372
|
+
|
|
373
|
+
完整示例见 [`tutorials/`](https://github.com/MiniBtMaster/minibt/tree/main/tutorials) 目录:
|
|
374
|
+
|
|
375
|
+
| 功能 | 示例文件 |
|
|
376
|
+
| ----------------------------------------------- | ---------------------------------------- |
|
|
377
|
+
| 信号回测与止损止盈(20 种组合) | `signal_stop.py` |
|
|
378
|
+
| 信号级参数优化(网格/Optuna) | `signal_optimize.py` |
|
|
379
|
+
| 策略级参数优化 | `optstrategy.py` |
|
|
380
|
+
| 自定义止损(ATR 动态止损) | `stop.py`、`stop_params.py` |
|
|
381
|
+
| K 线回放 | `strategy_replay.py` |
|
|
382
|
+
| 实盘三种模式 | `live_trading_modes.py`、`live_chart.py` |
|
|
383
|
+
| 多策略加载 | `load_strategies_from_dir.py` |
|
|
384
|
+
| 指标交易逻辑 | `indicator_step.py` |
|
|
385
|
+
| 多指标策略 | `multi_indicator_strategies.py` |
|
|
386
|
+
| 经典策略库(CCI/RSI/Hull/VPT/随机森林等 17 个) | `strategy/` |
|
|
387
|
+
|
|
388
|
+
## 📚 相关资源
|
|
389
|
+
|
|
390
|
+
- **在线教程**:[https://www.minibt.cn](https://www.minibt.cn)
|
|
391
|
+
- **更新日志**:[https://www.minibt.cn/changelog/](https://www.minibt.cn/changelog/)
|
|
392
|
+
- **GitHub**:[https://github.com/MiniBtMaster/minibt](https://github.com/MiniBtMaster/minibt)
|
|
393
|
+
- **PyPI**:[https://pypi.org/project/minibt/](https://pypi.org/project/minibt/)
|
|
394
|
+
- **作者**: owen | **邮箱**: 407841129@qq.com
|
|
395
|
+
|
|
396
|
+
## 📄 许可证
|
|
397
|
+
|
|
398
|
+
本项目采用 [MIT](https://github.com/MiniBtMaster/minibt/blob/main/LICENSE) 许可证。
|