Administrator
2022-10-21 892b50e242e3c59a738b92dfdfee1bf1ff8932f2
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()