22
22
BarOverview ,
23
23
TickOverview ,
24
24
DB_TZ ,
25
- convert_tz
25
+ convert_tz ,
26
26
)
27
27
from vnpy .trader .setting import SETTINGS
28
28
33
33
password = SETTINGS ["database.password" ],
34
34
host = SETTINGS ["database.host" ],
35
35
port = SETTINGS ["database.port" ],
36
- autorollback = True
36
+ autorollback = True ,
37
37
)
38
38
39
39
40
40
class DbBarData (Model ):
41
- """K线数据表映射对象 """
41
+ """K-Line Data Table Mapping Objects """
42
42
43
43
id : AutoField = AutoField ()
44
44
@@ -61,7 +61,7 @@ class Meta:
61
61
62
62
63
63
class DbTickData (Model ):
64
- """TICK数据表映射对象 """
64
+ """TICK data table mapping object """
65
65
66
66
id : AutoField = AutoField ()
67
67
@@ -115,7 +115,7 @@ class Meta:
115
115
116
116
117
117
class DbBarOverview (Model ):
118
- """K线汇总数据表映射对象 """
118
+ """K-Line Summary Data Table Mapping Objects """
119
119
120
120
id : AutoField = AutoField ()
121
121
@@ -132,7 +132,7 @@ class Meta:
132
132
133
133
134
134
class DbTickOverview (Model ):
135
- """Tick汇总数据表映射对象 """
135
+ """Tick Summary Data Table Mapping Object """
136
136
137
137
id : AutoField = AutoField ()
138
138
@@ -148,7 +148,7 @@ class Meta:
148
148
149
149
150
150
class PostgresqlDatabase (BaseDatabase ):
151
- """PostgreSQL数据库接口 """
151
+ """PostgreSQL Database Interface """
152
152
153
153
def __init__ (self ) -> None :
154
154
""""""
@@ -157,14 +157,14 @@ def __init__(self) -> None:
157
157
self .db .create_tables ([DbBarData , DbTickData , DbBarOverview , DbTickOverview ])
158
158
159
159
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
162
162
bar : BarData = bars [0 ]
163
163
symbol : str = bar .symbol
164
164
exchange : Exchange = bar .exchange
165
165
interval : Interval = bar .interval
166
166
167
- # 将BarData数据转换为字典,并调整时区
167
+ # Converting BarData data to a dictionary and adjusting the time zone
168
168
data : list = []
169
169
170
170
for bar in bars :
@@ -177,7 +177,7 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
177
177
d .pop ("vt_symbol" )
178
178
data .append (d )
179
179
180
- # 使用upsert操作将数据更新到数据库中 chunked批量操作加快速度
180
+ # Updating data to the database using the upsert operation chunked batch operation to speed it up
181
181
with self .db .atomic ():
182
182
for c in chunked (data , 100 ):
183
183
DbBarData .insert_many (c ).on_conflict (
@@ -188,7 +188,7 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
188
188
DbBarData .open_price : DbBarData .open_price ,
189
189
DbBarData .high_price : DbBarData .high_price ,
190
190
DbBarData .low_price : DbBarData .low_price ,
191
- DbBarData .close_price : DbBarData .close_price
191
+ DbBarData .close_price : DbBarData .close_price ,
192
192
},
193
193
conflict_target = (
194
194
DbBarData .symbol ,
@@ -198,7 +198,7 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
198
198
),
199
199
).execute ()
200
200
201
- # 更新K线汇总数据
201
+ # Updated K-line summary data
202
202
overview : DbBarOverview = DbBarOverview .get_or_none (
203
203
DbBarOverview .symbol == symbol ,
204
204
DbBarOverview .exchange == exchange .value ,
@@ -232,13 +232,13 @@ def save_bar_data(self, bars: List[BarData], stream: bool = False) -> bool:
232
232
return True
233
233
234
234
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
237
237
tick : TickData = ticks [0 ]
238
238
symbol : str = tick .symbol
239
239
exchange : Exchange = tick .exchange
240
240
241
- # 将TickData数据转换为字典,并调整时区
241
+ # Converting TickData data to a dictionary and adjusting the time zone
242
242
data : list = []
243
243
244
244
for tick in ticks :
@@ -250,7 +250,7 @@ def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
250
250
d .pop ("vt_symbol" )
251
251
data .append (d )
252
252
253
- # 使用upsert操作将数据更新到数据库中
253
+ # Updating data into the database using the upsert operation
254
254
with self .db .atomic ():
255
255
for d in data :
256
256
DbTickData .insert (d ).on_conflict (
@@ -259,8 +259,6 @@ def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
259
259
DbTickData .symbol ,
260
260
DbTickData .exchange ,
261
261
DbTickData .datetime ,
262
-
263
-
264
262
),
265
263
).execute ()
266
264
@@ -308,7 +306,7 @@ def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
308
306
),
309
307
).execute ()
310
308
311
- # 更新Tick汇总数据
309
+ # Update Tick summary data
312
310
overview : DbTickOverview = DbTickOverview .get_or_none (
313
311
DbTickOverview .symbol == symbol ,
314
312
DbTickOverview .exchange == exchange .value ,
@@ -329,8 +327,7 @@ def save_tick_data(self, ticks: List[TickData], stream: bool = False) -> bool:
329
327
overview .end = max (ticks [- 1 ].datetime , overview .end )
330
328
331
329
s : ModelSelect = DbTickData .select ().where (
332
- (DbTickData .symbol == symbol )
333
- & (DbTickData .exchange == exchange .value )
330
+ (DbTickData .symbol == symbol ) & (DbTickData .exchange == exchange .value )
334
331
)
335
332
overview .count = s .count ()
336
333
@@ -344,17 +341,19 @@ def load_bar_data(
344
341
exchange : Exchange ,
345
342
interval : Interval ,
346
343
start : datetime ,
347
- end : datetime
344
+ end : datetime ,
348
345
) -> List [BarData ]:
349
- """读取K线数据 """
346
+ """Read K-line data """
350
347
s : ModelSelect = (
351
- DbBarData .select ().where (
348
+ DbBarData .select ()
349
+ .where (
352
350
(DbBarData .symbol == symbol )
353
351
& (DbBarData .exchange == exchange .value )
354
352
& (DbBarData .interval == interval .value )
355
353
& (DbBarData .datetime >= start )
356
354
& (DbBarData .datetime <= end )
357
- ).order_by (DbBarData .datetime )
355
+ )
356
+ .order_by (DbBarData .datetime )
358
357
)
359
358
360
359
bars : List [BarData ] = []
@@ -371,27 +370,25 @@ def load_bar_data(
371
370
high_price = db_bar .high_price ,
372
371
low_price = db_bar .low_price ,
373
372
close_price = db_bar .close_price ,
374
- gateway_name = "DB"
373
+ gateway_name = "DB" ,
375
374
)
376
375
bars .append (bar )
377
376
378
377
return bars
379
378
380
379
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
386
381
) -> List [TickData ]:
387
- """读取TICK数据 """
382
+ """Read TICK data """
388
383
s : ModelSelect = (
389
- DbTickData .select ().where (
384
+ DbTickData .select ()
385
+ .where (
390
386
(DbTickData .symbol == symbol )
391
387
& (DbTickData .exchange == exchange .value )
392
388
& (DbTickData .datetime >= start )
393
389
& (DbTickData .datetime <= end )
394
- ).order_by (DbTickData .datetime )
390
+ )
391
+ .order_by (DbTickData .datetime )
395
392
)
396
393
397
394
ticks : List [TickData ] = []
@@ -433,27 +430,24 @@ def load_tick_data(
433
430
ask_volume_4 = db_tick .ask_volume_4 ,
434
431
ask_volume_5 = db_tick .ask_volume_5 ,
435
432
localtime = db_tick .localtime ,
436
- gateway_name = "DB"
433
+ gateway_name = "DB" ,
437
434
)
438
435
ticks .append (tick )
439
436
440
437
return ticks
441
438
442
439
def delete_bar_data (
443
- self ,
444
- symbol : str ,
445
- exchange : Exchange ,
446
- interval : Interval
440
+ self , symbol : str , exchange : Exchange , interval : Interval
447
441
) -> int :
448
- """删除K线数据 """
442
+ """Delete K-line data """
449
443
d : ModelDelete = DbBarData .delete ().where (
450
444
(DbBarData .symbol == symbol )
451
445
& (DbBarData .exchange == exchange .value )
452
446
& (DbBarData .interval == interval .value )
453
447
)
454
448
count : int = d .execute ()
455
449
456
- # 删除K线汇总数据
450
+ # Delete K-line summary data
457
451
d2 : ModelDelete = DbBarOverview .delete ().where (
458
452
(DbBarOverview .symbol == symbol )
459
453
& (DbBarOverview .exchange == exchange .value )
@@ -462,19 +456,14 @@ def delete_bar_data(
462
456
d2 .execute ()
463
457
return count
464
458
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"""
471
461
d : ModelDelete = DbTickData .delete ().where (
472
- (DbTickData .symbol == symbol )
473
- & (DbTickData .exchange == exchange .value )
462
+ (DbTickData .symbol == symbol ) & (DbTickData .exchange == exchange .value )
474
463
)
475
464
count : int = d .execute ()
476
465
477
- # 删除Tick汇总数据
466
+ # Delete Tick Summary Data
478
467
d2 : ModelDelete = DbTickOverview .delete ().where (
479
468
(DbTickOverview .symbol == symbol )
480
469
& (DbTickOverview .exchange == exchange .value )
@@ -484,8 +473,8 @@ def delete_tick_data(
484
473
return count
485
474
486
475
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
489
478
data_count : int = DbBarData .select ().count ()
490
479
overview_count : int = DbBarOverview .select ().count ()
491
480
if data_count and not overview_count :
@@ -500,7 +489,7 @@ def get_bar_overview(self) -> List[BarOverview]:
500
489
return overviews
501
490
502
491
def get_tick_overview (self ) -> List [TickOverview ]:
503
- """查询数据库中的Tick汇总信息 """
492
+ """Query the database for Tick summary information """
504
493
s : ModelSelect = DbTickOverview .select ()
505
494
overviews : list = []
506
495
for overview in s :
@@ -509,19 +498,13 @@ def get_tick_overview(self) -> List[TickOverview]:
509
498
return overviews
510
499
511
500
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 )
525
508
526
509
for data in s :
527
510
overview : DbBarOverview = DbBarOverview ()
0 commit comments