Administrator
2024-03-25 a4af22e525c8dc0a5b178f05f2b2f745233990bb
G撤修改/买单价30块以下的票/L2接口修改
7个文件已修改
191 ■■■■ 已修改文件
constant.py 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/cancel_buy_strategy.py 94 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/l2_transaction_data_processor.py 34 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
third_data/data_server.py 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/huaxin_trade_server.py 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/data_export_util.py 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/tool.py 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
constant.py
@@ -115,6 +115,9 @@
# L撤下单之后多久开始守护
L_CANCEL_START_TIME = -1
# L前的守护时间
L_CANCEL_UP_EXPIRE_TIME = 3 * 60
L_CANCEL_MIN_WATCH_COUNT = 10
# 撤单比例
L_CANCEL_RATE = 0.6
@@ -139,7 +142,6 @@
# 行情好时的撤单比例
G_CANCEL_RATE_FOR_GOOD_MARKET = 0.49
# 华鑫L2的卡位数量
HUAXIN_L2_MAX_CODES_COUNT = 70
@@ -162,5 +164,5 @@
    TRADE_ENABLE = True
# 最大的代码价格
MAX_CODE_PRICE = 50
MAX_SUBSCRIPT_CODE_PRICE = 50
MAX_CODE_PRICE = 30
MAX_SUBSCRIPT_CODE_PRICE = 30
l2/cancel_buy_strategy.py
@@ -135,8 +135,10 @@
        real_order_index = real_order_index_info[0]
        total_deal_money = sum([x[1] * x[2] for x in big_sell_order_info[1]])
        start_order_no = big_sell_order_info[1][0][3][1]
        total_num = 0
        # 防止分母位0
        total_num = 1
        # 获取正在成交的数据
        dealing_info = HuaXinBuyOrderManager.get_dealing_order_info(code)
        for i in range(trade_index, real_order_index):
            data = total_datas[i]
            val = data['val']
@@ -144,6 +146,10 @@
                continue
            if int(val['orderNo']) < start_order_no:
                continue
            if i == trade_index and dealing_info and str(total_datas[trade_index]["val"]["orderNo"]) == str(
                    dealing_info[0]):
                # 减去当前正在成交的数据中已经成交了的数据
                total_num -= dealing_info[1] // 100
            left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, i,
                                                                                                     total_datas,
                                                                                                     local_today_canceled_buyno_map.get(
@@ -926,7 +932,8 @@
            l2_log.l_cancel_debug(code, f"计算L后囊括范围:{start_index}-{end_index}")
            total_datas = local_today_datas.get(code)
            if re_compute > 0 and tool.trade_time_sub(total_datas[-1]["val"]["time"],
                                                      total_datas[buy_single_index]["val"]["time"]) < 2 * 60 and min_cancel_time_with_ms is None:
                                                      total_datas[buy_single_index]["val"][
                                                          "time"]) < 2 * 60 and min_cancel_time_with_ms is None:
                # 封单额稳了以后,间隔超过2分钟才能重新计算
                l2_log.l_cancel_debug(code, f"要间隔2分钟过后才能重新计算")
                return
@@ -1340,8 +1347,9 @@
    def __compute_near_by_trade_progress_need_cancel(self, code, buy_exec_index, start_index, end_index, total_data,
                                                     is_first_code):
        # L前不生效
        if True:
        # L前守护时间为3分钟
        if tool.trade_time_sub(total_data[-1]['val']['time'],
                               total_data[buy_exec_index]['val']['time']) > constant.L_CANCEL_UP_EXPIRE_TIME:
            return False, None
        watch_indexes = self.__get_near_by_trade_progress_indexes_cache(code)
@@ -1691,6 +1699,7 @@
    __trade_progress_index_dict = {}
    __watch_indexes_dict = {}
    __watch_indexes_by_dict = {}
    __expire_time_dict = {}
    __instance = None
@@ -1704,10 +1713,57 @@
        start_index = buy_single_index
        if code in self.__trade_progress_index_dict:
            start_index = self.__trade_progress_index_dict.get(code)
        total_datas = local_today_datas.get(code)
        # 设置生效截至时间
        self.__reset_expire_time(code, total_datas[index]['val']['time'])
        self.__commpute_watch_indexes(code, start_index, (index, is_default), from_real_order_index_changed=True)
    # 重新设置G撤守护时间
    def __reset_expire_time(self, code, now_time):
        self.__expire_time_dict[code] = tool.trade_time_add_second(now_time, 15)
    # 大有单卖
    def set_big_sell_order_info(self, code, big_sell_order_info):
        money = big_sell_order_info[0]
        if money < 50 * 10000:
            # 无大卖单
            return
        # 获取真实下单位置
        real_place_order_info = self.__real_place_order_index_dict.get(code)
        if not real_place_order_info:
            # 没有真实下单位置
            return
        if real_place_order_info[1]:
            # 不能是默认下单位置
            return
        trade_index = self.__trade_progress_index_dict.get(code)
        # 下单15s之后才会重新触发
        total_datas = local_today_datas.get(code)
        if tool.trade_time_sub(total_datas[-1]['val']['time'],
                               total_datas[real_place_order_info[0]]['val']['time']) <= 15:
            return
        # 查找真实成交位置
        real_order_index = real_place_order_info[0]
        start_order_no = big_sell_order_info[1][-1][4][1]
        real_trade_index = trade_index
        for i in range(trade_index, real_order_index):
            data = total_datas[i]
            val = data['val']
            if not L2DataUtil.is_limit_up_price_buy(val):
                continue
            if int(val['orderNo']) < start_order_no:
                continue
            real_trade_index = i
            break
        l2_log.g_cancel_debug(code, f"大单卖触发重新囊括,真实成交位置:{real_trade_index},卖信息:{big_sell_order_info}")
        self.__commpute_watch_indexes(code, real_trade_index, real_place_order_info, recompute=True)
        self.__reset_expire_time(code, total_datas[real_trade_index]['val']['time'])
    def clear(self, code=None):
        if code:
            if code in self.__expire_time_dict:
                self.__expire_time_dict.pop(code)
            if code in self.__real_place_order_index_dict:
                self.__real_place_order_index_dict.pop(code)
            if code in self.__watch_indexes_dict:
@@ -1723,7 +1779,9 @@
            self.__trade_progress_index_dict.clear()
            self.__watch_indexes_by_dict.clear()
    def __commpute_watch_indexes(self, code, traded_index, real_order_index_info, from_real_order_index_changed=False):
    # recompute:是否重新计算
    def __commpute_watch_indexes(self, code, traded_index, real_order_index_info, from_real_order_index_changed=False,
                                 recompute=False):
        if traded_index is None or real_order_index_info is None:
            return
        real_order_index, is_default = real_order_index_info[0], real_order_index_info[1]
@@ -1734,8 +1792,14 @@
        if origin_watch_index_by is None:
            origin_watch_index_by = set()
        # 重新计算需要清除之前的数据
        if recompute:
            origin_watch_index.clear()
            origin_watch_index_by.clear()
        start_index = traded_index
        if traded_index in origin_watch_index or traded_index in origin_watch_index_by:
            # 正在成交的是已经囊括了的大单
            start_index = traded_index + 1
        total_datas = local_today_datas.get(code)
@@ -1757,16 +1821,16 @@
                watch_indexes.add(i)
        if watch_indexes:
            # 还有300万以上的大单没有撤单
            if from_real_order_index_changed:
            if from_real_order_index_changed or recompute:
                # 真实下单位改变后才会更新
                final_watch_indexes = origin_watch_index | watch_indexes
                self.__watch_indexes_dict[code] = final_watch_indexes
                l2_log.g_cancel_debug(code, f"大单监听:{final_watch_indexes}")
                l2_log.g_cancel_debug(code, f"大单监听:{final_watch_indexes} 是否重新计算:{recompute}")
                # 有大单监听,需要移除之前的小单监听
                if code in self.__watch_indexes_by_dict:
                    self.__watch_indexes_by_dict[code].clear()
        else:
            l2_log.g_cancel_debug(code, f"没有大单监听,开始计算小单:{start_index}-{real_order_index}")
            l2_log.g_cancel_debug(code, f"没有大单监听,开始计算小单:{start_index}-{real_order_index}, 是否重新计算:{recompute}")
            # 没有300万以上的大单了,计算备用
            # 只有备用单成交了或者没有备用单,才会再次寻找备用单
            need_find_by = False
@@ -1807,11 +1871,19 @@
    def need_cancel(self, code, buy_exec_index, start_index, end_index):
        if code not in self.__real_place_order_index_dict:
            return False, None, "没有找到真实下单位"
        expire_time = self.__expire_time_dict.get(code)
        if not expire_time:
            return False, None, "尚未设置生效时间"
        real_place_order_index, is_default = self.__real_place_order_index_dict.get(code)
        total_datas = local_today_datas.get(code)
        if tool.trade_time_sub(total_datas[end_index]["val"]["time"], total_datas[buy_exec_index]['val']['time']) > 180:
            return False, None, "超过180s的生效时间"
        # 30s内有效
        if tool.trade_time_sub(total_datas[end_index]["val"]["time"], total_datas[buy_exec_index]["val"]["time"]) > 15:
            return False, None, "下单15s内才生效"
        if tool.trade_time_sub(total_datas[end_index]["val"]["time"], expire_time) > 0:
            return False, None, "超过生效时间"
        watch_indexes = self.__watch_indexes_dict.get(code)
        if watch_indexes is None:
l2/l2_transaction_data_processor.py
@@ -9,8 +9,7 @@
from l2.l2_transaction_data_manager import HuaXinBuyOrderManager, HuaXinSellOrderStatisticManager
from log_module import async_log_util
from log_module.log import hx_logger_l2_debug, logger_l2_trade_buy_queue, logger_debug, hx_logger_l2_upload
from msg import buy_order_msg_manager
from trade import current_price_process_manager, trade_manager
from trade import current_price_process_manager
from trade.deal_big_money_manager import DealOrderNoManager
@@ -25,7 +24,6 @@
            d = datas[i]
            buy_no = f"{d[6]}"
            if buyno_map and buy_no in buyno_map:
                async_log_util.info(hx_logger_l2_debug, f"{code}成交进度:{buyno_map[buy_no]['index']}")
                buy_progress_index = buyno_map[buy_no]["index"]
                break
        return buy_progress_index
@@ -38,11 +36,12 @@
        __start_time = time.time()
        try:
            buyno_map = l2_data_util.local_today_buyno_map.get(code)
            if not buyno_map:
                if trade_manager.CodesTradeStateManager().get_trade_state(
                        code) != trade_manager.TRADE_STATE_NOT_TRADE:
                    l2_data_util.load_l2_data(code)
                    buyno_map = l2_data_util.local_today_buyno_map.get(code)
            # 暂时不需要重新加载获取
            # if not buyno_map:
            #     if trade_manager.CodesTradeStateManager().get_trade_state_cache(
            #             code) != trade_manager.TRADE_STATE_NOT_TRADE:
            #         l2_data_util.load_l2_data(code)
            #         buyno_map = l2_data_util.local_today_buyno_map.get(code)
            if buyno_map is None:
                buyno_map = {}
@@ -78,7 +77,7 @@
                if need_cancel:
                    # async_log_util.error(logger_debug, f"{code} S前撤单:{cancel_msg}")
                    L2TradeDataProcessor.cancel_buy(code, f"S后撤:{cancel_msg}")
                GCancelBigNumComputer().set_big_sell_order_info(code, big_sell_order_info)
            except Exception as e:
                async_log_util.error(logger_debug, f"卖单统计异常:{big_sell_order_info}")
                logger_debug.exception(e)
@@ -120,28 +119,11 @@
                if order_begin_pos and order_begin_pos.buy_exec_index and order_begin_pos.buy_exec_index > -1:
                    HourCancelBigNumComputer().set_transaction_index(code, order_begin_pos.buy_single_index,
                                                                     buy_progress_index)
                    # cresult = FCancelBigNumComputer().need_cancel_for_deal_fast(code, buy_progress_index)
                    # if cresult[0] and not DCancelBigNumComputer().has_auto_cancel_rules(code):
                    #     L2TradeDataProcessor.cancel_buy(code, f"下单30s内排单不足:{cresult[1]}")
                    cresult = FCancelBigNumComputer().need_cancel_for_deal_fast_with_total_sell(code,
                                                                                                buy_progress_index,
                                                                                                order_begin_pos)
                    if cresult[0] and not DCancelBigNumComputer().has_auto_cancel_rules(code):
                        L2TradeDataProcessor.cancel_buy(code, f"3s内成交太多:{cresult[1]}")
                    # ---------------------------------成交进度位变化-------------------------------
                    # if buy_progress_index_changed:
                    #     # 交易进度变化,判断到真实下单位置的距离
                    #     real_order_index = SCancelBigNumComputer().get_real_place_order_index_cache(code)
                    #     if real_order_index and real_order_index >= buy_progress_index:
                    #         # 发送下单消息
                    #         try:
                    #             buy_order_msg_manager.almost_deal(code, real_order_index, buy_progress_index)
                    #             buy_order_msg_manager.follow_not_enough(code, order_begin_pos.buy_exec_index,
                    #                                                     real_order_index)
                    #         except Exception as e:
                    #             logger_debug.exception(e)
            else:
                pass
            if order_begin_pos and order_begin_pos.buy_exec_index and order_begin_pos.buy_exec_index > -1:
third_data/data_server.py
@@ -361,9 +361,12 @@
                    total_datas = None
                else:
                    date = tool.get_now_date_str()
                datas = data_export_util.get_l2_datas(code, total_datas, date=date)
                delegate_datas = data_export_util.get_l2_datas(code, total_datas, date=date)
                transaction_datas = data_export_util.get_l2_transaction_datas(code, date=date)
                code_name = gpcode_manager.get_code_name(code)
                response_data = json.dumps({"code": 0, "data": {"code": code, "code_name": code_name, "data": datas}})
                response_data = json.dumps({"code": 0, "data": {"code": code, "code_name": code_name,
                                                                "data": {"delegates": delegate_datas,
                                                                         "transactions": transaction_datas}}})
            except Exception as e:
                logger_debug.exception(e)
        elif url.path == "/get_trade_progress":
@@ -541,7 +544,7 @@
                    limit_up_count_dict[d[3]] = d[12]
            for x in statistic_list:
                fresults.append((x[0], gpcode_manager.get_code_name(x[0]), x[1],limit_up_count_dict.get(x[0])))
                fresults.append((x[0], gpcode_manager.get_code_name(x[0]), x[1], limit_up_count_dict.get(x[0])))
            fresults = fresults[:30]
            response_data = json.dumps({"code": 0, "data": fresults})
trade/huaxin/huaxin_trade_server.py
@@ -386,17 +386,18 @@
        use_time = int((now_time - timestamp) * 1000)
        thread_id = random.randint(0, 100000)
        l2_log.threadIds[code] = thread_id
        l2_data_count = len(_datas)
        l2_log.info(code, hx_logger_l2_orderdetail,
                    f"{code}#耗时:{use_time}-{thread_id}#数量:{len(_datas)}#{_datas[-1]}")
                    f"{code}#耗时:{use_time}-{thread_id}#数量:{l2_data_count}#{_datas[-1]}")
        # l2_data_log.l2_time_log(code, "开始处理L2逐笔委托")
        try:
            l2_data_manager_new.L2TradeDataProcessor.process_huaxin(code, _datas)
        finally:
            use_time = time.time() - now_time
            if use_time > 0.01:
            if use_time > 0.008:
                l2_data_log.l2_time_log(code,
                                        f"处理L2逐笔委托结束:处理数据数量: {len(_datas)} 最终处理时间:{round(use_time * 1000, 2)}ms")
                                        f"处理L2逐笔委托结束:处理数据数量: {l2_data_count} 最终处理时间:{round(use_time * 1000, 2)}ms")
    @classmethod
    def l2_transaction(cls, code, datas):
utils/data_export_util.py
@@ -49,6 +49,15 @@
    return fdatas
def get_l2_transaction_datas(code, date=None):
    if date is None:
        date = tool.get_now_date_str()
    sell_no_dict = log_export.load_huaxin_transaction_sell_no(code=code, date=date)
    sell_nos = sell_no_dict.get(code)
    fdatas = export_l2_transaction_data(code,sell_nos)
    return fdatas
def export_l2_data(code, datas, process_indexs, trade_indexs, real_position_indexes, deal_list_dict, sell_nos):
    def find_process_index(index):
        for i in range(0, len(process_indexs)):
@@ -217,12 +226,37 @@
                    item.append(l2_huaxin_util.convert_time(sell_info[4][0], with_ms=True))
                    item.append(None)
                    item.append(sell_info[0])
                    fdatas.insert(order_no_indexes[i + 1][1], (0,None,item))
                    fdatas.insert(order_no_indexes[i + 1][1], (0, None, item))
                    break
    return fdatas
def export_l2_transaction_data(code, sell_nos):
    fdatas = []
    if sell_nos:
        index = 0
        for sell_info in sell_nos:
            if sell_info[1] * sell_info[2] < 50 * 10000:
                continue
            index += 1
            item = []
            item.append(index)
            item.append(l2_huaxin_util.convert_time(sell_info[3][0], with_ms=True))
            item.append("")
            item.append(
                "{}万".format(round(sell_info[1] * sell_info[2] / 10000, 1)))
            item.append(sell_info[2])
            item.append(sell_info[1] // 100)
            item.append("主动卖")
            item.append(1)
            item.append(l2_huaxin_util.convert_time(sell_info[4][0], with_ms=True))
            item.append(None)
            item.append(sell_info[0])
            fdatas.append((0, None, item))
    return fdatas
def __save_l2_datas(code, fdatas, dest_dir=f"{constant.get_path_prefix()}/export/l2"):
    local_time = time.strftime("%Y%m%dT%H%M%S", time.localtime(time.time()))
    file_name = "{}/{}_{}.xls".format(dest_dir, code, local_time)
utils/tool.py
@@ -329,5 +329,4 @@
if __name__ == "__main__":
    print(compare_time_with_ms("10:00:00.123","10:01:01.123"))
    print(trade_time_add_millionsecond("11:29:59.123", 1888))
    print(trade_time_add_second("11:29:50",15))