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