Administrator
2023-09-14 3ea5e7ec00b813f5c311d423267a9be4db09c3fe
撤单顺序调整
4个文件已修改
100 ■■■■ 已修改文件
huaxin_client/l2_client.py 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/cancel_buy_strategy.py 77 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/l2_data_manager_new.py 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/trade_server.py 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
huaxin_client/l2_client.py
@@ -587,9 +587,7 @@
            pipe_strategy = _pipe_strategy
            t1 = threading.Thread(target=__receive_from_pipe_strategy, args=(_pipe_strategy,), daemon=True)
            t1.start()
        # TODO 测试注释
        # __init_l2()
        __init_l2()
        global l2_data_callback
        l2_data_callback = _l2_data_callback
        l2_data_manager.run_upload_common(l2_data_callback)
l2/cancel_buy_strategy.py
@@ -616,7 +616,7 @@
                    self.__del_real_order_index(code)
    # 设置成交位
    def set_trade_progress(self, code, index, buy_exec_index, total_data, local_today_num_operate_map, m_value,
    def set_trade_progress(self, code, index, buy_exec_index, total_data, local_today_num_operate_map, m_base_value,
                           limit_up_price):
        # 离下单执行位2分钟内的有效
        sub_time = tool.trade_time_sub(total_data[-1]['val']['time'], total_data[buy_exec_index]['val']['time'])
@@ -641,9 +641,9 @@
                                                                                                  local_today_num_operate_map)
            left_num += val['num'] * left_count
        # 剩下的不足动态M值的1/2
        rate = round(float(limit_up_price) * left_num * 100 / m_value, 3)
        rate = round(float(limit_up_price) * left_num * 100 / m_base_value, 3)
        logger_l2_d_cancel.info(
            f"{code}成交进度({index})到下单位置({real_order_index})的剩余笔数:{left_num},撤单比例:{rate},m值:{m_value}")
            f"{code}成交进度({index})到下单位置({real_order_index})的剩余笔数:{left_num},撤单比例:{rate},m值:{m_base_value}")
        if rate < constant.D_CANCEL_RATE:
            l2_log.cancel_debug(code, "D撤撤单,比例为:{},目标比例{}", rate, constant.D_CANCEL_RATE)
            return True, f"D撤比例为:{rate}"
@@ -669,6 +669,9 @@
    __last_trade_progress_dict = {}
    __real_place_order_index_dict = {}
    __cancel_watch_index_cache = {}
    # 成交位附近临近大单索引
    __near_by_trade_progress_big_num_index_cache = {}
    __SecondCancelBigNumComputer = SecondCancelBigNumComputer()
    __instance = None
@@ -688,6 +691,12 @@
                code = k.split("-")[-1]
                val = RedisUtils.smembers(__redis, k)
                CodeDataCacheUtil.set_cache(cls.__cancel_watch_index_cache, code, val)
            keys = RedisUtils.keys(__redis, "l_cancel_near_by_big_num_index-*")
            for k in keys:
                code = k.split("-")[-1]
                val = RedisUtils.get(__redis, k)
                val = int(val)
                CodeDataCacheUtil.set_cache(cls.__near_by_trade_progress_big_num_index_cache, code, val)
        finally:
            RedisUtils.realse(__redis)
@@ -727,6 +736,22 @@
        if cache_result[0]:
            return cache_result[1]
        return set()
    def __set_near_by_big_num_index(self, code, index):
        self.__near_by_trade_progress_big_num_index_cache[code] = index
        RedisUtils.setex_async(self.__db, f"l_cancel_near_by_big_num_index-{code}", tool.get_expire(), index)
    def __get_near_by_big_num_index(self, code):
        val = RedisUtils.get(self.__get_redis(), f"l_cancel_near_by_big_num_index-{code}")
        if val is None:
            return None
        return int(val)
    def __get_near_by_big_num_index_cache(self, code):
        cache_result = CodeDataCacheUtil.get_cache(self.__near_by_trade_progress_big_num_index_cache, code)
        if cache_result[0]:
            return cache_result[1]
        return None
    def del_watch_index(self, code):
        CodeDataCacheUtil.clear_cache(self.__cancel_watch_index_cache, code)
@@ -790,11 +815,53 @@
            if buy_single_index:
                self.compute_watch_index(code, buy_single_index, index)
    # 计算范围内的成交位临近未撤大单
    def __compute_near_by_big_num_index(self, code, start_index, end_index, total_datas):
        MIN_MONEYS = [300, 200, 100, 50]
        for min_money in MIN_MONEYS:
            final_index = None
            for i in range(start_index, end_index):
                data = total_datas[i]
                val = data['val']
                if not L2DataUtil.is_limit_up_price_buy(val):
                    continue
                # 小金额过滤
                if float(val['price']) * val['num'] < min_money * 100:
                    continue
                left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count(code, i,
                                                                                                      total_datas,
                                                                                                      local_today_num_operate_map.get(
                                                                                                          code))
                if left_count > 0:
                    final_index = i
                    break
            if final_index:
                self.__near_by_trade_progress_big_num_index_cache[code] = final_index
                break
    # 设置成交位置,成交位置变化之后相应的监听数据也会发生变化
    def set_trade_progress(self, code, index, total_data):
    def set_trade_progress(self, code, index, total_datas):
        # 临近撤先撤单
        # 如果成交位置并未该改变且没有没有找到成交位置临近大单
        # need_compute_big = False
        # if self.__last_trade_progress_dict.get(
        #         code) == index and code not in self.__near_by_trade_progress_big_num_index_cache:
        #     need_compute_big = True
        # elif self.__last_trade_progress_dict.get(code) != index:
        #     need_compute_big = True
        #
        # # 设置成交位置临近大单监听
        # real_place_order = self.__real_place_order_index_dict.get(code)
        # # 真实下单位置
        # end_index = real_place_order
        # if not end_index:
        #     end_index = total_datas[-1]["index"]
        #
        # if need_compute_big:
        #     self.__compute_near_by_big_num_index(code, index+1, end_index, total_datas)
        if self.__last_trade_progress_dict.get(code) == index:
            return
        try:
            # 已经有计算的无法触发计算
            old_watch_indexes = self.__get_watch_indexes_cache(code)
l2/l2_data_manager_new.py
@@ -549,10 +549,9 @@
            buy_volume_rate = 0.2
        # 依次处理
        cancel_data, cancel_msg = s_cancel(buy_single_index, buy_exec_index)
        cancel_data, cancel_msg = l_cancel(buy_single_index, buy_exec_index)
        if not cancel_data:
            cancel_data, cancel_msg = l_cancel(buy_single_index, buy_exec_index)
            cancel_data, cancel_msg = s_cancel(buy_single_index, buy_exec_index)
        if not cancel_data:
            cancel_data, cancel_msg = h_cancel(buy_single_index, buy_exec_index)
        # l2_log.debug(code, "撤单计算结束")
@@ -822,10 +821,10 @@
                                                                                                              num_operate_map)
                        if left_count > 0:
                            not_cancel_num += total_data[i]["val"]["num"]
                m, msg = cls.__get_threshmoney(code)
                m_base_val = l2_trade_factor.L2PlaceOrderParamsManager.get_base_m_val(code)
                not_cancel_money = not_cancel_num * 100 * float(gpcode_manager.get_limit_up_price(code))
                if m > not_cancel_money:
                    return False, False, f"成交位置距离当前位置纯买额({not_cancel_money})小于m值({m})"
                if m_base_val > not_cancel_money:
                    return False, False, f"成交位置距离当前位置纯买额({not_cancel_money})小于m值({m_base_val})"
        else:
            # 判断买1价格档位
            zyltgb = global_util.zyltgb_map.get(code)
trade/huaxin/trade_server.py
@@ -334,13 +334,13 @@
                        buy_time = total_datas[buy_progress_index]["val"]["time"]
                        limit_up_price = gpcode_manager.get_limit_up_price(code)
                        if buy_exec_index:
                            m_base_val = L2PlaceOrderParamsManager.get_base_m_val(code)
                            need_cancel, msg = DCancelBigNumComputer().set_trade_progress(code,
                                                                                          buy_progress_index,
                                                                                          buy_exec_index,
                                                                                          total_datas,
                                                                                          num_operate_map,
                                                                                          num * 100 * float(
                                                                                              limit_up_price),
                                                                                          m_base_val,
                                                                                          limit_up_price)
                            if need_cancel:
                                L2TradeDataProcessor.cancel_buy(code, f"D撤:{msg}", source="d_cancel")
@@ -410,9 +410,7 @@
        is_normal = l2_data_util.load_l2_data(code, load_latest=False)
        volume_rate = code_volumn_manager.get_volume_rate(code)
        volume_rate_index = code_volumn_manager.get_volume_rate_index(volume_rate)
        m_val = \
            L2PlaceOrderParamsManager(code, True, volume_rate, volume_rate_index, None).get_m_val()[
                0]
        m_val = L2PlaceOrderParamsManager.get_base_m_val(code)
        limit_up_price = gpcode_manager.get_limit_up_price(code)
        m_val_num = int(m_val / (float(limit_up_price) * 100))