Skip to content

Commit 19a5f33

Browse files
committed
feat: translated
1 parent 3937404 commit 19a5f33

File tree

3 files changed

+73
-90
lines changed

3 files changed

+73
-90
lines changed

CHANGELOG.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# 1.0.2版本
1+
# Version 1.0.2
22

3-
1. 采用批量方式写入数据,提高效率
3+
1. Use batch writing to improve efficiency
44

5-
# 1.0.1版本
5+
# Version 1.0.1
66

7-
1. 优化代码中的类型声明信息
8-
2. 增加对Tick汇总数据的支持
9-
3. 增加对流式写入参数的支持
7+
1. Optimize type declaration information in code
8+
2. Add support for Tick summary data
9+
3. Add support for streaming write parameters

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# VeighNa框架的PostgreSQL数据库接口
1+
# PostgreSQL Database Interface for VeighNa Framework
22

33
<p align="center">
44
<img src ="https://vnpy.oss-cn-shanghai.aliyuncs.com/vnpy-logo.png"/>
@@ -10,29 +10,29 @@
1010
<img src ="https://img.shields.io/badge/python-3.7|3.8|3.9|3.10-blue.svg" />
1111
</p>
1212

13-
## 说明
13+
## Description
1414

15-
基于peewee开发的PostgreSQL数据库接口。
15+
Based on the PostgreSQL database interface developed by peewee.
1616

17-
## 使用
17+
## Use
1818

19-
### 全局配置
19+
### Global Configuration
2020

21-
在VeighNa中使用PostgreSQL时,需要在全局配置中填写以下字段信息:
21+
To use PostgreSQL in VeighNa, you need to fill in the following field information in the global configuration:
2222

23-
|名称|含义|必填|举例|
23+
|name|meaning|required|examples|
2424
|---------|----|---|---|
25-
|database.name|名称||postgresql|
26-
|database.host|地址||localhost|
27-
|database.port|端口||5432|
28-
|database.database|实例||vnpy|
29-
|database.user|用户名||postgres|
30-
|database.password|密码||123456|
25+
|database.name|Name|Yes|postgresql|
26+
|database.host|Address|Yes|localhost|
27+
|database.port|Port|Yes|5432|
28+
|database.database|Instance|Yes|vnpy|
29+
|database.user|Username|Yes|postgres|
30+
|database.password|Password|Yes|123456|
3131

32-
请注意,VeighNa不会主动为关系型数据库创建数据库,所以请确保你所填的database.database字段对应的数据库已经创建好了。若未创建数据库,请手动连上数据库并创建。
32+
Please note that VeighNa does not actively create databases for relational databases, so please make sure that the database corresponding to the database.database field you filled in has been created. If no database has been created, please connect to it manually and create it.
3333

34-
### 创建实例
34+
### Creating an instance
3535

36-
VeighNa不会主动为PostgreSQL数据库创建实例,所以使用前请确保database.database字段中填写的的数据库实例已经创建了。
36+
VeighNa does not create instances of PostgreSQL databases on its own initiative, so please make sure that the instance of the database you filled in the database.database field has been created before using it.
3737

38-
若实例尚未创建,可以使用【pgAdmin 4】客户端的【Create Database】进行操作。
38+
If an instance has not been created, you can use [Create Database] in the [pgAdmin 4] client.

vnpy_postgresql/postgresql_database.py

Lines changed: 50 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
BarOverview,
2323
TickOverview,
2424
DB_TZ,
25-
convert_tz
25+
convert_tz,
2626
)
2727
from vnpy.trader.setting import SETTINGS
2828

@@ -33,12 +33,12 @@
3333
password=SETTINGS["database.password"],
3434
host=SETTINGS["database.host"],
3535
port=SETTINGS["database.port"],
36-
autorollback=True
36+
autorollback=True,
3737
)
3838

3939

4040
class DbBarData(Model):
41-
"""K线数据表映射对象"""
41+
"""K-Line Data Table Mapping Objects"""
4242

4343
id: AutoField = AutoField()
4444

@@ -61,7 +61,7 @@ class Meta:
6161

6262

6363
class DbTickData(Model):
64-
"""TICK数据表映射对象"""
64+
"""TICK data table mapping object"""
6565

6666
id: AutoField = AutoField()
6767

@@ -115,7 +115,7 @@ class Meta:
115115

116116

117117
class DbBarOverview(Model):
118-
"""K线汇总数据表映射对象"""
118+
"""K-Line Summary Data Table Mapping Objects"""
119119

120120
id: AutoField = AutoField()
121121

@@ -132,7 +132,7 @@ class Meta:
132132

133133

134134
class DbTickOverview(Model):
135-
"""Tick汇总数据表映射对象"""
135+
"""Tick Summary Data Table Mapping Object"""
136136

137137
id: AutoField = AutoField()
138138

@@ -148,7 +148,7 @@ class Meta:
148148

149149

150150
class PostgresqlDatabase(BaseDatabase):
151-
"""PostgreSQL数据库接口"""
151+
"""PostgreSQL Database Interface"""
152152

153153
def __init__(self) -> None:
154154
""""""
@@ -157,14 +157,14 @@ def __init__(self) -> None:
157157
self.db.create_tables([DbBarData, DbTickData, DbBarOverview, DbTickOverview])
158158

159159
def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
160-
"""保存K线数据"""
161-
# 读取主键参数
160+
"""Save K-Line Data"""
161+
# Read the primary key parameters
162162
bar: BarData = bars[0]
163163
symbol: str = bar.symbol
164164
exchange: Exchange = bar.exchange
165165
interval: Interval = bar.interval
166166

167-
# 将BarData数据转换为字典,并调整时区
167+
# Converting BarData data to a dictionary and adjusting the time zone
168168
data: list = []
169169

170170
for bar in bars:
@@ -177,7 +177,7 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
177177
d.pop("vt_symbol")
178178
data.append(d)
179179

180-
# 使用upsert操作将数据更新到数据库中 chunked批量操作加快速度
180+
# Updating data to the database using the upsert operation chunked batch operation to speed it up
181181
with self.db.atomic():
182182
for c in chunked(data, 100):
183183
DbBarData.insert_many(c).on_conflict(
@@ -188,7 +188,7 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
188188
DbBarData.open_price: DbBarData.open_price,
189189
DbBarData.high_price: DbBarData.high_price,
190190
DbBarData.low_price: DbBarData.low_price,
191-
DbBarData.close_price: DbBarData.close_price
191+
DbBarData.close_price: DbBarData.close_price,
192192
},
193193
conflict_target=(
194194
DbBarData.symbol,
@@ -198,7 +198,7 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
198198
),
199199
).execute()
200200

201-
# 更新K线汇总数据
201+
# Updated K-line summary data
202202
overview: DbBarOverview = DbBarOverview.get_or_none(
203203
DbBarOverview.symbol == symbol,
204204
DbBarOverview.exchange == exchange.value,
@@ -232,13 +232,13 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
232232
return True
233233

234234
def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
235-
"""保存TICK数据"""
236-
# 读取主键参数
235+
"""Save TICK data"""
236+
# Read the primary key parameters
237237
tick: TickData = ticks[0]
238238
symbol: str = tick.symbol
239239
exchange: Exchange = tick.exchange
240240

241-
# 将TickData数据转换为字典,并调整时区
241+
# Converting TickData data to a dictionary and adjusting the time zone
242242
data: list = []
243243

244244
for tick in ticks:
@@ -250,7 +250,7 @@ def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
250250
d.pop("vt_symbol")
251251
data.append(d)
252252

253-
# 使用upsert操作将数据更新到数据库中
253+
# Updating data into the database using the upsert operation
254254
with self.db.atomic():
255255
for d in data:
256256
DbTickData.insert(d).on_conflict(
@@ -259,8 +259,6 @@ def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
259259
DbTickData.symbol,
260260
DbTickData.exchange,
261261
DbTickData.datetime,
262-
263-
264262
),
265263
).execute()
266264

@@ -308,7 +306,7 @@ def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
308306
),
309307
).execute()
310308

311-
# 更新Tick汇总数据
309+
# Update Tick summary data
312310
overview: DbTickOverview = DbTickOverview.get_or_none(
313311
DbTickOverview.symbol == symbol,
314312
DbTickOverview.exchange == exchange.value,
@@ -329,8 +327,7 @@ def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
329327
overview.end = max(ticks[-1].datetime, overview.end)
330328

331329
s: ModelSelect = DbTickData.select().where(
332-
(DbTickData.symbol == symbol)
333-
& (DbTickData.exchange == exchange.value)
330+
(DbTickData.symbol == symbol) & (DbTickData.exchange == exchange.value)
334331
)
335332
overview.count = s.count()
336333

@@ -344,17 +341,19 @@ def load_bar_data(
344341
exchange: Exchange,
345342
interval: Interval,
346343
start: datetime,
347-
end: datetime
344+
end: datetime,
348345
) -> List[BarData]:
349-
"""读取K线数据"""
346+
"""Read K-line data"""
350347
s: ModelSelect = (
351-
DbBarData.select().where(
348+
DbBarData.select()
349+
.where(
352350
(DbBarData.symbol == symbol)
353351
& (DbBarData.exchange == exchange.value)
354352
& (DbBarData.interval == interval.value)
355353
& (DbBarData.datetime >= start)
356354
& (DbBarData.datetime <= end)
357-
).order_by(DbBarData.datetime)
355+
)
356+
.order_by(DbBarData.datetime)
358357
)
359358

360359
bars: List[BarData] = []
@@ -371,27 +370,25 @@ def load_bar_data(
371370
high_price=db_bar.high_price,
372371
low_price=db_bar.low_price,
373372
close_price=db_bar.close_price,
374-
gateway_name="DB"
373+
gateway_name="DB",
375374
)
376375
bars.append(bar)
377376

378377
return bars
379378

380379
def load_tick_data(
381-
self,
382-
symbol: str,
383-
exchange: Exchange,
384-
start: datetime,
385-
end: datetime
380+
self, symbol: str, exchange: Exchange, start: datetime, end: datetime
386381
) -> List[TickData]:
387-
"""读取TICK数据"""
382+
"""Read TICK data"""
388383
s: ModelSelect = (
389-
DbTickData.select().where(
384+
DbTickData.select()
385+
.where(
390386
(DbTickData.symbol == symbol)
391387
& (DbTickData.exchange == exchange.value)
392388
& (DbTickData.datetime >= start)
393389
& (DbTickData.datetime <= end)
394-
).order_by(DbTickData.datetime)
390+
)
391+
.order_by(DbTickData.datetime)
395392
)
396393

397394
ticks: List[TickData] = []
@@ -433,27 +430,24 @@ def load_tick_data(
433430
ask_volume_4=db_tick.ask_volume_4,
434431
ask_volume_5=db_tick.ask_volume_5,
435432
localtime=db_tick.localtime,
436-
gateway_name="DB"
433+
gateway_name="DB",
437434
)
438435
ticks.append(tick)
439436

440437
return ticks
441438

442439
def delete_bar_data(
443-
self,
444-
symbol: str,
445-
exchange: Exchange,
446-
interval: Interval
440+
self, symbol: str, exchange: Exchange, interval: Interval
447441
) -> int:
448-
"""删除K线数据"""
442+
"""Delete K-line data"""
449443
d: ModelDelete = DbBarData.delete().where(
450444
(DbBarData.symbol == symbol)
451445
& (DbBarData.exchange == exchange.value)
452446
& (DbBarData.interval == interval.value)
453447
)
454448
count: int = d.execute()
455449

456-
# 删除K线汇总数据
450+
# Delete K-line summary data
457451
d2: ModelDelete = DbBarOverview.delete().where(
458452
(DbBarOverview.symbol == symbol)
459453
& (DbBarOverview.exchange == exchange.value)
@@ -462,19 +456,14 @@ def delete_bar_data(
462456
d2.execute()
463457
return count
464458

465-
def delete_tick_data(
466-
self,
467-
symbol: str,
468-
exchange: Exchange
469-
) -> int:
470-
"""删除TICK数据"""
459+
def delete_tick_data(self, symbol: str, exchange: Exchange) -> int:
460+
"""Delete TICK data"""
471461
d: ModelDelete = DbTickData.delete().where(
472-
(DbTickData.symbol == symbol)
473-
& (DbTickData.exchange == exchange.value)
462+
(DbTickData.symbol == symbol) & (DbTickData.exchange == exchange.value)
474463
)
475464
count: int = d.execute()
476465

477-
# 删除Tick汇总数据
466+
# Delete Tick Summary Data
478467
d2: ModelDelete = DbTickOverview.delete().where(
479468
(DbTickOverview.symbol == symbol)
480469
& (DbTickOverview.exchange == exchange.value)
@@ -484,8 +473,8 @@ def delete_tick_data(
484473
return count
485474

486475
def get_bar_overview(self) -> List[BarOverview]:
487-
"""查询数据库中的K线汇总信息"""
488-
# 如果已有K线,但缺失汇总信息,则执行初始化
476+
"""Query the K-line summary information in the database"""
477+
# If there is already a K-line, but summary information is missing, perform initialization
489478
data_count: int = DbBarData.select().count()
490479
overview_count: int = DbBarOverview.select().count()
491480
if data_count and not overview_count:
@@ -500,7 +489,7 @@ def get_bar_overview(self) -> List[BarOverview]:
500489
return overviews
501490

502491
def get_tick_overview(self) -> List[TickOverview]:
503-
"""查询数据库中的Tick汇总信息"""
492+
"""Query the database for Tick summary information"""
504493
s: ModelSelect = DbTickOverview.select()
505494
overviews: list = []
506495
for overview in s:
@@ -509,19 +498,13 @@ def get_tick_overview(self) -> List[TickOverview]:
509498
return overviews
510499

511500
def init_bar_overview(self) -> None:
512-
"""初始化数据库中的K线汇总信息"""
513-
s: ModelSelect = (
514-
DbBarData.select(
515-
DbBarData.symbol,
516-
DbBarData.exchange,
517-
DbBarData.interval,
518-
fn.COUNT(DbBarData.id).alias("count")
519-
).group_by(
520-
DbBarData.symbol,
521-
DbBarData.exchange,
522-
DbBarData.interval
523-
)
524-
)
501+
"""Initialize the K-line summary information in the database"""
502+
s: ModelSelect = DbBarData.select(
503+
DbBarData.symbol,
504+
DbBarData.exchange,
505+
DbBarData.interval,
506+
fn.COUNT(DbBarData.id).alias("count"),
507+
).group_by(DbBarData.symbol, DbBarData.exchange, DbBarData.interval)
525508

526509
for data in s:
527510
overview: DbBarOverview = DbBarOverview()

0 commit comments

Comments
 (0)