Ejemplo n.º 1
0
    def subscribeEvent(self, symbol):
        """订阅对应合约的事件"""
        contract = self.mainEngine.getContract(symbol)
        if not contract:
            self.writeLog(u'行情订阅失败,找不到合约:%s' % symbol)
            return

        vtSymbol = contract.vtSymbol

        # 订阅行情
        req = VtSubscribeReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange

        gateway = self.mainEngine.getGateway(contract.gatewayName)
        if gateway.mdConnected:
            self.mainEngine.subscribe(req, contract.gatewayName)
        else:
            self.mainEngine.subscribe(req, 'SEC')
            self.mainEngine.subscribe(req, 'CSHSHLP')

        # 订阅事件
        if req.symbol == '510050':
            #做这个处理是因为,中信接口没有行情接口,需要用其他的接口获取行情,但是有时候会出现vtSymbol对应不上的情况
            self.eventEngine.register(EVENT_TICK + req.symbol,
                                      self.processTickEvent)
            vtSymbol = '510050.SSE'
        self.eventEngine.register(EVENT_TICK + vtSymbol, self.processTickEvent)
        self.eventEngine.register(EVENT_TRADE + vtSymbol,
                                  self.processTradeEvent)
Ejemplo n.º 2
0
    def addContract(self, event):
        """更新合约数据"""
        contract = event.dict_['data']

        if contract.productClass == u'期货':
            #print(contract.symbol)
            req = VtSubscribeReq()
            req.symbol = contract.symbol
            self.mainEngine.subscribe(req, "CTP")

            # 创建BarManager对象
            self.bmDict[contract.symbol] = BarManager(self.onBar, self.onXBar,
                                                      self.handleDayBar)

            self.tickSymbolSet.add(contract.symbol)

            #判断是否新合约
            if contract.symbol not in self.mongoSymbolSet:
                #新合约要先给数据库表建索datetime索引
                self.mainEngine.dbCreateIndex(TICK_DB_NAME, contract.symbol)
                for xmin in MINUTE_TO_DB_NAME:
                    self.mainEngine.dbCreateIndex(MINUTE_TO_DB_NAME[xmin],
                                                  contract.symbol)

            # 保存到配置字典中
            if contract.symbol not in self.settingDict:
                d = {'symbol': contract.symbol, 'gateway': "CTP", 'tick': True}
                self.settingDict[contract.symbol] = d
Ejemplo n.º 3
0
def main():
    global me

    ee = EventEngine()
    me = MainEngine(ee)
    ee.register(EVENT_TICK, on_tick_update)

    me.addGateway(ctpGateway)
    me.getGateway('CTP').filePath = os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        'CTP_connect.simnow.mock.json')
    me.connect('CTP')

    # 等待 1 秒是为了避免订阅时机 bug
    import time
    time.sleep(1)

    # vt_symbols = ['SR901C5300.CZCE', 'SR901C5200.CZCE']
    vt_symbols = ['ru1908.SHFE', 'rb1908.SHFE']
    symbols, exchanges = zip(
        *[vs.split('.') if '.' in vs else (vs, '') for vs in vt_symbols])
    for s, e in zip(symbols, exchanges):
        req = VtSubscribeReq()
        req.symbol, req.exchange = s, e
        me.subscribe(req, 'CTP')
    print('Futures subscribed.')
Ejemplo n.º 4
0
    def subscribeForStrategy(self, stra):
        try:
            stockCodeList = stra.getSubscribeList()
            req = VtSubscribeReq()
            req.symbol = stockCodeList
            self.mainEngine.subscribe(req, 'FUTU')

        except:
            traceback.print_exc()
Ejemplo n.º 5
0
 def subscribeSymobls(self):
     import string
     symbols = map(string.strip,
                   self.cfgs.get('sub_symbols', '').split(','))
     for symbol in symbols:
         req = VtSubscribeReq()
         req.symbol = symbol
         req.exchange = EXCHANGE_SSE
         self.mainEngine.subscribe(req, xtpGateway.gatewayName)  #
Ejemplo n.º 6
0
    def subscribe(self, *symbols):
        for symbol in symbols:
            if not self.symbols.has_key(symbol):
                req = VtSubscribeReq()
                req.symbol = symbol
                self.mainEngine.subscribe(req, xtpGateway.gatewayName)  # XTP
                self.symbols[symbol] = symbol

        if not symbols:
            self.mainEngine.subscribe(None,
                                      xtpGateway.gatewayName)  # 订阅全市场所有股票
Ejemplo n.º 7
0
 def subscribeForOption(self, optionCode):
     try:
         optionDetail = self.workingOptionDict[optionCode]
         codeList = (optionCode, optionDetail["OwnerCode"])
         for stockCode in codeList:
             req = VtSubscribeReq()
             req.symbol = stockCode
             self.mainEngine.subscribe(req, 'FUTU')
             # time.sleep(1)
     except:
         traceback.print_exc()
Ejemplo n.º 8
0
 def subscribeMarketData(self, vtSymbol):
     """订阅行情"""
     contract = self.mainEngine.getContract(vtSymbol)
     if not contract:
         self.writeLog(u'订阅行情失败,找不到该合约%s' %vtSymbol)
         return
     
     req = VtSubscribeReq()
     req.symbol = contract.symbol
     req.exchange = contract.exchange
     
     self.mainEngine.subscribe(req, contract.gatewayName)
Ejemplo n.º 9
0
    def subscribeMarketData(self, vtSymbol):
        """订阅行情"""
        contract = self.mainEngine.getContract(vtSymbol)
        if not contract:
            self.writeLog(u'订阅行情失败,找不到该合约%s' % vtSymbol)
            return

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

        self.mainEngine.subscribe(req, contract.gatewayName)
Ejemplo n.º 10
0
 def updatePos(self, data):
     """"""
     vtSymbol = data['vtSymbol']
     if vtSymbol not in self.subscribeSet:
         contract = self.mainEngine.getContract(vtSymbol)
         
         req = VtSubscribeReq()
         req.symbol = contract.symbol
         req.exchange = contract.exchange
         self.mainEngine.subscribe(req, contract.gatewayName)
     
     vtPositionName = data['vtPositionName']
     target = int(data['pos'] * self.copyRatio)
     self.targetDict[vtPositionName] = target
     
     self.checkAndTrade(vtSymbol)
Ejemplo n.º 11
0
    def updatePos(self, data):
        """"""
        vtSymbol = data['vtSymbol']
        if vtSymbol not in self.subscribeSet:
            contract = self.mainEngine.getContract(vtSymbol)

            req = VtSubscribeReq()
            req.symbol = contract.symbol
            req.exchange = contract.exchange
            self.mainEngine.subscribe(req, contract.gatewayName)

        vtPositionName = data['vtPositionName']
        target = int(data['pos'] * self.copyRatio)
        self.targetDict[vtPositionName] = target

        self.checkAndTrade(vtSymbol)
Ejemplo n.º 12
0
 def subscribeEvent(self, symbol):
     """订阅对应合约的事件"""
     contract = self.mainEngine.getContract(symbol)
     if not contract:
         self.writeLog(u'行情订阅失败,找不到合约:%s' %symbol)
         return
         
     vtSymbol = contract.vtSymbol
     
     # 订阅行情
     req = VtSubscribeReq()
     req.symbol = contract.symbol
     req.exchange = contract.exchange
     self.mainEngine.subscribe(req, contract.gatewayName)
     
     # 订阅事件
     self.eventEngine.register(EVENT_TICK + vtSymbol, self.processTickEvent)
     self.eventEngine.register(EVENT_TRADE + vtSymbol, self.processTradeEvent)
Ejemplo n.º 13
0
 def subscribeEvent(self, symbol):
     """订阅对应合约的事件"""
     contract = self.mainEngine.getContract(symbol)
     if not contract:
         self.writeLog(u'行情订阅失败,找不到合约:%s' %symbol)
         return
         
     vtSymbol = contract.vtSymbol
     
     # 订阅行情
     req = VtSubscribeReq()
     req.symbol = contract.symbol
     req.exchange = contract.exchange
     self.mainEngine.subscribe(req, contract.gatewayName)
     
     # 订阅事件
     self.eventEngine.register(EVENT_TICK + vtSymbol, self.processTickEvent)
     self.eventEngine.register(EVENT_TRADE + vtSymbol, self.processTradeEvent)
Ejemplo n.º 14
0
    def post(self):
        """订阅"""
        args = self.parser.parse_args()
        token = args['token']
        if token != TOKEN:
            return {'result_code':'error','message':'token error'}

        vtSymbol = args['vtSymbol']

        contract = me.getContract(vtSymbol)
        if not contract:
            return {'result_code':'error','message':'contract error'}

        req = VtSubscribeReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange
        req.vtSymbol = contract.vtSymbol
        
        me.subscribe(req, contract.gatewayName)
        return {'result_code':'success','data':''}
Ejemplo n.º 15
0
    def post(self):
        """订阅"""
        args = self.parser.parse_args()
        token = args['token']
        if token != TOKEN:
            return {'result_code': 'error', 'message': 'token error'}

        vtSymbol = args['vtSymbol']

        contract = me.getContract(vtSymbol)
        if not contract:
            return {'result_code': 'error', 'message': 'contract error'}

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

        me.subscribe(req, contract.gatewayName)
        return {'result_code': 'success', 'data': ''}
Ejemplo n.º 16
0
def runChildProcess():
    """子进程运行函数"""
    print('-' * 20)

    # 创建日志引擎
    le = LogEngine()
    le.setLogLevel(le.LEVEL_INFO)
    le.addConsoleHandler()
    le.info(u'启动行情记录运行子进程')

    ee = EventEngine2()
    le.info(u'事件引擎创建成功')

    me = MainEngine(ee)
    me.addGateway(ctpGateway)
    me.addApp(dataRecorder)
    le.info(u'主引擎创建成功')

    ee.register(EVENT_LOG, le.processLogEvent)
    ee.register(EVENT_ERROR, processErrorEvent)
    le.info(u'注册日志事件监听')

    me.connect('CTP')
    le.info(u'连接CTP接口')

    #获取全部合约信息并自动订阅
    contracts = me.getAllContracts()
    while len(contracts) == 0:
        sleep(1)
        contracts = me.getAllContracts()
    print(u"共获取" + str(len(contracts)) + u"条合约信息")
    for contract in contracts:
        if contract.productClass == u"期货":
            req = VtSubscribeReq()
            req.symbol = contract.symbol
            me.subscribe(req, contract.gatewayName)
            me.getApp(dataRecorder.appName).tickSymbolSet.add(req.symbol)

    while True:
        sleep(1)
Ejemplo n.º 17
0
    def subscribe(self, algo, vtSymbol):
        """"""
        contract = self.mainEngine.getContract(vtSymbol)
        if not contract:
            self.writeLog(u'%s订阅行情失败,找不到合约%s' %(algo.algoName, vtSymbol))
            return        

        # 如果vtSymbol已存在于字典,说明已经订阅过
        if vtSymbol in self.symbolAlgoDict:
            s = self.symbolAlgoDict[vtSymbol]
            s.add(algo)
            return
        # 否则需要添加到字典中并执行订阅
        else:
            s = set()
            self.symbolAlgoDict[vtSymbol] = s
            s.add(algo)
            
            req = VtSubscribeReq()
            req.symbol = contract.symbol
            req.exchange = contract.exchange
            self.mainEngine.subscribe(req, contract.gatewayName)
Ejemplo n.º 18
0
    def subscribe(self, algo, vtSymbol):
        """"""
        contract = self.mainEngine.getContract(vtSymbol)
        if not contract:
            self.writeLog(u'%s订阅行情失败,找不到合约%s' % (algo.algoName, vtSymbol))
            return

        # 如果vtSymbol已存在于字典,说明已经订阅过
        if vtSymbol in self.symbolAlgoDict:
            s = self.symbolAlgoDict[vtSymbol]
            s.add(algo)
            return
        # 否则需要添加到字典中并执行订阅
        else:
            s = set()
            self.symbolAlgoDict[vtSymbol] = s
            s.add(algo)

            req = VtSubscribeReq()
            req.symbol = contract.symbol
            req.exchange = contract.exchange
            self.mainEngine.subscribe(req, contract.gatewayName)
Ejemplo n.º 19
0
    def addContract(self, event):
        """更新合约数据"""
        contract = event.dict_['data']

        # print(contract.productClass)
        if contract.productClass == u'期货':
            #print(contract.symbol)
            req = VtSubscribeReq()
            req.symbol = contract.symbol
            self.mainEngine.subscribe(req, "CTP")

            # 创建BarManager对象
            self.bmDict[contract.symbol] = RecorderBarManager(self.onBar, self.onXBar)

            self.tickSymbolSet.add(contract.symbol)

            # 保存到配置字典中
            if contract.symbol not in self.settingDict:
                d = {
                    'symbol': contract.symbol,
                    'gateway': "CTP",
                    'tick': True
                }
                self.settingDict[contract.symbol] = d
Ejemplo n.º 20
0
    def loadSetting(self):
        """加载配置"""
        with open(self.settingFilePath) as f:
            drSetting = json.load(f)

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

            # Tick记录配置
            if 'tick' in drSetting:
                l = drSetting['tick']

                for setting in l:
                    symbol = setting[0]
                    gateway = setting[1]
                    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, gateway)

                    #tick = VtTickData()           # 该tick实例可以用于缓存部分数据(目前未使用)
                    #self.tickDict[vtSymbol] = tick
                    self.tickSymbolSet.add(vtSymbol)
                    
                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': gateway,
                            'tick': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['tick'] = True

            # 分钟线记录配置
            if 'bar' in drSetting:
                l = drSetting['bar']

                for setting in l:
                    symbol = setting[0]
                    gateway = setting[1]
                    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, gateway)  
                    
                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': gateway,
                            'bar': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['bar'] = True     
                        
                    # 创建BarManager对象
                    self.bgDict[vtSymbol] = BarGenerator(self.onBar)

            # 主力合约记录配置
            if 'active' in drSetting:
                d = drSetting['active']
                self.activeSymbolDict = {vtSymbol:activeSymbol for activeSymbol, vtSymbol in d.items()}
Ejemplo n.º 21
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])

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

            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 = VtBarData()
                    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()
Ejemplo n.º 22
0
    def loadSetting(self):
        """加载配置"""
        with open(self.settingFileName, 'r') 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])

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

                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': setting[1],
                            'tick': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['tick'] = True

            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 = VtBarData()
                    self.barDict[vtSymbol] = bar

                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': setting[1],
                            'bar': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['bar'] = True

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

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

                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': setting[1],
                            'active': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['active'] = True
Ejemplo n.º 23
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])

                    tick = VtTickData()           # 该tick实例可以用于缓存部分数据(目前未使用)
                    self.tickDict[vtSymbol] = tick
                    
                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': setting[1],
                            'tick': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['tick'] = True

            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 = VtBarData() 
                    self.barDict[vtSymbol] = bar
                    
                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': setting[1],
                            'bar': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['bar'] = True                    

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

                # 注意这里的vtSymbol对于IB和LTS接口,应该后缀.交易所
                for activeSymbol, vtSymbol in d.items():
                    self.activeSymbolDict[vtSymbol] = activeSymbol    
                    
                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': setting[1],
                            'active': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['active'] = True                    
Ejemplo n.º 24
0
    def subscribe(self, stockCodeList):

        for stockCode in stockCodeList:
            req = VtSubscribeReq()
            req.symbol = stockCode
            self.mainEngine.subscribe(req, 'FUTU')
Ejemplo n.º 25
0
    def loadSetting(self):
        """加载配置"""
        with open(self.settingFilePath) as f:
            drSetting = json.load(f)

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

            # Tick记录配置
            if 'tick' in drSetting:
                l = drSetting['tick']

                for setting in l:
                    symbol = setting[0]
                    gateway = setting[1]
                    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, gateway)

                    #tick = VtTickData()           # 该tick实例可以用于缓存部分数据(目前未使用)
                    #self.tickDict[vtSymbol] = tick
                    self.tickSymbolSet.add(vtSymbol)
                    
                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': gateway,
                            'tick': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['tick'] = True

            # 分钟线记录配置
            if 'bar' in drSetting:
                l = drSetting['bar']

                for setting in l:
                    symbol = setting[0]
                    gateway = setting[1]
                    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, gateway)  
                    
                    # 保存到配置字典中
                    if vtSymbol not in self.settingDict:
                        d = {
                            'symbol': symbol,
                            'gateway': gateway,
                            'bar': True
                        }
                        self.settingDict[vtSymbol] = d
                    else:
                        d = self.settingDict[vtSymbol]
                        d['bar'] = True     
                        
                    # 创建BarManager对象
                    self.bgDict[vtSymbol] = BarGenerator(self.onBar)

            # 主力合约记录配置
            if 'active' in drSetting:
                d = drSetting['active']
                self.activeSymbolDict = {vtSymbol:activeSymbol for activeSymbol, vtSymbol in d.items()}
Ejemplo n.º 26
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])
                    
                    tick = VtTickData()           # 该tick实例可以用于缓存部分数据(目前未使用)
                    self.tickDict[vtSymbol] = tick
                    
            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 = VtBarData() 
                    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()