Administrator
2023-09-01 3c36073fa926e9fb2571baaac77012cb4a32ef3f
真实下单位置校验
6个文件已修改
117 ■■■■ 已修改文件
l2/huaxin/huaxin_delegate_postion_manager.py 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/l2_data_manager_new.py 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/huaxin_trade_api.py 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/huaxin_trade_data_update.py 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/trade_api_server.py 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/trade_huaxin.py 31 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/huaxin/huaxin_delegate_postion_manager.py
@@ -7,6 +7,8 @@
_place_order_info_dict = {}
__place_order_position = {}
# 下单
def place_order(code, price, volume, exec_index):
@@ -37,7 +39,7 @@
    exec_index = order_info[2]
    # 获取量
    for d in datas:
        if d["val"]["num"] != volume//100:
        if d["val"]["num"] != volume // 100:
            continue
        if abs(float(price) - float(d["val"]["price"])) >= 0.01:
            continue
@@ -49,5 +51,15 @@
        logger_real_place_order_position.info(f"真实下单位置:{code}-{d['index']}")
        if code in _place_order_info_dict:
            _place_order_info_dict.pop(code)
        __place_order_position[code] = d['index']
        return d["index"]
    return None
# 获取真实下单位置
def get_place_order_position(code):
    return __place_order_position.get(code)
def set_place_order_position(code, index):
    __place_order_position[code] = index
l2/l2_data_manager_new.py
@@ -286,6 +286,11 @@
            if code in cls.unreal_buy_dict:
                cls.unreal_buy_dict.pop(code)
    @classmethod
    def set_real_place_order_index(cls, code, index):
        cls.__DCancelBigNumComputer.set_real_order_index(code, index)
        cls.__SecondCancelBigNumComputer.set_real_place_order_index(code, index)
    # 处理华鑫L2数据
    @classmethod
    def process_huaxin(cls, code, origin_datas):
@@ -310,8 +315,7 @@
            # 获取下单位置
            place_order_index = huaxin_delegate_postion_manager.get_l2_place_order_position(code, datas)
            if place_order_index:
                cls.__DCancelBigNumComputer.set_real_order_index(code, place_order_index)
                cls.__SecondCancelBigNumComputer.set_real_place_order_index(code, place_order_index)
                cls.set_real_place_order_index(code, place_order_index)
                async_log_util.info(logger_l2_process, "code:{} 获取到下单真实位置:{}", code, place_order_index)
            __start_time = round(t.time() * 1000)
            if len(datas) > 0:
@@ -1022,12 +1026,13 @@
        # 买入纯买额统计
        new_buy_exec_index, buy_nums, buy_count, rebegin_buy_pos, max_num_set_new = cls.__sum_buy_num_for_order_3(code,
                                                                                                             start_process_index,
                                                                                                             compute_end_index,
                                                                                                             num, count,
                                                                                                             threshold_money,
                                                                                                             buy_single_index,
                                                                                                             max_num_set)
                                                                                                                  start_process_index,
                                                                                                                  compute_end_index,
                                                                                                                  num,
                                                                                                                  count,
                                                                                                                  threshold_money,
                                                                                                                  buy_single_index,
                                                                                                                  max_num_set)
        # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "纯买额统计时间")
        l2_log.debug(code, "m值-{} 量比:{} rebegin_buy_pos:{}", threshold_money, cls.volume_rate_info[code][0],
@@ -1056,9 +1061,11 @@
            # 数据是否处理完毕
            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)
                    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,
                    cls.__start_compute_buy(code, new_buy_exec_index + 1, compute_end_index, threshold_money,
                                            capture_time,
                                            is_first_code, False)
        else:
            # 未达到下单条件,保存纯买额,设置纯买额
trade/huaxin/huaxin_trade_api.py
@@ -304,6 +304,7 @@
                    localOrderId = data.get('localOrderId')
                    orderSysID = data.get('orderSysID')
                    accountID = data.get('accountID')
                    insertTime = data.get('insertTime')
                    code = data.get('securityId')
                    if localOrderId and orderSysID:
                        # 移除本地单号,添加系统单号
@@ -337,14 +338,14 @@
                            "price_type": price_type,
                            "price": price, "sinfo": sinfo}, request_id=request_id, blocking=blocking,
                           is_pipe=is_pipe_channel_normal())
    if blocking:
        try:
    try:
        if blocking:
            return __read_response(request_id, blocking)
        finally:
            huaxin_trade_data_update.add_delegate_list("下单")
            huaxin_trade_data_update.add_money_list()
    else:
        return {"local_order_id": local_order_id}
        else:
            return {"local_order_id": local_order_id}
    finally:
        huaxin_trade_data_update.add_delegate_list("下单", delay=0.2)
        huaxin_trade_data_update.add_money_list()
def cancel_order(direction, code, orderSysID, localOrderID=None, blocking=False, sinfo=None, request_id=None):
trade/huaxin/huaxin_trade_data_update.py
@@ -6,6 +6,7 @@
import threading
import time
from l2.l2_data_manager_new import L2TradeDataProcessor
from log_module.log import hx_logger_trade_debug, logger_system
from trade import trade_huaxin, trade_manager
from trade.huaxin import huaxin_trade_api, huaxin_trade_record_manager
@@ -22,6 +23,9 @@
            data = trade_data_request_queue.get()
            if data:
                type_ = data["type"]
                delay = data.get("delay")
                if delay and delay > 0:
                    time.sleep(delay)
                hx_logger_trade_debug.info(f"获取交易数据开始:{type_}")
                try:
                    if type_ == "delegate_list":
@@ -37,14 +41,18 @@
                                    if huaxin_util.is_can_cancel(d["orderStatus"]):
                                        codes.append(d["securityID"])
                                        # 设置下单成功
                                        trade_huaxin.order_success(d['securityID'],
                                                                   d['accountID'],
                                                                   d['orderSysID'])
                                        new_place_order_index = trade_huaxin.order_success(d['securityID'],
                                                                                           d['accountID'],
                                                                                           d['orderSysID'],
                                                                                           d['insertTime'])
                                        if new_place_order_index:
                                            L2TradeDataProcessor.set_real_place_order_index(d['securityID'],
                                                                                            new_place_order_index)
                                    elif huaxin_util.is_canceled(d["orderStatus"]) or huaxin_util.is_deal(d["orderStatus"]):
                                        # 已经撤单/已经成交,需要处理临时保存的系统订单号
                                        TradeOrderIdManager().remove_order_id(d['securityID'],
                                                                                           d['accountID'],
                                                                                           d['orderSysID'])
                                                                              d['accountID'],
                                                                              d['orderSysID'])
                                if codes:
                                    try:
                                        trade_manager.process_trade_delegate_data([{"code": c} for c in codes])
@@ -98,8 +106,8 @@
    trade_data_request_queue.put_nowait(data)
def add_delegate_list(source):
    __add_data({"type": "delegate_list"})
def add_delegate_list(source, delay=0):
    __add_data({"type": "delegate_list", "delay": delay})
    hx_logger_trade_debug.info(f"请求委托列表,来源:{source}")
@@ -107,8 +115,8 @@
    __add_data({"type": "deal_list"})
def add_money_list():
    __add_data({"type": "money"})
def add_money_list(delay=0):
    __add_data({"type": "money", "delay": delay})
def add_position_list():
trade/huaxin/trade_api_server.py
@@ -118,7 +118,9 @@
                                        else:
                                            trade_huaxin.order_success(resultJSON['securityId'],
                                                                       resultJSON['accountID'],
                                                                       resultJSON['orderSysID'])
                                                                       resultJSON['orderSysID'],
                                                                       resultJSON['insertTime']
                                                                       )
                                            return_str = json.dumps({"code": 0})
                                    finally:
                                        # 更新委托列表
trade/trade_huaxin.py
@@ -6,12 +6,13 @@
import time
import constant
from l2.huaxin import huaxin_delegate_postion_manager
from log_module import async_log_util
from log_module.log import logger_juejin_trade, hx_logger_trade_debug, logger_trade
from trade.huaxin import huaxin_trade_api
from trade.huaxin.huaxin_trade_record_manager import TradeOrderIdManager
from utils import tool, huaxin_util
from l2 import huaxin
from l2 import huaxin, l2_data_util
__context_dict = {}
@@ -77,8 +78,34 @@
        raise Exception("下单失败,无返回")
def order_success(code, accountId, orderSysID):
def order_success(code, accountId, orderSysID, insertTime):
    __TradeOrderIdManager.add_order_id(code, accountId, orderSysID)
    # 根据插入时间判断下单位置是否正确
    try:
        place_index = huaxin_delegate_postion_manager.get_place_order_position(code)
        if place_index and insertTime:
            # 大致判断是否为真实下单位置
            total_datas = l2_data_util.local_today_datas.get(code)
            if total_datas:
                if 0 < tool.trade_time_sub(insertTime, total_datas[place_index]["val"]["time"]) < 4:
                    # 4s内才会校验
                    pre_place_time = total_datas[place_index]["val"]["time"]
                    volume = total_datas[place_index]["val"]["num"]
                    for i in range(place_index + 1, len(total_datas)):
                        if total_datas[i]["val"]["num"] == volume and insertTime == total_datas[i]["val"]["time"]:
                            huaxin_delegate_postion_manager.set_place_order_position(code, i)
                            hx_logger_trade_debug.info("{}校验真实下单成功,{}->{}", code, place_index, i)
                            return i
                else:
                    raise Exception(f"不满足校验条件,真实下单时间:{insertTime}  预估下单时间:{total_datas[place_index]['val']['time']}")
            else:
                raise Exception("未获取到L2数据")
        else:
            raise Exception(f"尚未获取到数据(place_index-{place_index} insertTime-{insertTime})")
    except Exception as e:
        hx_logger_trade_debug.warning("{}校验真实下单位置出错:{}", code, str(e))
    return None
def cancel_order_success(code, accountId, orderSysID):