| | |
| | | def cancel_buy(self, code, order_sys_id, sinfo): |
| | | if sinfo in self.__cancel_buy_sinfo_set: |
| | | raise Exception(f'撤单请求已经提交:{sinfo}') |
| | | logger_local_huaxin_trade_debug.info(f"进入撤单方法:code-{code} order_sys_id-{order_sys_id} sinfo-{sinfo}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, |
| | | f"进入撤单方法:code-{code} order_sys_id-{order_sys_id} sinfo-{sinfo}") |
| | | self.__cancel_buy_sinfo_set.add(sinfo) |
| | | self.req_id += 1 |
| | | # 请求撤单 |
| | |
| | | pRspInfoField: "CTORATstpRspInfoField", nRequestID: "int") -> "void": |
| | | try: |
| | | if pRspInfoField.ErrorID == 0: |
| | | logger_local_huaxin_trade_debug.info('OnRspOrderAction: OK! [%d]' % nRequestID) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, 'OnRspOrderAction: OK! [%d]' % nRequestID) |
| | | threading.Thread(target=lambda: self.__data_callback(TYPE_CANCEL_ORDER, nRequestID, |
| | | {"sinfo": pInputOrderActionField.SInfo, |
| | | "orderSysID": pInputOrderActionField.OrderSysID, |
| | | "cancel": 1}), daemon=True).start() |
| | | |
| | | else: |
| | | logger_local_huaxin_trade_debug.info('OnRspOrderAction: Error! [%d] [%d] [%s]' |
| | | % (nRequestID, pRspInfoField.ErrorID, pRspInfoField.ErrorMsg)) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, 'OnRspOrderAction: Error! [%d] [%d] [%s]' |
| | | % (nRequestID, pRspInfoField.ErrorID, pRspInfoField.ErrorMsg)) |
| | | threading.Thread(target=lambda: self.__data_callback(TYPE_CANCEL_ORDER, nRequestID, |
| | | {"sinfo": pInputOrderActionField.SInfo, |
| | | "orderSysID": pInputOrderActionField.OrderSysID, |
| | |
| | | pRspInfoField: "CTORATstpRspInfoField", nRequestID: "int") -> "void": |
| | | try: |
| | | if pInputOrderActionField and pRspInfoField: |
| | | logger_local_huaxin_trade_debug.info('OnErrRtnOrderAction: Error! [%d] [%d] [%d] [%s]' |
| | | % (nRequestID, pInputOrderActionField.OrderSysID, |
| | | pRspInfoField.ErrorID, |
| | | pRspInfoField.ErrorMsg)) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, 'OnErrRtnOrderAction: Error! [%d] [%d] [%d] [%s]' |
| | | % (nRequestID, pInputOrderActionField.OrderSysID, |
| | | pRspInfoField.ErrorID, |
| | | pRspInfoField.ErrorMsg)) |
| | | except: |
| | | logger_local_huaxin_trade_debug.info("OnErrRtnOrderAction: 撤单出错") |
| | | |
| | |
| | | |
| | | def OnRtnOrder(self, pOrderField: "CTORATstpOrderField") -> "void": |
| | | try: |
| | | logger_local_huaxin_trade_debug.info( |
| | | '[%d] OnRtnOrder: SInfo[%s] InvestorID[%s] SecurityID[%s] OrderRef[%d] OrderLocalID[%s] LimitPrice[%.2f] VolumeTotalOriginal[%d] OrderSysID[%s] OrderStatus[%s] InsertTime[%s]' |
| | | % (round(time.time() * 1000), pOrderField.SInfo, pOrderField.InvestorID, pOrderField.SecurityID, |
| | | pOrderField.OrderRef, pOrderField.OrderLocalID, |
| | | pOrderField.LimitPrice, pOrderField.VolumeTotalOriginal, pOrderField.OrderSysID, |
| | | pOrderField.OrderStatus, pOrderField.InsertTime)) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, |
| | | '[%d] OnRtnOrder: SInfo[%s] InvestorID[%s] SecurityID[%s] OrderRef[%d] OrderLocalID[%s] LimitPrice[%.2f] VolumeTotalOriginal[%d] OrderSysID[%s] OrderStatus[%s] InsertTime[%s]' |
| | | % (round(time.time() * 1000), pOrderField.SInfo, pOrderField.InvestorID, |
| | | pOrderField.SecurityID, |
| | | pOrderField.OrderRef, pOrderField.OrderLocalID, |
| | | pOrderField.LimitPrice, pOrderField.VolumeTotalOriginal, pOrderField.OrderSysID, |
| | | pOrderField.OrderStatus, pOrderField.InsertTime)) |
| | | |
| | | OrderIDManager.set_system_order_id(pOrderField.SecurityID, pOrderField.SInfo, pOrderField.OrderSysID) |
| | | if pOrderField.OrderStatus != traderapi.TORA_TSTP_OST_Unknown: |
| | |
| | | |
| | | def OnRtnTrade(self, pTradeField: "CTORATstpTradeField") -> "void": |
| | | try: |
| | | logger_local_huaxin_trade_debug.info( |
| | | 'OnRtnTrade: TradeID[%s] InvestorID[%s] SecurityID[%s] OrderRef[%d] OrderLocalID[%s] Price[%.2f] Volume[%d]' |
| | | % (pTradeField.TradeID, pTradeField.InvestorID, pTradeField.SecurityID, |
| | | pTradeField.OrderRef, pTradeField.OrderLocalID, pTradeField.Price, pTradeField.Volume)) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, |
| | | 'OnRtnTrade: TradeID[%s] InvestorID[%s] SecurityID[%s] OrderRef[%d] OrderLocalID[%s] Price[%.2f] Volume[%d]' |
| | | % (pTradeField.TradeID, pTradeField.InvestorID, pTradeField.SecurityID, |
| | | pTradeField.OrderRef, pTradeField.OrderLocalID, pTradeField.Price, |
| | | pTradeField.Volume)) |
| | | except: |
| | | pass |
| | | |
| | |
| | | |
| | | def OnTrade(self, client_id, request_id, sk, type_, data): |
| | | if type_ == 1: |
| | | async_log_util.info(logger_local_huaxin_trade_debug,f"\n---------------------\n请求下单:client_id-{client_id} request_id-{request_id}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, |
| | | f"\n---------------------\n请求下单:client_id-{client_id} request_id-{request_id}") |
| | | # 下单 |
| | | # 1-买 2-卖 |
| | | direction = data["direction"] |
| | |
| | | request_id) |
| | | |
| | | elif type_ == 2: |
| | | async_log_util.info(logger_local_huaxin_trade_debug, f"\n---------------------\n请求撤单:client_id-{client_id} request_id-{request_id} data-{data}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, |
| | | f"\n---------------------\n请求撤单:client_id-{client_id} request_id-{request_id} data-{data}") |
| | | # 撤单 |
| | | direction = data["direction"] |
| | | code = data["code"] |
| | |
| | | request_id) |
| | | |
| | | def OnDealList(self, client_id, request_id, sk): |
| | | logger_local_huaxin_trade_debug.info(f"请求成交列表:client_id-{client_id} request_id-{request_id}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, f"请求成交列表:client_id-{client_id} request_id-{request_id}") |
| | | try: |
| | | print("开始请求成交列表") |
| | | req_id = self.__tradeSimpleApi.list_traded_orders() |
| | |
| | | SendResponseSkManager.send_error_response("common", request_id, client_id, str(e)) |
| | | |
| | | def OnDelegateList(self, client_id, request_id, sk, is_cancel): |
| | | logger_local_huaxin_trade_debug.info(f"请求委托列表:client_id-{client_id} request_id-{request_id}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, f"请求委托列表:client_id-{client_id} request_id-{request_id}") |
| | | try: |
| | | req_id = self.__tradeSimpleApi.list_delegate_orders(is_cancel) |
| | | req_rid_dict[req_id] = (client_id, request_id, sk) |
| | |
| | | request_id) |
| | | |
| | | def OnMoney(self, client_id, request_id, sk): |
| | | logger_local_huaxin_trade_debug.info(f"请求账户:client_id-{client_id} request_id-{request_id}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, f"请求账户:client_id-{client_id} request_id-{request_id}") |
| | | try: |
| | | req_id = self.__tradeSimpleApi.get_money_account() |
| | | req_rid_dict[req_id] = (client_id, request_id, sk) |
| | |
| | | request_id) |
| | | |
| | | def OnPositionList(self, client_id, request_id, sk): |
| | | logger_local_huaxin_trade_debug.info(f"请求持仓:client_id-{client_id} request_id-{request_id}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, f"请求持仓:client_id-{client_id} request_id-{request_id}") |
| | | try: |
| | | req_id = self.__tradeSimpleApi.list_positions() |
| | | req_rid_dict[req_id] = (client_id, request_id, sk) |
| | |
| | | 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 |
| | | async_log_util.info( logger_local_huaxin_trade_debug, f"本地订单号与系统订单号映射,{code}:{local_order_id} {orderSystemId}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, |
| | | f"本地订单号与系统订单号映射,{code}:{local_order_id} {orderSystemId}") |
| | | if local_order_id in cls.not_canceled_local_order_ids: |
| | | async_log_util.info(logger_local_huaxin_trade_debug,f"执行等待撤单,{code}:{local_order_id} {orderSystemId}") |
| | | async_log_util.info(logger_local_huaxin_trade_debug, f"执行等待撤单,{code}:{local_order_id} {orderSystemId}") |
| | | # 执行撤单 |
| | | cls.not_canceled_local_order_ids.discard(local_order_id) |
| | | for i in range(3): |
| | |
| | | |
| | | # 交易反馈回调 |
| | | def __traderapi_callback(type, req_id, data): |
| | | logger_local_huaxin_trade_debug.info("回调:type-{} req_id-{}", type, req_id) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, "回调:type-{} req_id-{}", type, req_id) |
| | | key = req_id |
| | | if type == TYPE_ORDER or type == TYPE_CANCEL_ORDER: |
| | | key = data["sinfo"] |
| | |
| | | local_order_id = temp_params[3] |
| | | data["localOrderId"] = local_order_id |
| | | |
| | | logger_local_huaxin_trade_debug.info("API回调 request_id-{}", request_id) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, "API回调 request_id-{}", request_id) |
| | | # 测试 |
| | | # send_response( |
| | | # json.dumps({"type": "response", "data": {"code": 0, "data": data}, "client_id": client_id, |
| | |
| | | {"type": "response", "data": {"code": 0, "data": data}, "client_id": client_id, |
| | | "request_id": request_id}) |
| | | |
| | | logger_local_huaxin_trade_debug.info("API回调结束 req_id-{} request_id-{}", req_id, request_id) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, "API回调结束 req_id-{} request_id-{}", req_id, request_id) |
| | | print("API回调结束") |
| | | else: |
| | | logger_local_huaxin_trade_debug.info("非API回调 req_id-{}", req_id) |
| | | async_log_util.info(logger_local_huaxin_trade_debug, "非API回调 req_id-{}", req_id) |
| | | print("非API回调") |
| | | trade_response.OnTradeCallback({"type": "trade_callback", "data": {"code": 0, "data": data, "type": type}}) |
| | | # # 非API回调 |