| | |
| | | |
| | | _place_order_info_dict = {} |
| | | |
| | | __place_order_position = {} |
| | | |
| | | |
| | | # 下单 |
| | | def place_order(code, price, volume, exec_index): |
| | |
| | | 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 |
| | |
| | | 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 |
| | |
| | | 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): |
| | |
| | | # 获取下单位置 |
| | | 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: |
| | |
| | | |
| | | # 买入纯买额统计 |
| | | 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], |
| | |
| | | # 数据是否处理完毕 |
| | | 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: |
| | | # 未达到下单条件,保存纯买额,设置纯买额 |
| | |
| | | localOrderId = data.get('localOrderId') |
| | | orderSysID = data.get('orderSysID') |
| | | accountID = data.get('accountID') |
| | | insertTime = data.get('insertTime') |
| | | code = data.get('securityId') |
| | | if localOrderId and orderSysID: |
| | | # 移除本地单号,添加系统单号 |
| | |
| | | "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): |
| | |
| | | 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 |
| | |
| | | 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": |
| | |
| | | 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]) |
| | |
| | | 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}") |
| | | |
| | | |
| | |
| | | __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(): |
| | |
| | | else: |
| | | trade_huaxin.order_success(resultJSON['securityId'], |
| | | resultJSON['accountID'], |
| | | resultJSON['orderSysID']) |
| | | resultJSON['orderSysID'], |
| | | resultJSON['insertTime'] |
| | | ) |
| | | return_str = json.dumps({"code": 0}) |
| | | finally: |
| | | # 更新委托列表 |
| | |
| | | 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 = {} |
| | | |
| | |
| | | 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): |