18
18
from vnpy .trader .database import (
19
19
BaseDatabase ,
20
20
BarOverview ,
21
+ TickOverview ,
21
22
DB_TZ ,
22
23
convert_tz
23
24
)
@@ -128,16 +129,32 @@ class Meta:
128
129
indexes = ((("symbol" , "exchange" , "interval" ), True ),)
129
130
130
131
132
+ class DbTickOverview (Model ):
133
+ """Tick汇总数据表映射对象"""
134
+
135
+ id : AutoField = AutoField ()
136
+
137
+ symbol : str = CharField ()
138
+ exchange : str = CharField ()
139
+ count : int = IntegerField ()
140
+ start : datetime = DateTimeField ()
141
+ end : datetime = DateTimeField ()
142
+
143
+ class Meta :
144
+ database : PeeweePostgresqlDatabase = db
145
+ indexes : tuple = ((("symbol" , "exchange" ), True ),)
146
+
147
+
131
148
class PostgresqlDatabase (BaseDatabase ):
132
149
"""PostgreSQL数据库接口"""
133
150
134
151
def __init__ (self ) -> None :
135
152
""""""
136
153
self .db = db
137
154
self .db .connect ()
138
- self .db .create_tables ([DbBarData , DbTickData , DbBarOverview ])
155
+ self .db .create_tables ([DbBarData , DbTickData , DbBarOverview , DbTickOverview ])
139
156
140
- def save_bar_data (self , bars : List [BarData ]) -> bool :
157
+ def save_bar_data (self , bars : List [BarData ], stream : bool = False ) -> bool :
141
158
"""保存K线数据"""
142
159
# 读取主键参数
143
160
bar = bars [0 ]
@@ -186,6 +203,9 @@ def save_bar_data(self, bars: List[BarData]) -> bool:
186
203
overview .start = bars [0 ].datetime
187
204
overview .end = bars [- 1 ].datetime
188
205
overview .count = len (bars )
206
+ elif stream :
207
+ overview .end = bars [- 1 ].datetime
208
+ overview .count += len (bars )
189
209
else :
190
210
overview .start = min (bars [0 ].datetime , overview .start )
191
211
overview .end = max (bars [- 1 ].datetime , overview .end )
@@ -201,8 +221,13 @@ def save_bar_data(self, bars: List[BarData]) -> bool:
201
221
202
222
return True
203
223
204
- def save_tick_data (self , ticks : List [TickData ]) -> bool :
224
+ def save_tick_data (self , ticks : List [TickData ], stream : bool = False ) -> bool :
205
225
"""保存TICK数据"""
226
+ # 读取主键参数
227
+ tick : TickData = ticks [0 ]
228
+ symbol : str = tick .symbol
229
+ exchange : Exchange = tick .exchange
230
+
206
231
# 将TickData数据转换为字典,并调整时区
207
232
data = []
208
233
@@ -227,6 +252,34 @@ def save_tick_data(self, ticks: List[TickData]) -> bool:
227
252
),
228
253
).execute ()
229
254
255
+ # 更新Tick汇总数据
256
+ overview : DbTickOverview = DbTickOverview .get_or_none (
257
+ DbTickOverview .symbol == symbol ,
258
+ DbTickOverview .exchange == exchange .value ,
259
+ )
260
+
261
+ if not overview :
262
+ overview : DbTickOverview = DbTickOverview ()
263
+ overview .symbol = symbol
264
+ overview .exchange = exchange .value
265
+ overview .start = ticks [0 ].datetime
266
+ overview .end = ticks [- 1 ].datetime
267
+ overview .count = len (ticks )
268
+ elif stream :
269
+ overview .end = ticks [- 1 ].datetime
270
+ overview .count += len (ticks )
271
+ else :
272
+ overview .start = min (ticks [0 ].datetime , overview .start )
273
+ overview .end = max (ticks [- 1 ].datetime , overview .end )
274
+
275
+ s : ModelSelect = DbTickData .select ().where (
276
+ (DbTickData .symbol == symbol )
277
+ & (DbTickData .exchange == exchange .value )
278
+ )
279
+ overview .count = s .count ()
280
+
281
+ overview .save ()
282
+
230
283
return True
231
284
232
285
def load_bar_data (
@@ -364,6 +417,14 @@ def delete_tick_data(
364
417
& (DbTickData .exchange == exchange .value )
365
418
)
366
419
count = d .execute ()
420
+
421
+ # 删除Tick汇总数据
422
+ d2 : ModelDelete = DbTickOverview .delete ().where (
423
+ (DbTickOverview .symbol == symbol )
424
+ & (DbTickOverview .exchange == exchange .value )
425
+ )
426
+ d2 .execute ()
427
+
367
428
return count
368
429
369
430
def get_bar_overview (self ) -> List [BarOverview ]:
@@ -382,6 +443,15 @@ def get_bar_overview(self) -> List[BarOverview]:
382
443
overviews .append (overview )
383
444
return overviews
384
445
446
+ def get_tick_overview (self ) -> List [TickOverview ]:
447
+ """查询数据库中的Tick汇总信息"""
448
+ s : ModelSelect = DbTickOverview .select ()
449
+ overviews : list = []
450
+ for overview in s :
451
+ overview .exchange = Exchange (overview .exchange )
452
+ overviews .append (overview )
453
+ return overviews
454
+
385
455
def init_bar_overview (self ) -> None :
386
456
"""初始化数据库中的K线汇总信息"""
387
457
s : ModelSelect = (
0 commit comments