vnpy-baostock-x 1.0.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.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 EddyXie
|
|
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,42 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: vnpy_baostock_x
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: vnpy_baostock这个库,不是vnpy官方的,好久不维护了,里面的数据异常处理,没有做好,下载数据的时候,容易报错,特此改进
|
|
5
|
+
Author-email: EddyXie <xieshufan@163.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: quant,stock,vnpy
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
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
|
+
Requires-Python: >=3.9
|
|
19
|
+
Requires-Dist: baostock>=0.9.1
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: build>=1.0; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
23
|
+
Requires-Dist: ruff>=0.1; extra == 'dev'
|
|
24
|
+
Requires-Dist: twine>=4.0; extra == 'dev'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# vnpy_baostock_x
|
|
28
|
+
|
|
29
|
+
[](https://pypi.org/project/your-project-name/)
|
|
30
|
+
[](https://pypi.org/project/your-project-name/)
|
|
31
|
+
[](https://opensource.org/licenses/MIT)
|
|
32
|
+
|
|
33
|
+
一句话说明这个项目是做什么的。
|
|
34
|
+
|
|
35
|
+
## 简介
|
|
36
|
+
|
|
37
|
+
vnpy_baostock这个库,不是vnpy官方的,好久不维护了,里面的数据异常处理,没有做好,下载数据的时候,容易报错,特此改进
|
|
38
|
+
|
|
39
|
+
## 安装
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install vnpy_baostock_x
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# vnpy_baostock_x
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/your-project-name/)
|
|
4
|
+
[](https://pypi.org/project/your-project-name/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
一句话说明这个项目是做什么的。
|
|
8
|
+
|
|
9
|
+
## 简介
|
|
10
|
+
|
|
11
|
+
vnpy_baostock这个库,不是vnpy官方的,好久不维护了,里面的数据异常处理,没有做好,下载数据的时候,容易报错,特此改进
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install vnpy_baostock_x
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "vnpy_baostock_x"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "vnpy_baostock这个库,不是vnpy官方的,好久不维护了,里面的数据异常处理,没有做好,下载数据的时候,容易报错,特此改进"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICEN[CS]E*"]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "EddyXie", email = "xieshufan@163.com" },
|
|
15
|
+
]
|
|
16
|
+
keywords = ["quant", "stock", "vnpy"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Programming Language :: Python :: 3",
|
|
22
|
+
"Programming Language :: Python :: 3.9",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
"Operating System :: OS Independent",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"baostock>=0.9.1",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
dev = [
|
|
34
|
+
"pytest>=7.0",
|
|
35
|
+
"ruff>=0.1",
|
|
36
|
+
"build>=1.0",
|
|
37
|
+
"twine>=4.0",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[tool.hatch.build.targets.wheel]
|
|
41
|
+
packages = ["src/vnpy_baostock_x"]
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
#
|
|
4
|
+
# Created by flytrap
|
|
5
|
+
# Created time: 2022/02/12
|
|
6
|
+
from datetime import datetime
|
|
7
|
+
from typing import Callable, List, Optional
|
|
8
|
+
|
|
9
|
+
import baostock as bs
|
|
10
|
+
from vnpy.trader.constant import Exchange, Interval
|
|
11
|
+
from vnpy.trader.datafeed import BaseDatafeed
|
|
12
|
+
from vnpy.trader.object import BarData, HistoryRequest
|
|
13
|
+
from vnpy.trader.setting import SETTINGS
|
|
14
|
+
from vnpy.trader.utility import round_to
|
|
15
|
+
|
|
16
|
+
# 数据频率映射
|
|
17
|
+
INTERVAL_VT2TS = {
|
|
18
|
+
Interval.MINUTE: "5", # 只支持5分钟
|
|
19
|
+
Interval.HOUR: "60",
|
|
20
|
+
Interval.DAILY: "d",
|
|
21
|
+
Interval.WEEKLY: "w",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
# 股票支持列表
|
|
25
|
+
STOCK_LIST = [
|
|
26
|
+
Exchange.SSE,
|
|
27
|
+
Exchange.SZSE,
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class BaoStockDatafeed(BaseDatafeed):
|
|
32
|
+
"""BaoStock数据服务接口"""
|
|
33
|
+
|
|
34
|
+
def __init__(self):
|
|
35
|
+
"""初始化"""
|
|
36
|
+
self.username: str = SETTINGS["datafeed.username"]
|
|
37
|
+
self.password: str = SETTINGS["datafeed.password"]
|
|
38
|
+
|
|
39
|
+
self.inited: bool = False
|
|
40
|
+
|
|
41
|
+
def init(self, output: Callable = print) -> bool:
|
|
42
|
+
"""初始化"""
|
|
43
|
+
if self.inited:
|
|
44
|
+
return True
|
|
45
|
+
|
|
46
|
+
bs.login()
|
|
47
|
+
self.inited = True
|
|
48
|
+
output("BaoStock数据服务连接成功")
|
|
49
|
+
|
|
50
|
+
return True
|
|
51
|
+
|
|
52
|
+
def __delete__(self):
|
|
53
|
+
bs.logout()
|
|
54
|
+
|
|
55
|
+
def query_bar_history(self, req: HistoryRequest, output: Callable = print) -> Optional[List[BarData]]:
|
|
56
|
+
"""查询k线数据"""
|
|
57
|
+
if not self.inited:
|
|
58
|
+
self.init()
|
|
59
|
+
|
|
60
|
+
symbol = req.symbol
|
|
61
|
+
exchange = req.exchange
|
|
62
|
+
interval = req.interval
|
|
63
|
+
start = req.start.strftime("%Y-%m-%d")
|
|
64
|
+
end = req.end.strftime("%Y-%m-%d")
|
|
65
|
+
bs_symbol = f"sz.{symbol}" if exchange == Exchange.SZSE else f"sh.{symbol}"
|
|
66
|
+
|
|
67
|
+
bs_interval = INTERVAL_VT2TS.get(interval)
|
|
68
|
+
if not bs_interval:
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
if interval in (Interval.MINUTE, Interval.HOUR):
|
|
72
|
+
fields = "time,code,open,high,low,close,volume,amount"
|
|
73
|
+
else:
|
|
74
|
+
fields = "date,code,open,high,low,close,volume,amount"
|
|
75
|
+
try:
|
|
76
|
+
rs = bs.query_history_k_data_plus(
|
|
77
|
+
bs_symbol,
|
|
78
|
+
fields,
|
|
79
|
+
start_date=start,
|
|
80
|
+
end_date=end,
|
|
81
|
+
frequency=bs_interval,
|
|
82
|
+
adjustflag="3",
|
|
83
|
+
)
|
|
84
|
+
except Exception as e:
|
|
85
|
+
return []
|
|
86
|
+
data: List[BarData] = []
|
|
87
|
+
|
|
88
|
+
while (rs.error_code == "0") & rs.next():
|
|
89
|
+
item = rs.get_row_data()
|
|
90
|
+
str_format = "%Y-%m-%d"
|
|
91
|
+
if interval in (Interval.MINUTE, Interval.HOUR):
|
|
92
|
+
str_format = "%Y%m%d%H%M%S%f"
|
|
93
|
+
dt = datetime.strptime(item[0], str_format)
|
|
94
|
+
bar: BarData = BarData(
|
|
95
|
+
symbol=symbol,
|
|
96
|
+
exchange=exchange,
|
|
97
|
+
interval=interval,
|
|
98
|
+
datetime=dt,
|
|
99
|
+
open_price=0 if item[2]=='' else round_to(item[2], 0.000001),
|
|
100
|
+
high_price=0 if item[3]=='' else round_to(item[3], 0.000001),
|
|
101
|
+
low_price=0 if item[4]=='' else round_to(item[4], 0.000001),
|
|
102
|
+
close_price=0 if item[5]=='' else round_to(item[5], 0.000001),
|
|
103
|
+
volume=0 if item[6]=='' else float(item[6]),
|
|
104
|
+
turnover=0 if item[7]=='' else float(item[7]),
|
|
105
|
+
open_interest=0,
|
|
106
|
+
gateway_name="BS",
|
|
107
|
+
)
|
|
108
|
+
data.append(bar)
|
|
109
|
+
|
|
110
|
+
return data
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
if __name__ == "__main__":
|
|
114
|
+
bsd = BaoStockDatafeed()
|
|
115
|
+
r = HistoryRequest(
|
|
116
|
+
symbol="sh.600000",
|
|
117
|
+
exchange=Exchange.SZSE,
|
|
118
|
+
start=datetime.strptime("2017-07-01", "%Y-%m-%d"),
|
|
119
|
+
end=datetime.strptime("2017-12-31", "%Y-%m-%d"),
|
|
120
|
+
interval=Interval.DAILY,
|
|
121
|
+
)
|
|
122
|
+
data = bsd.query_bar_history(r)
|
|
123
|
+
print(data)
|