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

---
 utils/tool.py |  215 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 207 insertions(+), 8 deletions(-)

diff --git a/utils/tool.py b/utils/tool.py
index e23600c..e191df7 100644
--- a/utils/tool.py
+++ b/utils/tool.py
@@ -21,6 +21,22 @@
     return wrapper
 
 
+def singleton(cls):
+    """
+    鍗曚緥瑁呴グ鍣�
+    @param cls:
+    @return:
+    """
+    instances = {}
+
+    def get_instance(*args, **kwargs):
+        if cls not in instances:
+            instances[cls] = cls(*args, **kwargs)
+        return instances[cls]
+
+    return get_instance
+
+
 def get_expire():
     now = int(t.time())
     end = int(t.time()) + 60 * 60 * 24
@@ -28,7 +44,7 @@
     end = int(t.mktime(t.strptime(local_time, "%Y-%m-%d")))
     expire = end - now
     # 鍔犻殢鏈烘暟锛岄槻姝竴璧烽攢姣佹暟鎹�
-    expire += random.randint(0, 60 * 60)
+    expire += random.randint(0, 60 * 30)
     return expire
 
 
@@ -51,6 +67,18 @@
 
 def get_now_time_str():
     time_str = datetime.datetime.now().strftime("%H:%M:%S")
+    return time_str
+
+
+def get_now_time_as_int():
+    time_str = datetime.datetime.now().strftime("%H:%M:%S")
+    return int(time_str.replace(":", ""))
+
+
+def get_now_time_with_ms_str():
+    now = datetime.datetime.now()
+    ms = int(now.microsecond / 1000)
+    time_str = now.strftime(f"%H:%M:%S.{ms:03d}")
     return time_str
 
 
@@ -156,12 +184,37 @@
     return int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])
 
 
+def get_time_as_millionsecond(time_str):
+    s_str, ms_str = time_str.split(".")
+    ts = s_str.split(":")
+    return (int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])) * 1000 + int(ms_str)
+
+
 # 灏嗙鏁版牸寮忓寲涓烘椂闂�
 def time_seconds_format(seconds):
     h = seconds // 3600
     m = seconds % 3600 // 60
     s = seconds % 60
     return "{0:0>2}:{1:0>2}:{2:0>2}".format(h, m, s)
+
+
+def time_millionseconds_format(millionseconds):
+    ms = millionseconds % 1000
+    seconds = millionseconds // 1000
+    h = seconds // 3600
+    m = seconds % 3600 // 60
+    s = seconds % 60
+    return "{0:0>2}:{1:0>2}:{2:0>2}.{3:0>3}".format(h, m, s, ms)
+
+
+def timestamp_format(timestamp, format):
+    """
+    鏃堕棿鎴虫牸寮忓寲
+    @param timestamp:
+    @param format:
+    @return:
+    """
+    return datetime.datetime.fromtimestamp(timestamp).strftime(format)
 
 
 # 浜ゆ槗鏅傞枔鐨勫樊鍊�
@@ -178,6 +231,26 @@
     return time_1 - time_2
 
 
+def trade_time_sub_with_ms(time_str_1, time_str_2):
+    split_time = get_time_as_second("11:30:00") * 1000
+    time_1 = get_time_as_millionsecond(time_str_1)
+    time_2 = get_time_as_millionsecond(time_str_2)
+    if time_1 < split_time < time_2:
+        time_2 = time_2 - 90 * 60 * 1000
+    elif time_2 < split_time < time_1:
+        time_2 = time_2 + 90 * 60 * 1000
+
+    return time_1 - time_2
+
+
+def compare_time(time_1: str, time_2: str):
+    return int(time_1.replace(":", "")) - int(time_2.replace(":", ""))
+
+
+def compare_time_with_ms(time_1: str, time_2: str):
+    return int(time_1.replace(":", "").replace(".", "")) - int(time_2.replace(":", "").replace(".", ""))
+
+
 # 浜ゆ槗鏃堕棿鍔犲嚑s
 def trade_time_add_second(time_str, second):
     ts = time_str.split(":")
@@ -189,11 +262,34 @@
     return time_seconds_format(s)
 
 
+def trade_time_add_millionsecond(time_str, millionsecond):
+    s_str, ms_str = time_str.split(".")
+    ts = s_str.split(":")
+    s_ = int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])
+    ms_ = s_ * 1000
+    ms_ += int(ms_str)
+    ms = ms_ + millionsecond
+    # 鏄惁鍦�11:30:00
+    if ms >= 11 * 3600 * 1000 + 30 * 60 * 1000 > ms_:
+        ms += 90 * 60 * 1000
+    return time_millionseconds_format(ms)
+
+
 def compute_buy1_real_time(time_):
     ts = time_.split(":")
     s = int(ts[0]) * 3600 + int(ts[1]) * 60 + int(ts[2])
     cha = (s - 2) % 3
     return time_seconds_format(s - 2 - cha)
+
+
+def to_time_with_ms(time_s_str, time_ms):
+    """
+    灏嗘椂闂�+姣鏁� 杞负瀛楃涓�
+    @param time_s_str: 鏃堕棿濡�:10:00:00
+    @param time_ms: 姣濡�: 12
+    @return: 濡�:10:00:00.001
+    """
+    return f"{time_s_str}." + "{0:0>3}".format(time_ms)
 
 
 # 鍏ㄨ杞崐瑙�
@@ -222,11 +318,22 @@
     return max(price1, price2)
 
 
+# 鑾峰彇涔板叆浠锋牸绗煎瓙鐨勬渶浣庝环
+def get_shadow_price(price):
+    # fprice = round((100 - random.randint(2, 10)) * price / 100, 2)
+    # if price - 0.1 < fprice:
+    #     fprice = price - 0.1
+    # return round(fprice, 2)
+    if price < 20:
+        return round(get_buy_min_price(price) - 0.03, 2)
+    else:
+        # 澶ц偂浠风洿鎺ュ悜涓嬪彇2%
+        return round(price * (1 - 0.02), 2)
+
+
 if __name__ == "__main__":
-    print(get_buy_min_price(20))
-    # print(trade_time_sub("11:29:59", 5))
-    # print(trade_time_sub("10:29:59", 10))
-    # print(trade_time_add_second("13:29:59", 60))
+    print(trade_time_sub("13:00:01",
+                         "11:29:55"))
 
 
 class CodeDataCacheUtil:
@@ -247,11 +354,39 @@
         return False, None
 
 
-# 鏄惁涓轰富鏉夸唬鐮�
-def is_shsz_code(code):
-    if code.find("00") == 0 or code.find("60") == 0:
+def is_can_buy_code(code):
+    if code.find("00") == 0 or code.find("60") == 0 or code.find("30") == 0:
         return True
     return False
+
+
+def is_target_code(code):
+    """
+    鏄惁鏄洰鏍囦唬鐮�
+    @param code:
+    @return:
+    """
+    prefixes = ["00", "60", "30"]
+    for prefix in prefixes:
+        if code.find(prefix) == 0:
+            return True
+    return False
+
+
+def get_limit_up_rate(code):
+    # 鑾峰彇娑ㄥ仠鍊嶆暟
+    if code.find("00") == 0 or code.find("60") == 0:
+        return 1.1
+    else:
+        return 1.2
+
+
+def get_limit_down_rate(code):
+    # 鑾峰彇娑ㄥ仠鍊嶆暟
+    if code.find("00") == 0 or code.find("60") == 0:
+        return 0.9
+    else:
+        return 0.8
 
 
 def get_thread_id():
@@ -264,3 +399,67 @@
     except:
         pass
     return None
+
+
+def get_buy_volume(limit_up_price):
+    return get_buy_volume_by_money(limit_up_price, constant.BUY_MONEY_PER_CODE)
+
+
+def get_buy_volume_by_money(limit_up_price, money):
+    count = (money // int(round(float(limit_up_price) * 100))) * 100
+    if count < 100:
+        count = 100
+    return count
+
+
+# 娣辫瘉
+MARKET_TYPE_SZSE = 1
+# 涓婅瘉
+MARKET_TYPE_SSE = 0
+# 鏈煡
+MARKET_TYPE_UNKNOWN = -1
+
+
+def get_market_type(code):
+    """
+    鏍规嵁鑲$エ浠g爜
+    :param code:
+    :return:
+    """
+    if code.find("00") == 0 or code.find("30") == 0 or code.find("12") == 0:
+        return MARKET_TYPE_SZSE
+    elif code.find("60") == 0 or code.find("68") == 0 or code.find("11") == 0:
+        return MARKET_TYPE_SSE
+    else:
+        return MARKET_TYPE_UNKNOWN
+
+
+def is_sh_code(code):
+    """
+    鏄惁鏄笂璇�
+    @param code:
+    @return:
+    """
+    return get_market_type(code) == MARKET_TYPE_SSE
+
+
+def is_sz_code(code):
+    """
+    鏄惁鏄繁璇�
+    @param code:
+    @return:
+    """
+    return get_market_type(code) == MARKET_TYPE_SZSE
+
+
+def is_ge_code(code):
+    """
+    鏄惁鏄垱涓氭澘
+    @param code:
+    @return:
+    """
+    return code.find("30") == 0
+
+
+if __name__ == "__main__":
+    print(timestamp_format(1726034271, "%H%M%S"))

--
Gitblit v1.8.0