Beispiel #1
0
    def subscribe(self, strategy, symbolList):
        name = strategy.name
        for vtSymbol in symbolList:
            # 保存Tick映射关系
            if vtSymbol in self.tickStrategyDict:
                l = self.tickStrategyDict[vtSymbol]
            else:
                l = []
                self.tickStrategyDict[vtSymbol] = l
            l.append(strategy)

            # 订阅合约
            contract = self.mainEngine.getContract(vtSymbol)
            if contract:
                req = VtSubscribeReq()
                req.symbol = contract.symbol
                req.exchange = contract.exchange

                # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
                req.currency = strategy.currency
                req.productClass = strategy.productClass

                self.mainEngine.subscribe(req, contract.gatewayName)
            else:
                self.writeArbLog(u'%s的交易合约%s无法找到' % (name, vtSymbol))

            self.putStrategyEvent(name)
Beispiel #2
0
    def initStrategyBySymbol(self, symbol, setting):

        # 创建策略实例
        # 防止策略重名
        if symbol in self.strategyDict:
            self.writeCtaLog(u'策略实例重名:%s' % name)
            return

        setting["vtSymbol"] = symbol
        strategy = ZzsdStrategy(self, setting)

        self.strategyDict[symbol] = strategy
        strategy.setFixSize(self.FIX_SIZE_AUDO)
        strategy.initDayBar(self.loadAllBar(DAY_DB_NAME, symbol))
        strategy.initDayCurrentBar(
            self.getCurrentBar(self.loadCurrentDayMinBar(symbol)))

        if setting["strategyCycle"] == "60min":
            strategy.initHourBar(self.loadAllBar(MINUTE_60_DB_NAME, symbol))
            strategy.initHourCurrentBar(
                self.getCurrentBar(self.loadMinuteBar(symbol, 60)))
        if setting["strategyCycle"] == "30min":
            strategy.initHourBar(self.loadAllBar(MINUTE_30_DB_NAME, symbol))
            strategy.initHourCurrentBar(
                self.getCurrentBar(self.loadMinuteBar(symbol, 30)))
        if setting["strategyCycle"] == "5min":
            strategy.initHourBar(self.loadAllBar(MINUTE_5_DB_NAME, symbol))
            strategy.initHourCurrentBar(
                self.getCurrentBar(self.loadMinuteBar(symbol, 5)))

        # 创建委托号列表
        self.strategyOrderDict[symbol] = set()

        # 保存Tick映射关系
        if strategy.vtSymbol in self.tickStrategyDict:
            l = self.tickStrategyDict[strategy.vtSymbol]
        else:
            l = []
            self.tickStrategyDict[strategy.vtSymbol] = l
        l.append(strategy)

        # 订阅合约
        contract = self.mainEngine.getContract(strategy.vtSymbol)
        if contract:
            req = VtSubscribeReq()
            req.symbol = contract.symbol
            req.exchange = contract.exchange

            # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
            req.currency = strategy.currency
            req.productClass = strategy.productClass

            self.mainEngine.subscribe(req, contract.gatewayName)
        else:
            self.writeCtaLog(u'%s的交易合约%s无法找到' % (name, strategy.vtSymbol))
Beispiel #3
0
 def loadStrategy(self, setting):
     """载入策略"""
     try:
         name = setting['name']
         className = setting['className']
     except Exception:
         msg = traceback.format_exc()
         self.writeCtaLog(u'载入策略出错:%s' %msg)
         return
     
     # 获取策略类
     strategyClass = STRATEGY_CLASS.get(className, None)
     if not strategyClass:
         self.writeCtaLog(u'找不到策略类:%s' %className)
         return
     
     # 防止策略重名
     if name in self.strategyDict:
         self.writeCtaLog(u'策略实例重名:%s' %name)
     else:
         # 创建策略实例
         strategy = strategyClass(self, setting)  
         self.strategyDict[name] = strategy
         
         # 创建委托号列表
         self.strategyOrderDict[name] = set()
         
         # 保存Tick映射关系
         if strategy.vtSymbol in self.tickStrategyDict:
             l = self.tickStrategyDict[strategy.vtSymbol]
         else:
             l = []
             self.tickStrategyDict[strategy.vtSymbol] = l
         l.append(strategy)
         
         # 订阅合约
         contract = self.mainEngine.getContract(strategy.vtSymbol)
         if contract:
             req = VtSubscribeReq()
             req.symbol = contract.symbol
             req.exchange = contract.exchange
             
             # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
             req.currency = strategy.currency
             req.productClass = strategy.productClass
             
             self.mainEngine.subscribe(req, contract.gatewayName)
         else:
             self.writeCtaLog(u'%s的交易合约%s无法找到' %(name, strategy.vtSymbol))
Beispiel #4
0
 def subscribeMarketData(self, strategy):
     """订阅行情"""
     # 订阅合约
     contract = self.mainEngine.getContract(strategy.vtSymbol)
     if contract:
         req = VtSubscribeReq()
         req.symbol = contract.symbol
         req.exchange = contract.exchange
         
         # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
         req.currency = strategy.currency
         req.productClass = strategy.productClass
         
         self.mainEngine.subscribe(req, contract.gatewayName)
     else:
         self.writeCtaLog(u'%s的交易合约%s无法找到' %(strategy.name, strategy.vtSymbol))
Beispiel #5
0
 def subscribeMarketData(self, strategy):
     """订阅行情"""
     # 订阅合约
     contract = self.mainEngine.getContract(strategy.vtSymbol)
     if contract:
         req = VtSubscribeReq()
         req.symbol = contract.symbol
         req.exchange = contract.exchange
         
         # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
         req.currency = strategy.currency
         req.productClass = strategy.productClass
         
         self.mainEngine.subscribe(req, contract.gatewayName)
     else:
         self.writeCtaLog(u'%s的交易合约%s无法找到' %(strategy.name, strategy.vtSymbol))
Beispiel #6
0
    def subscribe(self, strategy, symbol):
        """订阅合约,不成功时,加入到待订阅列表"""
        contract = self.mainEngine.getContract(symbol)

        if contract:
            # 4.构造订阅请求包
            req = VtSubscribeReq()
            req.symbol = contract.symbol
            req.exchange = contract.exchange

            # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
            req.currency = strategy.currency
            req.productClass = strategy.productClass

            # 5.调用主引擎的订阅接口
            self.mainEngine.subscribe(req, contract.gatewayName)
        else:
            print u'Warning, can not find {0} in contracts'.format(symbol)
            self.writeCtaLog(u'交易合约{}无法找到,添加到待订阅列表'.format(symbol))
            self.pendingSubcribeSymbols[symbol] = strategy
Beispiel #7
0
    def loadStrategy(self, setting):
        """载入策略"""
        try:
            name = setting['name']
            className = setting['className']
        except Exception as e:
            self.writeCtaLog(u'载入策略出错:%s' %e)
            return

        # 获取策略类
        strategyClass = STRATEGY_CLASS.get(className, None)
        if not strategyClass:
            self.writeCtaLog(u'找不到策略类:%s' %className)
            return

        # 防止策略重名
        if name in self.strategyDict:
            self.writeCtaLog(u'策略实例重名:%s' %name)

        else:
            # 创建策略实例
            strategy = strategyClass(self, setting)
            self.strategyDict[name] = strategy

            # 创建委托号列表
            self.strategyOrderDict[name] = set()

            # 保存Tick映射关系
            if strategy.vtSymbol in self.tickStrategyDict:
                l = self.tickStrategyDict[strategy.vtSymbol]
            else:
                l = []
                self.tickStrategyDict[strategy.vtSymbol] = l
            l.append(strategy)

            # 订阅合约
            # print(strategy.vtSymbol)
            contract = self.mainEngine.getContract(strategy.vtSymbol)
            # print '222222222222222222222222222222   ctaEngine.py_line471    contract:', contract
            if contract:
                # print(3333333333333333333333333333, 'ctaEngine.py_line473')
                req = VtSubscribeReq()
                req.symbol = contract.symbol
                req.exchange = contract.exchange

                # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
                req.currency = strategy.currency
                req.productClass = strategy.productClass
                # print(req.productClass) #这个的编码待修改
                self.mainEngine.subscribe(req, contract.gatewayName)

            else:
                # # 下面是zls自己添加的,是否保存待确定
                # req = VtSubscribeReq()
                # req.symbol = 'USD.CNH'
                # req.exchange = 'IDEALPRO'
                # # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
                # req.currency = ''
                # req.productClass = ''
                # self.mainEngine.subscribe(req, 'IB')

                self.writeCtaLog(u'%s的交易合约%s无法找到' %(name, strategy.vtSymbol))
Beispiel #8
0
    def initStrategyBySymbol(self, symbol, setting):

        # 创建策略实例
        # 防止策略重名
        if (symbol + setting['className']) in self.strategyDict:
            self.writeCtaLog(u'策略实例重名:%s' % name)
            return

        setting["vtSymbol"] = symbol

        className = setting['className']

        # 获取策略类
        strategyClass = STRATEGY_CLASS.get(className, None)
        if not strategyClass:
            self.writeCtaLog(u'找不到策略类:%s' % className)
            return

        strategy = strategyClass(self, setting)

        self.strategyDict[symbol + className] = strategy
        strategy.setFixSize(self.FIX_SIZE_AUDO)

        # 创建委托号列表
        self.strategyOrderDict[symbol + strategy.className] = set()

        # 保存Tick映射关系
        if strategy.vtSymbol in self.tickStrategyDict:
            l = self.tickStrategyDict[strategy.vtSymbol]
        else:
            l = []
            self.tickStrategyDict[strategy.vtSymbol] = l
        l.append(strategy)

        ##读数据库
        traderOrd = TraderOrder()
        traderOrd.symbol = symbol
        traderOrd.strategyName = strategy.className
        traderOrd.offset = OFFSET_OPEN

        t = self.mainEngine.mysqlClient.dbSelect(SQL_TABLENAME_TRADER,
                                                 traderOrd, "one")
        if t:
            traderOrd.orderID = t["orderID"]
            traderOrd.orderUuid = t["orderUuid"]
            traderOrd.direction = t["direction"]
            traderOrd.orderVolume = t["orderVolume"]
            traderOrd.orderPrice = t["orderPrice"]
            traderOrd.tradeVolume = t["tradeVolume"]
            traderOrd.tradePrice = t["tradePrice"]

            if traderOrd.offset == OFFSET_OPEN:
                self.lastOpenTrade[traderOrd.symbol] = traderOrd

            self.traderDict[traderOrd.orderID] = traderOrd

        # 订阅合约
        contract = self.mainEngine.getContract(strategy.vtSymbol)
        if contract:

            strategy.setPriceTick(contract.size, contract.priceTick)

            req = VtSubscribeReq()
            req.symbol = contract.symbol
            req.exchange = contract.exchange

            # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
            req.currency = strategy.currency
            req.productClass = strategy.productClass

            self.mainEngine.subscribe(req, contract.gatewayName)

            self.writeCtaLog("subscribe===%s" % (strategy.vtSymbol))

        else:
            self.writeCtaLog(u'%s的交易合约%s无法找到' % (name, strategy.vtSymbol))
Beispiel #9
0
    def loadSetting(self):
        """载入设置"""
        with open(self.settingFileName) as f:
            drSetting = json.load(f)

            # 如果working设为False则不启动行情记录功能
            working = drSetting['working']
            if not working:
                return

            if 'tick' in drSetting:
                l = drSetting['tick']

                for setting in l:
                    symbol = setting[0]
                    vtSymbol = symbol

                    req = VtSubscribeReq()
                    req.symbol = setting[0]

                    # 针对LTS和IB接口,订阅行情需要交易所代码
                    if len(setting) >= 3:
                        req.exchange = setting[2]
                        vtSymbol = '.'.join([symbol, req.exchange])

                    # 针对IB接口,订阅行情需要货币和产品类型
                    if len(setting) >= 5:
                        req.currency = setting[3]
                        req.productClass = setting[4]

                    self.mainEngine.subscribe(req, setting[1])

                    drTick = DrTickData()  # 该tick实例可以用于缓存部分数据(目前未使用)
                    self.tickDict[vtSymbol] = drTick

            if 'bar' in drSetting:
                l = drSetting['bar']

                for setting in l:
                    symbol = setting[0]
                    vtSymbol = symbol

                    req = VtSubscribeReq()
                    req.symbol = symbol

                    if len(setting) >= 3:
                        req.exchange = setting[2]
                        vtSymbol = '.'.join([symbol, req.exchange])

                    if len(setting) >= 5:
                        req.currency = setting[3]
                        req.productClass = setting[4]

                    self.mainEngine.subscribe(req, setting[1])

                    bar = DrBarData()
                    self.barDict[vtSymbol] = bar

            if 'active' in drSetting:
                d = drSetting['active']

                # 注意这里的vtSymbol对于IB和LTS接口,应该后缀.交易所
                for activeSymbol, vtSymbol in d.items():
                    self.activeSymbolDict[vtSymbol] = activeSymbol

            # 启动数据插入线程
            self.start()

            # 注册事件监听
            self.registerEvent()
Beispiel #10
0
    def loadSetting(self):
        """载入设置"""
        with open(self.settingFilePath) as f:
            drSetting = json.load(f)
            
            # 如果working设为False则不启动行情记录功能
            working = drSetting['working']
            if not working:
                return
            
            if 'tick' in drSetting:
                l = drSetting['tick']
                
                for setting in l:
                    symbol = setting[0]
                    vtSymbol = symbol

                    req = VtSubscribeReq()
                    req.symbol = setting[0]
                    
                    # 针对LTS和IB接口,订阅行情需要交易所代码
                    if len(setting)>=3:
                        req.exchange = setting[2]
                        vtSymbol = '.'.join([symbol, req.exchange])
                    
                    # 针对IB接口,订阅行情需要货币和产品类型
                    if len(setting)>=5:
                        req.currency = setting[3]
                        req.productClass = setting[4]
                    
                    self.mainEngine.subscribe(req, setting[1])
                    
                    drTick = DrTickData()           # 该tick实例可以用于缓存部分数据(目前未使用)
                    self.tickDict[vtSymbol] = drTick
                    
            if 'bar' in drSetting:
                l = drSetting['bar']
                
                for setting in l:
                    symbol = setting[0]
                    vtSymbol = symbol
                    
                    req = VtSubscribeReq()
                    req.symbol = symbol                    

                    if len(setting)>=3:
                        req.exchange = setting[2]
                        vtSymbol = '.'.join([symbol, req.exchange])

                    if len(setting)>=5:
                        req.currency = setting[3]
                        req.productClass = setting[4]                    
                    
                    self.mainEngine.subscribe(req, setting[1])  
                    
                    bar = DrBarData() 
                    self.barDict[vtSymbol] = bar
                    
            if 'active' in drSetting:
                d = drSetting['active']
                
                # 注意这里的vtSymbol对于IB和LTS接口,应该后缀.交易所
                for activeSymbol, vtSymbol in d.items():
                    self.activeSymbolDict[vtSymbol] = activeSymbol

            # 启动数据插入线程
            self.start()

            # 注册事件监听
            self.registerEvent()
Beispiel #11
0
    def loadStrategy(self, setting):
        """载入策略"""
        try:
            name = setting['name']
            className = setting['className']
        except Exception:
            msg = traceback.format_exc()
            self.writeMmkLog(u'载入策略出错:%s' % msg)
            return

        # 获取策略类
        strategyClass = STRATEGY_CLASS.get(className, None)
        if not strategyClass:
            self.writeMmkLog(u'找不到策略类:%s' % className)
            return

        # 防止策略重名
        if name in self.strategyDict:
            self.writeMmkLog(u'策略实例重名:%s' % name)
        else:
            # 创建策略实例
            strategy = strategyClass(self, setting)
            self.strategyDict[name] = strategy
            # 创建委托号列表
            self.strategyOrderDict[name] = set()

            # 保存Tick映射关系
            # if strategy.vtSymbol in self.tickStrategyDict:
            #     l = self.tickStrategyDict[strategy.vtSymbol]
            # else:
            #     l = []
            #     self.tickStrategyDict[strategy.vtSymbol] = l
            # l.append(strategy)
            for vtsym in strategy.vtSymbol:
                # 初始化posStrategyNameDict
                for sym in [vtsym.split('.')[0][:3], vtsym.split('.')[0][3:]]:
                    if sym not in self.posStrategyNameDict:
                        self.posStrategyNameDict[sym] = [strategy.name]
                    elif sym in self.posStrategyNameDict:
                        if self.posStrategyNameDict[sym][0] != strategy.name:
                            self.posStrategyNameDict[sym].append(strategy.name)

                if vtsym in self.tickStrategyDict:
                    l = self.tickStrategyDict[vtsym]
                else:
                    l = []
                    self.tickStrategyDict[vtsym] = l
                l.append(strategy)

                # 订阅合约
                contract = self.mainEngine.getContract(vtsym)
                if contract:
                    req = VtSubscribeReq()
                    req.symbol = contract.symbol
                    req.exchange = contract.exchange

                    # 对于IB接口订阅行情时所需的货币和产品类型,从策略属性中获取
                    req.currency = strategy.currency
                    req.productClass = strategy.productClass

                    self.mainEngine.subscribe(req, contract.gatewayName)
                else:
                    self.writeMmkLog(u'%s的交易合约%s无法找到' % (name, vtsym))
Beispiel #12
0
    def loadSetting(self):
        """载入设置"""
        with open(self.settingFilePath) as f:
            drSetting = json.load(f)

            # 如果working设为False则不启动行情记录功能
            working = drSetting['working']
            if not working:
                return

            if 'tick' in drSetting:
                l = drSetting['tick']

                for setting in l:
                    symbol = setting[0]
                    vtSymbol = symbol

                    req = VtSubscribeReq()
                    req.symbol = setting[0]

                    # 针对LTS和IB接口,订阅行情需要交易所代码
                    if len(setting) >= 3:
                        req.exchange = setting[2]
                        vtSymbol = '.'.join([symbol, req.exchange])

                    # 针对IB接口,订阅行情需要货币和产品类型
                    if len(setting) >= 5:
                        req.currency = setting[3]
                        req.productClass = setting[4]

                    self.mainEngine.subscribe(req, setting[1])

                    drTick = DrTickData()  # 该tick实例可以用于缓存部分数据(目前未使用)
                    self.tickDict[vtSymbol] = drTick

            if 'bar' in drSetting:
                l = drSetting['bar']

                for setting in l:
                    symbol = setting[0]
                    vtSymbol = symbol

                    req = VtSubscribeReq()
                    req.symbol = symbol

                    if len(setting) >= 3:
                        req.exchange = setting[2]
                        vtSymbol = '.'.join([symbol, req.exchange])

                    if len(setting) >= 5:
                        req.currency = setting[3]
                        req.productClass = setting[4]

                    self.mainEngine.subscribe(req, setting[1])

                    bar = DrBarData()
                    self.barDict[vtSymbol] = bar

            if 'renko' in drSetting:
                l = drSetting.get('renko')
                req_set = set()
                for setting in l:
                    # 获取合约,合约短号,renko的高度(多少个跳)
                    vtSymbol = setting.get('vtSymbol', None)
                    if vtSymbol is None:
                        continue
                    short_symbol = getShortSymbol(vtSymbol).upper()
                    height = setting.get('height', 5)
                    minDiff = setting.get('minDiff', 1)

                    # 获取vtSymbol的多个renkobar列表,添加新的CtaRenkoBar
                    renko_list = self.renkoDict.get(vtSymbol, [])

                    bar_setting = {
                        'name': '{}_{}'.format(vtSymbol, height),
                        'shortSymbol': short_symbol,
                        'vtSymbol': vtSymbol,
                        'minDiff': minDiff,
                        'height': minDiff * height
                    }
                    renko_bar = CtaRenkoBar(strategy=None,
                                            onBarFunc=self.onRenkoBar,
                                            setting=bar_setting)
                    renko_list.append(renko_bar)
                    self.renkoDict.update({vtSymbol: renko_list})

                    req = VtSubscribeReq()
                    req.symbol = vtSymbol
                    req_set.add((req, setting.get('gateway', None)))
                # 更新合约的历史数据
                self.add_gap_ticks()

                # 订阅行情
                for req, gw in req_set:
                    self.mainEngine.subscribe(req, gw)

            if 'active' in drSetting:
                d = drSetting['active']

                # 注意这里的vtSymbol对于IB和LTS接口,应该后缀.交易所
                for activeSymbol, vtSymbol in d.items():
                    self.activeSymbolDict[vtSymbol] = activeSymbol

            # 启动数据插入线程
            self.start()

            # 注册事件监听
            self.registerEvent()