def QA_fetch_stock_realtime_adv(code=None, num=1, collections=DATABASE.get_collection('realtime_{}'.format(datetime.date.today()))): ''' 返回当日的上下五档, code可以是股票可以是list, num是每个股票获取的数量 :param code: :param num: :param collections: realtime_XXXX-XX-XX 每天实时时间 :return: DataFrame ''' if code is not None: # code 必须转换成list 去查询数据库 if isinstance(code, str): code = [code] elif isinstance(code, list): pass else: print( "QA Error QA_fetch_stock_realtime_adv parameter code is not List type or String type") items_from_collections = [item for item in collections.find( {'code': {'$in': code}}, limit=num*len(code), sort=[('datetime', pymongo.DESCENDING)])] if items_from_collections is None: print("QA Error QA_fetch_stock_realtime_adv find parameter code={} num={} collection={} return NOne".format( code, num, collections)) return data = pd.DataFrame(items_from_collections) data_set_index = data.set_index( ['datetime', 'code'], drop=False).drop(['_id'], axis=1) return data_set_index else: print("QA Error QA_fetch_stock_realtime_adv parameter code is None")
def QA_fetch_stock_realtime_adv(code=None, num=1, collections=DATABASE.get_collection( 'realtime_{}'.format( datetime.date.today()))): """ 返回当日的上下五档, code可以是股票可以是list, num是每个股票获取的数量 """ if code is not None: if isinstance(code, str): code = list(code) elif isinstance(code, list): pass data = pd.DataFrame([ item for item in collections.find({'code': { '$in': code }}, limit=num * len(code), sort=[('datetime', pymongo.DESCENDING)]) ]).set_index(['datetime', 'code'], drop=False).drop(['_id'], axis=1) return data else: pass
def collections_of_today(): database = DATABASE.get_collection('realtime_{}'.format( datetime.date.today())) database.create_index([('code', QA_util_sql_mongo_sort_ASCENDING)]) database.create_index([('datetime', QA_util_sql_mongo_sort_ASCENDING)]) database.create_index([("code", QA_util_sql_mongo_sort_ASCENDING), ("datetime", QA_util_sql_mongo_sort_ASCENDING)], #unique=True, ) return database