From 892b50e242e3c59a738b92dfdfee1bf1ff8932f2 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期五, 21 十月 2022 16:59:58 +0800
Subject: [PATCH] 新策略修改

---
 l2_data_util.py |  168 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 159 insertions(+), 9 deletions(-)

diff --git a/l2_data_util.py b/l2_data_util.py
index c3de649..4fe0481 100644
--- a/l2_data_util.py
+++ b/l2_data_util.py
@@ -13,6 +13,19 @@
 from trade_gui import async_call
 
 
+def run_time():
+    def decorator(func):
+        def infunc(*args, **kwargs):
+            start = round(time.time() * 1000)
+            result = func(args, **kwargs)
+            print("鎵ц鏃堕棿", round(time.time() * 1000) - start)
+            return result
+
+        return infunc
+
+    return decorator
+
+
 def compare_time(time1, time2):
     result = int(time1.replace(":", "", 2)) - int(time2.replace(":", "", 2))
     return result
@@ -110,6 +123,33 @@
     return None, None
 
 
+# 鍒ゆ柇鍗栨挙鐨勫崠淇″彿鏄惁鍦ㄧ洰鏍囦俊鍙蜂箣鍓�
+def is_sell_index_before_target(sell_cancel_data, target_data, local_today_num_operate_map):
+    min_space, max_space = compute_time_space_as_second(sell_cancel_data["val"]["cancelTime"],
+                                                        sell_cancel_data["val"]["cancelTimeUnit"])
+    max_time = __sub_time(sell_cancel_data["val"]["time"], min_space)
+    min_time = __sub_time(sell_cancel_data["val"]["time"], max_space)
+    # 濡傛灉鏈�澶у�奸兘鍦ㄧ洰鏍囦俊鍙蜂箣鍓嶅垯淇″彿鑲畾鍦ㄧ洰鏍囦俊鍙蜂箣鍓�
+    if int(target_data["val"]["time"].replace(":", "")) > int(max_time.replace(":", "")):
+        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:
+                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
+
+
 __last_big_data = {}
 
 
@@ -140,17 +180,127 @@
             break
 
 
+# l2鏁版嵁鎷兼帴宸ュ叿
+class L2DataConcatUtil:
+
+    # 鍒濆鍖�
+    def __init__(self, code, last_datas, datas):
+        self.last_datas = last_datas
+        self.datas = datas
+        self.code = code
+
+    def __get_data_identity(self, data_):
+        data=data_["val"]
+        return "{}-{}-{}-{}-{}-{}".format(data.get("time"), data.get("num"), data.get("price"), data.get("operateType"),
+                                          data.get("cancelTime"), data.get("cancelTimeUnit"))
+
+    # 鑾峰彇鎷兼帴鐨勭壒寰�,鑾峰彇鏈�鍚�3绗�
+    def __get_concat_feature(self):
+        # 鏈�灏戦渶瑕�3鏉℃暟鎹�+2鏉¢渶瑕佹湁鐗瑰緛鐐圭殑鏁版嵁
+        min_identity = 2
+        min_count = 3
+
+        identity_set = set()
+        count = 0
+        start_index = -1
+        for i in range(len(self.last_datas) - 1, -1, -1):
+            identity_set.add(self.__get_data_identity(self.last_datas[i]))
+            count += 1
+            start_index = i
+            if count >= min_count and len(identity_set) >= min_identity:
+                break
+        return start_index, len(self.last_datas) - 1
+
+    # 鑾峰彇鏂板鏁版嵁
+    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:
+                return []
+
+        # 鑾峰彇鎷兼帴鐐�
+        start_index, end_index = self.__get_concat_feature()
+        if start_index < 0:
+            return self.datas
+        print("鐗瑰緛浣嶇疆锛�", start_index, end_index)
+        # 鎻愬彇鐗瑰緛鐐圭殑鏍囪瘑鏁版嵁
+        identity_list = []
+        for i in range(start_index, end_index + 1):
+            identity_list.append(self.__get_data_identity(self.last_datas[i]))
+
+        # 鏌ユ壘瀹屾暣鐨勭壒寰�
+        identity_count = len(identity_list)
+        for n in range(0, identity_count):
+            # 姣忔閬嶅巻鍑忓皯鏈�鍓嶉潰涓�涓壒寰侀噺
+            for i in range(0, len(self.datas) - len(identity_list) + n):
+                if self.__get_data_identity(self.datas[i]) == identity_list[n]:
+                    # n==0 琛ㄧず瀹屽叏鍖归厤 锛� i=0 琛ㄧず鍗充娇涓嶆槸瀹屽叏鍖归厤锛屼絾蹇呴』鏂版暟鎹涓�涓厓绱犲尮閰�
+                    if n == 0 or i == 0:
+                        find_identity = True
+                        for j in range(n + 1, len(identity_list)):
+                            if identity_list[j] != self.__get_data_identity(self.datas[i + j - n]):
+                                find_identity = False
+                                break
+
+                        if find_identity:
+                            return self.datas[i + len(identity_list) - n:]
+                else:
+                    continue
+        print("鏂版暟鎹腑鏈壘鍒扮壒寰佹爣璇�")
+        return self.datas
+
+
+def test_add_datas():
+    def load_data(datas):
+        data_list = []
+        for data in datas:
+            data_list.append({"val":{"time": data}})
+        return data_list
+
+    # 涓嶅尮閰�
+    latest_datas = []
+    datas = ["10:00:02", "10:00:02", "10:00:03", "10:00:04", "10:00:05"]
+    latest_datas = load_data(latest_datas)
+    datas = load_data(datas)
+    print(L2DataConcatUtil("000333", latest_datas, datas).get_add_datas())
+
+    # 涓嶅尮閰�
+    latest_datas = ["10:00:02"]
+    datas = ["10:00:02", "10:00:02", "10:00:03", "10:00:04", "10:00:05"]
+    latest_datas = load_data(latest_datas)
+    datas = load_data(datas)
+    print(L2DataConcatUtil("000333", latest_datas, datas).get_add_datas())
+
+    # 涓嶅尮閰�
+    latest_datas = ["10:00:00", "10:00:01", "10:00:02", "10:00:03"]
+    datas = ["10:00:02", "10:00:02", "10:00:03", "10:00:04", "10:00:05"]
+    latest_datas = load_data(latest_datas)
+    datas = load_data(datas)
+    print(L2DataConcatUtil("000333", latest_datas, datas).get_add_datas())
+
+    # 鍖归厤
+    latest_datas = ["10:00:00", "10:00:01", "10:00:02", "10:00:03"]
+    datas = ["10:00:01", "10:00:02", "10:00:03", "10:00:04", "10:00:05"]
+    latest_datas = load_data(latest_datas)
+    datas = load_data(datas)
+    print(L2DataConcatUtil("000333", latest_datas, datas).get_add_datas())
+
+    latest_datas = ["10:00:00", "10:00:01", "10:00:02", "10:00:02"]
+    datas = ["10:00:02", "10:00:02", "10:00:03", "10:00:04", "10:00:05"]
+    latest_datas = load_data(latest_datas)
+    datas = load_data(datas)
+    print(L2DataConcatUtil("000333", latest_datas, datas).get_add_datas())
+
+    latest_datas = ["10:00:00", "10:00:01", "10:00:02", "10:00:02"]
+    datas = ["10:00:02", "10:00:02", "10:00:00", "10:00:01", "10:00:02", "10:00:02", "10:00:04", "10:00:05"]
+    latest_datas = load_data(latest_datas)
+    datas = load_data(datas)
+    print(L2DataConcatUtil("000333", latest_datas, datas).get_add_datas())
+
+
 def test(datas):
     datas["code"] = "test"
 
 
 if __name__ == "__main__":
-    # cancel_data = {"val": {"operateType": 1, "num": 1520, "cancelTime": 1, "cancelTimeUnit": 1, "time": "09:32:30"}}
-    # today_datas=[{"val": {"operateType": 1, "num": 1520, "cancelTime": 1, "cancelTimeUnit": 0, "time": "09:32:30"}},{"val": {"operateType": 0, "num": 1520, "cancelTime": 0, "cancelTimeUnit": 0, "time": "09:31:31"}}]
-    # result= get_buy_data_with_cancel_data(cancel_data,today_datas)
-    # print(result)
-    code = "001209"
-    l2_data_manager.load_l2_data(code)
-    total_datas = l2_data_manager.local_today_datas[code]
-    index, data = get_buy_data_with_cancel_data(total_datas[118], l2_data_manager.local_today_num_operate_map.get(code))
-    print(index, data)
+    test_add_datas()

--
Gitblit v1.8.0