| | |
| | | pOrderField.LimitPrice, pOrderField.VolumeTotalOriginal, pOrderField.OrderSysID, |
| | | pOrderField.OrderStatus)) |
| | | if pOrderField.OrderStatus != traderapi.TORA_TSTP_OST_Unknown: |
| | | set_system_order_id(pOrderField.SInfo, pOrderField.OrderSysID) |
| | | OrderIDManager.set_system_order_id(pOrderField.SecurityID, pOrderField.SInfo, pOrderField.OrderSysID) |
| | | threading.Thread(target=lambda: self.__data_callback(TYPE_ORDER, 0, {"sinfo": pOrderField.SInfo, |
| | | "securityId": pOrderField.SecurityID, |
| | | "orderLocalId": pOrderField.OrderLocalID, |
| | |
| | | |
| | | |
| | | # 获取响应发送socket |
| | | |
| | | |
| | | global req_rid_dict |
| | | req_rid_dict = {} |
| | | |
| | |
| | | |
| | | def OnTrade(self, client_id, request_id, type_, data): |
| | | if type_ == 1: |
| | | logger_local_huaxin_trade_debug.info(f"---------------------\n请求下单:client_id-{client_id} request_id-{request_id}") |
| | | logger_local_huaxin_trade_debug.info( |
| | | f"---------------------\n请求下单:client_id-{client_id} request_id-{request_id}") |
| | | # 下单 |
| | | # 1-买 2-卖 |
| | | direction = data["direction"] |
| | |
| | | sinfo = data["sinfo"] |
| | | if direction == 1: |
| | | if not orderSysID and localOrderID: |
| | | orderSysID = local_order_id_map.get(localOrderID) |
| | | orderSysID = OrderIDManager.get_system_id_by_local_id(localOrderID) |
| | | # 撤买 |
| | | try: |
| | | if not orderSysID: |
| | | if localOrderID: |
| | | OrderIDManager.add_need_cancel_local_order_id(localOrderID) |
| | | raise Exception("没有找到系统订单号") |
| | | req_rid_dict[sinfo] = (client_id, request_id) |
| | | self.__tradeSimpleApi.cancel_buy(code, orderSysID, sinfo) |
| | |
| | | strategy_pipe.send(data) |
| | | |
| | | |
| | | local_order_id_map = {} |
| | | # 订单号管理 |
| | | class OrderIDManager: |
| | | # 尚未撤单的本地订单号 |
| | | not_canceled_local_order_ids = set() |
| | | local_order_id_map = {} |
| | | |
| | | __TradeSimpleApi = TradeSimpleApi() |
| | | |
| | | # 设置系统订单ID |
| | | def set_system_order_id(sinfo, orderSystemId): |
| | | # 获取本地订单ID |
| | | local_order_id = None |
| | | if req_rid_dict and sinfo in req_rid_dict: |
| | | temp_params = req_rid_dict.get(sinfo) |
| | | if len(temp_params) > 2: |
| | | local_order_id = temp_params[2] |
| | | if local_order_id and local_order_id not in local_order_id_map and orderSystemId: |
| | | local_order_id_map[local_order_id] = orderSystemId |
| | | @classmethod |
| | | def get_system_id_by_local_id(cls, local_order_id): |
| | | return cls.local_order_id_map.get(local_order_id) |
| | | |
| | | # 设置系统订单ID |
| | | @classmethod |
| | | def set_system_order_id(cls, code, sinfo, orderSystemId): |
| | | # 获取本地订单ID |
| | | local_order_id = None |
| | | if req_rid_dict and sinfo in req_rid_dict: |
| | | temp_params = req_rid_dict.get(sinfo) |
| | | if len(temp_params) > 2: |
| | | local_order_id = temp_params[2] |
| | | if local_order_id: |
| | | if local_order_id not in cls.local_order_id_map and orderSystemId: |
| | | cls.local_order_id_map[local_order_id] = orderSystemId |
| | | if local_order_id in cls.not_canceled_local_order_ids: |
| | | # 执行撤单 |
| | | cls.not_canceled_local_order_ids.discard(local_order_id) |
| | | cls.__TradeSimpleApi.cancel_buy(code, orderSystemId, |
| | | f"local_cancel_buy-{code}-{round(time.time() * 1000)}") |
| | | |
| | | @classmethod |
| | | def add_need_cancel_local_order_id(cls, local_order_id): |
| | | cls.not_canceled_local_order_ids.add(local_order_id) |
| | | |
| | | |
| | | # 交易反馈回调 |