Administrator
2024-09-09 e9680308bd91bb112c29f03a1316a0fc49e1917a
一个板块扫入之后不能再次扫入
5个文件已修改
215 ■■■■■ 已修改文件
l2/l2_data_manager_new.py 175 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
servers/huaxin_trade_server.py 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
third_data/code_plate_key_manager.py 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
third_data/kpl_data_constant.py 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/trade_data_manager.py 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/l2_data_manager_new.py
@@ -691,29 +691,29 @@
    @classmethod
    def get_active_buy_blocks(cls, code):
        """
        获取激进买入的板块
        获取积极买入的板块
        @param code:
        @return:
        """
        if tool.is_sh_code(code):
            # 上证不激进下单
            # 上证不积极下单
            return None
        current_total_sell_data = L2MarketSellManager().get_current_total_sell_data(code)
        place_order_count = trade_data_manager.PlaceOrderCountManager().get_place_order_count(code)
        # 只有初次下单 + 总抛压大于500w能激进下单
        # 只有初次下单 + 总抛压大于500w能积极下单
        if place_order_count > 0 or current_total_sell_data is None or current_total_sell_data[1] < 500 * 10000:
            return None
        if global_util.zyltgb_map.get(code) > 50 * 100000000:
            # 50亿以上的无法激进下单
            # 50亿以上的无法积极下单
            return None
        can_buy_result = CodePlateKeyBuyManager.can_buy(code)
        if can_buy_result:
            if can_buy_result[5]:
                return can_buy_result[5]
            elif not can_buy_result[0] and can_buy_result[1]:
                # 10:00:00 前的独苗可激进买入
                # 10:00:00 前的独苗可积极买入
                if tool.trade_time_sub(tool.get_now_time_str(), "10:00:00") <= 0:
                    return ["独苗"]
        return None
@@ -735,7 +735,7 @@
                code)
            if order_begin_pos.MODE_RADICAL == order_begin_pos.mode:
                # 激进下单不判断条件
                # 积极下单不判断条件
                can, need_clear_data, reason, is_valid_exec_index = True, False, "激进下单", True
            else:
                can, need_clear_data, reason, is_valid_exec_index = cls.__can_buy_first(code)
@@ -804,7 +804,7 @@
                                temps = []
                                temps.extend([f"{x[0]}" for x in can_buy_result[0]])
                                if can_buy_result[5]:
                                    temps.append(f"激进买入:{can_buy_result[5]}")
                                    temps.append(f"积极买入:{can_buy_result[5]}")
                                info.set_kpl_match_blocks(temps)
                        trade_record_log_util.add_place_order_log(code, info)
                    except Exception as e:
@@ -856,8 +856,17 @@
        return True, ""
    @classmethod
    def __is_on_limit_up_buy(cls, code, buy_exec_index):
    def __is_at_limit_up_buy(cls, code, buy_exec_index=None):
        """
        是否是板上放量买
        @param code:
        @param buy_exec_index:
        @return:
        """
        total_data = local_today_datas.get(code)
        if buy_exec_index is None:
            # 执行位置为空,就取最近的一条数据
            buy_exec_index = total_data[-1]["index"]
        latest_exec_indexes = cls.__latest_exec_indexes[code]
        if not latest_exec_indexes:
            latest_exec_indexes = []
@@ -984,7 +993,7 @@
            # 1.当前成交价为涨停价
            # 2.距离最近的非板上成交的时间大于一个阈值
            if abs(limit_up_price - float(trade_price)) < 0.001:
                is_limit_up_buy = cls.__is_on_limit_up_buy(code, order_begin_pos.buy_exec_index)
                is_limit_up_buy = cls.__is_at_limit_up_buy(code, order_begin_pos.buy_exec_index)
                if is_limit_up_buy:
                    # 板上买且非加绿
                    # 获取最近的非涨停价成交时间
@@ -1360,21 +1369,42 @@
        cls.unreal_buy_dict[code] = (buy_exec_index, capture_time)
        trade_result_manager.virtual_buy_success(code)
    # 是否是在板上买
    @classmethod
    def __is_at_limit_up_buy(cls, code_):
        # 总卖为0,当前成交价为涨停价就判断为板上买
        current_sell_data = cls.__L2MarketSellManager.get_current_total_sell_data(code_)
        if current_sell_data and tool.trade_time_sub(tool.get_now_time_str(), current_sell_data[0]) < 5:
            # 5s内的数据才有效
            if current_sell_data[1] <= 0:
                # 总卖为0
                trade_price = current_price_process_manager.get_trade_price(code_)
                limit_up_price = gpcode_manager.get_limit_up_price(code_)
                if trade_price and limit_up_price and abs(float(trade_price) - float(limit_up_price)) <= 0.001:
                    # 当前成交价为涨停价
                    return True
        return False
    def __process_with_find_exec_index(cls, code, order_begin_pos: OrderBeginPosInfo, compute_end_index):
        """
        处理找到执行位置
        @return:
        """
        total_datas = local_today_datas.get(code)
        l2_log.debug(code, "获取到买入执行位置:{} m值:{} 纯买手数:{} 纯买单数:{} 是否板上买:{} 数据:{} ,量比:{} ,下单模式:{}",
                     order_begin_pos.buy_exec_index,
                     order_begin_pos.threshold_money,
                     order_begin_pos.num,
                     order_begin_pos.count, order_begin_pos.at_limit_up, total_datas[order_begin_pos.buy_exec_index],
                     cls.volume_rate_info[code], order_begin_pos.mode)
        cls.__save_order_begin_data(code, order_begin_pos)
        cls.__LimitUpTimeManager.save_limit_up_time(code, total_datas[order_begin_pos.buy_exec_index]["val"]["time"])
        l2_log.debug(code, "delete_buy_cancel_point")
        if code not in cls.__latest_exec_indexes:
            cls.__latest_exec_indexes[code] = []
        cls.__latest_exec_indexes[code].append(order_begin_pos.buy_exec_index)
        # 保留最近3次的买入执行位置
        if len(cls.__latest_exec_indexes[code]) > 3:
            cls.__latest_exec_indexes[code] = cls.__latest_exec_indexes[code][-3:]
        # 直接下单
        ordered = cls.__buy(code, 0, total_datas[-1], total_datas[-1]["index"], True)
        # 数据是否处理完毕
        if order_begin_pos.buy_exec_index < compute_end_index:
            if ordered:
                cls.__process_order(code, order_begin_pos.buy_exec_index + 1, compute_end_index, 0, True,
                                    False)
            else:
                cls.__start_compute_buy(code, order_begin_pos.buy_exec_index + 1, compute_end_index,
                                        order_begin_pos.threshold_money,
                                        0,
                                        True, False)
    @classmethod
    def __start_compute_buy(cls, code, compute_start_index, compute_end_index, threshold_money, capture_time,
@@ -1408,18 +1438,44 @@
        _start_time = tool.get_now_timestamp()
        total_datas = local_today_datas[code]
        # ---------计算激进买入的信号---------
        radical_result = cls.__compute_radical_order_begin_pos(code, compute_start_index, compute_end_index)
        if radical_result[0]:
            buy_single_index, buy_exec_index = radical_result[1], radical_result[1]
            buy_volume_rate = cls.volume_rate_info[code][0]
            refer_sell_data = cls.__L2MarketSellManager.get_refer_sell_data(code, total_datas[buy_single_index]["val"][
                "time"])
            if refer_sell_data:
                sell_info = (refer_sell_data[0], refer_sell_data[1])
            else:
                sell_info = (total_datas[buy_single_index]["val"]["time"], 0)
            threshold_money = 0
            order_begin_pos_info = OrderBeginPosInfo(buy_single_index=buy_single_index,
                                                     buy_exec_index=buy_exec_index,
                                                     buy_compute_index=buy_exec_index,
                                                     num=total_datas[buy_single_index]["val"]["num"], count=1,
                                                     max_num_set=set(),
                                                     buy_volume_rate=buy_volume_rate,
                                                     mode=OrderBeginPosInfo.MODE_RADICAL,
                                                     mode_desc="激进买入",
                                                     sell_info=sell_info,
                                                     threshold_money=threshold_money)
            order_begin_pos_info.at_limit_up = cls.__is_at_limit_up_buy(code)
            cls.__process_with_find_exec_index(code, order_begin_pos_info, compute_end_index)
            return
        # 获取买入信号计算起始位置
        order_begin_pos = cls.__get_order_begin_pos(
            code)
        order_begin_pos = cls.__get_order_begin_pos(code)
        # 是否为新获取到的位置
        new_get_single = False
        buy_single_index = order_begin_pos.buy_single_index
        if buy_single_index is None:
            # ------------------------------确定信号种类----------------------------------
            # 第一步:获取激进下单信号
            # 第一步:获取积极下单信号
            # if code.find('60') == 0:
            # 激进买
            # 积极买
            continue_count = 1
            has_single, _index, sell_info, single_msg, mode = cls.__compute_active_order_begin_pos(code, continue_count,
                                                                                                   compute_start_index,
@@ -1475,12 +1531,6 @@
        if new_get_single:
            start_process_index = order_begin_pos.buy_single_index
        # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "计算m值大单")
        # threshold_money, msg = cls.__get_threshmoney(code)
        # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "m值阈值计算")
        # 设置为总卖额
        new_buy_exec_index, buy_nums, buy_count, rebegin_buy_pos, threshold_money_new, max_num_set_new, not_buy_msg, clear_buy_single = cls.__sum_buy_num_for_order_active(
            code,
@@ -1506,42 +1556,17 @@
            return
        if new_buy_exec_index is not None:
            l2_log.debug(code, "获取到买入执行位置:{} m值:{} 纯买手数:{} 纯买单数:{} 是否板上买:{} 数据:{} ,量比:{} ,下单模式:{}", new_buy_exec_index,
                         threshold_money,
                         buy_nums,
                         buy_count, order_begin_pos.at_limit_up, total_datas[new_buy_exec_index],
                         cls.volume_rate_info[code], order_begin_pos.mode)
            cls.__save_order_begin_data(code, OrderBeginPosInfo(buy_single_index=buy_single_index,
                                                                buy_exec_index=new_buy_exec_index,
                                                                buy_compute_index=new_buy_exec_index,
                                                                num=buy_nums, count=buy_count,
                                                                max_num_set=max_num_set_new,
                                                                buy_volume_rate=cls.volume_rate_info[code][0],
                                                                mode=order_begin_pos.mode,
                                                                mode_desc=order_begin_pos.mode_desc,
                                                                sell_info=order_begin_pos.sell_info,
                                                                threshold_money=threshold_money))
            cls.__LimitUpTimeManager.save_limit_up_time(code, total_datas[new_buy_exec_index]["val"]["time"])
            l2_log.debug(code, "delete_buy_cancel_point")
            if code not in cls.__latest_exec_indexes:
                cls.__latest_exec_indexes[code] = []
            cls.__latest_exec_indexes[code].append(new_buy_exec_index)
            # 保留最近3次的买入执行位置
            if len(cls.__latest_exec_indexes[code]) > 3:
                cls.__latest_exec_indexes[code] = cls.__latest_exec_indexes[code][-3:]
            # 直接下单
            ordered = cls.__buy(code, capture_time, total_datas[-1], total_datas[-1]["index"], is_first_code)
            # 数据是否处理完毕
            if new_buy_exec_index < compute_end_index:
                if ordered:
                    cls.__process_order(code, new_buy_exec_index + 1, compute_end_index, capture_time, is_first_code,
                                        False)
                else:
                    cls.__start_compute_buy(code, new_buy_exec_index + 1, compute_end_index, threshold_money,
                                            capture_time,
                                            is_first_code, False)
            cls.__process_with_find_exec_index(code, OrderBeginPosInfo(buy_single_index=buy_single_index,
                                                                       buy_exec_index=new_buy_exec_index,
                                                                       buy_compute_index=new_buy_exec_index,
                                                                       num=buy_nums, count=buy_count,
                                                                       max_num_set=max_num_set_new,
                                                                       buy_volume_rate=cls.volume_rate_info[code][0],
                                                                       mode=order_begin_pos.mode,
                                                                       mode_desc=order_begin_pos.mode_desc,
                                                                       sell_info=order_begin_pos.sell_info,
                                                                       threshold_money=threshold_money),
                                               compute_end_index)
        else:
            # 未达到下单条件,保存纯买额,设置纯买额
            # 记录买入信号位置
@@ -1773,7 +1798,7 @@
                return True, i, [refer_sell_data[0],
                                 0], '上证买入', OrderBeginPosInfo.MODE_ACTIVE if active_buy_blocks else OrderBeginPosInfo.MODE_FAST
            return False, -1, "未获取到激进买的起始信号", '', OrderBeginPosInfo.MODE_NORMAL
            return False, -1, "未获取到积极买的起始信号", '', OrderBeginPosInfo.MODE_NORMAL
        else:
            # 深证
            if refer_sell_data is None:
@@ -1796,7 +1821,7 @@
                # 判断最近有没有涨停卖数据
                limit_up_sell_count = L2TradeSingleDataProcessor.get_latest_limit_up_sell_order_count(code)
                if (limit_up_sell_count == 0 or active_buy_blocks) and not single:
                    # 如果没有涨停卖数据/激进下单而且还没有成交买入信号,就按照原来的总卖额计算
                    # 如果没有涨停卖数据/积极下单而且还没有成交买入信号,就按照原来的总卖额计算
                    threshold_money, sell_1_price = copy.deepcopy(refer_sell_data[1]), refer_sell_data[3][0]
                    for i in range(start_index - 1, -1, -1):
                        val = total_datas[i]["val"]
@@ -1832,7 +1857,7 @@
                    return True, buy_single_index, [refer_sell_data[0],
                                                    0], "板上放量", OrderBeginPosInfo.MODE_ACTIVE if active_buy_blocks else OrderBeginPosInfo.MODE_NORMAL
        return False, -1, "未获取到激进买的起始信号", '', OrderBeginPosInfo.MODE_NORMAL
        return False, -1, "未获取到积极买的起始信号", '', OrderBeginPosInfo.MODE_NORMAL
    # 计算激进买的下单信号
    @classmethod
@@ -1844,10 +1869,9 @@
        @param code:
        @param start_index:
        @param end_index:
        @return: (是否获取到信号, 信号位置)
        @return: (是否获取到信号, 信号位置, 消息)
        """
        # 激进买信号的时间
        radical_data = RadicalBuyDealCodesManager.buy_by_l2_delegate_expire_time_dict.get(code)
        if not radical_data:
@@ -1855,9 +1879,8 @@
        if t.time() > radical_data[0]:
            return False, None, "超过生效时间"
        # 判断是否是板上放量
        if cls.__is_on_limit_up_buy(code, start_index):
        if cls.__is_at_limit_up_buy(code, start_index):
            return False, None, "板上放量"
        total_datas = local_today_datas[code]
        min_num = int(29900 / gpcode_manager.get_limit_up_price_as_num(code))
servers/huaxin_trade_server.py
@@ -40,7 +40,8 @@
    hx_logger_l2_orderdetail, hx_logger_l2_market_data, logger_l2_g_cancel, logger_debug, \
    logger_system, logger_trade, logger_local_huaxin_l1_trade_info, logger_l2_codes_subscript, logger_l2_radical_buy
from third_data import block_info, kpl_data_manager, history_k_data_manager, huaxin_l1_data_manager
from third_data.code_plate_key_manager import KPLCodeJXBlockManager, CodePlateKeyBuyManager, RadicalBuyBlockManager
from third_data.code_plate_key_manager import KPLCodeJXBlockManager, CodePlateKeyBuyManager, RadicalBuyBlockManager, \
    TargetCodePlateKeyManager
from third_data.history_k_data_util import JueJinApi
from trade import trade_manager, l2_trade_util, \
    trade_data_manager, trade_constant
@@ -787,11 +788,35 @@
                    result = RadicalBuyBlockManager.is_radical_buy(code, yesterday_codes)
                    async_log_util.info(logger_l2_radical_buy, f"计算板块结果:{code}-{result}")
                    self.__radical_buy_by_blocks_result_cache[code] = (time.time() + 3, result)
                    RadicalBuyDealCodesManager.radical_buy_blocks_dict[code] = result[0]
                else:
                    # 取缓存
                    result = result[1]
                if result[0]:
                    # 如果关键词包含已成交的原因就不再下单
                    # 获取已经成交代码的板块
                    # ---------------获取已经成交代码的板块----------------
                    deal_reasons = set()
                    for dc in deal_codes:
                        # 获取涨停原因
                        limit_up_reason = kpl_data_manager.LimitUpDataConstant.get_limit_up_reason_with_history(dc)
                        if limit_up_reason in constant.KPL_INVALID_BLOCKS:
                            limit_up_reason = None
                        # 如果涨停原因为空就需要获取上次激进买的原因
                        if limit_up_reason is None:
                            radical_buy_blocks = RadicalBuyDealCodesManager.radical_buy_blocks_dict.get(dc)
                            deal_reasons |= radical_buy_blocks
                        else:
                            deal_reasons.add(limit_up_reason)
                    # 成交代码的板块如果和目标代码板块有交集就不扫
                    keys_, k1_, k11_, k2_, k3_, k4_ = TargetCodePlateKeyManager().get_plate_keys(code,
                                                                                                 contains_today=False)
                    match_blocks = keys_ & deal_reasons
                    if match_blocks:
                        async_log_util.info(logger_l2_radical_buy, f"板块有成交:{code}-{match_blocks}")
                        return
                    # 有可以扫的板块
                    # 判断最近的主动买占了总买额的多少
                    refer_sell_data = L2MarketSellManager().get_refer_sell_data(code, l2_huaxin_util.convert_time(
third_data/code_plate_key_manager.py
@@ -1211,7 +1211,8 @@
            if _code in yesterday_limit_up_codes:
                continue
            # 剔除炸板代码持续涨停时间小于1分钟的代码
            if _code not in cls.__current_limit_up_codes and _code in cls.__total_limit_up_space_dict and cls.__total_limit_up_space_dict[_code] < 60:
            if _code not in cls.__current_limit_up_codes and _code in cls.__total_limit_up_space_dict and \
                    cls.__total_limit_up_space_dict[_code] < 60:
                continue
            # 代码,涨停时间
            block_codes_infos.append((_code, int(k[5])))
@@ -1323,6 +1324,7 @@
        # 获取代码的板块
        keys_, k1_, k11_, k2_, k3_, k4_ = cls.__TargetCodePlateKeyManager.get_plate_keys(code, contains_today=False)
        match_blocks = open_limit_up_blocks & keys_
        can_buy_blocks = set()
        fmsges = []
        msges = []
third_data/kpl_data_constant.py
@@ -42,6 +42,13 @@
        return cls.__history_code_blocks_dict.get(code)
    @classmethod
    def get_limit_up_reason_with_history(cls, code):
        d = cls.__history_code_data_dict.get(code)
        if d:
            return d[2]
        return None
    @classmethod
    def get_first_limit_up_time(cls, code):
        if code in cls.__history_code_data_dict:
            return int(cls.__history_code_data_dict[code][5])
trade/trade_data_manager.py
@@ -471,6 +471,8 @@
    __mysqldb = Mysqldb()
    # 根据L2数据来激进买入的有效时间:{"code":(有效截至时间, 买单号)}
    buy_by_l2_delegate_expire_time_dict = {}
    # 仅仅买的板块
    radical_buy_blocks_dict = {}
    def __new__(cls, *args, **kwargs):
        if not cls.__instance: