Beispiel #1
0
    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
Beispiel #3
0
    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
Beispiel #5
0
    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
    def unsubscribe(contract, gatewayId=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

        rpcUnsubscribeReq = RpcUnsubscribeReq()

        rpcUnsubscribeReq.commonReq.CopyFrom(commonReq)
        rpcUnsubscribeReq.contract.CopyFrom(contract)

        if gatewayId:
            rpcUnsubscribeReq.gatewayId = gatewayId

        if sync:
            RpcClientRspHandler.registerWaitReqId(reqId)

        sendResult = RpcClientProcessService.sendRoutineCoreRpc(
            0, rpcUnsubscribeReq.SerializeToString(), reqId,
            RpcId.UNSUBSCRIBE_REQ)

        if sync and not sendResult:
            RpcClientRspHandler.unregisterWaitReqId(reqId)
            return False

        if sync:
            startTime = time.time()
            while True:
                if time.time() - startTime < Config.rpcTimeOut:
                    rpcUnsubscribeRsp = RpcClientRspHandler.getAndRemoveRpcUnsubscribeRsp(
                        reqId)
                    if not rpcUnsubscribeRsp:
                        rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(
                            reqId)
                        if rpcExceptionRsp:
                            logger.error("取消订阅错误,请求ID: %s, 远程错误回报 %s", reqId,
                                         rpcExceptionRsp.info)
                            return False
                        time.sleep(0.02)
                    else:
                        commonRsp = rpcUnsubscribeRsp.commonRsp
                        requestStatus = commonRsp.requestStatus
                        if requestStatus == CommonStatusEnum.SUCCESS:
                            return True
                        elif requestStatus == CommonStatusEnum.INFO:
                            logger.info("取消订阅错误,请求ID:%s,远程信息回报:%s", reqId,
                                        commonRsp.info)
                            return True
                        elif requestStatus == CommonStatusEnum.WARN:
                            logger.warning("取消订阅错误,请求ID:%s,远程警告回报:%s", reqId,
                                           commonRsp.info)
                            return True
                        elif requestStatus == CommonStatusEnum.ERROR:
                            logger.error("取消订阅错误,请求ID:%s,远程错误回报:%s", reqId,
                                         commonRsp.info)
                            return False
                        else:
                            logger.error("取消订阅错误,请求ID:%s,未知的请求状态", reqId)
                            return False
                else:
                    RpcClientRspHandler.unregisterWaitReqId(reqId)
                    logger.error("取消订阅错误,请求ID: %s,等待回报超时", reqId)
                    return False
    def getPositionList(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

        rpcGetPositionListReq = RpcGetPositionListReq()

        rpcGetPositionListReq.commonReq.CopyFrom(commonReq)

        if sync:
            RpcClientRspHandler.registerWaitReqId(reqId)

        sendResult = RpcClientProcessService.sendRoutineCoreRpc(
            0, rpcGetPositionListReq.SerializeToString(), reqId,
            RpcId.GET_POSITION_LIST_REQ)

        if sync and not sendResult:
            RpcClientRspHandler.unregisterWaitReqId(reqId)
            return None

        if sync:
            startTime = time.time()
            while True:
                if time.time() - startTime < Config.rpcTimeOut:
                    rpcGetPositionListRsp = RpcClientRspHandler.getAndRemoveRpcGetPositionListRsp(
                        reqId)
                    if not rpcGetPositionListRsp:
                        rpcExceptionRsp = RpcClientRspHandler.getAndRemoveRpcExceptionRsp(
                            reqId)
                        if rpcExceptionRsp:
                            logger.error("获取持仓列表错误,请求ID: %s, 远程错误回报 %s", reqId,
                                         rpcExceptionRsp.info)
                            return None
                        time.sleep(0.02)
                    else:
                        commonRsp = rpcGetPositionListRsp.commonRsp
                        requestStatus = commonRsp.requestStatus
                        if requestStatus == CommonStatusEnum.SUCCESS:
                            return rpcGetPositionListRsp.position
                        elif requestStatus == CommonStatusEnum.INFO:
                            logger.info("获取持仓列表错误,请求ID:%s,远程信息回报:%s", reqId,
                                        commonRsp.info)
                            return rpcGetPositionListRsp.position
                        elif requestStatus == CommonStatusEnum.WARN:
                            logger.warning("获取持仓列表错误,请求ID:%s,远程警告回报:%s", reqId,
                                           commonRsp.info)
                            return rpcGetPositionListRsp.position
                        elif requestStatus == CommonStatusEnum.ERROR:
                            logger.error("获取持仓列表错误,请求ID:%s,远程错误回报:%s", reqId,
                                         commonRsp.info)
                            return None
                        else:
                            logger.error("获取持仓列表错误,请求ID:%s,未知的请求状态", reqId)
                            return None
                else:
                    RpcClientRspHandler.unregisterWaitReqId(reqId)
                    logger.error("获取持仓列表错误,请求ID: %s,等待回报超时", reqId)
                    return None