def QA_fetch_index_day_adv( code, start, end=None, if_drop_index=False, collections=DATABASE.index_day): '获取指数日线' end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] if isinstance(code, str): if QA_util_date_valid(end) == True: __data = [] for item in collections.find({ 'code': str(code)[0:6], "date_stamp": { "$lte": QA_util_date_stamp(end), "$gte": QA_util_date_stamp(start)}}): __data.append([str(item['code']), float(item['open']), float(item['high']), float( item['low']), float(item['close']), float(item['vol']), item['date']]) __data = DataFrame(__data, columns=[ 'code', 'open', 'high', 'low', 'close', 'volume', 'date']) __data['date'] = pd.to_datetime(__data['date']) return QA_DataStruct_Index_day(__data.query('volume>1').set_index(['date', 'code'], drop=if_drop_index).sort_index()) else: QA_util_log_info('something wrong with date') elif isinstance(code, list): return QA_DataStruct_Index_day(pd.concat(QA_fetch_indexlist_day(code, [start, end])).query('volume>1').set_index(['date', 'code'], drop=if_drop_index).sort_index())
def QA_fetch_index_day_adv( code, start, end=None, if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.index_day): ''' :param code: code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: ''' '获取指数日线' end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 # 🛠 todo 如果相等 res = QA_fetch_index_day(code, start, end, format='pd') if res is None: print( "QA Error QA_fetch_index_day_adv parameter code=%s start=%s end=%s call QA_fetch_index_day return None" % (code, start, end)) else: res_set_index = res.set_index(['date', 'code']) # if res_set_index is None: # print("QA Error QA_fetch_index_day_adv set index 'date, code' return None") # return None return QA_DataStruct_Index_day(res_set_index)
def QA_fetch_index_day_adv(code, start, end=None, if_drop_index=False, collections=DATABASE.index_day): '获取指数日线' end = start if end is None else end start = str(start)[0:10] end = str(end)[0:10] return QA_DataStruct_Index_day( QA_fetch_index_day(code, start, end, format='pd').set_index(['date', 'code'], drop=if_drop_index))
def test_data_struct_index(self): """指数(或etf)DataStruture """ code = '000001' days = 365 * 1.2 start = datetime.datetime.now() - datetime.timedelta(days) end = datetime.datetime.now() - datetime.timedelta(0) df = qmi.get(code, start, end) if df is not None: df = df.set_index(['date', 'code'], drop=True) ds = qd(type='index').dataStruct(df) ds2 = QA_DataStruct_Index_day(df) self.assertIsInstance(ds, QA_DataStruct_Index_day) self.assertIsInstance(ds2, QA_DataStruct_Index_day)
def get_lof_day(self, codelist, start, end): codelist = promise_list(codelist) # if 'XS' not in codelist[0]: # codelist = pd.Series(codelist).apply( # lambda x: x+'.XSHG' if x[0] != 6 else x+'.XSHE').tolist() columns_raw = [ 'date', 'order_book_id', 'num_trades', 'open', 'high', 'low', 'close', 'volume', 'total_turnover' ] res = self.client.query_dataframe( "SELECT * FROM quantaxis.lof_cn_day WHERE ((`date` >= '{}')) \ AND (`date` <= '{}') AND (`order_book_id` IN ({}))". format(start, end, "'{}'".format( "','".join(codelist)))).loc[:, columns_raw].drop_duplicates( ['date', 'order_book_id']) return QA_DataStruct_Index_day( res.assign(date=pd.to_datetime(res.date), code=res.order_book_id, amount=res.total_turnover).set_index(['date', 'code' ]).sort_index())