def cancelOrder(orderId=None, originOrderId=None, reqId=None, sync=False): if not reqId: reqId = str(uuid.uuid4()) operatorId = Config.operatorId sourceNodeId = Config.nodeId commonReq = CommonReqField() commonReq.sourceNodeId = sourceNodeId commonReq.targetNodeId = 0 commonReq.operatorId = operatorId commonReq.reqId = reqId cancelOrderReq = CancelOrderReqField() if not orderId and not originOrderId: logger.error("定单ID和原始定单ID不可同时为空") return False if originOrderId: cancelOrderReq.originOrderId = originOrderId if orderId: cancelOrderReq.orderId = orderId rpcCancelOrderReq = RpcCancelOrderReq() rpcCancelOrderReq.commonReq.CopyFrom(commonReq) rpcCancelOrderReq.cancelOrderReq.CopyFrom(cancelOrderReq) if sync: RpcClientRspHandler.registerWaitReqId(reqId) sendResult = RpcClientProcessService.sendCoreRpc(0, rpcCancelOrderReq.SerializeToString(), reqId, RpcId.CANCEL_ORDER_REQ) if sync and not sendResult: RpcClientRspHandler.unregisterWaitReqId(reqId) return False if sync: startTime = time.time() while True: if time.time() - startTime < Config.rpcTimeOut: rpcCancelOrderRsp = RpcClientRspHandler.getAndRemoveRpcCancelOrderRsp(reqId) if not rpcCancelOrderRsp: rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(reqId); if rpcExceptionRsp: logger.error("撤销定单错误,请求ID: %s, 远程错误回报 %s", reqId, rpcExceptionRsp.info) return False time.sleep(0.02) else: commonRsp = rpcCancelOrderRsp.commonRsp errorId = commonRsp.errorId if errorId == 0: return True else: logger.error("撤销定单错误,请求ID:%s,错误ID:%s,远程错误回报:%s", reqId, errorId, commonRsp.errorMsg) return False else: RpcClientRspHandler.unregisterWaitReqId(reqId) logger.error("撤销定单错误,请求ID: %s,等待回报超时", reqId) return False
def subscribe(contract, reqId=None, sync=False): if not reqId: reqId = str(uuid.uuid4()) operatorId = RtConfig.operatorId sourceNodeId = RtConfig.nodeId commonReq = CommonReqField() commonReq.sourceNodeId = sourceNodeId commonReq.targetNodeId = 0 commonReq.operatorId = operatorId commonReq.reqId = reqId rpcSubscribeReq = RpcSubscribeReq() rpcSubscribeReq.commonReq.CopyFrom(commonReq) rpcSubscribeReq.contract.CopyFrom(contract) if sync: RpcClientRspHandler.registerWaitReqId(reqId) sendResult = RpcClientProcessService.sendCoreRpc( 0, rpcSubscribeReq.SerializeToString(), reqId, RpcId.SUBSCRIBE_REQ) if sync and not sendResult: RpcClientRspHandler.unregisterWaitReqId(reqId) return False if sync: startTime = time.time() while True: if time.time() - startTime < RtConfig.rpcTimeOut: rpcSubscribeRsp = RpcClientRspHandler.getAndRemoveRpcSubscribeRsp( reqId) if not rpcSubscribeRsp: rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp( reqId) if rpcExceptionRsp: logger.error("订阅错误,请求ID: %s, 远程错误回报 %s", reqId, rpcExceptionRsp.info) return False time.sleep(0.02) else: commonRsp = rpcSubscribeRsp.commonRsp errorId = commonRsp.errorId if errorId == 0: RpcClientApiService.subscribedContractDict[ contract.unifiedSymbol + "@" + str(contract.gatewayId)] = contract return True else: logger.error("订阅错误,请求ID:%s,错误ID:%s,远程错误回报:%s", reqId, errorId, commonRsp.errorMsg) return False else: RpcClientRspHandler.unregisterWaitReqId(reqId) logger.error("订阅错误,请求ID: %s,等待回报超时", reqId) return False
def queryDBBarList(startTimestamp, endTimestamp, unifiedSymbol, barCycle, marketDataDBType, reqId=None, timeoutSeconds=None): if not reqId: reqId = str(uuid.uuid4()) if not timeoutSeconds: timeoutSeconds = Config.rpcTimeOut operatorId = Config.operatorId sourceNodeId = Config.nodeId commonReq = CommonReqField() commonReq.sourceNodeId = sourceNodeId commonReq.targetNodeId = 0 commonReq.operatorId = operatorId commonReq.reqId = reqId rpcQueryDBBarListReq = RpcQueryDBBarListReq() rpcQueryDBBarListReq.commonReq.CopyFrom(commonReq) rpcQueryDBBarListReq.startTimestamp = startTimestamp rpcQueryDBBarListReq.endTimestamp = endTimestamp rpcQueryDBBarListReq.unifiedSymbol = unifiedSymbol rpcQueryDBBarListReq.barCycle = barCycle rpcQueryDBBarListReq.marketDataDBType = marketDataDBType RpcClientRspHandler.registerWaitReqId(reqId) sendResult = RpcClientProcessService.sendCoreRpc(0, rpcQueryDBBarListReq.SerializeToString(), reqId, RpcId.QUERY_DB_BAR_LIST_REQ) if not sendResult: RpcClientRspHandler.unregisterWaitReqId(reqId) return None startTime = time.time() while True: if time.time() - startTime < timeoutSeconds: rpcQueryDBBarListRsp = RpcClientRspHandler.getAndRemoveRpcQueryDBBarListRsp(reqId) if not rpcQueryDBBarListRsp: rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(reqId) if rpcExceptionRsp: logger.error("获取Bar列表错误,请求ID: %s, 远程错误回报 %s", reqId, rpcExceptionRsp.info) return None time.sleep(0.02) else: commonRsp = rpcQueryDBBarListRsp.commonRsp errorId = commonRsp.errorId if errorId == 0: return rpcQueryDBBarListRsp.bar else: logger.error("获取Bar列表错误,请求ID:%s,错误ID:%s,远程错误回报:%s", reqId, errorId, commonRsp.errorMsg) return None else: RpcClientRspHandler.unregisterWaitReqId(reqId) logger.error("获取Bar列表错误,请求ID: %s,等待回报超时", reqId) return None
def getTickList(reqId=None, sync=False): if not reqId: reqId = str(uuid.uuid4()) operatorId = RtConfig.operatorId sourceNodeId = RtConfig.nodeId commonReq = CommonReqField() commonReq.sourceNodeId = sourceNodeId commonReq.targetNodeId = 0 commonReq.operatorId = operatorId commonReq.reqId = reqId rpcGetTickListReq = RpcGetTickListReq() rpcGetTickListReq.commonReq.CopyFrom(commonReq) if sync: RpcClientRspHandler.registerWaitReqId(reqId) sendResult = RpcClientProcessService.sendCoreRpc( 0, rpcGetTickListReq.SerializeToString(), reqId, RpcId.GET_TICK_LIST_REQ) if sync and not sendResult: RpcClientRspHandler.unregisterWaitReqId(reqId) return None if sync: startTime = time.time() while True: if time.time() - startTime < RtConfig.rpcTimeOut: rpcGetTickListRsp = RpcClientRspHandler.getAndRemoveRpcGetTickListRsp( reqId) if not rpcGetTickListRsp: rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp( reqId) if rpcExceptionRsp: logger.error("获取Tick列表错误,请求ID: %s, 远程错误回报 %s", reqId, rpcExceptionRsp.info) return None time.sleep(0.02) else: commonRsp = rpcGetTickListRsp.commonRsp errorId = commonRsp.errorId if errorId == 0: return rpcGetTickListRsp.tick else: logger.error( "获取Tick列表错误,请求ID:%s,错误ID:%s,远程错误回报:%s", reqId, errorId, commonRsp.errorMsg) return None else: RpcClientRspHandler.unregisterWaitReqId(reqId) logger.error("获取Tick列表错误,请求ID: %s,等待回报超时", reqId) return None
def searchContract(contract, reqId=None, sync=False): if not reqId: reqId = str(uuid.uuid4()) operatorId = Config.operatorId sourceNodeId = Config.nodeId commonReq = CommonReqField() commonReq.sourceNodeId = sourceNodeId commonReq.targetNodeId = 0 commonReq.operatorId = operatorId commonReq.reqId = reqId rpcSearchContractReq = RpcSearchContractReq() rpcSearchContractReq.commonReq.CopyFrom(commonReq) rpcSearchContractReq.contract.CopyFrom(contract) if sync: RpcClientRspHandler.registerWaitReqId(reqId) sendResult = RpcClientProcessService.sendCoreRpc(0, rpcSearchContractReq.SerializeToString(), reqId, RpcId.SEARCH_CONTRACT_REQ) if sync and not sendResult: RpcClientRspHandler.unregisterWaitReqId(reqId) return False if sync: startTime = time.time() while True: if time.time() - startTime < Config.rpcTimeOut: rpcSearchContractRsp = RpcClientRspHandler.getAndRemoveRpcSearchContractRsp(reqId) if not rpcSearchContractRsp: rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(reqId); if rpcExceptionRsp: logger.error("搜寻合约错误,请求ID: %s, 远程错误回报 %s", reqId, rpcExceptionRsp.info) return False time.sleep(0.02) else: commonRsp = rpcSearchContractRsp.commonRsp errorId = commonRsp.errorId if errorId == 0: return True else: logger.error("搜寻合约错误,请求ID:%s,错误ID:%s,远程错误回报:%s", reqId, errorId, commonRsp.errorMsg) return False else: RpcClientRspHandler.unregisterWaitReqId(reqId) logger.error("搜寻合约错误,请求ID: %s,等待回报超时", reqId) return False