def QA_fetch_stock_min_adv( code, start, end=None, frequence='1min', if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.stock_min): ''' '获取股票分钟线' :param code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param frequence: 字符串str 分钟线的类型 支持 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m 类型 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: QA_DataStruct_Stock_min 类型 ''' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' else: print("QA Error QA_fetch_stock_min_adv parameter frequence=%s is none of 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m" % frequence) return None # __data = [] 未使用 end = start if end is None else end if len(start) == 10: start = '{} 09:30:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) if start == end: # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_stock_min, 不支持start end是相等的 print("QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % ( code, start, end)) return None # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 res = QA_fetch_stock_min( code, start, end, format='pd', frequence=frequence) if res is None: print("QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s frequence=%s call QA_fetch_stock_min return None" % ( code, start, end, frequence)) return None else: res_set_index = res.set_index(['datetime', 'code'], drop=if_drop_index) # if res_set_index is None: # print("QA Error QA_fetch_stock_min_adv set index 'datetime, code' return None") # return None return QA_DataStruct_Stock_min(res_set_index)
def QA_fetch_stock_min_adv( code, start, end=None, frequence='1min', if_drop_index=True, # 🛠 todo collections 参数没有用到, 且数据库是固定的, 这个变量后期去掉 collections=DATABASE.stock_min): ''' '获取股票分钟线' :param code: 字符串str eg 600085 :param start: 字符串str 开始日期 eg 2011-01-01 :param end: 字符串str 结束日期 eg 2011-05-01 :param frequence: 字符串str 分钟线的类型 支持 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m 类型 :param if_drop_index: Ture False , dataframe drop index or not :param collections: mongodb 数据库 :return: QA_DataStruct_Stock_min 类型 ''' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' else: print("QA Error QA_fetch_stock_min_adv parameter frequence=%s is none of 1min 1m 5min 5m 15min 15m 30min 30m 60min 60m" % frequence) return None # __data = [] 未使用 end = start if end is None else end if len(start) == 10: start = '{} 09:30:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) if start == end: # 🛠 todo 如果相等,根据 frequence 获取开始时间的 时间段 QA_fetch_stock_min, 不支持start end是相等的 print("QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s is equal, should have time span! " % ( code, start, end)) return None # 🛠 todo 报告错误 如果开始时间 在 结束时间之后 res = QA_fetch_stock_min( code, start, end, format='pd', frequence=frequence) if res is None: print("QA Error QA_fetch_stock_min_adv parameter code=%s , start=%s, end=%s frequence=%s call QA_fetch_stock_min return None" % ( code, start, end, frequence)) return None else: res_set_index = res.set_index(['datetime', 'code'], drop=if_drop_index) # if res_set_index is None: # print("QA Error QA_fetch_stock_min_adv set index 'datetime, code' return None") # return None return QA_DataStruct_Stock_min(res_set_index)
def QA_fetch_stock_min_adv(code, start, end=None, frequence='1min', if_drop_index=False, collections=DATABASE.stock_min): '获取股票分钟线' if frequence in ['1min', '1m']: frequence = '1min' elif frequence in ['5min', '5m']: frequence = '5min' elif frequence in ['15min', '15m']: frequence = '15min' elif frequence in ['30min', '30m']: frequence = '30min' elif frequence in ['60min', '60m']: frequence = '60min' __data = [] end = start if end is None else end if len(start) == 10: start = '{} 09:30:00'.format(start) if len(end) == 10: end = '{} 15:00:00'.format(end) return QA_DataStruct_Stock_min( QA_fetch_stock_min(code, start, end, format='pd', frequence=frequence).set_index(['datetime', 'code'], drop=if_drop_index))
def get(self): """ 采用了get_arguents来获取参数 默认参数: code-->000001 start-->2017-01-01 09:00:00 end-->now """ code = self.get_argument('code', default='000001') start = self.get_argument('start', default='2017-01-01 09:00:00') end = self.get_argument('end', default=str(datetime.datetime.now())) frequence = self.get_argument('frequence', default='1min') if_fq = self.get_argument('if_fq', default=False) if if_fq: data = QA_util_to_json_from_pandas( QA_fetch_stock_min_adv(code, start, end, frequence).to_qfq().data) self.write({'result': data}) else: data = QA_util_to_json_from_pandas( QA_fetch_stock_min(code, start, end, format='pd', frequence=frequence)) self.write({'result': data}) self.write({'result': data})
def __get_stock_min_data(self, __bid): __data = QA_util_to_json_from_pandas(QA_fetch_stock_min(str( __bid.code)[0:6], str(__bid.datetime)[0:19], str(__bid.datetime)[0:10], 'pd')) if len(__data) == 0: pass else: __data = __data[0] return __data
def test_getMin_diffQA(self): code = '000001' days = 20 * 1.2 start = str(datetime.datetime.now() - datetime.timedelta(days)) end = str(datetime.datetime.now() - datetime.timedelta(0)) df = qm.get(code, start, end, frequence='1min') df2 = QA_fetch_stock_min(code, start, end=end, format='pd', frequence='1min') # todo df的长度比df2长。未找出原因 data1, data2 = df, df2 self.assertTrue(len(data1) == len(data2), "和QA返回的分钟线数据长度不一致:{}:{}".format(len(data1), len(data2))) if len(data1) > len(data2): print("array1f的长度比array2长") data1 = data1[-len(data2):] elif len(data1) < len(data2): print("array2的长度比array1长") data2 = data2[-len(data1):] obo = self.differOneByOne(data1, data2) self.assertTrue(data1.equals(data2), "和QA返回的分钟线数据不一致:{}".format(obo))
def test_get_min_diffQA(self): code = '000001' days = 10 * 1.2 start = datetime.datetime.now() - datetime.timedelta(days) end = datetime.datetime.now() - datetime.timedelta(0) array1 = qm.get(code, start, end, frequence='1min') array2 = QA_fetch_stock_min(code, start, end, frequence='1min') # todo df的长度比df2长。未找出原因 if len(array1) > len(array2): print("array1f的长度比array2长") array1 = array1[-len(array2):] elif len(array1) < len(array2): print("array2的长度比array1长") array2 = array2[-len(array1):] self.assertTrue( len(array1) == len(array2), "和QA返回的分钟线数据长度不一致:{}:{}".format(len(array1), len(array2))) # todo 连续获取分钟数据时,不定时返回结果不想等。报错 self.assertTrue( np.array_equal(array1, array2), "和QA返回的分钟线数据不一致:{}".format( np.setdiff1d(array1, array2, assume_unique=True)))
def __get_data(__bid): __data = QA_util_to_json_from_pandas( QA_fetch_stock_min( str(__bid['code'])[0:6], __bid['time'], __bid['time'], 'pd')) # 我们只需要找到这个时间点下一条数据就行 return __data