From 60d02096d9dc4bc9cb54f8cf81becd10a9dd0c61 Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期二, 24 十月 2023 16:22:47 +0800 Subject: [PATCH] L前3s囊括一次/增加L2数据接口/接口输出交易信息 --- utils/data_export_util.py | 156 +++++++++++++++++++++++++++++++++------------------ 1 files changed, 101 insertions(+), 55 deletions(-) diff --git a/utils/data_export_util.py b/utils/data_export_util.py index 5954772..fa26cd3 100644 --- a/utils/data_export_util.py +++ b/utils/data_export_util.py @@ -24,10 +24,21 @@ # 鑾峰彇L2澶勭悊浣嶇疆淇℃伅 process_indexs = log_export.get_l2_process_position(code, date) trade_indexs = log_export.get_l2_trade_position(code, date) - export_l2_data(code, datas, process_indexs, trade_indexs) + fdatas = export_l2_data(code, datas, process_indexs, trade_indexs) + __save_l2_datas(code, fdatas) -def export_l2_data(code, datas, process_indexs, trade_indexs, dest_dir=f"{constant.get_path_prefix()}/export/l2"): +# 鑾峰彇L2鐨勬暟鎹� +def get_l2_datas(code, date=None): + local_today_datas = log_export.load_l2_from_log(date) + datas = local_today_datas[code] + process_indexs = log_export.get_l2_process_position(code, date) + trade_indexs = log_export.get_l2_trade_position(code, date) + fdatas = export_l2_data(code, datas, process_indexs, trade_indexs) + return fdatas + + +def export_l2_data(code, datas, process_indexs, trade_indexs): def find_process_index(index): for i in range(0, len(process_indexs)): if process_indexs[i][0] <= index <= process_indexs[i][1]: @@ -54,48 +65,23 @@ # num_dict[data["val"]["num"]] = [] # num_dict[data["val"]["num"]].append(data) - local_time = time.strftime("%Y%m%dT%H%M%S", time.localtime(time.time())) - file_name = "{}/{}_{}.xls".format(dest_dir, code, local_time) - file_name_txt = "{}/{}_{}.txt".format(dest_dir, code, local_time) - with open(file_name_txt, 'w') as openfile: - for data in datas: - openfile.write(json.dumps(data) + "\n") - wb = xlwt.Workbook(encoding="utf-8") - ws = wb.add_sheet('sheet1') - ws.write(0, 0, '搴忓彿') - ws.write(0, 1, '鏃堕棿') - ws.write(0, 2, '涔版挙闂撮殧') - ws.write(0, 3, '閲戦') - ws.write(0, 4, '浠锋牸') - ws.write(0, 5, '鎵嬫暟') - ws.write(0, 6, '绫诲瀷') - ws.write(0, 7, '閲嶅鏁伴噺') - ws.write(0, 8, '鎾ゅ崟鏃堕棿') - ws.write(0, 9, '璁㈠崟鍙�') - index = 0 + fdatas = [] for data in datas: index += 1 trade_info = find_trade_index(data["index"]) - font = xlwt.Font() - if trade_info: - if trade_info[0] == 0: - font.colour_index = 53 - elif trade_info[0] == 1: - font.colour_index = 17 - elif trade_info[0] == 2: - font.colour_index = 10 - ws.write(index, 8, trade_info[2]) - style = None + style_int = None if find_process_index(data["index"]) % 2 == 0: - style = xlwt.easyxf('pattern: pattern solid') + style_int = 0 else: - style = xlwt.easyxf('pattern: pattern solid, fore_colour light_yellow') - style.font = font - cancel_style = xlwt.easyxf('pattern: pattern solid, fore_colour gray25') + style_int = 1 - ws.write(index, 0, data["index"], style) - ws.write(index, 1, data["val"]["time"] + (f".{data['val']['tms']}" if "tms" in data["val"] else ''), style) + format_data = [] + # 绱㈠紩 + format_data.append(data["index"]) + # 鏃堕棿 + format_data.append(data["val"]["time"] + (f".{data['val']['tms']}" if "tms" in data["val"] else '')) + cancel_time = data["val"]["cancelTime"] if cancel_time == '0': cancel_time = '' @@ -108,55 +94,115 @@ cancel_time += "m" elif int(data["val"]["cancelTimeUnit"]) == 2: cancel_time += "h" + # 鎾ゅ崟闂撮殧鏃堕棿 + format_data.append(cancel_time) + # 閲戦 + format_data.append("{}涓�".format(round(int(data["val"]["num"]) * float(data["val"]["price"]) / 100, 1))) + # 鍗曚环 + format_data.append(data["val"]["price"]) - ws.write(index, 2, cancel_time, style) - ws.write(index, 4, data["val"]["price"], style) if int(data["val"]["operateType"]) == 1 or int(data["val"]["operateType"]) == 2: - ws.write(index, 5, 0 - int(data["val"]["num"]), style) + format_data.append(0 - int(data["val"]["num"])) else: - ws.write(index, 5, int(data["val"]["num"]), style) + format_data.append(int(data["val"]["num"])) limit_price = "" if int(data["val"]["limitPrice"]) == 1: limit_price = "娑ㄥ仠" elif int(data["val"]["limitPrice"]) == 2: limit_price = "璺屽仠" - + operateDesc = "" if int(data["val"]["operateType"]) == 0: if len(limit_price) > 0: - ws.write(index, 6, '涔� ({})'.format(limit_price), style) + operateDesc = '涔� ({})'.format(limit_price) else: - ws.write(index, 6, '涔�', style) + operateDesc = '涔�' elif int(data["val"]["operateType"]) == 1: if len(limit_price) > 0: - ws.write(index, 6, '涔版挙 ({})'.format(limit_price), style) + operateDesc = '涔版挙 ({})'.format(limit_price) + else: - ws.write(index, 6, '涔版挙', style) + operateDesc = '涔版挙' + elif int(data["val"]["operateType"]) == 2: if len(limit_price) > 0: - ws.write(index, 6, '鍗� ({})'.format(limit_price), style) + operateDesc = '鍗� ({})'.format(limit_price) else: - ws.write(index, 6, '鍗�', style) + operateDesc = '鍗�' + elif int(data["val"]["operateType"]) == 3: if len(limit_price) > 0: - ws.write(index, 6, '鍗栨挙 ({})'.format(limit_price), style) + operateDesc = '鍗栨挙 ({})'.format(limit_price) else: - ws.write(index, 6, '鍗栨挙', style) - ws.write(index, 7, data["re"], style) - ws.write(index, 9, data["val"].get("orderNo"), style) + operateDesc = '鍗栨挙' + format_data.append(operateDesc) + format_data.append(data["re"]) # 鏌ヨ鏄惁鎾ゅ崟 + cancel_info = None if int(data["val"]["operateType"]) == 0: cancel_data = l2.l2_data_util.local_today_canceled_buyno_map.get(code).get(str(data["val"]["orderNo"])) # 涔� if cancel_data: try: - ws.write(index, 8, "{}-{}".format(cancel_data["index"], cancel_data["val"]["time"]), cancel_style) + cancel_info = "{}-{}".format(cancel_data["index"], cancel_data["val"]["time"]) except Exception as e: logging.exception(e) + format_data.append(cancel_info) + cancel_order_info = None + if trade_info: + if trade_info[0] == 0: + # font.colour_index = 53 + pass + elif trade_info[0] == 1: + # font.colour_index = 17 + pass + elif trade_info[0] == 2: + # font.colour_index = 10 + cancel_order_info = trade_info[2] + format_data.append(cancel_order_info) + format_data.append(data["val"].get("orderNo")) + fdatas.append((style_int, format_data)) + return fdatas - ws.write(index, 3, "{}涓�".format(round(int(data["val"]["num"]) * float(data["val"]["price"]) / 100, 2)), style) + +def __save_l2_datas(code, fdatas, dest_dir=f"{constant.get_path_prefix()}/export/l2"): + local_time = time.strftime("%Y%m%dT%H%M%S", time.localtime(time.time())) + file_name = "{}/{}_{}.xls".format(dest_dir, code, local_time) + wb = xlwt.Workbook(encoding="utf-8") + ws = wb.add_sheet('sheet1') + ws.write(0, 0, '搴忓彿') + ws.write(0, 1, '鏃堕棿') + ws.write(0, 2, '涔版挙闂撮殧') + ws.write(0, 3, '閲戦') + ws.write(0, 4, '浠锋牸') + ws.write(0, 5, '鎵嬫暟') + ws.write(0, 6, '绫诲瀷') + ws.write(0, 7, '閲嶅鏁伴噺') + ws.write(0, 8, '鎾ゅ崟鏃堕棿') + ws.write(0, 9, '璁㈠崟鍙�') + index = 0 + font = xlwt.Font() + + cancel_style = xlwt.easyxf('pattern: pattern solid, fore_colour gray25') + for fdata in fdatas: + index += 1 + style_int = fdata[0] + data = fdatas[1] + style = None + if style_int == 0: + style = xlwt.easyxf('pattern: pattern solid') + else: + style = xlwt.easyxf('pattern: pattern solid, fore_colour light_yellow') + style.font = font + + for i in range(len(data)): + if data[i] is None: + continue + if i == 8 and data[i]: + ws.write(index, i, data[i], cancel_style) + else: + ws.write(index, i, data[i], style) wb.save(file_name) - return file_name def export_l2_data_origin(code, datas, key, dest_dir=f"{constant.get_path_prefix()}/export/l2_origin"): -- Gitblit v1.8.0