Skip to content

Commit 7a4b0b7

Browse files
committed
[Mod] 初始化项目
1 parent 74e7bac commit 7a4b0b7

File tree

10 files changed

+959
-4
lines changed

10 files changed

+959
-4
lines changed

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
exclude = build,__pycache__,__init__.py
3+
ignore =
4+
E501 line too long, fixed by black
5+
W503 line break before binary operator

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
MIT License
1+
The MIT License (MIT)
22

3-
Copyright (c) 2021 vn.py
3+
Copyright (c) 2015-present, Xiaoyou Chen
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1-
# vnpy_datamanager
2-
vn.py框架的数据管理模块
1+
# vn.py框架的数据管理模块
2+
3+
<p align="center">
4+
<img src ="https://vnpy.oss-cn-shanghai.aliyuncs.com/vnpy-logo.png"/>
5+
</p>
6+
7+
<p align="center">
8+
<img src ="https://img.shields.io/badge/version-1.0.0-blueviolet.svg"/>
9+
<img src ="https://img.shields.io/badge/platform-windows|linux|macos-yellow.svg"/>
10+
<img src ="https://img.shields.io/badge/python-3.7-blue.svg" />
11+
<img src ="https://img.shields.io/github/license/vnpy/vnpy.svg?color=orange"/>
12+
</p>
13+
14+
## 说明
15+
16+
通过UI界面直观查询数据库中已有的数据概况,选择任意时间段数据查看字段细节,支持CSV文件的数据导入和导出。
17+
18+
## 安装
19+
20+
安装需要基于2.4.0版本以上的[VN Studio](https://www.vnpy.com)
21+
22+
直接使用pip命令:
23+
24+
```
25+
pip install vnpy_datamanager
26+
```
27+
28+
下载解压后在cmd中运行
29+
30+
```
31+
python setup.py install
32+
```

setup.cfg

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[metadata]
2+
name = vnpy_datamanager
3+
version = 1.0.0
4+
url = https://www.vnpy.com
5+
license = MIT
6+
author = Xiaoyou Chen
7+
author_email = xiaoyou.chen@mail.vnpy.com
8+
description = Data management application for vn.py quant trading framework.
9+
long_description = file: README.md
10+
long_description_content_type = text/markdown
11+
keywords =
12+
quant
13+
quantitative
14+
investment
15+
trading
16+
algotrading
17+
classifiers =
18+
Development Status :: 5 - Production/Stable
19+
Operating System :: OS Independent
20+
Programming Language :: Python :: 3
21+
Programming Language :: Python :: 3.7
22+
Programming Language :: Python :: 3.8
23+
Programming Language :: Python :: 3.9
24+
Programming Language :: Python :: 3.10
25+
Topic :: Office/Business :: Financial :: Investment
26+
Programming Language :: Python :: Implementation :: CPython
27+
License :: OSI Approved :: MIT License
28+
Natural Language :: Chinese (Simplified)
29+
30+
[options]
31+
packages = find:
32+
zip_safe = False
33+
install_requires =
34+
pandas

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from setuptools import setup
2+
3+
4+
setup()

vnpy_datamanager/__init__.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2015-present, Xiaoyou Chen
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+
23+
from pathlib import Path
24+
25+
from vnpy.trader.app import BaseApp
26+
27+
from .engine import APP_NAME, ManagerEngine
28+
29+
30+
class DataManagerApp(BaseApp):
31+
""""""
32+
33+
app_name = APP_NAME
34+
app_module = __module__
35+
app_path = Path(__file__).parent
36+
display_name = "数据管理"
37+
engine_class = ManagerEngine
38+
widget_name = "ManagerWidget"
39+
icon_name = "manager.ico"

vnpy_datamanager/engine.py

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
import csv
2+
from datetime import datetime
3+
from typing import List, Tuple
4+
5+
from pytz import timezone
6+
7+
from vnpy.trader.database import BarOverview, DB_TZ
8+
from vnpy.trader.engine import BaseEngine, MainEngine, EventEngine
9+
from vnpy.trader.constant import Interval, Exchange
10+
from vnpy.trader.object import BarData, HistoryRequest
11+
from vnpy.trader.rqdata import rqdata_client
12+
from vnpy.trader.database import database_manager
13+
14+
15+
APP_NAME = "DataManager"
16+
17+
18+
class ManagerEngine(BaseEngine):
19+
""""""
20+
21+
def __init__(
22+
self,
23+
main_engine: MainEngine,
24+
event_engine: EventEngine,
25+
):
26+
""""""
27+
super().__init__(main_engine, event_engine, APP_NAME)
28+
29+
def import_data_from_csv(
30+
self,
31+
file_path: str,
32+
symbol: str,
33+
exchange: Exchange,
34+
interval: Interval,
35+
tz_name: str,
36+
datetime_head: str,
37+
open_head: str,
38+
high_head: str,
39+
low_head: str,
40+
close_head: str,
41+
volume_head: str,
42+
open_interest_head: str,
43+
datetime_format: str
44+
) -> Tuple:
45+
""""""
46+
with open(file_path, "rt") as f:
47+
buf = [line.replace("\0", "") for line in f]
48+
49+
reader = csv.DictReader(buf, delimiter=",")
50+
51+
bars = []
52+
start = None
53+
count = 0
54+
tz = timezone(tz_name)
55+
56+
for item in reader:
57+
if datetime_format:
58+
dt = datetime.strptime(item[datetime_head], datetime_format)
59+
else:
60+
dt = datetime.fromisoformat(item[datetime_head])
61+
dt = tz.localize(dt)
62+
63+
open_interest = item.get(open_interest_head, 0)
64+
65+
bar = BarData(
66+
symbol=symbol,
67+
exchange=exchange,
68+
datetime=dt,
69+
interval=interval,
70+
volume=float(item[volume_head]),
71+
open_price=float(item[open_head]),
72+
high_price=float(item[high_head]),
73+
low_price=float(item[low_head]),
74+
close_price=float(item[close_head]),
75+
open_interest=float(open_interest),
76+
gateway_name="DB",
77+
)
78+
79+
bars.append(bar)
80+
81+
# do some statistics
82+
count += 1
83+
if not start:
84+
start = bar.datetime
85+
86+
# insert into database
87+
database_manager.save_bar_data(bars)
88+
89+
end = bar.datetime
90+
return start, end, count
91+
92+
def output_data_to_csv(
93+
self,
94+
file_path: str,
95+
symbol: str,
96+
exchange: Exchange,
97+
interval: Interval,
98+
start: datetime,
99+
end: datetime
100+
) -> bool:
101+
""""""
102+
bars = self.load_bar_data(symbol, exchange, interval, start, end)
103+
104+
fieldnames = [
105+
"symbol",
106+
"exchange",
107+
"datetime",
108+
"open",
109+
"high",
110+
"low",
111+
"close",
112+
"volume",
113+
"open_interest"
114+
]
115+
116+
try:
117+
with open(file_path, "w") as f:
118+
writer = csv.DictWriter(f, fieldnames=fieldnames, lineterminator="\n")
119+
writer.writeheader()
120+
121+
for bar in bars:
122+
d = {
123+
"symbol": bar.symbol,
124+
"exchange": bar.exchange.value,
125+
"datetime": bar.datetime.strftime("%Y-%m-%d %H:%M:%S"),
126+
"open": bar.open_price,
127+
"high": bar.high_price,
128+
"low": bar.low_price,
129+
"close": bar.close_price,
130+
"volume": bar.volume,
131+
"open_interest": bar.open_interest,
132+
}
133+
writer.writerow(d)
134+
135+
return True
136+
except PermissionError:
137+
return False
138+
139+
def get_bar_overview(self) -> List[BarOverview]:
140+
""""""
141+
return database_manager.get_bar_overview()
142+
143+
def load_bar_data(
144+
self,
145+
symbol: str,
146+
exchange: Exchange,
147+
interval: Interval,
148+
start: datetime,
149+
end: datetime
150+
) -> List[BarData]:
151+
""""""
152+
bars = database_manager.load_bar_data(
153+
symbol,
154+
exchange,
155+
interval,
156+
start,
157+
end
158+
)
159+
160+
return bars
161+
162+
def delete_bar_data(
163+
self,
164+
symbol: str,
165+
exchange: Exchange,
166+
interval: Interval
167+
) -> int:
168+
""""""
169+
count = database_manager.delete_bar_data(
170+
symbol,
171+
exchange,
172+
interval
173+
)
174+
175+
return count
176+
177+
def download_bar_data(
178+
self,
179+
symbol: str,
180+
exchange: Exchange,
181+
interval: str,
182+
start: datetime
183+
) -> int:
184+
"""
185+
Query bar data from RQData.
186+
"""
187+
req = HistoryRequest(
188+
symbol=symbol,
189+
exchange=exchange,
190+
interval=Interval(interval),
191+
start=start,
192+
end=datetime.now(DB_TZ)
193+
)
194+
195+
vt_symbol = f"{symbol}.{exchange.value}"
196+
contract = self.main_engine.get_contract(vt_symbol)
197+
198+
# If history data provided in gateway, then query
199+
if contract and contract.history_data:
200+
data = self.main_engine.query_history(
201+
req, contract.gateway_name
202+
)
203+
# Otherwise use RQData to query data
204+
else:
205+
if not rqdata_client.inited:
206+
rqdata_client.init()
207+
208+
data = rqdata_client.query_history(req)
209+
210+
if data:
211+
database_manager.save_bar_data(data)
212+
return(len(data))
213+
214+
return 0
215+
216+
def download_tick_data(
217+
self,
218+
symbol: str,
219+
exchange: Exchange,
220+
start: datetime
221+
) -> int:
222+
"""
223+
Query tick data from RQData.
224+
"""
225+
req = HistoryRequest(
226+
symbol=symbol,
227+
exchange=exchange,
228+
start=start,
229+
end=datetime.now(DB_TZ)
230+
)
231+
232+
if not rqdata_client.inited:
233+
rqdata_client.init()
234+
235+
data = rqdata_client.query_tick_history(req)
236+
237+
if data:
238+
database_manager.save_tick_data(data)
239+
return(len(data))
240+
241+
return 0

vnpy_datamanager/ui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .widget import ManagerWidget

vnpy_datamanager/ui/manager.ico

61.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)