From 19e27ea031c716b02ce3653ca7038da0a273276a Mon Sep 17 00:00:00 2001 From: nanoric Date: Wed, 17 Apr 2019 23:16:57 -0400 Subject: [PATCH] [Add] get_newest_bar_data in database_mongo.py [Add] get_newest_tick_data in database_mongo.py --- vnpy/trader/database/database_mongo.py | 42 +++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/vnpy/trader/database/database_mongo.py b/vnpy/trader/database/database_mongo.py index cdf68000..7f5bcb90 100644 --- a/vnpy/trader/database/database_mongo.py +++ b/vnpy/trader/database/database_mongo.py @@ -1,6 +1,6 @@ from datetime import datetime from enum import Enum -from typing import Sequence +from typing import Sequence, Optional from mongoengine import DateTimeField, Document, FloatField, StringField, connect @@ -54,8 +54,10 @@ class DbBarData(Document): meta = { "indexes": [ - {"fields": ("datetime", "interval", "symbol", - "exchange"), "unique": True} + { + "fields": ("datetime", "interval", "symbol", "exchange"), + "unique": True, + } ] } @@ -145,8 +147,14 @@ class DbTickData(Document): ask_volume_4: float = FloatField() ask_volume_5: float = FloatField() - meta = {"indexes": [ - {"fields": ("datetime", "symbol", "exchange"), "unique": True}]} + meta = { + "indexes": [ + { + "fields": ("datetime", "symbol", "exchange"), + "unique": True, + } + ], + } @staticmethod def from_tick(tick: TickData): @@ -305,3 +313,27 @@ class MongoManager(BaseDatabaseManager): symbol=d.symbol, exchange=d.exchange.value, datetime=d.datetime ).update_one(upsert=True, **updates) ) + + def get_newest_bar_data( + self, symbol: str, exchange: "Exchange", interval: "Interval" + ) -> Optional["BarData"]: + s = ( + DbBarData.objects(symbol=symbol, exchange=exchange.value) + .order_by("-datetime") + .first() + ) + if len(s): + return list(s)[0] + return None + + def get_newest_tick_data( + self, symbol: str, exchange: "Exchange" + ) -> Optional["TickData"]: + s = ( + DbTickData.objects(symbol=symbol, exchange=exchange.value) + .order_by("-datetime") + .first() + ) + if len(s): + return list(s)[0] + return None