def __init__(self): super(TimerTask, self).__init__() self.logger = get_log("timerTask") self.max_instances = 20 # 最大并发数 # self.scheduler = BlockingScheduler() self.scheduler = AsyncIOScheduler() self.common = Common()
def __init__(self, is_subscribe_record=False): if not SqliteDB.__first_init: # 先创建目录 if not os.path.exists(db_save_folder): os.makedirs(db_save_folder) self.common = Common() self.logger = get_log() self.conn = sqlite3.connect(dbPath, check_same_thread=False, timeout=15) currentDayTimeStampInfo = self.common.getCurrentDayTimeStampInfo() self.todayBeginTimeStamp = currentDayTimeStampInfo[ 'todayBeginTimeStamp'] self.todayEndTimeStamp = currentDayTimeStampInfo[ 'todayEndTimeStamp'] self.is_subscribe_record = is_subscribe_record self.assemble_num = sql_assemble_num self.assemble_sql = '' self.assemble_float_num = 0 self.transaction_num = sql_transaction_num self.transaction_float_num = 0 # db_file = SETUP_DIR + "\\zmq_py3\\zmq_save_files\\db\\SIT_market_test.db" # if not os.path.exists(db_file): self.create_table() SqliteDB.__first_init = True
class HttpClient(object): # 解决请求的时候添加 verify=False 时的报错 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) def __init__(self): self.cookies = RequestsCookieJar() self.verify = False self.common = Common() def get(self, url, header=None, param=None): res = None if header is not None: res = requests.get(url=url, headers=header, verify=self.verify, params=param) else: res = requests.get(url=url, verify=self.verify, params=param) if self.common.isJson(res.text): return [res.status_code, json.loads(res.text)] else: return [res.status_code, res.text] def post(self, url, data=None, header=None): res = None if header is not None: if 'application/json' == header['Content-Type']: if type(data) == 'list': pass else: data = json.dumps(data) res = requests.post(url=url, data=data, headers=header, verify=self.verify) else: res = requests.post(url=url, data=data, verify=self.verify) if self.common.isJson(res.text): return [res.status_code, json.loads(res.text)] else: return [res.status_code, res.text] def delete(self, url, data=None, header=None): res = None if header is not None: res = requests.delete(url=url, data=data, headers=header) else: res = requests.delete(url=url, data=data) if self.common.isJson(res.text): return [res.status_code, json.loads(res.text)] else: return [res.status_code, res.text]
async def get_eastMoney_basic(stockList): stockBase = {} async with aiohttp.ClientSession() as session: tasks = [get(session, stock) for stock in stockList] doneSet, pendingSet = await asyncio.wait(tasks) print("成功个数", len(doneSet)) print("失败个数", len(pendingSet)) for doneInfo in doneSet: if doneInfo._result != None: # 因断连等原因导致返回为None时,这里过滤掉 # print(doneInfo._result) stockBase.update(doneInfo._result) if pendingSet: print("失败了???") for pending in pendingSet: pending.cancel() file_path = txt_file_save_folder + "eastMoney.txt" with open(file_path, "a", encoding='utf-8') as f: f.write(str(Common().to_json(stockBase))) print(len(stockBase))
def __init__(self): self.common = Common() self.logger = get_log() self.http = MarketHttpClient() self.market_token = self.http.get_market_token( self.http.get_login_token(phone=login_phone, pwd=login_pwd, device_id=login_device_id)) self.new_loop = self.common.getNewLoop() asyncio.set_event_loop(self.new_loop) self.api = SubscribeApi(union_ws_url, self.new_loop) asyncio.get_event_loop().run_until_complete( future=self.api.client.ws_connect()) asyncio.get_event_loop().run_until_complete(future=self.api.LoginReq( token=self.market_token)) asyncio.run_coroutine_threadsafe(self.api.hearbeat_job(), self.new_loop)
def QueryExchangeSort(exchange: str, count: int, sortFiled: str): frequence = None start_time_stamp = int(time.time() * 1000) # http = MarketHttpClient() # market_token = http.get_market_token( # http.get_login_token(phone=login_phone, pwd=login_pwd, device_id=login_device_id)) market_token = None loop = Common().getNewLoop() api = SubscribeApi(union_ws_url, loop) loop.run_until_complete(future=api.client.ws_connect()) loop.run_until_complete( future=api.LoginReq(token=market_token, start_time_stamp=start_time_stamp, frequence=frequence)) # asyncio.run_coroutine_threadsafe(api.hearbeat_job(), new_loop) rsp_sort_list = asyncio.get_event_loop().run_until_complete( future=api.QueryExchangeSortMsgReqApi( isSubTrade=None, exchange=exchange, sortFiled=sortFiled, count=count, start_time_stamp=start_time_stamp, recv_num=1)) app_sort = rsp_sort_list[0] api.client.disconnect() return app_sort
def StartChartData(exchange: str, code: str): # exchange = exchange # code = code frequence = None start_time_stamp = int(time.time() * 1000) # http = MarketHttpClient() # market_token = http.get_market_token( # http.get_login_token(phone=login_phone, pwd=login_pwd, device_id=login_device_id)) market_token = None loop = Common().getNewLoop() api = SubscribeApi(union_ws_url, loop) loop.run_until_complete(future=api.client.ws_connect()) loop.run_until_complete( future=api.LoginReq(token=market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(api.hearbeat_job(), loop) app_rsp_list = loop.run_until_complete(future=api.StartChartDataReqApi( exchange, code, start_time_stamp, recv_num=1)) app_rsp = app_rsp_list[0] api.client.disconnect() # return app_rsp return """ <html> <head> <title>查询合约</title> </head> <body> <pre id="out_pre"></pre> </body> <script type="text/javascript"> var result = JSON.stringify({text}, null, 2);//将字符串转换成json对象 document.getElementById('out_pre').innerText= result ; </script> </html> """.format(text=app_rsp)
class AuthApi(object): def __init__(self, ws_url, new_loop, extra_headers=None): self.new_loop = new_loop self.client = BaseWebSocketClient(url=ws_url, loop=self.new_loop, extra_headers=extra_headers) self.common = Common() async def AuthReqApi(self, token): msg_id = self.common.randomNum(10) data_send = AuthReq(token=token) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.AUTH_CENTER_AUTH, data=data_send.SerializeToString(), id=msg_id, version=ProtoVersion.VERSION_1) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg, recv_num=1) json_rsp_list = [] for base_req in all_req: if base_req == None: continue if base_req == '_Heart_PONG': base_req = await self.client.recv() rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.AUTH_CENTER_AUTH: single_data = AuthRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) # print('AuthReqApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list async def HearBeatApi(self): await self.client.send('_Heart_PING')
for base_req in all_req: if base_req == None: continue if base_req == '_Heart_PONG': base_req = await self.client.recv() rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.AUTH_CENTER_AUTH: single_data = AuthRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) # print('AuthReqApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list async def HearBeatApi(self): await self.client.send('_Heart_PING') if __name__ == '__main__': common = Common() api = AuthApi(auth_ws_url, asyncio.get_event_loop(), extra_headers=auth_headers) start_time = time.time() asyncio.get_event_loop().run_until_complete(api.client.ws_connect()) asyncio.get_event_loop().run_until_complete( api.AuthReqApi(token=str(market_token))) asyncio.get_event_loop().run_until_complete(api.client.stress_disconnect())
def __init__(self): self.common = Common() self.logger = self.common.logger
class DBMethod(object): def __init__(self): self.common = Common() self.logger = self.common.logger def get_recent_stamp_list(self, peroid_type_int, exchange_int): time_str = time.strftime("%Y%m%d%H%M%S", time.localtime(int(time.time()))) time_str_list = [] recent_time_stamp_list = [] if exchange_int == ExchangeType.HKFE: if peroid_type_int == KLinePeriodType.THREE_MIN: time_str_prefix = time_str[:-6] # 暂时只校验白天的 for hour_str in ['09', '10', '11', '12', '13', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str if hour_str not in ['09', '12', '13', '16']: for min_str in range(0, 60, 3): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '09': for min_str in range(18, 60, 3): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '12': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif hour_str == '13': for min_str in range(3, 60, 3): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '16': for min_str in range(0, 33, 3): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif peroid_type_int == KLinePeriodType.FIVE_MIN: time_str_prefix = time_str[:-6] # 暂时只校验白天的 for hour_str in ['09', '10', '11', '12', '13', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str if hour_str not in ['09', '12', '13', '16']: for min_str in range(0, 60, 5): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '09': for min_str in range(20, 60, 5): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '12': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif hour_str == '13': for min_str in range(5, 60, 5): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '16': for min_str in range(0, 35, 5): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif peroid_type_int == KLinePeriodType.FIFTEEN_MIN: time_str_prefix = time_str[:-6] # 暂时只校验白天的 for hour_str in ['09', '10', '11', '12', '13', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str if hour_str not in ['09', '12', '13', '16']: for min_str in range(0, 60, 15): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '09': for min_str in range(30, 60, 15): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '12': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif hour_str == '13': for min_str in range(15, 60, 15): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '16': for min_str in range(0, 45, 15): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif peroid_type_int == KLinePeriodType.THIRTY_MIN: time_str_prefix = time_str[:-6] # 暂时只校验白天的 for hour_str in ['09', '10', '11', '12', '13', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str if hour_str not in ['09', '12', '13', '16']: for min_str in range(0, 60, 30): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '09': pass # 9点区间的半小时K只有9.30这一根,区间属于 0245-0300、0915-0930之和,跨区间,此处不便校验,先跳过 elif hour_str == '12': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif hour_str == '13': for min_str in range(30, 60, 30): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '16': for min_str in range(0, 60, 30): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif peroid_type_int == KLinePeriodType.HOUR: time_str_prefix = time_str[:-6] # 暂时只校验白天的 # 9点区间的1小时K只有9.30这一根,区间属于 0215-0300、0915-0930之和,跨区间,此处不便校验,先跳过; 13.30同理 for hour_str in ['10', '11', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str min_str = '30' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) else: break elif peroid_type_int == KLinePeriodType.TWO_HOUR: time_str_prefix = time_str[:-6] # 暂时只校验白天的 # 14.30这一根,区间属于 1130-1200、1300-1430之和,跨区间,此处不便校验,先跳过 for hour_str in ['11', '16']: time_str_fix = time_str_prefix + hour_str min_str = '30' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) else: break elif peroid_type_int == KLinePeriodType.FOUR_HOUR: # 4小时K连续的k线区间只有1715-2115和2115-0115两个区间,校验这两个区间 today = datetime.date.today() today_info1 = datetime.datetime(today.year, today.month, today.day, 21, 15, 0) last_day = today_info1 - datetime.timedelta(days=1) recent_time_stamp1 = int(time.mktime(time.strptime(str(last_day), '%Y-%m-%d %H:%M:%S'))) recent_time_stamp_list.append(recent_time_stamp1) today_info2 = datetime.datetime(today.year, today.month, today.day, 1, 15, 0) recent_time_stamp2 = int(time.mktime(time.strptime(str(today_info2), '%Y-%m-%d %H:%M:%S'))) recent_time_stamp_list.append(recent_time_stamp2) return recent_time_stamp_list elif peroid_type_int == KLinePeriodType.DAY: get_num = 3 # 暂时只返回校验过去的三天 today = datetime.date.today() today_info = datetime.datetime(today.year, today.month, today.day, 0, 0, 0) last_day = today_info for _ in range(get_num): last_day = last_day - datetime.timedelta(days=1) recent_time_stamp = int(time.mktime(time.strptime(str(last_day), '%Y-%m-%d %H:%M:%S'))) recent_time_stamp_list.append(recent_time_stamp) return recent_time_stamp_list elif peroid_type_int == KLinePeriodType.WEEK: get_num = 3 # 暂时只返回校验过去的三周 test_day = time.localtime() today_wday = test_day.tm_wday # [0,1,2,3,4,5,6]代表周一到周日 today = datetime.date.today() today_info = datetime.datetime(today.year, today.month, today.day, 0, 0, 0) this_monday = today_info - datetime.timedelta(days=today_wday) last_monday = this_monday for _ in range(get_num): last_monday = last_monday - datetime.timedelta(days=7) recent_time_stamp = int(time.mktime(time.strptime(str(last_monday), '%Y-%m-%d %H:%M:%S'))) recent_time_stamp_list.append(recent_time_stamp) return recent_time_stamp_list elif peroid_type_int == KLinePeriodType.MONTH: get_num = 1 # 暂时只返回校验过去的三月 now = arrow.now() for _ in range(get_num): last_month_str = now.shift(months=-1).format('YYYYMM') recent_time_stamp = int(time.mktime(time.strptime(str(last_month_str), '%Y%m'))) recent_time_stamp_list.append(recent_time_stamp) now = now.shift(months=-1) return recent_time_stamp_list elif exchange_int == ExchangeType.SEHK: if peroid_type_int == KLinePeriodType.THREE_MIN: time_str_prefix = time_str[:-6] # 暂时只校验白天的 for hour_str in ['09', '10', '11', '12', '13', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str if hour_str not in ['09', '12', '13', '16']: for min_str in range(0, 60, 3): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '09': for min_str in range(33, 60, 3): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '12': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif hour_str == '13': for min_str in range(3, 60, 3): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '16': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif peroid_type_int == KLinePeriodType.FIVE_MIN: time_str_prefix = time_str[:-6] # 暂时只校验白天的 for hour_str in ['09', '10', '11', '12', '13', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str if hour_str not in ['09', '12', '13', '16']: for min_str in range(0, 60, 5): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '09': for min_str in range(35, 60, 5): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '12': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif hour_str == '13': for min_str in range(5, 60, 5): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '16': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif peroid_type_int == KLinePeriodType.FIFTEEN_MIN: time_str_prefix = time_str[:-6] # 暂时只校验白天的 for hour_str in ['09', '10', '11', '12', '13', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str if hour_str not in ['09', '12', '13', '16']: for min_str in range(0, 60, 15): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '09': for min_str in range(45, 60, 15): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '12': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif hour_str == '13': for min_str in range(15, 60, 15): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '16': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif peroid_type_int == KLinePeriodType.THIRTY_MIN: time_str_prefix = time_str[:-6] # 暂时只校验白天的 for hour_str in ['09', '10', '11', '12', '13', '14', '15', '16']: time_str_fix = time_str_prefix + hour_str if hour_str not in ['09', '12', '13', '16']: for min_str in range(0, 60, 30): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '09': pass # 9点区间没有半小时K elif hour_str == '12': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif hour_str == '13': for min_str in range(30, 60, 30): min_str = self.common.fixIntNum(min_str, 2) time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) time_str_fix = time_str_prefix + hour_str else: break elif hour_str == '16': min_str = '00' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) elif peroid_type_int == KLinePeriodType.HOUR: time_str_prefix = time_str[:-6] for hour_str in ['10', '11', '14', '15']: time_str_fix = time_str_prefix + hour_str if hour_str in ['10', '11', '14', '15']: min_str = '30' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) else: break elif peroid_type_int == KLinePeriodType.TWO_HOUR: time_str_prefix = time_str[:-6] for hour_str in ['11']: if hour_str == '11': time_str_fix = time_str_prefix + hour_str min_str = '30' time_str_fix = time_str_fix + str(min_str) + '00' if int(time_str_fix) < int(time_str): time_str_list.append(time_str_fix) else: break elif peroid_type_int == KLinePeriodType.FOUR_HOUR: # 4小时K无连续的k线区间 pass elif peroid_type_int == KLinePeriodType.DAY: get_num = 3 # 暂时只返回校验过去的三天 today = datetime.date.today() today_info = datetime.datetime(today.year, today.month, today.day, 0, 0, 0) last_day = today_info for _ in range(get_num): last_day = last_day - datetime.timedelta(days=1) recent_time_stamp = int(time.mktime(time.strptime(str(last_day), '%Y-%m-%d %H:%M:%S'))) recent_time_stamp_list.append(recent_time_stamp) return recent_time_stamp_list elif peroid_type_int == KLinePeriodType.WEEK: get_num = 3 # 暂时只返回校验过去的三周 test_day = time.localtime() today_wday = test_day.tm_wday # [0,1,2,3,4,5,6]代表周一到周日 today = datetime.date.today() today_info = datetime.datetime(today.year, today.month, today.day, 0, 0, 0) this_monday = today_info - datetime.timedelta(days=today_wday) last_monday = this_monday for _ in range(get_num): last_monday = last_monday - datetime.timedelta(days=7) recent_time_stamp = int(time.mktime(time.strptime(str(last_monday), '%Y-%m-%d %H:%M:%S'))) recent_time_stamp_list.append(recent_time_stamp) return recent_time_stamp_list elif peroid_type_int == KLinePeriodType.MONTH: get_num = 1 # 暂时只返回校验过去的三月 now = arrow.now() for _ in range(get_num): last_month_str = now.shift(months=-1).format('YYYYMM') recent_time_stamp = int(time.mktime(time.strptime(str(last_month_str), '%Y%m'))) recent_time_stamp_list.append(recent_time_stamp) now = now.shift(months=-1) return recent_time_stamp_list recent_time_stamp_list = [int(time.mktime(time.strptime(time_str, "%Y%m%d%H%M%S"))) for time_str in time_str_list] recent_time_stamp_list = sorted(recent_time_stamp_list) return recent_time_stamp_list def get_minute_keys(self, instr_code, exchange, peroid_type, kline_key_stamp): # 传入K线的相关信息,输出对应的分时线的key值(此方法要求区间连续) minutes_num = 0 min_time_stamp_list = [] if peroid_type == KLinePeriodType.MINUTE: minutes_num = 1 elif peroid_type == KLinePeriodType.THREE_MIN: minutes_num = 3 elif peroid_type == KLinePeriodType.FIVE_MIN: minutes_num = 5 elif peroid_type == KLinePeriodType.FIFTEEN_MIN: minutes_num = 15 elif peroid_type == KLinePeriodType.THIRTY_MIN: minutes_num = 30 elif peroid_type == KLinePeriodType.HOUR: minutes_num = 60 elif peroid_type == KLinePeriodType.TWO_HOUR: minutes_num = 60 * 2 elif peroid_type == KLinePeriodType.FOUR_HOUR: minutes_num = 60 * 4 elif peroid_type == KLinePeriodType.DAY: minutes_num = 60 * 24 elif peroid_type == KLinePeriodType.WEEK: minutes_num = 60 * 24 * 7 elif peroid_type == KLinePeriodType.MONTH: time_struct = time.localtime(kline_key_stamp) month_contain_days = calendar.monthrange(time_struct.tm_year, time_struct.tm_mon)[1] minutes_num = 60 * 24 * month_contain_days if peroid_type not in [KLinePeriodType.DAY, KLinePeriodType.WEEK, KLinePeriodType.MONTH]: min_time_stamp_list = reversed([(kline_key_stamp - i * 60) for i in range(minutes_num)]) elif peroid_type == KLinePeriodType.DAY: if exchange == ExchangeType.HKFE: # 港期每日开盘时间是夜市开始时间,下午5.15 min_time_stamp_list = reversed([(kline_key_stamp + 17 * 60 * 60 + 15 * 60 - i * 60) for i in range(minutes_num)]) elif peroid_type == KLinePeriodType.WEEK: if exchange == ExchangeType.HKFE: # 港期每周开盘时间是周五夜市开始时间,下午5.15 min_time_stamp_list = reversed( [(kline_key_stamp + 4 * 24 * 60 * 60 + 17 * 60 * 60 + 15 * 60 - i * 60) for i in range(minutes_num)]) elif peroid_type == KLinePeriodType.MONTH: if exchange == ExchangeType.HKFE: # 港期每日开盘时间是夜市开始时间,下午5.15 min_time_stamp_list = [(kline_key_stamp - (6 * 60 * 60 + 45 * 60) + i * 60) for i in range(1, minutes_num + 1)] else: self.logger.debug('Error peroid_type: {}'.format(peroid_type)) key_list = [ ('KLINEMIN_{}_{}_{}'.format(exchange, instr_code, time.strftime("%Y%m%d%H%M%S", time.localtime(j)))) for j in min_time_stamp_list] return key_list def get_minute_keys_by_time(self, instr_code, exchange, start_time_stamp, end_time_stamp): # 传入开始及结束对应的时间戳,则把对应区间的key值返回 key_list = [ ('KLINEMIN_{}_{}_{}'.format(exchange, instr_code, time.strftime("%Y%m%d%H%M%S", time.localtime(time_stamp)))) for time_stamp in range(start_time_stamp + 60, end_time_stamp + 60, 60)] return key_list def get_recent_interval_list(self, peroid_type_int, exchange_int): # 此方法用于校验跨区间的K线的情况 interval_list = [] if exchange_int == ExchangeType.HKFE: if peroid_type_int == KLinePeriodType.THIRTY_MIN: # 半小时K只有9.30这一根,区间属于 0245-0300、0915-0930之和,跨区间 today = datetime.date.today() end_info = datetime.datetime(today.year, today.month, today.day, 9, 30, 0) end_info_time_stamp = int(time.mktime(time.strptime(str(end_info), '%Y-%m-%d %H:%M:%S'))) if time.localtime().tm_wday != 0: # 非周一 start_info = end_info - datetime.timedelta(hours=6, minutes=45) else: start_info = end_info - datetime.timedelta(days=2, hours=6, minutes=45) start_info_time_stamp = int(time.mktime(time.strptime(str(start_info), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp < time.time(): interval_list.append([start_info_time_stamp, end_info_time_stamp]) elif peroid_type_int == KLinePeriodType.HOUR: # 9.30这一根,区间属于 0215-0300、0915-0930之和,跨区间; today = datetime.date.today() end_info = datetime.datetime(today.year, today.month, today.day, 9, 30, 0) end_info_time_stamp = int(time.mktime(time.strptime(str(end_info), '%Y-%m-%d %H:%M:%S'))) if time.localtime().tm_wday != 0: # 非周一 start_info = end_info - datetime.timedelta(hours=7, minutes=15) else: start_info = end_info - datetime.timedelta(days=2, hours=7, minutes=15) start_info_time_stamp = int(time.mktime(time.strptime(str(start_info), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp < time.time(): interval_list.append([start_info_time_stamp, end_info_time_stamp]) # 13.30 end_info2 = datetime.datetime(today.year, today.month, today.day, 13, 30, 0) end_info_time_stamp2 = int(time.mktime(time.strptime(str(end_info2), '%Y-%m-%d %H:%M:%S'))) start_info2 = datetime.datetime(today.year, today.month, today.day, 11, 30, 0) start_info_time_stamp2 = int(time.mktime(time.strptime(str(start_info2), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp2 < time.time(): interval_list.append([start_info_time_stamp2, end_info_time_stamp2]) elif peroid_type_int == KLinePeriodType.TWO_HOUR: # 14.30这一根,区间属于 1130-1200、1300-1430之和,跨区间 today = datetime.date.today() end_info = datetime.datetime(today.year, today.month, today.day, 14, 30, 0) end_info_time_stamp = int(time.mktime(time.strptime(str(end_info), '%Y-%m-%d %H:%M:%S'))) start_info = datetime.datetime(today.year, today.month, today.day, 11, 30, 0) start_info_time_stamp = int(time.mktime(time.strptime(str(start_info), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp < time.time(): interval_list.append([start_info_time_stamp, end_info_time_stamp]) # 0930: 0115-0300 , 0915-0930 end_info2 = datetime.datetime(today.year, today.month, today.day, 9, 30, 0) end_info_time_stamp2 = int(time.mktime(time.strptime(str(end_info2), '%Y-%m-%d %H:%M:%S'))) if time.localtime().tm_wday != 0: # 非周一 start_info2 = end_info2 - datetime.timedelta(hours=8, minutes=15) else: start_info2 = end_info2 - datetime.timedelta(days=2, hours=8, minutes=15) start_info_time_stamp2 = int(time.mktime(time.strptime(str(start_info2), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp2 < time.time(): interval_list.append([start_info_time_stamp2, end_info_time_stamp2]) elif peroid_type_int == KLinePeriodType.FOUR_HOUR: # 11.30这一根,区间属于 0115-0300、0915-1130,跨区间; today = datetime.date.today() end_info = datetime.datetime(today.year, today.month, today.day, 11, 30, 0) end_info_time_stamp = int(time.mktime(time.strptime(str(end_info), '%Y-%m-%d %H:%M:%S'))) if time.localtime().tm_wday != 0: # 非周一 start_info = end_info - datetime.timedelta(hours=10, minutes=15) else: start_info = end_info - datetime.timedelta(days=2, hours=10, minutes=15) start_info_time_stamp = int(time.mktime(time.strptime(str(start_info), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp < time.time(): interval_list.append([start_info_time_stamp, end_info_time_stamp]) # 16.30这一根,区间属于 1130-1200、1300-1630之和,跨区间; end_info2 = datetime.datetime(today.year, today.month, today.day, 16, 30, 0) end_info_time_stamp2 = int(time.mktime(time.strptime(str(end_info2), '%Y-%m-%d %H:%M:%S'))) start_info2 = datetime.datetime(today.year, today.month, today.day, 11, 30, 0) start_info_time_stamp2 = int(time.mktime(time.strptime(str(start_info2), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp2 < time.time(): interval_list.append([start_info_time_stamp2, end_info_time_stamp2]) else: self.logger.debug('ktype error: {}'.format(peroid_type_int)) elif exchange_int == ExchangeType.SEHK: if peroid_type_int == KLinePeriodType.THIRTY_MIN: pass elif peroid_type_int == KLinePeriodType.HOUR: # 13.30这一根,区间属于 1130-1200、1300-1330之和,跨区间; today = datetime.date.today() end_info = datetime.datetime(today.year, today.month, today.day, 13, 30, 0) end_info_time_stamp = int(time.mktime(time.strptime(str(end_info), '%Y-%m-%d %H:%M:%S'))) start_info = datetime.datetime(today.year, today.month, today.day, 11, 30, 0) start_info_time_stamp = int(time.mktime(time.strptime(str(start_info), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp < time.time(): interval_list.append([start_info_time_stamp, end_info_time_stamp]) # 16.00这一根,区间属于 1530-1600、特殊区间; end_info2 = datetime.datetime(today.year, today.month, today.day, 15, 30, 0) end_info_time_stamp2 = int(time.mktime(time.strptime(str(end_info2), '%Y-%m-%d %H:%M:%S'))) start_info2 = datetime.datetime(today.year, today.month, today.day, 16, 0, 0) start_info_time_stamp2 = int(time.mktime(time.strptime(str(start_info2), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp2 < time.time(): interval_list.append([start_info_time_stamp2, end_info_time_stamp2]) elif peroid_type_int == KLinePeriodType.TWO_HOUR: # 14.30这一根,区间属于 1130-1200、1300-1430之和,跨区间 today = datetime.date.today() end_info = datetime.datetime(today.year, today.month, today.day, 14, 30, 0) end_info_time_stamp = int(time.mktime(time.strptime(str(end_info), '%Y-%m-%d %H:%M:%S'))) start_info = datetime.datetime(today.year, today.month, today.day, 11, 30, 0) start_info_time_stamp = int(time.mktime(time.strptime(str(start_info), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp < time.time(): interval_list.append([start_info_time_stamp, end_info_time_stamp]) # 16.00这一根,区间属于 1430-1600、特殊区间; today = datetime.date.today() end_info = datetime.datetime(today.year, today.month, today.day, 16, 0, 0) end_info_time_stamp = int(time.mktime(time.strptime(str(end_info), '%Y-%m-%d %H:%M:%S'))) start_info = datetime.datetime(today.year, today.month, today.day, 14, 30, 0) start_info_time_stamp = int(time.mktime(time.strptime(str(start_info), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp < time.time(): interval_list.append([start_info_time_stamp, end_info_time_stamp]) elif peroid_type_int == KLinePeriodType.FOUR_HOUR: # 14.30这一根,区间属于 0930-1200、1300-1430,跨区间; today = datetime.date.today() end_info = datetime.datetime(today.year, today.month, today.day, 14, 30, 0) end_info_time_stamp = int(time.mktime(time.strptime(str(end_info), '%Y-%m-%d %H:%M:%S'))) start_info = datetime.datetime(today.year, today.month, today.day, 9, 30, 0) start_info_time_stamp = int(time.mktime(time.strptime(str(start_info), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp < time.time(): interval_list.append([start_info_time_stamp, end_info_time_stamp]) # 16.00这一根,区间属于 1430-1600、特殊区间; end_info2 = datetime.datetime(today.year, today.month, today.day, 16, 0, 0) end_info_time_stamp2 = int(time.mktime(time.strptime(str(end_info2), '%Y-%m-%d %H:%M:%S'))) start_info2 = datetime.datetime(today.year, today.month, today.day, 14, 30, 0) start_info_time_stamp2 = int(time.mktime(time.strptime(str(start_info2), '%Y-%m-%d %H:%M:%S'))) if end_info_time_stamp2 < time.time(): interval_list.append([start_info_time_stamp2, end_info_time_stamp2]) else: self.logger.debug('ktype error: {}'.format(peroid_type_int)) return interval_list
def __init__(self, ws_url, new_loop): self.new_loop = new_loop self.client = BaseWebSocketClient(url=ws_url, loop=self.new_loop) self.common = Common()
class SqliteDB(object): __instance = None __first_init = False def __new__(cls, *args, **kwargs): # 单例模式 if not cls.__instance: cls.__instance = object.__new__(cls) return cls.__instance def __init__(self, is_subscribe_record=False): if not SqliteDB.__first_init: # 先创建目录 if not os.path.exists(db_save_folder): os.makedirs(db_save_folder) self.common = Common() self.logger = get_log() self.conn = sqlite3.connect(dbPath, check_same_thread=False, timeout=15) currentDayTimeStampInfo = self.common.getCurrentDayTimeStampInfo() self.todayBeginTimeStamp = currentDayTimeStampInfo[ 'todayBeginTimeStamp'] self.todayEndTimeStamp = currentDayTimeStampInfo[ 'todayEndTimeStamp'] self.is_subscribe_record = is_subscribe_record self.assemble_num = sql_assemble_num self.assemble_sql = '' self.assemble_float_num = 0 self.transaction_num = sql_transaction_num self.transaction_float_num = 0 # db_file = SETUP_DIR + "\\zmq_py3\\zmq_save_files\\db\\SIT_market_test.db" # if not os.path.exists(db_file): self.create_table() SqliteDB.__first_init = True def create_table(self): # 创建DB目录 create_pub_table = ''' CREATE TABLE IF NOT EXISTS {} ( "id" INTEGER NOT NULL, "data_type" TEXT NOT NULL, "origin_info" TEXT, "json_info" TEXT, "record_time" integer, PRIMARY KEY ("id") ); '''.format(pub_table) create_deal_table = ''' CREATE TABLE IF NOT EXISTS {} ( "id" INTEGER NOT NULL, "data_type" TEXT NOT NULL, "origin_info" TEXT, "json_info" TEXT, "record_time" integer, PRIMARY KEY ("id") ); '''.format(deal_table) create_subscribe_table = ''' CREATE TABLE IF NOT EXISTS {} ( "id" INTEGER NOT NULL, "data_type" integer NOT NULL, "product_code" TEXT, "instr_code" TEXT, "source_update_time" integer, "record_time" integer, "json_info" TEXT, period_type TEXT, PRIMARY KEY ("id") ); '''.format(subscribe_table) create_time_analysis_base_table = ''' CREATE TABLE IF NOT EXISTS {} ( "id" integer NOT NULL, "exchange" TEXT, "product_code" TEXT, "instr_code" TEXT, "data_type" integer, "collect2out" integer, "subscribe2out" integer, "collector2subscribe" integer, "source2collector" integer, "inner_total_time" integer, "json_info" TEXT, PRIMARY KEY ("id") ); CREATE INDEX IF NOT EXISTS analysis_index ON time_analysis_base_info ('instr_code', 'data_type'); '''.format(time_analysis_base_table) create_statistical_analysis_table = ''' CREATE TABLE IF NOT EXISTS {} ( "id" integer NOT NULL, "desc" TEXT, "exchange" TEXT, "product_code" TEXT, "instr_code" TEXT, "data_type" TEXT, "max_collect2out" TEXT, "min_collect2out" TEXT, "av_collect2out" TEXT, "max_subscribe2out" TEXT, "min_subscribe2out" TEXT, "av_subscribe2out" TEXT, "max_collector2subscribe" TEXT, "min_collector2subscribe" TEXT, "av_collector2subscribe" TEXT, "max_source2collector" TEXT, "min_source2collector" TEXT, "av_source2collector" TEXT, "max_inner_total_time" TEXT, "min_inner_total_time" TEXT, "av_inner_total_time" TEXT, "analysis_num" integer, PRIMARY KEY ("id") ); CREATE INDEX IF NOT EXISTS statistical_analysis_index ON statistical_analysis ('instr_code', 'data_type'); '''.format(statistical_analysis_table) create_cal_table = ''' CREATE TABLE IF NOT EXISTS {} ( "id" integer NOT NULL, "data_type" text, "product_code" TEXT, "instr_code" TEXT, "precision" integer, "price" integer, "vol" integer, "time" integer, PRIMARY KEY ("id") ); '''.format(cal_table) sql = create_pub_table + create_deal_table + create_subscribe_table + create_time_analysis_base_table + create_statistical_analysis_table + create_cal_table self.commit(sql) def commit(self, sql): start = time.time() try: cursor = self.conn.cursor() cursor.executescript(sql) self.conn.commit() result = cursor.fetchone() return result except Exception as e: self.logger.debug("sqlCommit exec error: {}".format(e)) finally: self.logger.debug( 'sql commit cost: {} seconds.'.format(time.time() - start)) def select(self, sql): self.logger.debug('sqlSelect Sql: {}'.format(sql)) try: cursor = self.conn.cursor() cursor.execute(sql) result = cursor.fetchone() return result except Exception as e: self.logger.debug("sqlSelect exec error: {}".format(e)) def multi_select(self, sql): self.logger.debug('sqlSelectMulil Sql:{}'.format(sql)) resList = [] try: cursor = self.conn.cursor() cursor.execute(sql) result = cursor.fetchall() for line in result: resList.append(line) return resList except Exception as e: self.logger.debug("sqlSelectMulil exec error: {}".format(e)) def multi_select_with_gen(self, sql): self.logger.debug('sqlSelectMulil Sql:{}'.format(sql)) try: cursor = self.conn.cursor() cursor.execute(sql) result = cursor.fetchall() for line in result: yield line except Exception as e: self.logger.debug("sqlSelectMulilWithGen exec error: {}".format(e)) def exit(self): self.conn.close() def stress_insert(self, assemble_first, single_info): self.assemble_float_num += 1 self.assemble_sql = self.assemble_sql + single_info if self.assemble_float_num >= self.assemble_num: single_finaly_sql = (assemble_first + self.assemble_sql).rstrip(', ') + ';\n' self.assemble_sql = '' self.assemble_float_num = 0 self.transaction_float_num += 1 if self.transaction_float_num == 1: self.finaly_sql = 'begin;\n' + single_finaly_sql elif self.transaction_float_num > 1 and self.transaction_float_num < self.transaction_num: self.finaly_sql = self.finaly_sql + single_finaly_sql elif self.transaction_float_num > 1 and self.transaction_float_num == self.transaction_num: self.finaly_sql = self.finaly_sql + single_finaly_sql + 'commit;' if self.transaction_num == 1: self.finaly_sql = self.finaly_sql + 'commit;' if self.transaction_float_num >= self.transaction_num: self.commit(self.finaly_sql) self.transaction_float_num = 0
class TimerTask(object): """docstring for TimerTask""" def __init__(self): super(TimerTask, self).__init__() self.logger = get_log("timerTask") self.max_instances = 20 # 最大并发数 # self.scheduler = BlockingScheduler() self.scheduler = AsyncIOScheduler() self.common = Common() # 清盘-实时行情校验 async def CleanData(self, exchange, code, loop, sub_quote_type=sub_quote_type): """ 测试清盘 """ self.logger.debug("执行的参数为: exchange: {}, code: {}, sub_quote_type: {}".format(exchange, code, sub_quote_type)) exchange = exchange code = code frequence = None isSubKLineMin = True query_type = 0 direct = 0 start = 0 end = 0 vol = 0 start_time_stamp = int(time.time() * 1000) isSubTrade = True type = "BY_VOL" start_time = start_time_stamp end_time = None vol = 100 count = 50 # http = MarketHttpClient() # market_token = http.get_market_token( # http.get_login_token(phone=login_phone, pwd=login_pwd, device_id=login_device_id)) market_token = None asyncio.set_event_loop(loop) try: api = SubscribeApi(union_ws_url, loop, logger=self.logger) await api.client.ws_connect() await api.LoginReq(token=market_token, start_time_stamp=start_time_stamp, frequence=frequence) asyncio.run_coroutine_threadsafe(api.hearbeat_job(), loop) self.logger.debug("订阅手机图表行情, 不会返回前快照数据和前盘口数据, {}, {}".format(sub_quote_type, code)) app_rsp_list = await api.StartChartDataReqApi(exchange, code, start_time_stamp, recv_num=1, sub_quote_type=sub_quote_type) app_rsp = app_rsp_list[0] basic_json_list = app_rsp.get("basicData") # 静态数据 assert self.common.searchDicKV(app_rsp.get("snapshot"), "high") is None assert self.common.searchDicKV(app_rsp.get("snapshot"), "open") is None assert self.common.searchDicKV(app_rsp.get("snapshot"), "low") is None assert self.common.searchDicKV(app_rsp.get("snapshot"), "close") is None assert self.common.searchDicKV(app_rsp.get("snapshot"), "last") is None if exchange not in ["ASE", "NYSE", "NASDAQ"]: # 美股盘前 assert app_rsp.get("orderbook") is None self.logger.debug("查询并订阅分时, 查询为空, 订阅成功, 不会返回前数据, {}, {}".format(sub_quote_type, code)) final_rsp = await api.QueryKLineMinMsgReqApi(isSubKLineMin, exchange, code, query_type, direct, start, end, vol, start_time_stamp, sub_quote_type=sub_quote_type) if sub_quote_type == "REAL_QUOTE_MSG": assert self.common.searchDicKV(final_rsp["query_kline_min_rsp_list"][0], 'retCode') == 'INITQUOTE_TIME' elif sub_quote_type == "DELAY_QUOTE_MSG": assert self.common.searchDicKV(final_rsp["query_kline_min_rsp_list"][0], 'data') is None assert self.common.searchDicKV(final_rsp["sub_kline_min_rsp_list"][0], 'retCode') == 'SUCCESS' assert final_rsp.get("before_kline_min_list") is None self.logger.debug("查询五日分时") fiveday_rsp = await api.QueryFiveDaysKLineMinReqApi(isSubKLineMin, exchange, code, start, start_time_stamp) query_5day_klinemin_rsp_list = fiveday_rsp['query_5day_klinemin_rsp_list'] sub_kline_min_rsp_list = fiveday_rsp['sub_kline_min_rsp_list'] assert self.common.searchDicKV(query_5day_klinemin_rsp_list[0], 'retCode') == 'SUCCESS' assert self.common.searchDicKV(sub_kline_min_rsp_list[0], 'retCode') == 'SUCCESS' # self.logger.debug(u'校验五日分时清盘时的数据, {}, {}'.format(sub_quote_type, code)) day_data_list = self.common.searchDicKV(query_5day_klinemin_rsp_list[0], 'dayData') assert day_data_list.__len__() == 5 # 获取五个交易日 fiveDateList = self.common.get_fiveDays(exchange) self.logger.debug("合约 {} , 五个交易日时间 : {}".format(code, fiveDateList)) for i in range(len(day_data_list)): # 校验五日date依次递增, 遇到节假日无法校验 assert day_data_list[i].get("date") == fiveDateList[i] info_list = self.common.searchDicKV(day_data_list[i], 'data') if info_list.__len__() > 0: if exchange == "HKFE": assert day_data_list[i].get("date") == info_list[-1].get("updateDateTime")[:8] else: assert day_data_list[i].get("date") == info_list[0].get("updateDateTime")[:8] self.logger.debug("查询并订阅逐笔, 查询为空, 订阅成功, 不会返回前数据, {}, {}".format(sub_quote_type, code)) final_rsp = await api.QueryTradeTickMsgReqApi(isSubTrade, exchange, code, type, direct, start_time, end_time, vol, start_time_stamp, sub_quote_type=sub_quote_type) try: assert final_rsp["query_trade_tick_rsp_list"] == [] except AssertionError: assert self.common.searchDicKV(final_rsp["query_trade_tick_rsp_list"], "data") is None assert self.common.searchDicKV(final_rsp["sub_trade_tick_rsp_list"][0], 'retCode') == 'SUCCESS' assert final_rsp.get("before_tickdata_list") is None # 港股才有经济席位 if exchange == "SEHK": # 只有港股有经济席位 self.logger.debug("订阅经济席位快照, 不会返回前数据, {}, {}".format(sub_quote_type, code)) final_rsp = await api.SubscribeBrokerSnapshotReqApi(exchange, code, start_time_stamp, sub_quote_type=sub_quote_type) assert self.common.searchDicKV(final_rsp["first_rsp_list"][0], 'retCode') == 'SUCCESS' assert final_rsp["before_broker_snapshot_json_list"] == [] # 查询指数成分股 self.logger.debug("查询港股指数成分股, {}, {}".format(sub_quote_type, code)) IndexShare = await api.QueryIndexShareMsgReqApi(isSubTrade=isSubTrade, exchange=exchange, sort_direct="DESCENDING_ORDER", indexCode="0000100", count=count, start_time_stamp=int(time.time() * 1000)) for indexData in self.common.searchDicKV(IndexShare, "snapshotData"): assert indexData.get("last") is None assert indexData.get("riseFall") is None # 按版块 self.logger.debug("查询港股版块信息, {}, {}".format(sub_quote_type, code)) for plate_type in ["MAIN", "LISTED_NEW_SHARES", "RED_SHIPS", "ETF", "GME"]: PlateSort = await api.QueryPlateSortMsgReqApi(isSubTrade=isSubTrade, zone="HK", plate_type=plate_type, sort_direct="DESCENDING_ORDER", count=count, start_time_stamp=int(time.time() * 1000)) if self.common.searchDicKV(PlateSort, "snapshotData"): for quoteData in self.common.searchDicKV(PlateSort, "snapshotData"): assert quoteData.get("last") is None assert quoteData.get("riseFall") is None if exchange in [ASE_exchange, NYSE_exchange, NASDAQ_exchange]: self.logger.debug("查询美股中概版和明星版, {}, {}".format(sub_quote_type, code)) for plate_type in ["START_STOCK", "CHINA_CONCEPT_STOCK"]: PlateSort = await api.QueryPlateSortMsgReqApi(isSubTrade=isSubTrade, zone="US", plate_type=plate_type, sort_direct="DESCENDING_ORDER", count=count, start_time_stamp=int(time.time() * 1000)) if self.common.searchDicKV(PlateSort, "snapshotData"): for quoteData in self.common.searchDicKV(PlateSort, "snapshotData"): assert quoteData.get("last") is None assert quoteData.get("riseFall") is None self.logger.debug("查询美股交易所排序--按涨跌排序, {}, {}".format(sub_quote_type, code)) ExchangeSort = await api.QueryExchangeSortMsgReqApi(isSubTrade=isSubTrade, exchange=exchange, sortFiled="R_F_RATIO", count=count, start_time_stamp=int(time.time() * 1000)) for ex_sort in self.common.searchDicKV(ExchangeSort, "snapshotData"): assert ex_sort.get("last") is None assert ex_sort.get("riseFall") is None finally: api.client.disconnect() # 验证状态改变后的推送通知 async def push_TradeStasut(self, exchange, code, loop): exchange = exchange product_list = [code] frequence = None start_time_stamp = int(time.time() * 1000) market_token = None asyncio.set_event_loop(loop) try: api = SubscribeApi(union_ws_url, loop, logger=self.logger) await api.client.ws_connect() await api.LoginReq(token=market_token, start_time_stamp=start_time_stamp, frequence=frequence) asyncio.run_coroutine_threadsafe(api.hearbeat_job(), loop) self.logger.debug(u'查询代码:{} 的交易状态, curtime : {}'.format(code, str(datetime.datetime.now()))) first_rsp_list = await api.QueryTradeStatusMsgReqApi(exchange=exchange, productList=product_list, recv_num=2) cur_status = self.common.searchDicKV(first_rsp_list, "status") self.logger.info("cur_status : {}".format(cur_status)) _start = time.time() PUSH_TRADE_STATUS = False while time.time() - _start < 120: # 循环120秒 rsp = await api.client.recv(recv_timeout_sec=5) if rsp: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(rsp[0]) # self.logger.debug(rev_data) if rev_data.type == QuoteMsgType.PUSH_TRADE_STATUS: PUSH_TRADE_STATUS = True tradeStatus = TradeStatusData() tradeStatus.ParseFromString(rev_data.data) self.logger.info(tradeStatus) assert tradeStatus.status != cur_status # 确认校验状态只变化的一次 assert PUSH_TRADE_STATUS self.logger.debug("代码 : {} 有推送交易状态".format(code)) finally: api.client.disconnect() async def Liquidation(self, exchange, code): # loop = asyncio.new_event_loop() # loop.run_until_complete(future=self.CleanData(exchange, code, loop, "REAL_QUOTE_MSG")) loop = asyncio.get_event_loop() await self.CleanData(exchange, code, loop, "REAL_QUOTE_MSG") # 清盘-延时行情校验 async def Liquidation_DELAY(self, exchange, code): """ 延迟清盘 """ # loop = asyncio.new_event_loop() # loop.run_until_complete(future=self.CleanData(exchange, code, loop, "DELAY_QUOTE_MSG")) loop = asyncio.get_event_loop() await self.CleanData(exchange, code, loop, "DELAY_QUOTE_MSG") async def check_TradeStatus(self, exchange, code): loop = asyncio.get_event_loop() await self.push_TradeStasut(exchange, code, loop) # 定时任务回调 def Listener(self, event): # 监听器, 输出对应的错误信息 if event.exception: self.logger.error("{} 异常, 错误信息为 : \n{}".format(event.job_id, event.traceback)) # 创建清盘定时任务 def run_CleanData(self): self.logger.debug("runner 定时任务验证清盘") HK_stock = [ [SEHK_exchange, SEHK_code1], [SEHK_exchange, SEHK_indexCode1], [SEHK_exchange, SEHK_TrstCode1], [SEHK_exchange, SEHK_WarrantCode1], [SEHK_exchange, SEHK_CbbcCode1], [SEHK_exchange, SEHK_InnerCode1], ] US_stock = [ [ASE_exchange, ASE_code1], [NYSE_exchange, NYSE_code1], [NASDAQ_exchange, NASDAQ_code1], [BATS_exchange, BATS_code1], ] # 实时订阅清盘定时任务 [self.scheduler.add_job(self.Liquidation, 'cron', day_of_week='mon-fri', hour="08", minute="55-59", args=product, id='CleanData>>{}'.format('-'.join(product)), max_instances=self.max_instances) for product in HK_stock] [self.scheduler.add_job(self.Liquidation, 'cron', day_of_week='mon-fri', hour="22", minute="25-29", args=product, id='CleanData>>{}'.format('-'.join(product)), max_instances=self.max_instances) for product in US_stock] [self.scheduler.add_job(self.Liquidation_DELAY, 'cron', day_of_week='mon-fri', hour="08", minute="55-59", args=product, id='DELAY_CleanData>>{}'.format('-'.join(product)), max_instances=self.max_instances) for product in HK_stock] [self.scheduler.add_job(self.Liquidation_DELAY, 'cron', day_of_week='mon-fri', hour="22", minute="25-29", args=product, id='DELAYCleanData>>{}'.format('-'.join(product)), max_instances=self.max_instances) for product in US_stock] # 从exchangeTradeTime遍历, 添加定时任务 curDate = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d") for key, value in exchangeTradeTime.items(): if key in ["HK_Stock", "US_Stock", "Grey"]: continue arg = key.split('_') arg[1] = arg[1] + "main" # print(arg) _time = datetime.datetime.strptime(curDate + value[0], '%Y-%m-%d%H:%M') # 开盘时间 if arg[0] not in ["HKFE", "SGX", "SEHK", "Grey"] and not isSummerTime: # 冬令时加一个小时 _time = datetime.datetime.strptime(curDate + value[0], '%Y-%m-%d%H:%M') + datetime.timedelta(hours=1) start_date = _time # copy一个变量 delay_endTime = _time start_date = start_date - datetime.timedelta(minutes=10) delay_endTime = delay_endTime + datetime.timedelta(minutes=15) # s_time = datetime.datetime.strftime(start_date, "%H%M") job_id = "CleanData_{}>>>start_date:{}, end_date:{}".format(arg, start_date, _time) # print(job_id) # self.scheduler.add_job(self.Liquidation, 'interval', minutes=1, start_date=start_date, end_date=_time, args=arg, id=job_id) self.scheduler.add_job(self.Liquidation, 'cron', day_of_week='mon-fri', hour=s_time[:-2], minute=s_time[-2:], args=arg, max_instances=self.max_instances, id=job_id) # 延时行情 delay_job_id = "DELAY_CleanData_{}>>>start_date:{}, end_date:{}".format(arg, start_date, delay_endTime) # print(delay_job_id) # self.scheduler.add_job(self.Liquidation_DELAY, 'interval', minutes=1, start_date=start_date, end_date=delay_endTime, args=arg, id=delay_job_id) self.scheduler.add_job(self.Liquidation_DELAY, 'cron', day_of_week='mon-fri', hour=s_time[:-2], minute=s_time[-2:], args=arg, max_instances=self.max_instances, id=delay_job_id) # 创建交易状态推送定时任务 def run_TradeStatus(self): self.logger.debug("交易交易状态推送通知") # 从exchangeTradeTime遍历, 添加定时任务 curDate = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d") for key, value in exchangeTradeTime.items(): if key in ["HK_Stock", "US_Stock", "Grey"]: continue arg = key.split('_') # 每个时间段, 交易状态都会发生变化 for val in value: _time = datetime.datetime.strptime(curDate + val, '%Y-%m-%d%H:%M') if arg[0] not in ["HKFE", "SGX", "SEHK", "Grey"] and not isSummerTime: # 冬令时加一个小时 _time = datetime.datetime.strptime(curDate + val, '%Y-%m-%d%H:%M') + datetime.timedelta(hours=1) _time = _time - datetime.timedelta(seconds=30) s_time = datetime.datetime.strftime(_time, "%H%M") job_id = "push_status_{}>>>BeginTime:{}".format('-'.join(arg), s_time) # print(job_id) self.scheduler.add_job(self.check_TradeStatus, 'cron', hour=s_time[:-2], minute=s_time[-2:], second="00", args=arg, max_instances=self.max_instances, id=job_id) # 港股 self.scheduler.add_job(self.check_TradeStatus, 'cron', day_of_week='mon-fri', hour="09", minute="29", second="00", args=["SEHK", "00700"], max_instances=self.max_instances, id="SEHK_pushStatus_open") self.scheduler.add_job(self.check_TradeStatus, 'cron', day_of_week='mon-fri', hour="11-12,15", minute="59", second="00", args=["SEHK", "00700"], max_instances=self.max_instances, id="SEHK_pushStatus") # 美股 self.scheduler.add_job(self.check_TradeStatus, 'cron', day_of_week='mon-fri', hour="22", minute="29", second="00", args=[NASDAQ_exchange, NASDAQ_code1], max_instances=self.max_instances, id="NASDAQ_pushStatus_open") self.scheduler.add_job(self.check_TradeStatus, 'cron', day_of_week='mon-fri', hour="04", minute="59", second="00", args=[NASDAQ_exchange, NASDAQ_code1], max_instances=self.max_instances, id="NASDAQ_pushStatus") self.scheduler.add_job(self.check_TradeStatus, 'cron', day_of_week='sat', hour="04", minute="59", second="00", args=[ NASDAQ_exchange, NASDAQ_code1], max_instances=self.max_instances, id="NASDAQ_pushStatus_sat") # 定时任务运行入口 def run_Scheduler(self): self.run_CleanData() self.run_TradeStatus() # 更新合约 self.scheduler.add_job(start, 'cron', hour="08", minute="10", args=['SYNC_INSTR_REQ', codegenerate_dealer_address], id="dealer_instr") self.scheduler.add_listener(self.Listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR) self.scheduler.start() try: asyncio.get_event_loop().run_forever() except (KeyboardInterrupt, SystemExit): pass
class Check(object): def __init__(self): self.common = Common() self.logger = get_log() self.http = MarketHttpClient() self.market_token = self.http.get_market_token( self.http.get_login_token(phone=login_phone, pwd=login_pwd, device_id=login_device_id)) self.new_loop = self.common.getNewLoop() asyncio.set_event_loop(self.new_loop) self.api = SubscribeApi(union_ws_url, self.new_loop) asyncio.get_event_loop().run_until_complete( future=self.api.client.ws_connect()) asyncio.get_event_loop().run_until_complete(future=self.api.LoginReq( token=self.market_token)) asyncio.run_coroutine_threadsafe(self.api.hearbeat_job(), self.new_loop) async def get_instrument(self, send_type, dealer_address): context = zmq.asyncio.Context(io_threads=5) dealer_socket = context.socket(socket_type=zmq.DEALER) dealer_socket.connect(dealer_address) poller = zmq.asyncio.Poller() poller.register(dealer_socket) send_flag = True json_rsp_data = None while True: for event in await poller.poll(): if send_type == 'SYNC_INSTR_REQ': if event[1] & zmq.POLLOUT and send_flag: data_send = SyncInstrReq( type=SyncInstrMsgType.ALL_INSTR, date_time=20200408 ) # ALL_INSTR 全量同步,INCREMENT_INSTR 增量同步 quote_msg = QuoteMsgCarrier( type=QuoteMsgType.SYNC_INSTR_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() await event[0].send(quote_msg) send_flag = False if event[1] & zmq.POLLIN: data = event[0].recv().result() rev_data = QuoteMsgCarrier() rev_data.ParseFromString(data) if rev_data.type == QuoteMsgType.SYNC_INSTR_RSP: rsp_data = SyncInstrRsp() rsp_data.ParseFromString(rev_data.data) json_rsp_data = json_format.MessageToJson(rsp_data) json_rsp_data = json.loads(json_rsp_data) break if event[1] & zmq.POLLERR: print("error:{0},{1}".format(event[0], event[1])) await asyncio.sleep(delay=5 ) # 使用 asyncio.sleep(), 它返回的是一个可等待的对象 if json_rsp_data: return json_rsp_data def get_mastr_code(self, exchange, product): start_time_stamp = int(time.time() * 1000) sub_type = SubscribeMsgType.SUB_WITH_PRODUCT base_info = [{'exchange': exchange, 'product_code': product}] rsp = asyncio.get_event_loop().run_until_complete( future=self.api.SubsQutoMsgReqApi( sub_type=sub_type, base_info=base_info, start_time_stamp=start_time_stamp)) master_code = None open_interrest = 0 for snapshot in rsp['before_snapshot_json_list']: if int(self.common.doDicEvaluate(snapshot['future'], 'openInterrest', 0)) >= open_interrest and \ 'main' not in snapshot['commonInfo']['instrCode']: master_code = snapshot['commonInfo']['instrCode'] open_interrest = int( self.common.doDicEvaluate(snapshot['future'], 'openInterrest', 0)) rsp = asyncio.get_event_loop().run_until_complete( future=self.api.UnSubsQutoMsgReqApi( unsub_type=sub_type, unbase_info=base_info, start_time_stamp=start_time_stamp)) return master_code def check_all_instr(self): instruments = asyncio.get_event_loop().run_until_complete( future=self.get_instrument('SYNC_INSTR_REQ', codegenerate_dealer_address)) self.logger.debug(instruments) for instrument in instruments['instruments']: if 'main' in instrument['base']['instrCode']: self.logger.debug(instrument) product = instrument['proc']['code'] exchange = instrument['base']['exchange'] # product = 'CUS' # exchange = 'HKFE' master_code = self.get_mastr_code(exchange, product) api_master_code = instrument['future']['relatedInstr'] assert master_code == api_master_code
def __init__(self): self.common = Common() self.sq = MarketSqliteDBClient() self.my_font = FontProperties( fname='/usr/share/fonts/cjkuni-uming/uming.ttc')
def __init__(self): self.cookies = RequestsCookieJar() self.verify = False self.common = Common()
def __init__(self, ws_url, new_loop, extra_headers=None): self.new_loop = new_loop self.client = BaseWebSocketClient(url=ws_url, loop=self.new_loop, extra_headers=extra_headers) self.common = Common()
def __init__(self, host=redis_host, port=redis_port): self.host = host self.port = port self.conn = self.__connectPika(self.host, self.port) self.logger = get_log() self.common = Common()
class SubscribeApi(object): def __init__(self, ws_url, new_loop): self.new_loop = new_loop self.client = BaseWebSocketClient(url=ws_url, loop=self.new_loop) self.common = Common() # 登录请求 async def LoginReq(self, token, start_time_stamp=None): data_send = LoginReq(auto_token=token, start_time_stamp=start_time_stamp) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.LOGIN_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg) json_login_list = [] for req in all_req: recv_data = QuoteMsgCarrier() recv_data.ParseFromString(req) if recv_data.type == QuoteMsgType.LOGIN_RSP: login_data = LoginRsp() login_data.ParseFromString(recv_data.data) json_login_data = json_format.MessageToJson(login_data) json_login_data = json.loads(json_login_data) json_login_list.append(json_login_data) # print(json_login_list) return json_login_list # 登出请求 async def LogoutReq(self, start_time_stamp=None, recv_num=1): data_send = LoginReq(start_time_stamp=start_time_stamp) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.LOGOUT_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg, recv_num=recv_num) json_logout_list = [] for req in all_req: recv_data = QuoteMsgCarrier() recv_data.ParseFromString(req) if recv_data.type == QuoteMsgType.LOGOUT_RSP: login_data = LogoutRsp() login_data.ParseFromString(recv_data.data) json_logout_data = json_format.MessageToJson(login_data) json_logout_data = json.loads(json_logout_data) json_logout_list.append(json_logout_data) print('LogoutReq json_logout_list:{}'.format(json_logout_list)) return json_logout_list # 心跳 async def HearbeatReqApi(self, connid, isKeep=False): data_send = HeartReqMsg(conn_id=connid) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.HEARTBEAT_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() if isKeep == False: all_req = await self.client.send_and_recv(quote_msg) json_rsp_list = [] for base_req in all_req: if base_req == None: continue rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.HEARTBEAT_RSP: single_data = HeartRspMsg() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) # print('HearbeatReqApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list else: await self.client.send(quote_msg) # 保持心跳 async def hearbeat_job(self, gap_time=1): i = 1 asyncio.set_event_loop(self.new_loop) while True: if self.client.is_disconnect(): break await self.HearbeatReqApi(connid=i, isKeep=True) # print('The {} time to keep heartbeat! SendTimeStamp:{}'.format(i, str(time.time()))) await asyncio.sleep(gap_time) i = i + 1 # 测速 async def VelocityReqApi(self, start_time=None): data_send = VelocityReqMsg(start_time=start_time) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.VELOCITY_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg) json_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.VELOCITY_RSP: single_data = VelocityRspMsg() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) # print('VelocityReqApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list # 订阅 async def SubsQutoMsgReqApi(self, sub_type=None, child_type=None, base_info=None, recv_num=1, start_time_stamp=None, sub_quote_type=SubQuoteMsgType.REAL_QUOTE_MSG): # Return: # { # 'first_rsp_list': [{}, {}, {}], # 表示订阅应答响应 SUBSCRIBE_RSP # 'snapshot_json_list': [{}, {}, {}, {}, {}], # 表示返回的当前时间之前的合约快照数据 # 'basic_json_list': [{}, {}, {}, {}, {}] # 表示返回的当前时间之前的合约静态数据 # } # print('''sub_type:{}, child_type:{}, base_info:{}'''.format(sub_type, child_type, base_info)) send_data = SubscribeQuoteMsgReq(sub_type=sub_type, child_type=child_type, base_info=base_info, start_time_stamp=start_time_stamp, sub_quote_type=sub_quote_type) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.SUBSCRIBE_REQ, data=send_data.SerializeToString()) quote_msg = quote_msg.SerializeToString() await self.HearbeatReqApi(connid=10086, isKeep=True) # 避免循环接消息时心跳断掉 all_req = await self.client.send_and_recv(quote_msg, recv_num=recv_num) first_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.SUBSCRIBE_RSP: single_data = SubscribeQuoteMsgRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) first_rsp_list.append(json_single_data) elif rev_data.type == QuoteMsgType.HEARTBEAT_RSP: # 如果接到心跳数据应忽略,继续接受一次 all_req = await self.client.recv(recv_num=1) for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.SUBSCRIBE_RSP: single_data = SubscribeQuoteMsgRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson( single_data) json_single_data = json.loads(json_single_data) first_rsp_list.append(json_single_data) finally_rsp = {} finally_rsp['first_rsp_list'] = first_rsp_list before_snapshot_json_list = [] before_basic_json_list = [] # 接收该合约前一刻的最新一笔静态、快照,直到更新时间超出请求时间则跳出 # while True: # before_time_reqs = await self.client.recv(recv_num=1) # if before_time_reqs != []: # base_req = before_time_reqs[0] # rev_data = QuoteMsgCarrier() # rev_data.ParseFromString(base_req) # if rev_data.type == QuoteMsgType.PUSH_SNAPSHOT: # single_data = QuoteSnapshot() # single_data.ParseFromString(rev_data.data) # json_snapshot_data = json_format.MessageToJson(single_data) # json_snapshot_data = json.loads(json_snapshot_data) # # print('before_json_snapshot_data:{}'.format(json_snapshot_data)) # req_source_time = self.common.searchDicKV(json_snapshot_data, 'sourceUpdateTime') # instr_code = self.common.searchDicKV(json_snapshot_data, 'instrCode') # # 毫秒级别对比 # if int(int(req_source_time) / (pow(10, 6))) < start_time_stamp: # before_snapshot_json_list.append(json_snapshot_data) # # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_snapshot_data) # else: # break # elif rev_data.type == QuoteMsgType.PUSH_BASIC: # single_data = QuoteBasicInfo() # single_data.ParseFromString(rev_data.data) # basic_single_data = json_format.MessageToJson(single_data) # basic_single_data = json.loads(basic_single_data) # # print('before_basic_single_data:{}'.format(basic_single_data)) # req_source_time = self.common.searchDicKV(basic_single_data, 'updateTimestamp') # instr_code = self.common.searchDicKV(basic_single_data, 'instrCode') # # 毫秒级别对比 # if int(int(req_source_time) / (pow(10, 6))) < start_time_stamp: # before_basic_json_list.append(basic_single_data) # # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, basic_single_data) # else: # break # elif rev_data.type not in [QuoteMsgType.PUSH_SNAPSHOT, QuoteMsgType.PUSH_BASIC, QuoteMsgType.HEARTBEAT_RSP, QuoteMsgType.SUBSCRIBE_RSP]: # break # else: # break finally_rsp['before_snapshot_json_list'] = before_snapshot_json_list finally_rsp['before_basic_json_list'] = before_basic_json_list return finally_rsp # 取消行情订阅 async def UnSubsQutoMsgReqApi(self, unsub_type, unchild_type=None, unbase_info=None, recv_num=20, start_time_stamp=None): # print('''sub_type:{}, child_type:{}, base_info:{}'''.format(unsub_type, unchild_type, unbase_info)) send_data = UnsubscribeQuoteMsgReq(sub_type=unsub_type, child_type=unchild_type, base_info=unbase_info, stat_time_stamp=start_time_stamp) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.UNSUBSCRIBE_REQ, data=send_data.SerializeToString()) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg, recv_num=recv_num) json_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.UNSUBSCRIBE_RSP: single_data = UnsubscribeQuoteMsgRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) # print('UnSubsQutoMsgReqApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list # 订阅盘口数据(依赖订阅接口) async def QuoteOrderBookDataApi(self, recv_num=10, is_filter=True): await self.HearbeatReqApi(connid=10086, isKeep=True) # 避免循环接消息时心跳断掉 all_req = await self.client.recv(recv_num) json_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if is_filter: if rev_data.type == QuoteMsgType.PUSH_ORDER_BOOK: single_data = QuoteOrderBookData() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV( json_single_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) else: single_data = QuoteOrderBookData() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_single_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) # print('QuoteOrderBookDataApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list # 订阅逐笔成交数据(依赖订阅接口) async def QuoteTradeDataApi(self, recv_num=10, is_filter=True): all_req = await self.client.recv(recv_num) json_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if is_filter: if rev_data.type == QuoteMsgType.PUSH_TRADE_DATA: single_data = QuoteTradeData() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV( json_single_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) else: single_data = QuoteTradeData() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_single_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) # print('QuoteTradeDataApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list # 订阅静态数据(依赖订阅接口) async def QuoteBasicInfoApi(self, recv_num=10, is_filter=True): all_req = await self.client.recv(recv_num) json_basic_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if is_filter: if rev_data.type == QuoteMsgType.PUSH_BASIC: basic_data = QuoteBasicInfo() basic_data.ParseFromString(rev_data.data) json_basic_data = json_format.MessageToJson(basic_data) json_basic_data = json.loads(json_basic_data) json_basic_list.append(json_basic_data) req_source_time = self.common.searchDicKV( json_basic_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV( json_basic_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_basic_data) else: basic_data = QuoteTradeData() basic_data.ParseFromString(rev_data.data) json_basic_data = json_format.MessageToJson(basic_data) json_basic_data = json.loads(json_basic_data) json_basic_list.append(json_basic_data) req_source_time = self.common.searchDicKV( json_basic_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_basic_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_basic_data) # print('QuoteBasicInfoApi json_rsp_list:{}'.format(json_basic_list)) return json_basic_list # 订阅快照数据(依赖订阅接口) async def QuoteSnapshotApi(self, recv_num=10, is_filter=True): if self.client.is_disconnect(): return [] await self.HearbeatReqApi(connid=10086, isKeep=True) # 避免循环接消息时心跳断掉 all_req = await self.client.recv(recv_num) json_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if is_filter: if rev_data.type == QuoteMsgType.PUSH_SNAPSHOT: basic_data = QuoteSnapshot() basic_data.ParseFromString(rev_data.data) json_basic_data = json_format.MessageToJson(basic_data) json_basic_data = json.loads(json_basic_data) json_rsp_list.append(json_basic_data) req_source_time = self.common.searchDicKV( json_basic_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV( json_basic_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_basic_data) else: basic_data = QuoteSnapshot() basic_data.ParseFromString(rev_data.data) json_basic_data = json_format.MessageToJson(basic_data) json_basic_data = json.loads(json_basic_data) json_rsp_list.append(json_basic_data) req_source_time = self.common.searchDicKV( json_basic_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_basic_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_basic_data) # print('QuoteSnapshotApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list # 手机Api---开始请求分时页面数据 async def StartChartDataReqApi(self, exchange, code, frequency, start_time, start_time_stamp): data_send = StartChartDataReq(exchange=exchange, code=code, frequency=frequency, start_time=start_time, start_time_stamp=start_time_stamp) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.START_CHART_DATA_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg) first_app_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.START_CHART_DATA_RSP: single_data = StartChartDataRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) first_app_rsp_list.append(json_single_data) # print('StartChartDataReqApi first_rsp_list:{}'.format(first_app_rsp_list)) finally_rsp = {} finally_rsp['first_app_rsp_list'] = first_app_rsp_list before_appsnapshot_json_list = [] before_appbasic_json_list = [] # 接收该app订阅前一刻的最新一笔静态、快照,直到更新时间超出请求时间则跳出 while True: before_time_reqs = await self.client.recv(recv_num=1) if before_time_reqs != []: for base_req in before_time_reqs: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.PUSH_SNAPSHOT: single_data = QuoteSnapshot() single_data.ParseFromString(rev_data.data) json_snapshot_data = json_format.MessageToJson( single_data) json_snapshot_data = json.loads(json_snapshot_data) req_source_time = self.common.searchDicKV( json_snapshot_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV( json_snapshot_data, 'instrCode') # 毫秒级别对比 if int(req_source_time / (pow(10, 6))) < start_time_stamp: before_appsnapshot_json_list.append( json_snapshot_data) # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_snapshot_data) else: break elif rev_data.type == QuoteMsgType.PUSH_BASIC: single_data = QuoteBasicInfo() single_data.ParseFromString(rev_data.data) basic_single_data = json_format.MessageToJson( single_data) basic_single_data = json.loads(basic_single_data) req_source_time = self.common.searchDicKV( basic_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV( basic_single_data, 'instrCode') # 毫秒级别对比 if int(req_source_time / (pow(10, 6))) < start_time_stamp: before_appbasic_json_list.append(basic_single_data) # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, basic_single_data) else: break else: break else: break finally_rsp[ 'before_appsnapshot_json_list'] = before_appsnapshot_json_list finally_rsp['before_appbasic_json_list'] = before_appbasic_json_list # print('StartChartDataReqApi finally_rsp:{}'.format(finally_rsp)) return finally_rsp # 手机Api---结束请求分时页面数据 async def StopChartDataReqApi(self, exchange, code, start_time_stamp): data_send = StopChartDataReq(exchange=exchange, code=code, start_time_stamp=start_time_stamp) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.STOP_CHART_DATA_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg) json_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.STOP_CHART_DATA_RSP: single_data = StopChartDataRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) # print('StopChartDataReqApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list # 手机Api---推送分时数据 async def PushKLineMinDataApi(self, recv_num=10, is_filter=True): all_req = await self.client.recv(recv_num) json_basic_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if is_filter: if rev_data.type == QuoteMsgType.PUSH_KLINE_MIN: basic_data = PushKLineMinData() basic_data.ParseFromString(rev_data.data) json_basic_data = json_format.MessageToJson(basic_data) json_basic_data = json.loads(json_basic_data) json_basic_list.append(json_basic_data) req_source_time = self.common.searchDicKV( json_basic_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV( json_basic_data, 'code') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_basic_data) else: basic_data = PushKLineMinData() basic_data.ParseFromString(rev_data.data) json_basic_data = json_format.MessageToJson(basic_data) json_basic_data = json.loads(json_basic_data) json_basic_list.append(json_basic_data) req_source_time = self.common.searchDicKV( json_basic_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_basic_data, 'code') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_basic_data) # print('PushKLineMinDataApi json_rsp_list:{}'.format(json_basic_list)) return json_basic_list # 手机Api---请求分时页面数据,会主推静态、快照、盘口、逐笔、分时数据,此方法用于接收 async def AppQuoteAllApi(self, recv_num=20): all_req = await self.client.recv(recv_num) json_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.PUSH_BASIC: single_data = QuoteBasicInfo() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_single_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) elif rev_data.type == QuoteMsgType.PUSH_SNAPSHOT: single_data = QuoteSnapshot() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_single_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) elif rev_data.type == QuoteMsgType.PUSH_ORDER_BOOK: single_data = QuoteOrderBookData() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_single_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) elif rev_data.type == QuoteMsgType.PUSH_TRADE_DATA: single_data = QuoteTradeData() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_single_data, 'instrCode') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) elif rev_data.type == QuoteMsgType.PUSH_KLINE_MIN: single_data = PushKLineMinData() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) req_source_time = self.common.searchDicKV( json_single_data, 'sourceUpdateTime') instr_code = self.common.searchDicKV(json_single_data, 'code') # self.sq.subscribe_new_record(rev_data.type, instr_code, req_source_time, json_single_data) # print('AppQuoteAllApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list # 手机Api---查询K线数据 async def QueryKLineMsgReqApi(self, exchange, code, peroid_type, type, direct, start, end): now_timestamp = int(time.time() * 1000) # 毫秒时间戳 data_send = QueryKLineMsgReq(exchange=exchange, code=code, peroid_type=peroid_type, type=type, direct=direct, start=start, end=end, start_time_stamp=now_timestamp) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.QUERY_KLINE_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg) json_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.QUERY_KLINE_RSP: single_data = QueryKLineMsgRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) json_rsp_list.append(json_single_data) # print('QueryKLineMsgReqApi json_rsp_list:{}'.format(json_rsp_list)) return json_rsp_list # app查询分时数据 async def QueryKLineMinMsgReqApi( self, isSubKLineMin, exchange, code, query_type=None, direct=None, start=None, end=None, vol=None, start_time_stamp=None, recv_num=1, sub_quote_type=SubQuoteMsgType.REAL_QUOTE_MSG): # app订阅服务中,该api只会自动返回当天的分时数据,与入参无关 ''' message QueryKLineMinMsgReq { bool isSubKLineMin = 1; // 是否订阅KLineMin common.ExchangeType exchange = 2; // 交易所 string code = 3; // 合约代码 QueryKLineMsgType type = 4; // 获取K线的方式 QueryKLineDirectType direct = 5; // 查询K线的方向 uint64 start = 6; // 开始时间 uint64 end = 7; // 结束时间 uint64 vol = 8; // 按量查询时此字段表示多少根 uint64 start_time_stamp = 9; // 发起请求的时间戳 UTC时间戳 } ''' await self.HearbeatReqApi(connid=10086, isKeep=True) # 避免循环接消息时心跳断掉 data_send = QueryKLineMinMsgReq(isSubKLineMin=isSubKLineMin, exchange=exchange, code=code, type=query_type, direct=direct, start=start, end=end, vol=vol, start_time_stamp=start_time_stamp, sub_quote_type=sub_quote_type) quote_msg = QuoteMsgCarrier(type=QuoteMsgType.QUERY_KLINE_MIN_REQ, data=data_send.SerializeToString()) quote_msg = quote_msg.SerializeToString() all_req = await self.client.send_and_recv(quote_msg, recv_num=recv_num) final_rsp = {} query_kline_min_rsp_list = [] sub_kline_min_rsp_list = [] for base_req in all_req: rev_data = QuoteMsgCarrier() rev_data.ParseFromString(base_req) if rev_data.type == QuoteMsgType.QUERY_KLINE_MIN_RSP: single_data = QueryKLineMinMsgRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) query_kline_min_rsp_list.append(json_single_data) elif rev_data.type == QuoteMsgType.SUBSCRIBE_KLINE_MIN_RSP: single_data = SubscribeKlineMinRsp() single_data.ParseFromString(rev_data.data) json_single_data = json_format.MessageToJson(single_data) json_single_data = json.loads(json_single_data) sub_kline_min_rsp_list.append(json_single_data) final_rsp['query_kline_min_rsp_list'] = query_kline_min_rsp_list final_rsp['sub_kline_min_rsp_list'] = sub_kline_min_rsp_list # print(final_rsp) return final_rsp
class Test_Comtrast_PRD(): common = Common() logger = get_log() market_token = None @pytest.mark.parametrize("exchange, code", [ ("SEHK", "00700"), ("SEHK", SEHK_indexCode1), (ASE_exchange, ASE_code1), (NASDAQ_exchange, NASDAQ_code1), (NYSE_exchange, NYSE_code1), (HK_exchange, HK_main1), (NYMEX_exchange, NYMEX_code1), ]) def test_contrast_StartChartDataReq(self, SZ_api, HK_api, exchange, code): """订阅手机图表数据(手机专用)--订阅一个港股,frequence=4""" # exchange = SEHK_exchange # code = "00700" exchange = exchange code = code frequence = None start_time_stamp = int(time.time() * 1000) loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete( future=SZ_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop) self.logger.debug(u'订阅深圳--手机图表') sz_rsp_list = loop.run_until_complete( future=SZ_api.StartChartDataReqApi( exchange, code, start_time_stamp, recv_num=1)) app_rsp = sz_rsp_list[0] assert self.common.searchDicKV(app_rsp, 'retCode') == 'SUCCESS' sz_basic_json_list = app_rsp['basicData'] sz_before_snapshot_json_list = app_rsp['snapshot'] sz_before_orderbook_json_list = [] if self.common.searchDicKV(sz_before_snapshot_json_list, "dataType") != "EX_INDEX": sz_before_orderbook_json_list = app_rsp['orderbook'] del sz_basic_json_list["commonInfo"] del sz_before_snapshot_json_list["commonInfo"] loop.run_until_complete( future=HK_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop) self.logger.debug(u'订阅香港--手机图表') hk_rsp_list = loop.run_until_complete( future=HK_api.StartChartDataReqApi( exchange, code, start_time_stamp, recv_num=1)) app_rsp = hk_rsp_list[0] assert self.common.searchDicKV(app_rsp, 'retCode') == 'SUCCESS' hk_basic_json_list = app_rsp['basicData'] hk_before_snapshot_json_list = app_rsp['snapshot'] hk_before_orderbook_json_list = [] if self.common.searchDicKV(hk_before_snapshot_json_list, "dataType") != "EX_INDEX": hk_before_orderbook_json_list = app_rsp['orderbook'] del hk_basic_json_list["commonInfo"] del hk_before_snapshot_json_list["commonInfo"] assert sz_basic_json_list == hk_basic_json_list assert sz_before_snapshot_json_list == hk_before_snapshot_json_list if sz_before_orderbook_json_list or hk_before_orderbook_json_list: del sz_before_orderbook_json_list["commonInfo"] del hk_before_orderbook_json_list["commonInfo"] assert sz_before_orderbook_json_list == hk_before_orderbook_json_list @pytest.mark.parametrize("exchange, code", [ ("SEHK", "00700"), ("SEHK", SEHK_indexCode1), (ASE_exchange, ASE_code1), (NASDAQ_exchange, NASDAQ_code1), (NYSE_exchange, NYSE_code1), (HK_exchange, HK_main1), (NYMEX_exchange, NYMEX_code1), ]) def test_contrast_QueryKLineMinMsgReqApi(self, SZ_api, HK_api, exchange, code): """分时查询--查询并订阅港股的分时数据: isSubKLineMin = True""" frequence = 100 isSubKLineMin = False exchange = exchange code = code query_type = QueryKLineMsgType.UNKNOWN_QUERY_KLINE # app 订阅服务该字段无意义 direct = QueryKLineDirectType.WITH_BACK # app 订阅服务该字段无意义 start = 0 # app 订阅服务该字段无意义 end = 0 # app 订阅服务该字段无意义 vol = 0 # app 订阅服务该字段无意义 start_time_stamp = int(time.time() * 1000) loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete( future=SZ_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop) self.logger.debug(u'查询深圳分时数据') final_rsp = loop.run_until_complete( future=SZ_api.QueryKLineMinMsgReqApi( isSubKLineMin, exchange, code, query_type, direct, start, end, vol, start_time_stamp)) sz_query_kline_min_rsp_list = final_rsp['query_kline_min_rsp_list'][0] assert self.common.searchDicKV(sz_query_kline_min_rsp_list, 'retCode') == 'SUCCESS' loop.run_until_complete( future=HK_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop) self.logger.debug(u'查询深圳分时数据') final_rsp = loop.run_until_complete( future=HK_api.QueryKLineMinMsgReqApi( isSubKLineMin, exchange, code, query_type, direct, start, end, vol, start_time_stamp)) hk_query_kline_min_rsp_list = final_rsp['query_kline_min_rsp_list'][0] assert self.common.searchDicKV(hk_query_kline_min_rsp_list, 'retCode') == 'SUCCESS' assert sz_query_kline_min_rsp_list.get( "data") == hk_query_kline_min_rsp_list.get("data") @pytest.mark.parametrize("exchange, code", [ ("SEHK", "00700"), ("SEHK", SEHK_indexCode1), (ASE_exchange, ASE_code1), (NASDAQ_exchange, NASDAQ_code1), (NYSE_exchange, NYSE_code1), (HK_exchange, HK_main1), (NYMEX_exchange, NYMEX_code1), ]) def test_contrast_QueryFiveDaysKLineMinReqApi(self, SZ_api, HK_api, exchange, code): """五日分时查询, 查询并订阅数据: isSubKLineMin = True""" frequence = 100 isSubKLineMin = False exchange = exchange code = code start = None # app 订阅服务该字段无意义 start_time_stamp = int(time.time() * 1000) loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) # 深圳 loop.run_until_complete( future=SZ_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop) final_rsp = loop.run_until_complete( future=SZ_api.QueryFiveDaysKLineMinReqApi( isSubKLineMin, exchange, code, start, start_time_stamp)) sz_query_5day_klinemin_rsp_list = final_rsp[ 'query_5day_klinemin_rsp_list'][0] assert self.common.searchDicKV(sz_query_5day_klinemin_rsp_list, 'retCode') == 'SUCCESS' # 香港 loop.run_until_complete( future=HK_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop) final_rsp = loop.run_until_complete( future=HK_api.QueryFiveDaysKLineMinReqApi( isSubKLineMin, exchange, code, start, start_time_stamp)) hk_query_5day_klinemin_rsp_list = final_rsp[ 'query_5day_klinemin_rsp_list'][0] assert self.common.searchDicKV(hk_query_5day_klinemin_rsp_list, 'retCode') == 'SUCCESS' assert sz_query_5day_klinemin_rsp_list.get( "hk_query_5day_klinemin_rsp_list" ) == hk_query_5day_klinemin_rsp_list.get( "hk_query_5day_klinemin_rsp_list") @pytest.mark.parametrize("exchange, code", [ ("SEHK", "00700"), ("SEHK", SEHK_indexCode1), (ASE_exchange, ASE_code1), (NASDAQ_exchange, NASDAQ_code1), (NYSE_exchange, NYSE_code1), (HK_exchange, HK_main1), (NYMEX_exchange, NYMEX_code1), ]) def test_contrast_QueryKLineMsgReqApi(self, SZ_api, HK_api, exchange, code): """K线查询港股: 按BY_DATE_TIME方式查询, 1分K, 前一小时的数据, 并订阅K线数据""" frequence = None isSubKLine = False exchange = SEHK_exchange code = SEHK_code1 peroid_type = KLinePeriodType.MINUTE query_type = QueryKLineMsgType.BY_VOL direct = QueryKLineDirectType.WITH_FRONT start_time_stamp = int(time.time() * 1000) start = start_time_stamp end = None vol = 80 loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete( future=SZ_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop) self.logger.debug(u'查询K线数据,并检查返回结果') final_rsp = loop.run_until_complete(future=SZ_api.QueryKLineMsgReqApi( isSubKLine, exchange, code, peroid_type, query_type, direct, start, end, vol, start_time_stamp)) sz_query_kline_rsp_list = final_rsp['query_kline_rsp_list'] assert self.common.searchDicKV(sz_query_kline_rsp_list[0], 'retCode') == 'SUCCESS' loop.run_until_complete( future=HK_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop) self.logger.debug(u'查询K线数据,并检查返回结果') final_rsp = loop.run_until_complete(future=HK_api.QueryKLineMsgReqApi( isSubKLine, exchange, code, peroid_type, query_type, direct, start, end, vol, start_time_stamp)) hk_query_kline_rsp_list = final_rsp['query_kline_rsp_list'] assert self.common.searchDicKV(hk_query_kline_rsp_list[0], 'retCode') == 'SUCCESS' assert sz_query_kline_rsp_list[0].get( "kData") == hk_query_kline_rsp_list[0].get("kData") @pytest.mark.parametrize("exchange, code", [ ("SEHK", "00700"), (ASE_exchange, ASE_code1), (NASDAQ_exchange, NASDAQ_code1), (NYSE_exchange, NYSE_code1), (HK_exchange, HK_main1), (NYMEX_exchange, NYMEX_code1), ]) def test_contrast_QueryTradeTickMsgReqApi(self, SZ_api, HK_api, exchange, code): """ 查询逐笔成交--查询5分钟的逐笔成交数据, 并订阅逐笔成交 """ frequence = 100 isSubTrade = False exchange = exchange code = code type = QueryKLineMsgType.BY_VOL direct = QueryKLineDirectType.WITH_FRONT start_time_stamp = int(time.time() * 1000) start_time = start_time_stamp end_time = None vol = 100 loop = asyncio.get_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete( future=SZ_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(SZ_api.hearbeat_job(), loop) self.logger.debug(u'逐笔成交查询,并检查返回结果') _start = time.time() final_rsp = loop.run_until_complete( future=SZ_api.QueryTradeTickMsgReqApi( isSubTrade, exchange, code, type, direct, start_time, end_time, vol, start_time_stamp)) self.logger.debug("查询回包时间 : {}".format(time.time() - _start)) sz_query_trade_tick_rsp_list = final_rsp['query_trade_tick_rsp_list'] assert self.common.searchDicKV(sz_query_trade_tick_rsp_list[0], 'retCode') == 'SUCCESS' loop.run_until_complete( future=HK_api.LoginReq(token=self.market_token, start_time_stamp=start_time_stamp, frequence=frequence)) asyncio.run_coroutine_threadsafe(HK_api.hearbeat_job(), loop) self.logger.debug(u'逐笔成交查询,并检查返回结果') final_rsp = loop.run_until_complete( future=HK_api.QueryTradeTickMsgReqApi( isSubTrade, exchange, code, type, direct, start_time, end_time, vol, start_time_stamp)) hk_query_trade_tick_rsp_list = final_rsp['query_trade_tick_rsp_list'] assert self.common.searchDicKV(hk_query_trade_tick_rsp_list[0], 'retCode') == 'SUCCESS' assert sz_query_trade_tick_rsp_list[0].get( "tickData") == hk_query_trade_tick_rsp_list[0].get("tickData")
def __init__(self, db_path): self.db = rocksdb.DB(db_path, rocksdb.Options(create_if_missing=False), read_only=True) self.logger = get_log() self.common = Common()