vn.py官网
开源量化社区
Member
avatar
加入于:
帖子: 189
声望: 43

vnpy\trader\engine.py修改如下

from vnpy.trader.event import (EVENT_ALL_CONTRACTS,EVENT_TIMER,EVENT_TICK, EVENT_ORDER, EVENT_TRADE, EVENT_POSITION,
                    EVENT_ACCOUNT, EVENT_CONTRACT, EVENT_LOG)
#--------------------------------------------------------------------------------------------------
class OmsEngine(BaseEngine):
        self.all_contracts:List[str] = []   #所有合约vt_symbol列表(合约+指数合约)
        self.last_all_contracts:List[str] = [] #上一个all_contracts
        self.count:int = 0 #  计时器
    #--------------------------------------------------------------------------------------------------
    def register_event(self):
        """
        注册事件
        """
        self.event_engine.register(EVENT_TICK, self.process_tick_event)
        self.event_engine.register(EVENT_ORDER, self.process_order_event)
        self.event_engine.register(EVENT_TRADE, self.process_trade_event)
        self.event_engine.register(EVENT_POSITION, self.process_position_event)
        self.event_engine.register(EVENT_ACCOUNT, self.process_account_event)
        self.event_engine.register(EVENT_CONTRACT, self.process_contract_event)
        self.event_engine.register(EVENT_TIMER, self.put_all_contracts)
    #--------------------------------------------------------------------------------------------------
    def process_contract_event(self, event: Event):
        """
        收到合约数据事件推送
        """
        contract:ContractData = event.data
        #只保存交易所更新后的contract数据
        if contract.size:
            self.contracts[contract.vt_symbol] = contract
            if contract.vt_symbol not in self.all_contracts:
                self.all_contracts.append(contract.vt_symbol)
    #--------------------------------------------------------------------------------------------------
    def put_all_contracts(self,event: Event) :
        """
        定时推送所有合约数据到EVENT_ALL_CONTRACTS事件
        """
        self.count += 1

        if self.count < 30:
            return
        self.count = 0
        if not self.all_contracts:
            return

        if self.last_all_contracts == self.all_contracts:
            #重复推送合约数据则推送一次空列表标记为所有合约推送完成,取消put_all_contracts函数注册
            event = Event(EVENT_ALL_CONTRACTS, [])
            self.event_engine.put(event)  
            self.event_engine.unregister(EVENT_TIMER, self.put_all_contracts)
        else:
            #推送EVENT_ALL_CONTRACTS事件
            event = Event(EVENT_ALL_CONTRACTS, self.all_contracts)
            self.event_engine.put(event) 

        self.last_all_contracts = self.all_contracts
Member
avatar
加入于:
帖子: 189
声望: 43

关于EVENT_ALL_CONTRACTS在data_recorder\engine.py的处理可以参考我分享的全市场订阅的帖子,我实现的是合约数据推送可以重订阅,改的比较多就不分享

Member
avatar
加入于:
帖子: 18
声望: 0

上弦之月 wrote:

关于EVENT_ALL_CONTRACTS在data_recorder\engine.py的处理可以参考我分享的全市场订阅的帖子,我实现的是合约数据推送可以重订阅,改的比较多就不分享

请教月总,在gateway 的处理之后,这里为什么还会丢掉合约事件呢?

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3

沪公网安备 31011502017034号