Administrator
2024-04-10 9a8a924d75fd4e29c4c14c86c041c00ba89f615a
下单策略修改
9个文件已修改
164 ■■■■■ 已修改文件
constant.py 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
huaxin_client/l2_data_manager.py 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/cancel_buy_strategy.py 26 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/l2_data_manager_new.py 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/l2_transaction_data_manager.py 29 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
log_module/log_export.py 9 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/l2_trade_test.py 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/test_process_time.py 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/test_sell.py 51 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
constant.py
@@ -167,5 +167,5 @@
    TRADE_ENABLE = True
# 最大的代码价格
MAX_CODE_PRICE = 20 if ALL_ACTIVE_BUY else 30
MAX_SUBSCRIPT_CODE_PRICE = 20 if ALL_ACTIVE_BUY else 30
MAX_CODE_PRICE = 10 if ALL_ACTIVE_BUY else 30
MAX_SUBSCRIPT_CODE_PRICE = 30 if ALL_ACTIVE_BUY else 30
huaxin_client/l2_data_manager.py
@@ -49,7 +49,7 @@
    # special_price:过滤的1手的价格
    def set_order_fileter_condition(self, code, min_volume, limit_up_price, shadow_price, buy_volume):
        if code not in self.filter_order_condition_dict:
            self.filter_order_condition_dict[code] = [(min_volume, limit_up_price, shadow_price, buy_volume, min_volume//10)]
            self.filter_order_condition_dict[code] = [(min_volume, limit_up_price, shadow_price, buy_volume,  min_volume//10 if code.find("00")==0 else min_volume)]
            huaxin_l2_log.info(logger_local_huaxin_l2_subscript,
                               f"({code})常规过滤条件设置:{self.filter_order_condition_dict[code]}")
l2/cancel_buy_strategy.py
@@ -235,17 +235,34 @@
    # 是否有大卖单需要撤
    def __need_cancel_for_big_sell_order(self, code, big_sell_order_info, order_begin_pos: OrderBeginPosInfo):
        total_deal_money = sum([x[1] * x[2] for x in big_sell_order_info[1]])
        # 需要排除成交时间在下单时间之前的
        total_deal_money = 0
        total_datas = local_today_datas.get(code)
        real_order_index_info = self.__get_real_place_order_index_info_cache(code)
        real_order_time_ms = None
        if real_order_index_info and not real_order_index_info[1]:
            real_order_index = real_order_index_info[0]
            real_order_time_ms = total_datas[real_order_index]["val"]["time"] + ".{0:0>3}".format(
                total_datas[real_order_index]["val"]["tms"])
        for x in big_sell_order_info[1]:
            deal_time = l2_huaxin_util.convert_time(x[4][0], with_ms=True)
            if real_order_time_ms:
                if tool.trade_time_sub_with_ms(deal_time, real_order_time_ms) >= 0:
                    total_deal_money += x[1] * x[2]
            else:
                total_deal_money += x[1] * x[2]
        # 判断是否为激进下单
        threash_money_w = 299
        if order_begin_pos.mode == OrderBeginPosInfo.MODE_ACTIVE:
            try:
                total_datas = local_today_datas.get(code)
                # 真实成交位距离真实下单位,10笔以内且金额≤1000万 , 且在180s内
                trade_index, is_default = TradeBuyQueue().get_traded_index(code)
                if trade_index is None:
                    trade_index = 0
                real_order_index_info = self.__get_real_place_order_index_info_cache(code)
                limit_up_price = gpcode_manager.get_limit_up_price(code)
                if real_order_index_info and not real_order_index_info[1]:
                    real_order_index = real_order_index_info[0]
@@ -1694,7 +1711,8 @@
                if dealing_info and str(dealing_info[0]) == str(val["orderNo"]):
                    total_left_num -= dealing_info[1] // 100
        limit_up_price = gpcode_manager.get_limit_up_price(code)
        if total_left_count <= THRESHOLD_COUNT and limit_up_price and total_left_num * float(limit_up_price) < 300 * 100:
        if total_left_count <= THRESHOLD_COUNT and limit_up_price and total_left_num * float(
                limit_up_price) < 300 * 100:
            return True, f"剩余笔数({total_left_count})/金额({round(total_left_num * float(limit_up_price) * 100)})不足,成交进度:{trade_index},真实下单位置:{real_order_index}"
        return False, "不满足撤单条件"
l2/l2_data_manager_new.py
@@ -458,8 +458,9 @@
                    cls.__process_order(code, start_index, end_index, capture_timestamp, is_first_code)
                else:
                    # 未挂单,时间相差不大才能挂单
                    # if tool.trade_time_sub(latest_time, "09:32:00") < 0 or l2.l2_data_util.L2DataUtil.is_same_time(now_time_str, latest_time):
                    cls.__process_not_order(code, start_index, end_index, capture_timestamp, is_first_code)
                    if tool.trade_time_sub(latest_time, "09:32:00") < 0 or l2.l2_data_util.L2DataUtil.is_same_time(
                            now_time_str, latest_time):
                        cls.__process_not_order(code, start_index, end_index, capture_timestamp, is_first_code)
            l2_log.info(code, logger_l2_process, "code:{} 处理数据范围: {}-{} 处理时间:{} 线程ID:{}", code,
                        add_datas[0]["index"],
                        add_datas[-1]["index"], round(t.time() * 1000) - __start_time,
@@ -1305,6 +1306,9 @@
            # ------------------------------确定信号种类----------------------------------
            # 第一步:获取激进下单信号
            continue_count = cls.__l2PlaceOrderParamsManagerDict[code].get_begin_continue_buy_count()
            # if code.find('60') == 0:
            # 激进买
            continue_count = 1
            has_single, _index, sell_info = cls.__compute_active_order_begin_pos(code, continue_count, max(
                (compute_start_index - continue_count - 1) if new_add else compute_start_index, 0), compute_end_index)
            fast_msg = None
@@ -1651,18 +1655,22 @@
            # 有可买板块,有激进买板块
            # 第一步: 计算总卖额
            threshold_money, sell_1_price = refer_sell_data[1], refer_sell_data[3][0]
            for i in range(start_index - 1, -1, -1):
                val = total_datas[i]["val"]
                if tool.compare_time(val["time"], refer_sell_data[0]) <= 0:
                    break
                if L2DataUtil.is_sell(val):
                    threshold_money += val["num"] * int(float(val["price"]) * 100)
                elif L2DataUtil.is_sell_cancel(val):
                    threshold_money -= val["num"] * int(float(val["price"]) * 100)
                elif L2DataUtil.is_buy(val):
                    # 判断价格(大于卖1) 被买吃掉
                    if round(float(val["price"]), 2) - sell_1_price >= 0:
            if code.find("60") == 0:
                threshold_money = 0
            else:
                for i in range(start_index - 1, -1, -1):
                    val = total_datas[i]["val"]
                    if tool.compare_time(val["time"], refer_sell_data[0]) < 0:
                        # 将本s的计算上去
                        break
                    if L2DataUtil.is_sell(val):
                        threshold_money += val["num"] * int(float(val["price"]) * 100)
                    elif L2DataUtil.is_sell_cancel(val):
                        threshold_money -= val["num"] * int(float(val["price"]) * 100)
                    elif L2DataUtil.is_buy(val):
                        # 判断价格(大于卖1) 被买吃掉
                        if round(float(val["price"]), 2) - sell_1_price >= 0:
                            threshold_money -= val["num"] * int(float(val["price"]) * 100)
            # 第二步:计算起始信号
            second_930 = 9 * 3600 + 30 * 60 + 0
            total_datas = local_today_datas.get(code)
l2/l2_transaction_data_manager.py
@@ -108,11 +108,8 @@
    @classmethod
    def add_transaction_datas(cls, code, datas, buy_exec_index=None):
        # 是否为主动卖
        def is_active_sell(sell_no):
            if buy_exec_index is not None:
                return sell_no > int(total_datas[buy_exec_index]["val"]["orderNo"])
            else:
                return f"{sell_no}" not in sell_no_map
        def is_active_sell(sell_no, buy_no):
            return sell_no > buy_no
        # q.append((data['SecurityID'], data['TradePrice'], data['TradeVolume'],
        #           data['OrderTime'], data['MainSeq'], data['SubSeq'], data['BuyNo'],
@@ -135,6 +132,8 @@
        if not sell_no_map:
            sell_no_map = {}
        for d in datas:
            if not is_active_sell(d[7], d[6]):
                continue
            cls.__latest_sell_order_info_list_dict[code].append(d)
            if code not in cls.__latest_sell_order_dict:
                cls.__latest_sell_order_dict[code] = [d[7], d[2], d[1], (d[3], d[6]), (d[3], d[6])]
@@ -152,12 +151,12 @@
                    # 大于50w加入卖单
                    money = info[1] * info[2]
                    if money >= 500000:
                        if is_active_sell(info[0]):
                            l2_log.info(code, hx_logger_l2_transaction_sell_order,
                                        f"{cls.__latest_sell_order_dict[code]}")
                            cls.__big_sell_order_ids_dict[code].add(info[0])
                            cls.__big_sell_order_info_dict[code][info[0]] = info
                            cls.__big_sell_order_info_list_dict[code].append(info)
                        # 订单里面有成交是主动卖就算主动卖
                        l2_log.info(code, hx_logger_l2_transaction_sell_order,
                                    f"{cls.__latest_sell_order_dict[code]}")
                        cls.__big_sell_order_ids_dict[code].add(info[0])
                        cls.__big_sell_order_info_dict[code][info[0]] = info
                        cls.__big_sell_order_info_list_dict[code].append(info)
                    # 只保留10w以上的单
                    if money > 100000:
                        cls.__latest_all_sell_orders_dict[code].append(info)
@@ -203,10 +202,10 @@
        # 统计最近的大单
        if latest_sell_order_info[1] * latest_sell_order_info[2] >= 500000:
            if latest_sell_order_info[0] not in temp_sell_order_ids:
                if is_active_sell(latest_sell_order_info[0]):
                    big_sell_orders.append(latest_sell_order_info)
                    temp_sell_order_ids.add(latest_sell_order_info[0])
                    total_sell_info[0] += int(latest_sell_order_info[1] * latest_sell_order_info[2])
                # if is_active_sell(latest_sell_order_info[0], latest_sell_order_info[3][1]):
                big_sell_orders.append(latest_sell_order_info)
                temp_sell_order_ids.add(latest_sell_order_info[0])
                total_sell_info[0] += int(latest_sell_order_info[1] * latest_sell_order_info[2])
        big_sell_orders.reverse()
        total_sell_info[1] = big_sell_orders
        return total_sell_info
log_module/log_export.py
@@ -501,7 +501,7 @@
# 读取系统日志
def load_huaxin_transaction_map(date=tool.get_now_date_str()):
def load_huaxin_transaction_map(date=tool.get_now_date_str(), with_time=False):
    path = f"{constant.get_path_prefix()}/logs/huaxin/l2/transaction.{date}.log"
    fdatas = {}
    if os.path.exists(path):
@@ -512,12 +512,17 @@
                    try:
                        data = line.split(" - ")[1].strip()
                        if data.startswith("["):
                            time_str = data[data.find("[") + 1:data.find("]")].strip()
                            data = data[data.find("]") + 1:].strip()
                        code = data.split("#")[0]
                        l2_data = eval(data.split("#")[1])
                        if code not in fdatas:
                            fdatas[code] = []
                        fdatas[code].append(l2_data)
                        if with_time:
                            fdatas[code].append((time_str, l2_data))
                        else:
                            fdatas[code].append(l2_data)
                    except:
                        pass
    return fdatas
test/l2_trade_test.py
@@ -206,9 +206,9 @@
            l2.l2_data_manager_new.L2TradeDataProcessor.process_add_datas(code, total_datas[indexs[0]:indexs[1] + 1], 0,
                                                                          0)
    @unittest.skip("跳过此单元测试")
    # @unittest.skip("跳过此单元测试")
    def test_s_cancel(self):
        code = "603363"
        code = "002288"
        l2.l2_data_util.load_l2_data(code)
        l2.l2_data_util.local_today_datas[code] = l2.l2_data_util.local_today_datas[code][:222]
        l2.cancel_buy_strategy.SCancelBigNumComputer().set_real_place_order_index(code, 153, False)
test/test_process_time.py
@@ -4,19 +4,20 @@
def print_big_use_time():
    with open(file="D:/logs/gp/l2/l2_process.2024-04-09.log", mode='r',encoding="utf-8") as f:
    with open(file="D:/logs/gp/l2/l2_process.2024-04-10.log", mode='r',encoding="utf-8") as f:
        while True:
            line = f.readline()
            if not line:
                break
            datas = line.split(" ")
            use_time=0
            for d in datas:
                if d.find("code:") > -1:
                    code = d.replace("code:", "")
                if d.find("处理时间:") > -1:
                    use_time = int(d.replace("处理时间:", ""))
            if use_time > 5:
                print(line)
                print(use_time, line)
if __name__ == "__main__":
test/test_sell.py
@@ -1,18 +1,29 @@
from l2.huaxin import l2_huaxin_util
from l2.l2_data_manager import OrderBeginPosInfo
from l2.l2_transaction_data_manager import HuaXinSellOrderStatisticManager
from l2 import l2_data_util
from l2 import l2_data_util, cancel_buy_strategy
from log_module import log_export, async_log_util
from utils import tool
def statistic_buy_order():
    code = "600768"
    data_map = log_export.load_huaxin_transaction_map(date=tool.get_now_date_str())
    code = "002187"
    data_map = log_export.load_huaxin_transaction_map(date="2024-04-10", with_time=True)
    datas = data_map.get(code)
    __latest_buy_order_dict = {}
    total_money = 0
    active_buy = 0
    passive_buy = 0
    for data in datas:
        for d in data:
        time_str = data[0]
        for d in data[1]:
            if d[6] == 990054:
                if d[7] < 990054:
                    active_buy += d[2]
                    print("主动买:", time_str, d)
                else:
                    passive_buy += d[2]
                    print("被动买:", time_str, d)
            if code not in __latest_buy_order_dict:
                # 格式:[买单号,手数,单价]
                __latest_buy_order_dict[code] = [d[6], d[2], d[1], (d[3], d[1]), (d[3], d[1])]
@@ -30,23 +41,31 @@
                    # 大于50w加入卖单
                    money = info[1] * info[2]
                    # if 101328000 > info[3] > 101324000:
                    if tool.trade_time_sub(l2_huaxin_util.convert_time(info[3][0]), "10:08:30") >= 0 and info[2] >= 11.54:
                    if tool.trade_time_sub(l2_huaxin_util.convert_time(info[3][0]), "10:08:30") >= 0 and info[
                        2] >= 11.54:
                        total_money += money
                        print(int(total_money), round(money / 10000, 1), info)
                    if money >= 500000:
                        pass
                    __latest_buy_order_dict[code] = [d[6], d[2], d[1], (d[3], d[1]), (d[3], d[1])]
    print(f"主动买:{active_buy}", f"被动买:{passive_buy}")
# 卖单统计
def statistic_sell_order():
    code = "600768"
    code = "002248"
    data_map = log_export.load_huaxin_transaction_map(date=tool.get_now_date_str())
    datas = data_map.get(code)
    __latest_sell_order_dict = {}
    total_money = 0
    for data in datas:
        for d in data:
            if d[7] == 559018:
                if d[7] > d[6]:
                    print("主动卖", d)
                else:
                    print("被动卖", d)
            if code not in __latest_sell_order_dict:
                __latest_sell_order_dict[code] = [d[7], d[2], d[1], (d[3], d[6]), (d[3], d[6])]
            else:
@@ -84,7 +103,7 @@
def test_sell_order_info():
    code = "002734"
    code = "002248"
    l2_data_util.load_l2_data(code, force=True)
    date = tool.get_now_date_str()
    data_map = log_export.load_huaxin_transaction_map(date=date)
@@ -92,13 +111,17 @@
    for i in range(len(datas)):
        d = datas[i]
        big_sell_order_info = HuaXinSellOrderStatisticManager.add_transaction_datas(code, d)
        if d[-1][7] >= 3911165:
            sell_infos = HuaXinSellOrderStatisticManager.get_latest_transaction_datas(code, 3910694, "09:35:27")
            print(sum([x[1] * x[2] for x in sell_infos]))
        if big_sell_order_info[0] < 50 * 10000:
            continue
        print(i, sum([x[1] * x[2] for x in big_sell_order_info[1]]), big_sell_order_info)
        if big_sell_order_info[1] and big_sell_order_info[1][-1][0] == 559018:
            print(big_sell_order_info)
            cancel_buy_strategy.SCancelBigNumComputer().set_real_place_order_index(code, 208, False)
            cancel_buy_strategy.SCancelBigNumComputer().set_big_sell_order_info_for_cancel(code, big_sell_order_info,
                                                                                           OrderBeginPosInfo(
                                                                                               mode=OrderBeginPosInfo.MODE_ACTIVE,buy_single_index=0,buy_exec_index=13))
        # if big_sell_order_info[0] < 50 * 10000:
        #     continue
        # print(i, sum([x[1] * x[2] for x in big_sell_order_info[1]]), big_sell_order_info)
if __name__ == '__main__':
    statistic_buy_order()
    test_sell_order_info()