From 48fb7a00951f91bdc707e5dd2d196e5bccb752c3 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期三, 18 六月 2025 18:41:30 +0800
Subject: [PATCH] 异常保护

---
 utils/data_export_util.py |  383 ++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 297 insertions(+), 86 deletions(-)

diff --git a/utils/data_export_util.py b/utils/data_export_util.py
index 4895f51..4de83e7 100644
--- a/utils/data_export_util.py
+++ b/utils/data_export_util.py
@@ -1,6 +1,7 @@
 """
 鏁版嵁瀵煎嚭宸ュ叿
 """
+import copy
 import json
 import logging
 import os
@@ -11,22 +12,93 @@
 import constant
 from code_attribute import gpcode_manager
 import l2.l2_data_util
+from l2.huaxin import l2_huaxin_util
+from l2.l2_transaction_data_manager import BigOrderDealManager
 from log_module import log, log_export
-from l2 import l2_data_source_util
+from l2 import l2_data_source_util, l2_data_util
+from log_module.log import logger_debug
 from trade import deal_big_money_manager
+from trade.buy_money_count_setting import BuyMoneyUtil
+from utils import tool
+
+# 缂撳瓨L2鏁版嵁锛屾牸寮忥細{"鏃ユ湡":{鏁版嵁}}
+__l2_data_cache = {}
 
 
 def export_l2_excel(code, date=None):
     # 鑾峰彇L2鐨勬暟鎹�
-    local_today_datas = log_export.load_l2_from_log(date)
-    datas = local_today_datas[code]
-    # 鑾峰彇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 = get_l2_datas(code, date=date)
+    __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, today_datas=None, date=None, end_index=None):
+    __start_time = time.time()
+    if date is None:
+        date = tool.get_now_date_str()
+    datas = today_datas
+    if datas is None:
+        if date in __l2_data_cache:
+            local_today_datas = __l2_data_cache.get(date)
+        else:
+            local_today_datas = log_export.load_l2_from_log(date)
+            __l2_data_cache[date] = local_today_datas
+        datas = local_today_datas.get(code)
+    if not datas:
+        datas = []
+    if not datas:
+        # 鍔犲揩娌℃湁L2鏁版嵁鐨勫鍑洪�熷害
+        process_indexs = []
+        trade_indexs = []
+        real_position_indexes = []
+        deal_list = []
+        cancel_reasons = {}
+    else:
+        process_indexs = log_export.get_l2_process_position(code, date)
+        trade_indexs = log_export.get_l2_trade_position(code, date)
+        real_position_indexes = log_export.get_real_place_order_positions(code, date)
+        deal_list = log_export.load_huaxin_deal_record(code, date)
+        cancel_reasons = log_export.load_cancel_buy_reasons(code, date)
+    deal_list_dict = {}
+    for d in deal_list:
+        deal_list_dict[str(d[0])] = d
+
+    sell_no_dict = log_export.load_huaxin_transaction_sell_no(code=code, date=date)
+    sell_nos = sell_no_dict.get(code)
+    active_sell_map = log_export.load_huaxin_active_sell_map(date=date)
+    active_sell_set = active_sell_map.get(code)
+    if not active_sell_set:
+        active_sell_set = set()
+    # 濡傛灉鏈夋埅鑷充綅缃�
+    if end_index:
+        datas = copy.deepcopy(datas)
+        for i in range(len(datas)):
+            if datas[i]["index"] == end_index:
+                datas = datas[:i]
+                break
+    # 鎴愪氦鐨勫ぇ鍗曚俊鎭痆(涔板崟鍙�,鎬昏偂鏁�,鎬绘垚浜ら,鎴愪氦寮�濮嬫椂闂�,鎴愪氦缁撴潫鏃堕棿)]
+    deal_big_buy_order_list = copy.deepcopy(BigOrderDealManager().get_total_buy_data_list(code))
+    deal_big_buy_order_no_dict = {}
+    if deal_big_buy_order_list:
+        for d in deal_big_buy_order_list:
+            deal_big_buy_order_no_dict[d[0]] = d
+
+    fdatas = export_l2_data(code, datas, process_indexs, trade_indexs, real_position_indexes, deal_list_dict, sell_nos,
+                            active_sell_set, cancel_reasons, deal_big_buy_order_no_dict)
+    return fdatas
+
+
+def get_l2_transaction_datas(code, date=None):
+    if date is None:
+        date = tool.get_now_date_str()
+    sell_no_dict = log_export.load_huaxin_transaction_sell_no(code=code, date=date)
+    sell_nos = sell_no_dict.get(code)
+    fdatas = export_l2_transaction_data(code, sell_nos)
+    return fdatas
+
+
+def export_l2_data(code, datas, process_indexs, trade_indexs, real_position_indexes, deal_list_dict, sell_nos,
+                   active_sell_nos, cancel_reasons, deal_big_buy_order_no_dict):
     def find_process_index(index):
         for i in range(0, len(process_indexs)):
             if process_indexs[i][0] <= index <= process_indexs[i][1]:
@@ -39,57 +111,71 @@
                 return trade_indexs[i]
         return None
 
+    # 鏈�鏂扮殑涓�鏉℃暟鎹�
+    latest_data = datas[-1]
+    latest_time_str_with_ms = l2.l2_data_util.L2DataUtil.get_time_with_ms(latest_data["val"])
+
     # 鏁版嵁棰勫鐞�
     num_operate_map = {}
     l2.l2_data_util.load_num_operate_map(num_operate_map, code, datas)
-    num_dict = {}
-    for data in datas:
-        if data["val"]["num"] not in num_dict:
-            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, '璁㈠崟鍙�')
-
+    buy_no_map = {}
+    sell_no_map = {}
+    l2.l2_data_util.load_buy_no_map(buy_no_map, code, datas)
+    l2.l2_data_util.load_sell_no_map(sell_no_map, code, datas)
+    l2.l2_data_util.load_canceled_buy_no_map(l2.l2_data_util.local_today_canceled_buyno_map, code, datas)
+    # num_dict = {}
+    # for data in datas:
+    #     if data["val"]["num"] not in num_dict:
+    #         num_dict[data["val"]["num"]] = []
+    #     num_dict[data["val"]["num"]].append(data)
+    logger_debug.info(f"寰幆缁勮鏁版嵁寮�濮嬶細闀垮害-{len(datas)} 涓诲姩鍗栭暱搴�-{len(active_sell_nos)}")
     index = 0
+    fdatas = []
+    # 鏁版嵁澶灏遍渶瑕佽繃婊ゆ帀灏忛噾棰�
+    is_data_too_large = len(datas) > 20000
+
+    limit_up_price = gpcode_manager.get_limit_up_price_as_num(code)
+    if limit_up_price:
+        # 闇�瑕佽闃呯殑鐗规畩鐨勯噺
+        special_volumes = BuyMoneyUtil.get_possible_buy_volumes(limit_up_price)
+        special_volumes = [x//100 for x in special_volumes]
+    else:
+        special_volumes = []
+
     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
-        if find_process_index(data["index"]) % 2 == 0:
-            style = xlwt.easyxf('pattern: pattern solid')
-        else:
-            style = xlwt.easyxf('pattern: pattern solid, fore_colour light_yellow')
-        style.font = font
-        cancel_style = xlwt.easyxf('pattern: pattern solid, fore_colour gray25')
+        if is_data_too_large and data['val']['num'] * float(data['val']['price']) < 5000:
+            # 灏忓崟
+            if data["index"] not in real_position_indexes and data['val']['num'] not in special_volumes:
+                continue
 
-        ws.write(index, 0, data["index"], style)
-        ws.write(index, 1, data["val"]["time"], style)
+        # 鍏堢Щ闄�
+        if data['val']['orderNo'] in active_sell_nos:
+            # 杩囨护涓诲姩鍗�
+            continue
+
+        # 绉婚櫎鎴愪氦澶у崟鍦↙2涓凡鏈夌殑璁㈠崟
+        if l2.l2_data_util.L2DataUtil.is_limit_up_price_buy(data["val"]):
+            if int(data['val']['orderNo']) in deal_big_buy_order_no_dict:
+                deal_big_buy_order_no_dict.pop(int(data['val']['orderNo']))
+
+        trade_info = find_trade_index(data["index"])
+        if not trade_info:
+            # 鑾峰彇鐪熷疄涓嬪崟浣嶇疆
+            if data["index"] in real_position_indexes:
+                trade_info = [3]
+        style_int = None
+        if find_process_index(data["index"]) % 2 == 0:
+            style_int = 0
+        else:
+            style_int = 1
+
+        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 = ''
@@ -102,61 +188,188 @@
                 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 = "娑ㄥ仠"
+            limit_price = "T"
         elif int(data["val"]["limitPrice"]) == 2:
-            limit_price = "璺屽仠"
-
+            limit_price = "D"
+        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 = False
+            cancel_data = l2.l2_data_util.local_today_canceled_buyno_map.get(code).get(str(data["val"]["orderNo"]))
             # 涔�
-            for d in num_dict[data["val"]["num"]]:
-                if int(d["val"]["operateType"]) == 1:
-                    buy_index = l2_data_source_util.L2DataSourceUtils.get_buy_index_with_cancel_data(code, d,
-                                                                                                     num_operate_map[
-                                                                                                         code])
-                    if buy_index == data["index"]:
-                        try:
-                            ws.write(index, 8, "{}-{}".format(d["index"], d["val"]["time"]), cancel_style)
-                            break
-                        except Exception as e:
-                            logging.exception(e)
+            if cancel_data and latest_data["index"] >= cancel_data["index"]:
+                # 濡傛灉鎾ゅ崟绱㈠紩涓嶈兘姣旀渶杩戞暟鎹储寮曡繕澶�
+                try:
+                    left_num = data["val"]["num"] - cancel_data["val"]["num"]
+                    if left_num > 0:
+                        cancel_info = f"鎴愪氦:{left_num} 搴忓彿:{cancel_data['index']}"
+                    else:
+                        cancel_info = "{}-{}".format(cancel_data["index"], f"{cancel_data['val']['time']}") + (
+                            f".{cancel_data['val']['tms']}" if "tms" in cancel_data["val"] else '')
+                except Exception as e:
+                    logging.exception(e)
+            else:
+                deal_info = deal_list_dict.get(str(data["val"].get("orderNo")))
+                if deal_info and len(deal_info) >= 5:
+                    # 鎴愪氦鏃堕棿涓嶈兘姣斿綋鍓嶇储寮曟椂闂村ぇ
+                    cancel_info = l2_huaxin_util.convert_time(deal_info[4],
+                                                              with_ms=True)
+                    if tool.trade_time_sub_with_ms(cancel_info, latest_time_str_with_ms) > 0:
+                        cancel_info = None
+        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]
+        if not cancel_order_info:
+            if data["index"] in cancel_reasons:
+                cancel_order_info = cancel_reasons[data["index"]]
 
-        ws.write(index, 3, "{}涓�".format(round(int(data["val"]["num"]) * float(data["val"]["price"]) / 100, 2)), style)
+        format_data.append(cancel_order_info)
+        format_data.append(data["val"].get("orderNo"))
+        fdatas.append((style_int, trade_info, format_data))
+
+    if tool.is_sh_code(code) and deal_big_buy_order_no_dict:
+        # 涓婅瘉锛屾湁涓诲姩鎴愪氦鐨勫ぇ鍗�
+        active_deal_list = [deal_big_buy_order_no_dict[order_no] for order_no in deal_big_buy_order_no_dict]
+        active_deal_list.sort(key=lambda x: x[3])
+        for i in range(len(active_deal_list)):
+            d = active_deal_list[i]
+            format_data = [0]
+            format_data.append(l2_huaxin_util.convert_time(d[3], with_ms=True))
+            format_data.append('')
+            format_data.append(f"{round(d[2] / 10000, 1)}涓�")
+            format_data.append(round(d[2] / d[1], 2))
+            format_data.append(d[1] // 100)
+            format_data.append('涓诲姩涔�')
+            format_data.append(1)
+            format_data.append(l2_huaxin_util.convert_time(d[4], with_ms=True))
+            format_data.append('')
+            format_data.append(d[0])
+            fdatas.insert(i, (0, None, format_data))
+    logger_debug.info("寰幆缁勮鏁版嵁瀹屾垚")
+    return fdatas
+
+
+def export_l2_transaction_data(code, sell_nos):
+    fdatas = []
+    if sell_nos:
+        index = 0
+        for sell_info in sell_nos:
+            if sell_info[1] * sell_info[2] < 50 * 10000:
+                continue
+            index += 1
+            item = []
+            item.append(index)
+            item.append(l2_huaxin_util.convert_time(sell_info[3][0], with_ms=True))
+            item.append("")
+            item.append(
+                "{}涓�".format(round(sell_info[1] * sell_info[2] / 10000, 1)))
+            item.append(sell_info[2])
+            item.append(sell_info[1] // 100)
+            item.append("涓诲姩鍗�")
+            item.append(1)
+            item.append(l2_huaxin_util.convert_time(sell_info[4][0], with_ms=True))
+            item.append(None)
+            item.append(sell_info[0])
+            fdatas.append((0, None, item))
+    return fdatas
+
+
+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, '澶囨敞')
+    ws.write(0, 10, '璁㈠崟鍙�')
+    index = 0
+
+    cancel_style = xlwt.easyxf('pattern: pattern solid, fore_colour gray25')
+    for fdata in fdatas:
+        index += 1
+        style_int = fdata[0]
+        trade_info = fdata[1]
+        data = fdata[2]
+        style = None
+        if style_int == 0:
+            style = xlwt.easyxf('pattern: pattern solid')
+        else:
+            style = xlwt.easyxf('pattern: pattern solid, fore_colour light_yellow')
+        font = xlwt.Font()
+        style.font = 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
+
+        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"):
@@ -205,12 +418,10 @@
     datas = local_today_datas[code]
     num_operate_map = {}
     l2.l2_data_util.load_num_operate_map(num_operate_map, code, datas)
-    for progress in progresses:
-        deal_big_money_manager.DealComputeProgressManager().set_trade_progress(code, progress, datas, num_operate_map[code])
 
 
 if __name__ == "__main__":
     try:
-        export_l2_excel("600615")
+        get_l2_datas("600990")
     except Exception as e:
         logging.exception(e)

--
Gitblit v1.8.0