-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 57e02c4
Showing
11 changed files
with
385 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: ci | ||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
permissions: | ||
contents: write | ||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: 3.x | ||
- run: echo "cache_id=$(date --utc '+%V')" >> $GITHUB_ENV | ||
- uses: actions/cache@v3 | ||
with: | ||
key: mkdocs-material-${{ env.cache_id }} | ||
path: .cache | ||
restore-keys: | | ||
mkdocs-material- | ||
- run: pip install mkdocs-material | ||
- run: mkdocs gh-deploy --force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## AKQuant | ||
|
||
AKQuant 是一个开源的量化投资教程项目,主要讲述 PyBroker 框架 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 使用数据源 | ||
|
||
建议使用国内的股票数据源 | ||
|
||
## 数据源的定义 | ||
|
||
## 数据源的扩展 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# PyBroker 介绍 | ||
|
||
配置文档:[点击进入](https://squidfunk.github.io/mkdocs-material/) | ||
|
||
PyBroker 项目 [GitHub 地址](https://github.com/edtechre/pybroker) | ||
|
||
PyBroker 英文文档地址:[点击进入英文文档](https://www.pybroker.com/en/latest/) | ||
|
||
PyBroker 中文文档地址:[点击进入中文文档](https://www.pybroker.com/zh_CN/latest/) | ||
|
||
## 安装 | ||
|
||
```shell | ||
pip install lib-pybroker | ||
``` | ||
|
||
## 环境配置 | ||
|
||
PyBroker 支持在 Windows、macOS 和 Linux 中 | ||
Python 3.9 及以上版本,建议使用 Python 3.11 及以上版本。 | ||
|
||
## 策略示例 | ||
|
||
``` py title="demo.py" linenums="1" hl_lines="2 7" | ||
# 导入所需的库和模块 | ||
import pybroker as pb | ||
from pybroker import Strategy, ExecContext | ||
from pybroker.ext.data import AKShare | ||
|
||
# 定义全局参数 "stock_code"(股票代码)、"percent"(持仓百分比)和 "stop_profit_pct"(止盈百分比) | ||
pb.param(name='stock_code', value='600000') | ||
pb.param(name='percent', value=1) | ||
pb.param(name='stop_loss_pct', value=10) | ||
pb.param(name='stop_profit_pct', value=10) | ||
|
||
# 初始化 AKShare 数据源 | ||
akshare = AKShare() | ||
|
||
# 使用 AKShare 数据源查询特定股票(由 "stock_code" 参数指定)在指定日期范围内的数据 | ||
df = akshare.query(symbols=[pb.param(name='stock_code')], start_date='20200131', end_date='20230228') | ||
|
||
|
||
# 定义交易策略:如果当前没有持有该股票,则买入股票,并设置止盈点位 | ||
def buy_with_stop_loss(ctx: ExecContext): | ||
pos = ctx.long_pos() | ||
if not pos: | ||
# 计算目标股票数量,根据 "percent" 参数确定应购买的股票数量 | ||
ctx.buy_shares = ctx.calc_target_shares(pb.param(name='percent')) | ||
ctx.hold_bars = 100 | ||
else: | ||
ctx.sell_shares = pos.shares | ||
# 设置止盈点位,根据 "stop_profit_pct" 参数确定止盈点位 | ||
ctx.stop_profit_pct = pb.param(name='stop_profit_pct') | ||
|
||
|
||
# 创建策略配置,初始资金为 500000 | ||
my_config = pb.StrategyConfig(initial_cash=500000) | ||
# 使用配置、数据源、起始日期、结束日期,以及刚才定义的交易策略创建策略对象 | ||
strategy = Strategy(akshare, start_date='20200131', end_date='20230228', config=my_config) | ||
# 添加执行策略,设置股票代码和要执行的函数 | ||
strategy.add_execution(fn=buy_with_stop_loss, symbols=[pb.param(name='stock_code')]) | ||
# 执行回测,并打印出回测结果的度量值(四舍五入到小数点后四位) | ||
result = strategy.backtest() | ||
print(result.metrics_df.round(4)) | ||
``` | ||
|
||
``` shell title="shell" linenums="1" hl_lines="2 7" | ||
Loading bar data... | ||
Loaded bar data: 0:00:00 | ||
Backtesting: 2020-01-31 00:00:00 to 2023-02-28 00:00:00 | ||
Loading bar data... | ||
Loaded bar data: 0:00:00 | ||
Test split: 2020-02-03 00:00:00 to 2023-02-28 00:00:00 | ||
100% (748 of 748) |######################| Elapsed Time: 0:00:00 Time: 0:00:00 | ||
Finished backtest: 0:00:03 | ||
name value | ||
0 trade_count 373.0000 | ||
1 initial_market_value 500000.0000 | ||
2 end_market_value 467328.0900 | ||
3 total_pnl -33322.7800 | ||
4 unrealized_pnl 650.8700 | ||
5 total_return_pct -6.6646 | ||
6 total_profit 530528.5100 | ||
7 total_loss -563851.2900 | ||
8 total_fees 0.0000 | ||
9 max_drawdown -113004.2700 | ||
10 max_drawdown_pct -20.2704 | ||
11 win_rate 45.9215 | ||
12 loss_rate 54.0785 | ||
13 winning_trades 152.0000 | ||
14 losing_trades 179.0000 | ||
15 avg_pnl -89.3372 | ||
16 avg_return_pct -0.0160 | ||
17 avg_trade_bars 1.0000 | ||
18 avg_profit 3490.3191 | ||
19 avg_profit_pct 0.6958 | ||
20 avg_winning_trade_bars 1.0000 | ||
21 avg_loss -3150.0072 | ||
22 avg_loss_pct -0.6241 | ||
23 avg_losing_trade_bars 1.0000 | ||
24 largest_win 31157.9400 | ||
25 largest_win_pct 5.9200 | ||
26 largest_win_bars 1.0000 | ||
27 largest_loss -12682.6000 | ||
28 largest_loss_pct -2.3100 | ||
29 largest_loss_bars 1.0000 | ||
30 max_wins 8.0000 | ||
31 max_losses 7.0000 | ||
32 sharpe -0.0132 | ||
33 sortino -0.0231 | ||
34 profit_factor 0.9638 | ||
35 ulcer_index 1.7639 | ||
36 upi -0.0039 | ||
37 equity_r2 0.5876 | ||
38 std_error 27448.1177 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# PyBroker 安装 | ||
|
||
PyBroker 目前支持在 Windows,macOS 和 Linux 中 Python 3.9 及以上版本,建议使用 Python 3.11 及以上版本。 | ||
|
||
> 此处需要注意 Windows 10 及以上版本 | ||
> 由于 PyBroker 需要安装的依赖库比较多,因此安装过程可能会比较慢,建议使用**清华镜像源**进行安装。 | ||
## PIP 安装 | ||
|
||
### PyPI 镜像源安装 | ||
|
||
``` shell | ||
pip install lib-pybroker -i https://pypi.org/simple | ||
``` | ||
|
||
### 清华镜像源安装 | ||
|
||
``` shell | ||
pip install lib-pybroker -i https://pypi.tuna.tsinghua.edu.cn/simple | ||
``` | ||
|
||
## PIP 升级 | ||
|
||
### PyPI 镜像源升级 | ||
|
||
``` shell | ||
pip install -U lib-pybroker -i https://pypi.org/simple | ||
``` | ||
|
||
### 清华镜像源升级 | ||
|
||
``` shell | ||
pip install -U lib-pybroker -i https://pypi.tuna.tsinghua.edu.cn/simple | ||
``` | ||
|
||
## 源码安装 | ||
|
||
目前 PyBroker 项目的源码托管在 GitHub 平台,可以通过以下命令下载源码并安装: | ||
|
||
``` shell | ||
git clone https://github.com/edtechre/pybroker | ||
cd pybroker | ||
python setup.py install | ||
``` | ||
|
||
## 查看版本 | ||
|
||
当成功安装 PyBroker 后,可以通过如下命令查看安装的版本: | ||
|
||
```python | ||
import pybroker as pb | ||
|
||
print(pb.__version__) | ||
``` | ||
|
||
结果显示: | ||
|
||
```shell | ||
1.1.29 | ||
``` | ||
|
||
## 依赖库 | ||
|
||
PyBroker 目前的依赖库比较多, | ||
可以通过官网的 [requirements.txt](https://github.com/edtechre/pybroker/blob/master/requirements.txt) | ||
查看最新的依赖: | ||
|
||
```shell | ||
akshare>=1.10.1 | ||
alpaca-py>=0.7.2 | ||
black>=22.10.0 | ||
diskcache>=5.4.0 | ||
flake8>=5.0.4 | ||
flake8-bugbear>=22.10.25 | ||
joblib>=1.2.0 | ||
jupyter>=1.0.0 | ||
matplotlib>=3.6.3 | ||
mypy>=0.982 | ||
nbsphinx>=0.8.11 | ||
numba>=0.56.3 | ||
numpy>=1.23.4 | ||
pandas>=1.5.1 | ||
progressbar2>=4.1.1 | ||
pytest>=7.2.0 | ||
pytest-cov>=4.0.0 | ||
pytest-instafail>=0.4.2 | ||
pytest-lazy-fixture>=0.6.3 | ||
pytest-randomly>=3.12.0 | ||
pytest-xdist>=3.0.2 | ||
scikit-learn>=1.2.1 | ||
Sphinx>=5.3.0 | ||
sphinx_rtd_theme>=1.1.1 | ||
sphinx-intl>=2.1.0 | ||
yfinance>=0.1.84 | ||
``` | ||
|
||
当然,也可以在本地环境中通过如下命令查看已安装的依赖库: | ||
|
||
```shell | ||
pip list | ||
``` | ||
|
||
结果显示: | ||
|
||
```shell | ||
Package Version | ||
-------------------------- --------- | ||
akshare 1.10.95 | ||
alpaca-py 0.8.2 | ||
appdirs 1.4.4 | ||
Babel 2.12.1 | ||
beautifulsoup4 4.12.2 | ||
certifi 2023.7.22 | ||
charset-normalizer 3.2.0 | ||
click 8.1.7 | ||
colorama 0.4.6 | ||
cssselect 1.2.0 | ||
decorator 5.1.1 | ||
diskcache 5.6.3 | ||
et-xmlfile 1.1.0 | ||
frozendict 2.3.8 | ||
ghp-import 2.1.0 | ||
html5lib 1.1 | ||
idna 3.4 | ||
Jinja2 3.1.2 | ||
joblib 1.3.2 | ||
jsonpath 0.82.2 | ||
lib-pybroker 1.1.29 | ||
llvmlite 0.40.1 | ||
lxml 4.9.3 | ||
Markdown 3.4.4 | ||
markdown2 2.4.10 | ||
MarkupSafe 2.1.3 | ||
mergedeep 1.3.4 | ||
mkdocs 1.5.2 | ||
mkdocs-material 9.2.6 | ||
mkdocs-material-extensions 1.1.1 | ||
msgpack 1.0.5 | ||
multitasking 0.0.11 | ||
numba 0.57.1 | ||
numpy 1.24.4 | ||
openpyxl 3.1.2 | ||
packaging 23.1 | ||
paginate 0.5.6 | ||
pandas 1.5.3 | ||
pathspec 0.11.2 | ||
pip 23.2.1 | ||
platformdirs 3.10.0 | ||
progressbar2 4.2.0 | ||
py-mini-racer 0.6.0 | ||
pydantic 1.10.12 | ||
Pygments 2.16.1 | ||
pymdown-extensions 10.2.1 | ||
pypinyin 0.49.0 | ||
pyquery 2.0.0 | ||
python-dateutil 2.8.2 | ||
python-utils 3.7.0 | ||
pytz 2023.3 | ||
PyYAML 6.0.1 | ||
pyyaml_env_tag 0.1 | ||
readtime 3.0.0 | ||
regex 2023.8.8 | ||
requests 2.31.0 | ||
setuptools 68.0.0 | ||
tabulate 0.9.0 | ||
tqdm 4.66.1 | ||
typing_extensions 4.7.1 | ||
urllib3 2.0.4 | ||
watchdog 3.0.0 | ||
webencodings 0.5.1 | ||
websockets 10.4 | ||
wheel 0.38.4 | ||
xlrd 2.0.1 | ||
yfinance 0.2.28 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
window.MathJax = { | ||
tex: { | ||
inlineMath: [["\\(", "\\)"]], | ||
displayMath: [["\\[", "\\]"]], | ||
processEscapes: true, | ||
processEnvironments: true | ||
}, | ||
options: { | ||
ignoreHtmlClass: ".*|", | ||
processHtmlClass: "arithmatex" | ||
} | ||
}; | ||
|
||
document$.subscribe(() => { | ||
MathJax.typesetPromise() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# 快速开始 | ||
|
||
快速运行一个策略,来进行学习 | ||
|
||
## 运行策略 | ||
|
||
## 查看结果 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import pybroker as pb | ||
|
||
print(pb.__version__) |
Oops, something went wrong.