def cancelOrder(self,
                 orderId=None,
                 originOrderId=None,
                 reqId=None,
                 sync=False):
     RpcClientApiService.cancelOrder(orderId=orderId,
                                     originOrderId=originOrderId,
                                     reqId=reqId,
                                     sync=sync)
 def unsubscribe(self, unifiedSymbol, gatewayId=None):
     if unifiedSymbol in ClientTradeCacheService.mixContractDict:
         self.subscribedUnifiedSymbolSet.add(unifiedSymbol)
         RpcClientApiService.unsubscribe(
             ClientTradeCacheService.mixContractDict[unifiedSymbol],
             gatewayId,
             sync=True)
     else:
         logger.error("策略%s退订行情错误,未能找到合约%s", self.strategyId, unifiedSymbol)
 def unsubscribe(self, unifiedSymbol, gatewayId=None):
     logger.info("策略%s取消订阅合约%s", self.strategyId, unifiedSymbol)
     contract = ClientTradeCacheService.getContractByUnifiedSymbol(
         unifiedSymbol)
     if contract is not None:
         if unifiedSymbol in self.subscribedUnifiedSymbolSet:
             self.subscribedUnifiedSymbolSet.remove(unifiedSymbol)
         RpcClientApiService.unsubscribe(contract, gatewayId, sync=True)
     else:
         logger.error("策略%s退订行情错误,未能找到合约%s", self.strategyId, unifiedSymbol)
    def submitOrder(self,
                    unifiedSymbol,
                    orderPriceType,
                    direction,
                    offsetFlag,
                    accountId,
                    price,
                    volume,
                    originOrderId=None,
                    sync=True):
        if self.initSwitch and self.tradingSwitch:
            submitOrderReq = SubmitOrderReqField()

            submitOrderReq.contract.CopyFrom(
                ClientTradeCacheService.getContractByUnifiedSymbol(
                    unifiedSymbol))
            submitOrderReq.direction = direction
            submitOrderReq.offsetFlag = offsetFlag
            submitOrderReq.orderPriceType = orderPriceType
            submitOrderReq.timeCondition = TimeConditionEnum.TC_GFD
            submitOrderReq.price = price
            submitOrderReq.minVolume = 1
            submitOrderReq.stopPrice = 0.0
            submitOrderReq.volumeCondition = VolumeConditionEnum.VC_AV
            submitOrderReq.contingentCondition = ContingentConditionEnum.CC_Immediately
            submitOrderReq.hedgeFlag = HedgeFlagEnum.HF_Speculation
            submitOrderReq.forceCloseReason = ForceCloseReasonEnum.FCR_NotForceClose
            submitOrderReq.volume = volume

            account = ClientTradeCacheService.getAccountByAccountId(accountId)

            submitOrderReq.gatewayId = account.gatewayId
            submitOrderReq.accountCode = account.code
            submitOrderReq.currency = account.currency

            if originOrderId is None:
                submitOrderReq.originOrderId = str(uuid.uuid4())
            else:
                submitOrderReq.originOrderId = originOrderId

            self.originOrderIdSet.add(submitOrderReq.originOrderId)

            logger.info("策略%s提交定单 %s", MessageToJson(submitOrderReq),
                        self.strategyId)
            if sync:
                orderId = RpcClientApiService.submitOrder(submitOrderReq,
                                                          sync=True)
                return orderId
            else:
                RpcClientApiService.submitOrder(submitOrderReq, sync=False)
                return None
        else:
            logger.error("策略尚未初始化或未处于交易状态")
            return None
 def subscribe(self, unifiedSymbol, gatewayId=None):
     if unifiedSymbol in ClientTradeCacheService.mixContractDict:
         self.subscribedUnifiedSymbolSet.add(unifiedSymbol)
         contract = ClientTradeCacheService.mixContractDict[unifiedSymbol]
         if gatewayId is not None:
             contract.gatewayId = gatewayId
         else:
             contract.gatewayId = ""
         RpcClientApiService.subscribe(
             ClientTradeCacheService.mixContractDict[unifiedSymbol],
             None,
             sync=True)
     else:
         logger.error("策略%s订阅行情错误,未能找到合约%s", self.strategyId, unifiedSymbol)
    def subscribe(self, unifiedSymbol, gatewayId=None):

        logger.info("策略%s订阅合约%s", self.strategyId, unifiedSymbol)
        contract = ClientTradeCacheService.getContractByUnifiedSymbol(
            unifiedSymbol)
        if contract is not None:
            self.subscribedUnifiedSymbolSet.add(unifiedSymbol)

            if gatewayId is not None:
                contract.gatewayId = gatewayId
            else:
                contract.gatewayId = ""
            RpcClientApiService.subscribe(contract, None, sync=True)
        else:
            logger.error("策略%s订阅行情错误,未能找到合约%s", self.strategyId, unifiedSymbol)
    def submitOrder(self,
                    unifiedSymbol,
                    priceType,
                    direction,
                    offset,
                    accountId,
                    price,
                    volume,
                    originOrderId=None):
        if self.initSwitch and self.tradingSwitch:
            submitOrderReq = SubmitOrderReqField()
            submitOrderReq.contract.CopyFrom(
                ClientTradeCacheService.mixContractDict[unifiedSymbol])
            submitOrderReq.price = price
            submitOrderReq.priceType = priceType
            submitOrderReq.volume = volume
            submitOrderReq.direction = direction
            submitOrderReq.offset = offset
            account = ClientTradeCacheService.accountDict[accountId]

            submitOrderReq.gatewayId = account.gateway.gatewayId
            submitOrderReq.accountCode = account.code
            submitOrderReq.currency = account.currency

            if originOrderId is None:
                submitOrderReq.originOrderId = str(uuid.uuid4())
            else:
                submitOrderReq.originOrderId = originOrderId

            self.originOrderIdSet.add(submitOrderReq.originOrderId)

            logger.warning("策略%s提交定单 \n %s", submitOrderReq, self.strategyId)
            orderId = RpcClientApiService.submitOrder(submitOrderReq,
                                                      sync=True)
            return orderId
        else:
            logger.error("策略尚未初始化或未处于交易状态")
            return None
 def onWsConnected():
     from xyz.redtorch.client.service.rpc.RpcClientApiService import RpcClientApiService
     for contract in RpcClientApiService.subscribedContractDict.values():
         RpcClientApiService.subscribe(contract)
        time.sleep(5)

    startDatetime = '2020-06-15 00:00:00.000000'
    startTimestamp = int(
        time.mktime(time.strptime(startDatetime, "%Y-%m-%d %H:%M:%S.%f")) *
        1000)

    endDatetime = '2020-06-23 00:00:00.000000'
    endTimestamp = int(
        time.mktime(time.strptime(endDatetime, "%Y-%m-%d %H:%M:%S.%f")) * 1000)
    print(startDatetime)
    print(endTimestamp)
    barList = RpcClientApiService.queryDBBarList(startTimestamp,
                                                 endTimestamp,
                                                 "IC2009@CFFEX@FUTURES",
                                                 BarCycleEnum.B_1Min,
                                                 MarketDataDBTypeEnum.MDDT_MIX,
                                                 reqId=None,
                                                 timeoutSeconds=None)

    print(len(barList))

    demoStrategyId = "TEST-STRATEGY-ID-000"
    strategyDemo = StrategyDemo({"strategyId": demoStrategyId})

    StrategyEngine.addStrategy(strategyDemo)
    strategyDemo.initStrategy()

    strategyDemo.startTrading()

    while True:
Exemple #10
0
if __name__ == "__main__":
    loginResult = HttpClient.login(Config.username, Config.password,
                                   Config.baseUrl)
    if loginResult:
        operatorId = loginResult['resultVo']['voData']['operatorId']
        nodeId = loginResult['resultVo']['voData']['recentlyNodeId']
        logging.warning("登录成功,操作者ID:%s,节点ID:%s", operatorId, nodeId)
        Config.operatorId = operatorId
        Config.nodeId = nodeId
        WebSocketClientHandler.connect(loginResult['cookie'])

        time.sleep(2)

        logging.info("获取账户列表")
        RpcClientApiService.getAccountList(sync=True)
        logging.info("获取持仓列表")
        RpcClientApiService.getPositionList(sync=True)
        logging.info("获取混合合约列表")
        RpcClientApiService.getMixContractList(sync=True)
        logging.info("获取委托列表")
        RpcClientApiService.getOrderList(sync=True)
        logging.info("获取成交列表")
        RpcClientApiService.getTradeList(sync=True)
        logging.info("获取Tick列表")
        RpcClientApiService.getTickList(sync=True)

        demoStrategyId = "TEST-STRATEGY-ID-000"
        strategyDemo = StrategyDemo(demoStrategyId)

        StrategyEngine.addStrategy(strategyDemo)
    def autoConnectAndUpdateTask(interval):
        try:

            from xyz.redtorch.client.web.socket.WebSocketClientHandler import WebSocketClientHandler
            if not WebSocketClientHandler.connected and not WebSocketClientHandler.connecting:
                loginResult = HttpClient.login(RtConfig.username,
                                               RtConfig.password,
                                               RtConfig.baseUrl)
                if loginResult:
                    operatorId = loginResult['resultVo']['voData'][
                        'operatorId']
                    nodeId = loginResult['resultVo']['voData'][
                        'recentlyNodeId']
                    logging.warning("登录成功,操作者ID:%s,节点ID:%s", operatorId,
                                    nodeId)
                    RtConfig.operatorId = operatorId
                    RtConfig.nodeId = nodeId
                    WebSocketClientHandler.connect(loginResult['cookie'])

                    time.sleep(2)

                    from xyz.redtorch.client.service.rpc.RpcClientApiService import RpcClientApiService

                    logging.info("获取账户列表")
                    RpcClientApiService.getAccountList(sync=True)
                    logging.info("获取持仓列表")
                    RpcClientApiService.getPositionList(sync=True)
                    logging.info("获取混合合约列表")
                    RpcClientApiService.getMixContractList(sync=True)
                    logging.info("获取委托列表")
                    RpcClientApiService.getOrderList(sync=True)
                    logging.info("获取成交列表")
                    RpcClientApiService.getTradeList(sync=True)
                    logging.info("获取Tick列表")
                    RpcClientApiService.getTickList(sync=True)
        except:
            logging.error("定时更新最新净值发生错误", exc_info=True)
        t = Timer(interval, RtConfig.autoConnectAndUpdateTask, (interval, ))
        t.start()
    # Config.baseUrl = "http://" +  Config.host + ":" + str(Config.port)

    loginResult = HttpClient.login(Config.username, Config.password,
                                   Config.baseUrl)
    if loginResult:
        operatorId = loginResult['resultVo']['voData']['operatorId']
        nodeId = loginResult['resultVo']['voData']['recentlyNodeId']
        logging.warning("登录成功,操作者ID:%s,节点ID:%s", operatorId, nodeId)
        Config.operatorId = operatorId
        Config.nodeId = nodeId
        WebSocketClientHandler.connect(loginResult['cookie'])

        time.sleep(2)

        logging.info("获取账户列表")
        RpcClientApiService.getAccountList(sync=True)
        logging.info("获取持仓列表")
        RpcClientApiService.getPositionList(sync=True)
        logging.info("获取混合合约列表")
        RpcClientApiService.getMixContractList(sync=True)
        logging.info("获取委托列表")
        RpcClientApiService.getOrderList(sync=True)
        logging.info("获取成交列表")
        RpcClientApiService.getTradeList(sync=True)
        logging.info("获取Tick列表")
        RpcClientApiService.getTickList(sync=True)

        startDatetime = '2020-06-15 00:00:00.000000'
        startTimestamp = int(
            time.mktime(time.strptime(startDatetime, "%Y-%m-%d %H:%M:%S.%f")) *
            1000)