| | |
| | | import time |
| | | |
| | | from huaxin_client import constant as huaxin_client_constant |
| | | from l2 import l2_data_util |
| | | from l2 import l2_data_util, l2_data_source_util |
| | | from l2.l2_data_util import L2DataUtil |
| | | from log_module import async_log_util |
| | | from log_module.log import hx_logger_trade_debug, logger_real_place_order_position, logger_debug |
| | | from trade.huaxin import huaxin_trade_record_manager |
| | | from utils import tool |
| | | import concurrent.futures |
| | | |
| | | _place_order_info_dict = {} |
| | | |
| | |
| | | if L2DataUtil.time_sub_as_ms(total_datas[i]['val'], exec_data["val"]) >= THRESH_MS: |
| | | return i |
| | | return None |
| | | |
| | | |
| | | __re_compute_threading_pool = concurrent.futures.ThreadPoolExecutor(max_workers=10) |
| | | |
| | | |
| | | # L2数据列表 |
| | |
| | | if code in _place_order_info_dict: |
| | | _place_order_info_dict.pop(code) |
| | | __place_order_position[code] = real_place_index_info[0] |
| | | __re_compute_threading_pool.submit(__recompute_for_slow_time, code, order_info, real_place_index_info[0]) |
| | | return real_place_index_info[0] |
| | | else: |
| | | return None |
| | | |
| | | |
| | | # 因为L2数据慢的问题而重新计算 |
| | | def __recompute_for_slow_time(code, order_info, real_place_index): |
| | | try: |
| | | # 计算当前时间是否满足时间条件 |
| | | now_time_str = tool.get_now_time_str() |
| | | if tool.trade_time_sub(now_time_str, "09:30:00") < 60 or 0 <= tool.trade_time_sub(now_time_str, "13:00:00") <= 60: |
| | | price = order_info[0] |
| | | volume = order_info[1] |
| | | exec_data = order_info[2] |
| | | order_time = order_info[3] # 下单时间 |
| | | order_ref = order_info[4] |
| | | shadow_price = order_info[5] |
| | | # 在重新计算真实下单位置,在2s后计算 |
| | | for t in range(0, 10): |
| | | time.sleep(1) |
| | | # 判断是否需要重新计算(手数不一致/已撤单) |
| | | total_datas = l2_data_util.local_today_datas.get(code) |
| | | real_place_index_data = total_datas[real_place_index] |
| | | try: |
| | | current_delegates = huaxin_trade_record_manager.DelegateRecordManager().list_current_delegates(code) |
| | | if not current_delegates: |
| | | continue |
| | | delegate_info = current_delegates[0] |
| | | |
| | | if str(delegate_info["orderRef"]) != str(order_ref): |
| | | continue |
| | | # 看是否需要矫正 |
| | | need_update = False |
| | | if not need_update and delegate_info["acceptTime"] != real_place_index_data["val"]["time"]: |
| | | # 下单时间不一致 |
| | | need_update = True |
| | | |
| | | if not need_update and real_place_index_data["val"]["num"] != volume: |
| | | # 下单量不一致 |
| | | need_update = True |
| | | |
| | | if not need_update and L2DataUtil.is_limit_up_price_buy(real_place_index_data["val"]): |
| | | # 不是涨停买 |
| | | need_update = True |
| | | |
| | | if not need_update: |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2(code, |
| | | real_place_index, |
| | | total_datas, |
| | | l2_data_util.local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count < 1: |
| | | # 已经撤单 |
| | | need_update = True |
| | | if need_update: |
| | | # 需要更新真实下单位置 |
| | | # 在时间内找引子单 |
| | | shadow_order_indexes = [] |
| | | for i in range(exec_data["index"] + 1, total_datas[-1]["index"]): |
| | | data = total_datas[i] |
| | | val = data['val'] |
| | | # 默认引子单与真实单下单时间一致 |
| | | if val["time"] != delegate_info["acceptTime"]: |
| | | continue |
| | | if not L2DataUtil.is_buy(val): |
| | | continue |
| | | if val["num"] != huaxin_client_constant.SHADOW_ORDER_VOLUME // 100: |
| | | continue |
| | | if abs(shadow_price - float(val["price"])) >= 0.01: |
| | | continue |
| | | shadow_order_indexes.append(i) |
| | | |
| | | # 获取引子单前最近且符合真实下单位置特征的数据 |
| | | findexes_info = [] |
| | | for index in shadow_order_indexes: |
| | | |
| | | for i in range(index - 1, exec_data["index"], -1): |
| | | data = total_datas[i] |
| | | val = data['val'] |
| | | if val["time"] != total_datas[index]["val"]["time"]: |
| | | continue |
| | | if not L2DataUtil.is_limit_up_price_buy(val): |
| | | continue |
| | | if volume != val["num"]: |
| | | continue |
| | | left_count = l2_data_source_util.L2DataSourceUtils.get_limit_up_buy_no_canceled_count_v2( |
| | | code, |
| | | i, |
| | | total_datas, |
| | | l2_data_util.local_today_canceled_buyno_map.get( |
| | | code)) |
| | | if left_count < 1: |
| | | continue |
| | | findexes_info.append((index, i)) |
| | | break |
| | | if findexes_info: |
| | | findexes_info.sort(key=lambda x: x[0] - x[1]) |
| | | # 获取成功 |
| | | real_place_index = findexes_info[0][1] |
| | | async_log_util.info(logger_real_place_order_position, |
| | | f"真实下单位矫正:{code}-{real_place_index} 下单数据:{order_info}") |
| | | finally: |
| | | pass |
| | | else: |
| | | return |
| | | pass |
| | | except Exception as e: |
| | | logger_real_place_order_position.exception(e) |
| | | |
| | | |
| | | # 获取真实下单位置 |
| | | def get_place_order_position(code): |
| | | return __place_order_position.get(code) |
| | |
| | | nameDict = HistoryKDatasUtils.get_gp_codes_names([d['securityID']]) |
| | | name = nameDict.get(d['securityID']) |
| | | cls.mysqldb.execute( |
| | | "insert into hx_trade_delegate_record values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % ( |
| | | "insert into hx_trade_delegate_record values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s', '%s')" % ( |
| | | _id, d["orderLocalID"], d["securityID"], name, d["direction"], |
| | | d["orderSysID"], d["insertTime"], d["insertDate"], d["acceptTime"], d["cancelTime"], |
| | | d["limitPrice"], d["turnover"], d["volume"], d["volumeTraded"], d["orderStatus"], |
| | | d["orderSubmitStatus"], d["statusMsg"], tool.get_now_datetime_str(), |
| | | tool.get_now_datetime_str(), d["accountID"])) |
| | | tool.get_now_datetime_str(), d["accountID"],d["orderRef"])) |
| | | else: |
| | | # 修改数据 |
| | | updateDict = {} |
| | |
| | | updateDict['orderSubmitStatus'] = d['orderSubmitStatus'] |
| | | if result[16] != d['statusMsg']: |
| | | updateDict['statusMsg'] = d['statusMsg'] |
| | | if result[20] != d['orderRef']: |
| | | updateDict['orderRef'] = d['orderRef'] |
| | | if updateDict: |
| | | # 有更新数据 |
| | | updateDict['updateTime'] = tool.get_now_datetime_str() |
| | |
| | | # 转dict |
| | | key_list = ["id", "orderLocalID", "securityID", "securityName", "direction", "orderSysID", "insertTime", |
| | | "insertDate", "acceptTime", "cancelTime", "limitPrice", "turnover", "volume", "volumeTraded", |
| | | "orderStatus", "orderSubmitStatus", "statusMsg", "createTime", "updateTime", "accountID"] |
| | | "orderStatus", "orderSubmitStatus", "statusMsg", "createTime", "updateTime", "accountID", "orderRef"] |
| | | fresults = [] |
| | | max_update_time = None |
| | | if results: |