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

---
 l2_data_util.py |  148 ++++++++++++++++++++++++++++++-------------------
 1 files changed, 91 insertions(+), 57 deletions(-)

diff --git a/l2_data_util.py b/l2_data_util.py
index 4fe0481..24c66ac 100644
--- a/l2_data_util.py
+++ b/l2_data_util.py
@@ -4,13 +4,14 @@
 """
 
 # 姣旇緝鏃堕棿鐨勫ぇ灏�
-import datetime
 import json
 import time
 
-import l2_data_manager
-import tool
-from trade_gui import async_call
+from db.redis_manager_delegate import RedisUtils
+from utils.tool import async_call
+
+from l2 import l2_data_manager
+from utils import tool
 
 
 def run_time():
@@ -24,6 +25,53 @@
         return infunc
 
     return decorator
+
+
+# 鏄惁涓哄ぇ鍗�
+def is_big_money(val, is_ge=False):
+    """
+    鍒ゆ柇鏄惁涓哄ぇ鍗�
+    @param val: l2鏁版嵁
+    @param is_ge: 鏄惁涓哄垱涓氭澘
+    @return:
+    """
+    price = float(val["price"])
+    money = round(price * val["num"], 2)
+    if is_ge:
+        if money >= 29900 or val["num"] >= 2999:
+            return True
+        else:
+            return False
+    else:
+        if price > 3.0:
+            if money >= 29900 or val["num"] >= 7999:
+                return True
+            else:
+                return False
+        else:
+            max_money = price * 10000
+            if money >= max_money * 0.95:
+                return True
+            else:
+                return False
+
+
+# 鑾峰彇澶ц祫閲戠殑閲戦
+def get_big_money_val(limit_up_price, is_ge=False):
+    if is_ge:
+        return min(299 * 10000, round(limit_up_price * 2900 * 100))
+    else:
+        if limit_up_price > 3.0:
+            return min(299 * 10000, round(limit_up_price * 7999 * 100))
+        else:
+            max_money = limit_up_price * 10000 * 100
+            return int(max_money * 0.95)
+
+# if int(val["num"]) >= constant.BIG_MONEY_NUM:
+#     return True
+# if int(val["num"]) * limit_up_price >= constant.BIG_MONEY_AMOUNT:
+#     return True
+# return False_
 
 
 def compare_time(time1, time2):
@@ -48,20 +96,6 @@
     json_value = json.loads(value)
     _data = {"key": key, "val": item, "re": json_value["re"], "index": int(json_value["index"])}
     return _data
-
-
-# 灏嗘暟鎹牴鎹畁um-operate鍒嗙被
-def load_num_operate_map(local_today_num_operate_map, code, source_datas, clear=False):
-    if local_today_num_operate_map.get(code) is None:
-        local_today_num_operate_map[code] = {}
-    if clear:
-        local_today_num_operate_map[code] = {}
-
-    for data in source_datas:
-        key = "{}-{}-{}".format(data["val"]["num"], data["val"]["operateType"], data["val"]["price"])
-        if local_today_num_operate_map[code].get(key) is None:
-            local_today_num_operate_map[code].setdefault(key, [])
-        local_today_num_operate_map[code].get(key).append(data)
 
 
 # 鍑忓幓鏃堕棿
@@ -96,31 +130,14 @@
         return __time * 3600, (__time + 1) * 3600
 
 
-# 鏍规嵁涔版挙鏁版嵁(涓庝粖鏃ユ�荤殑鏁版嵁)璁$畻涔板叆鏁版嵁
-def get_buy_data_with_cancel_data(cancel_data, local_today_num_operate_map):
+# 鑾峰彇涔板叆鏃堕棿鑼冨洿
+def get_buy_time_range(cancel_data):
     # 璁$畻鏃堕棿鍖洪棿
     min_space, max_space = compute_time_space_as_second(cancel_data["val"]["cancelTime"],
                                                         cancel_data["val"]["cancelTimeUnit"])
     max_time = __sub_time(cancel_data["val"]["time"], min_space)
     min_time = __sub_time(cancel_data["val"]["time"], max_space)
-    buy_datas = local_today_num_operate_map.get(
-        "{}-{}-{}".format(cancel_data["val"]["num"], "0", cancel_data["val"]["price"]))
-    if buy_datas is None:
-        # 鏃犳暟鎹�
-        return None, None
-    for i in range(0, len(buy_datas)):
-        data = buy_datas[i]
-        if int(data["val"]["operateType"]) != 0:
-            continue
-        if int(data["val"]["num"]) != int(cancel_data["val"]["num"]):
-            continue
-        if min_space == 0 and max_space == 0:
-            if compare_time(data["val"]["time"], min_time) == 0:
-                return data["index"], data
-
-        elif compare_time(data["val"]["time"], min_time) > 0 and compare_time(data["val"]["time"], max_time) <= 0:
-            return data["index"], data
-    return None, None
+    return min_time, max_time
 
 
 # 鍒ゆ柇鍗栨挙鐨勫崠淇″彿鏄惁鍦ㄧ洰鏍囦俊鍙蜂箣鍓�
@@ -134,19 +151,20 @@
         return True
     sell_datas = local_today_num_operate_map.get(
         "{}-{}-{}".format(sell_cancel_data["val"]["num"], "2", sell_cancel_data["val"]["price"]))
-    for i in range(0, len(sell_datas)):
-        data = sell_datas[i]
-        if int(data["val"]["operateType"]) != 2:
-            continue
-        if int(data["val"]["num"]) != int(sell_cancel_data["val"]["num"]):
-            continue
-        if min_space == 0 and max_space == 0:
-            # 鏈鍐�
-            if compare_time(data["val"]["time"], min_time) == 0:
+    if sell_datas:
+        for i in range(0, len(sell_datas)):
+            data = sell_datas[i]
+            if int(data["val"]["operateType"]) != 2:
+                continue
+            if int(data["val"]["num"]) != int(sell_cancel_data["val"]["num"]):
+                continue
+            if min_space == 0 and max_space == 0:
+                # 鏈鍐�
+                if compare_time(data["val"]["time"], min_time) == 0:
+                    return data["index"] < target_data["index"]
+            # 鏁版嵁鍦ㄦ纭殑鍖洪棿
+            elif compare_time(data["val"]["time"], min_time) > 0 and compare_time(data["val"]["time"], max_time) <= 0:
                 return data["index"] < target_data["index"]
-        # 鏁版嵁鍦ㄦ纭殑鍖洪棿
-        elif compare_time(data["val"]["time"], min_time) > 0 and compare_time(data["val"]["time"], max_time) <= 0:
-            return data["index"] < target_data["index"]
     return False
 
 
@@ -168,19 +186,34 @@
                 # 淇濆瓨蹇収
                 # logger_l2_big_data.debug("code:{} d1:{}  d2:{}", code, d1[i - 60: i + 30], d2[i - 60: i + 30])
                 break
-    time_str = datetime.datetime.now().strftime("%H:%M:%S")
+    time_str = tool.get_now_time_str()
 
     for time_ in same_time_nums:
         # 鍙繚鐣欐渶杩�3s鍐呯殑澶ф暟鎹�
         if abs(get_time_as_seconds(time_str) - get_time_as_seconds(time_)) > 3:
             continue
         if same_time_nums[time_] > 20:
-            redis = l2_data_manager._redisManager.getRedis()
-            redis.setex("big_data-{}-{}".format(code, int(round(time.time() * 1000))), tool.get_expire(), d1)
+            RedisUtils.setex(l2_data_manager._redisManager.getRedis(),
+                             "big_data-{}-{}".format(code, int(round(time.time() * 1000))), tool.get_expire(),
+                             d1)
             break
 
 
-# l2鏁版嵁鎷兼帴宸ュ叿
+# 淇濆瓨l2鏈�鏂版暟鎹殑澶у皬
+# @async_call
+def save_l2_latest_data_number(code, num):
+    RedisUtils.setex(l2_data_manager._redisManager.getRedis(), "l2_latest_data_num-{}".format(code), 3, num)
+
+
+# 鑾峰彇鏈�鏂版暟鎹潯鏁�
+def get_l2_latest_data_number(code):
+    num = RedisUtils.get(l2_data_manager._redisManager.getRedis(), "l2_latest_data_num-{}".format(code))
+    if num is not None:
+        return int(num)
+    return None
+
+
+# l2鏁版嵁鎷兼帴宸ュ叿  鏆傛椂杩樻湭鍚敤
 class L2DataConcatUtil:
 
     # 鍒濆鍖�
@@ -190,7 +223,7 @@
         self.code = code
 
     def __get_data_identity(self, data_):
-        data=data_["val"]
+        data = data_["val"]
         return "{}-{}-{}-{}-{}-{}".format(data.get("time"), data.get("num"), data.get("price"), data.get("operateType"),
                                           data.get("cancelTime"), data.get("cancelTimeUnit"))
 
@@ -215,7 +248,8 @@
     def get_add_datas(self):
         # 鏌ヨ褰撳墠鏁版嵁鏄惁鍦ㄦ渶杩戜竴娆℃暟鎹箣鍚�
         if self.last_datas and self.datas:
-            if int(self.datas[-1]["val"]["time"].replace(":", "")) - int(self.last_datas[-1]["val"]["time"].replace(":", "")) < 0:
+            if int(self.datas[-1]["val"]["time"].replace(":", "")) - int(
+                    self.last_datas[-1]["val"]["time"].replace(":", "")) < 0:
                 return []
 
         # 鑾峰彇鎷兼帴鐐�
@@ -254,7 +288,7 @@
     def load_data(datas):
         data_list = []
         for data in datas:
-            data_list.append({"val":{"time": data}})
+            data_list.append({"val": {"time": data}})
         return data_list
 
     # 涓嶅尮閰�

--
Gitblit v1.8.0