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
예제 #2
0
    def onGetAccountListRsp(rpcGetAccountListRsp):
        reqId = rpcGetAccountListRsp.commonRsp.reqId
        if reqId in RpcClientRspHandler.waitReqIdSet:
            RpcClientRspHandler.rpcGetAccountListRspDict[
                reqId] = rpcGetAccountListRsp
        else:
            logger.info("直接丢弃的回报,请求ID:%s", reqId)

        if rpcGetAccountListRsp.account:
            for tmpAccount in rpcGetAccountListRsp.account:
                ClientTradeCacheService.storeAccount(tmpAccount)
예제 #3
0
    def onGetTickListRsp(rpcGetTickListRsp):
        reqId = rpcGetTickListRsp.commonRsp.reqId
        if reqId in RpcClientRspHandler.waitReqIdSet:
            RpcClientRspHandler.rpcGetTickListRspDict[
                reqId] = rpcGetTickListRsp
        else:
            logger.info("直接丢弃的回报,请求ID:%s", reqId)

        if rpcGetTickListRsp.tick:
            for tmpTick in rpcGetTickListRsp.tick:
                ClientTradeCacheService.storeTick(tmpTick)
예제 #4
0
    def onGetOrderListRsp(rpcGetOrderListRsp):
        reqId = rpcGetOrderListRsp.commonRsp.reqId
        if reqId in RpcClientRspHandler.waitReqIdSet:
            RpcClientRspHandler.rpcGetOrderListRspDict[
                reqId] = rpcGetOrderListRsp
        else:
            logger.info("直接丢弃的回报,请求ID:%s", reqId)

        if rpcGetOrderListRsp.order:
            for tmpOrder in rpcGetOrderListRsp.order:
                ClientTradeCacheService.storeOrder(tmpOrder)
예제 #5
0
    def onGetPositionListRsp(rpcGetPositionListRsp):
        reqId = rpcGetPositionListRsp.commonRsp.reqId
        if reqId in RpcClientRspHandler.waitReqIdSet:
            RpcClientRspHandler.rpcGetPositionListRspDict[
                reqId] = rpcGetPositionListRsp
        else:
            logger.info("直接丢弃的回报,请求ID:%s", reqId)

        if rpcGetPositionListRsp.position:
            for tmpPosition in rpcGetPositionListRsp.position:
                ClientTradeCacheService.storePosition(tmpPosition)
예제 #6
0
    def onGetOrderListRsp(rpcGetOrderListRsp):
        reqId = rpcGetOrderListRsp.commonRsp.reqId

        RpcClientRspHandler.__waitReqIdSetLock.acquire()
        RpcClientRspHandler.__rpcGetOrderListRspDictLock.acquire()
        try:
            if reqId in RpcClientRspHandler.__waitReqIdSet:
                RpcClientRspHandler.__rpcGetOrderListRspDict[
                    reqId] = rpcGetOrderListRsp
            else:
                logger.info("直接丢弃的回报,请求ID:%s", reqId)
        finally:
            RpcClientRspHandler.__waitReqIdSetLock.release()
            RpcClientRspHandler.__rpcGetOrderListRspDictLock.release()

        if rpcGetOrderListRsp.order:
            ClientTradeCacheService.storeOrderList(rpcGetOrderListRsp.order)
 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 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 onRpcOrderListRtn(rpcOrderListRtn):
     for order in rpcOrderListRtn.order:
         ClientTradeCacheService.storeOrder(order)
         StrategyEngine.onOrder(order)
 def onRpcAccountListRtn(rpcAccountListRtn):
     for account in rpcAccountListRtn.account:
         ClientTradeCacheService.storeAccount(account)
 def onRpcTickRtn(rpcTickRtn):
     ClientTradeCacheService.storeTick(rpcTickRtn.tick)
     StrategyEngine.onTick(rpcTickRtn.tick)
 def onRpcOrderRtn(rpcOrderRtn):
     ClientTradeCacheService.storeOrder(rpcOrderRtn.order)
     StrategyEngine.onOrder(rpcOrderRtn.order)
 def onRpcAccountRtn(rpcAccountRtn):
     ClientTradeCacheService.storeAccount(rpcAccountRtn.account)
 def onRpcContractRtn(rpcContractRtn):
     ClientTradeCacheService.storeContract(rpcContractRtn.contract)
 def onRpcPositionRtn(rpcPositionRtn):
     ClientTradeCacheService.storePosition(rpcPositionRtn.position)
 def onRpcTradeRtn(rpcTradeRtn):
     ClientTradeCacheService.storeTrade(rpcTradeRtn.trade)
     StrategyEngine.onTrade(rpcTradeRtn.trade)
 def onRpcTradeListRtn(rpcTradeListRtn):
     for trade in rpcTradeListRtn.trade:
         ClientTradeCacheService.storeTrade(trade)
         StrategyEngine.onTrade(trade)
 def onRpcContractListRtn(rpcContractListRtn):
     for contract in rpcContractListRtn.contract:
         ClientTradeCacheService.storeContract(contract)
 def onRpcPositionListRtn(rpcPositionListRtn):
     for position in rpcPositionListRtn.position:
         ClientTradeCacheService.storePosition(position)
 def onRpcTickListRtn(rpcTickListRtn):
     for tick in rpcTickListRtn.tick:
         ClientTradeCacheService.storeTick(tick)
 def onRpcTickListRtn(rpcTickListRtn):
     ClientTradeCacheService.storeTickList(rpcTickListRtn.tick)