Administrator
2023-08-08 8c7519b0dc79d32a216765a1b46e736d53e3d786
l2/cancel_buy_strategy.py
@@ -156,7 +156,7 @@
        # 需要查询买入信号之前的同1s是否有涨停撤的数据
        process_index = process_index_old
        # 下单次数
        place_order_count = trade_data_manager.PlaceOrderCountManager.get_place_order_count(code)
        place_order_count = trade_data_manager.PlaceOrderCountManager().get_place_order_count(code)
        if buy_single_index == start_index:
            # 第1次计算需要计算买入信号-执行位的净值
@@ -477,7 +477,7 @@
        l2_log.cancel_debug(code, "H级是否需要撤单,数据范围:{}-{} ", start_index, end_index)
        # 获取下单次数
        place_order_count = trade_data_manager.PlaceOrderCountManager.get_place_order_count(code)
        place_order_count = trade_data_manager.PlaceOrderCountManager().get_place_order_count(code)
        cancel_rate_threshold = cls.__hCancelParamsManager.get_cancel_rate(volume_index)
        process_index = start_index
        # 是否有观测的数据撤单
@@ -1006,17 +1006,25 @@
# --------------------------------封单额变化撤------------------------
# 涨停封单额统计
class L2LimitUpMoneyStatisticUtil:
    _db = 1
    _redisManager = redis_manager.RedisManager(1)
    _thsBuy1VolumnManager = trade_queue_manager.THSBuy1VolumnManager()
    __instance = None
    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = super(L2LimitUpMoneyStatisticUtil, cls).__new__(cls, *args, **kwargs)
        return cls.__instance
    @classmethod
    def __get_redis(cls):
        return cls._redisManager.getRedis()
    # 设置l2的每一秒涨停封单额数据
    @classmethod
    def __set_l2_second_money_record(cls, code, time, num, from_index, to_index):
        old_num, old_from, old_to = cls.__get_l2_second_money_record(code, time)
    def __set_l2_second_money_record(self, code, time, num, from_index, to_index):
        old_num, old_from, old_to = self.__get_l2_second_money_record(code, time)
        if old_num is None:
            old_num = num
            old_from = from_index
@@ -1027,38 +1035,34 @@
        key = "l2_limit_up_second_money-{}-{}".format(code, time.replace(":", ""))
        RedisUtils.setex(cls.__get_redis(), key, tool.get_expire(), json.dumps((old_num, old_from, old_to)))
        RedisUtils.setex(self.__get_redis(), key, tool.get_expire(), json.dumps((old_num, old_from, old_to)))
    @classmethod
    def __get_l2_second_money_record(cls, code, time):
    def __get_l2_second_money_record(self, code, time):
        key = "l2_limit_up_second_money-{}-{}".format(code, time.replace(":", ""))
        val = RedisUtils.get(cls.__get_redis(), key)
        return cls.__format_second_money_record_val(val)
        val = RedisUtils.get(self.__get_redis(), key)
        return self.__format_second_money_record_val(val)
    @classmethod
    def __format_second_money_record_val(cls, val):
    def __format_second_money_record_val(self, val):
        if val is None:
            return None, None, None
        val = json.loads(val)
        return val[0], val[1], val[2]
    @classmethod
    def __get_l2_second_money_record_keys(cls, code, time_regex):
    def __get_l2_second_money_record_keys(self, code, time_regex):
        key = "l2_limit_up_second_money-{}-{}".format(code, time_regex)
        keys = RedisUtils.keys(cls.__get_redis(), key)
        keys = RedisUtils.keys(self.__get_redis(), key)
        return keys
    # 设置l2最新的封单额数据
    @classmethod
    def __set_l2_latest_money_record(cls, code, index, num):
    def __set_l2_latest_money_record(self, code, index, num):
        key = "l2_limit_up_money-{}".format(code)
        RedisUtils.setex(cls.__get_redis(), key, tool.get_expire(), json.dumps((num, index)))
        RedisUtils.setex(self.__get_redis(), key, tool.get_expire(), json.dumps((num, index)))
    # 返回数量,索引
    @classmethod
    def __get_l2_latest_money_record(cls, code):
    def __get_l2_latest_money_record(self, code):
        key = "l2_limit_up_money-{}".format(code)
        result = RedisUtils.get(cls.__get_redis(), key)
        result = RedisUtils.get(self.__get_redis(), key)
        if result:
            result = json.loads(result)
            return result[0], result[1]
@@ -1067,8 +1071,7 @@
    # 矫正数据
    # 矫正方法为取矫正时间两侧的秒分布数据,用于确定计算结束坐标
    @classmethod
    def verify_num(cls, code, num, time_str):
    def verify_num(self, code, num, time_str):
        # 记录买1矫正日志
        logger_buy_1_volumn.info("涨停封单量矫正:代码-{} 量-{} 时间-{}", code, num, time_str)
        time_ = time_str.replace(":", "")
@@ -1081,13 +1084,13 @@
                # 只处理9:30后的数据
                if int(temp_time.replace(":", "")) < int("093000"):
                    break
                keys_ = cls.__get_l2_second_money_record_keys(code, temp_time.replace(":", ""))
                keys_ = self.__get_l2_second_money_record_keys(code, temp_time.replace(":", ""))
                if len(keys_) > 0:
                    keys.append(keys_[0])
                if len(keys) >= 1:
                    break
        else:
            keys_ = cls.__get_l2_second_money_record_keys(code, "*")
            keys_ = self.__get_l2_second_money_record_keys(code, "*")
            key_list = []
            for k in keys_:
                time__ = k.split("-")[-1]
@@ -1101,11 +1104,11 @@
        keys.sort(key=lambda tup: int(tup.split("-")[-1]))
        if len(keys) > 0:
            key = keys[0]
            val = RedisUtils.get(cls.__get_redis(), key)
            old_num, old_from, old_to = cls.__format_second_money_record_val(val)
            val = RedisUtils.get(self.__get_redis(), key)
            old_num, old_from, old_to = self.__format_second_money_record_val(val)
            end_index = old_to
            # 保存最近的数据
            cls.__set_l2_latest_money_record(code, end_index, num)
            self.__set_l2_latest_money_record(code, end_index, num)
            logger_buy_1_volumn.info("涨停封单量矫正成功:代码-{} 位置-{} 量-{}", code, end_index, num)
        else:
            logger_buy_1_volumn.info("涨停封单量矫正失败:代码-{} 时间-{} 量-{}", code, time_str, num)
@@ -1150,8 +1153,7 @@
        #     logger_buy_1_volumn.info("涨停封单量矫正结果:代码-{} 位置-{} 量-{}", code, end_index, num)
    # 计算量,用于涨停封单量的计算
    @classmethod
    def __compute_num(cls, code, data, buy_single_data):
    def __compute_num(self, code, data, buy_single_data):
        if L2DataUtil.is_limit_up_price_buy_cancel(data["val"]) or L2DataUtil.is_sell(data["val"]):
            # 涨停买撤与卖
            return 0 - int(data["val"]["num"]) * data["re"]
@@ -1165,15 +1167,13 @@
            return int(data["val"]["num"]) * data["re"]
    @classmethod
    def clear(cls, code):
    def clear(self, code):
        key = "l2_limit_up_money-{}".format(code)
        RedisUtils.delete(cls.__get_redis(), key)
        RedisUtils.delete(self.__get_redis(), key)
    # 返回取消的标志数据
    # with_cancel 是否需要判断是否撤销
    @classmethod
    def process_data(cls, code, start_index, end_index, buy_single_begin_index, buy_exec_index,
    def process_data(self, code, start_index, end_index, buy_single_begin_index, buy_exec_index,
                     with_cancel=True):
        if buy_single_begin_index is None or buy_exec_index is None:
            return None, None
@@ -1201,17 +1201,17 @@
                time_dict_num[time_] = 0
                time_dict_num_index[time_] = {"s": i, "e": i}
            time_dict_num_index[time_]["e"] = i
            num = cls.__compute_num(code, data, total_datas[buy_single_begin_index])
            num = self.__compute_num(code, data, total_datas[buy_single_begin_index])
            num_dict[i] = num
            time_dict_num[time_] = time_dict_num[time_] + num
        for t_ in time_dict_num:
            cls.__set_l2_second_money_record(code, t_, time_dict_num[t_], time_dict_num_index[t_]["s"],
            self.__set_l2_second_money_record(code, t_, time_dict_num[t_], time_dict_num_index[t_]["s"],
                                             time_dict_num_index[t_]["e"])
        print("保存涨停封单额时间:", round(time.time() * 1000) - start_time)
        # 累计最新的金额
        total_num, index = cls.__get_l2_latest_money_record(code)
        total_num, index = self.__get_l2_latest_money_record(code)
        record_msg = f"同花顺买1信息 {total_num},{index}"
        if index == -1:
@@ -1237,7 +1237,7 @@
        buy_exec_time = tool.get_time_as_second(total_datas[buy_exec_index]["val"]["time"])
        # 获取最大封单额
        max_buy1_volume = cls._thsBuy1VolumnManager.get_max_buy1_volume(code)
        max_buy1_volume = self._thsBuy1VolumnManager.get_max_buy1_volume(code)
        # 从同花顺买1矫正过后的位置开始计算,到end_index结束
@@ -1283,7 +1283,7 @@
            val = num_dict.get(i)
            if val is None:
                val = cls.__compute_num(code, data, total_datas[buy_single_begin_index])
                val = self.__compute_num(code, data, total_datas[buy_single_begin_index])
            total_num += val
            # 在处理数据的范围内,就需要判断是否要撤单了
            if start_index <= i <= end_index: