diff --git a/rqalpha/mod/rqalpha_mod_sys_simulation/simulation_broker.py b/rqalpha/mod/rqalpha_mod_sys_simulation/simulation_broker.py index 0e15a1de5..abb1354a1 100644 --- a/rqalpha/mod/rqalpha_mod_sys_simulation/simulation_broker.py +++ b/rqalpha/mod/rqalpha_mod_sys_simulation/simulation_broker.py @@ -44,9 +44,9 @@ def __init__(self, env, mod_config): # 该事件会触发策略的before_trading函数 self._env.event_bus.add_listener(EVENT.BEFORE_TRADING, self.before_trading) # 该事件会触发策略的handle_bar函数 - self._env.event_bus.add_listener(EVENT.BAR, self.on_market_event) + self._env.event_bus.add_listener(EVENT.BAR, self.on_bar) # 该事件会触发策略的handel_tick函数 - self._env.event_bus.add_listener(EVENT.TICK, self.on_market_event) + self._env.event_bus.add_listener(EVENT.TICK, self.on_tick) # 该事件会触发策略的after_trading函数 self._env.event_bus.add_listener(EVENT.AFTER_TRADING, self.after_trading) @@ -123,13 +123,22 @@ def after_trading(self, event): self._open_orders = self._delayed_orders self._delayed_orders = [] - def on_market_event(self, event): + def on_bar(self, event): env = Environment.get_instance() self._matcher.update(env.calendar_dt, env.trading_dt) self._match() - def _match(self): - self._matcher.match(self._open_orders) + def on_tick(self, event): + tick = event.tick + env = Environment.get_instance() + self._matcher.update(env.calendar_dt, env.trading_dt) + self._match(tick.order_book_id) + + def _match(self, order_book_id=None): + open_orders = self._open_orders + if order_book_id is not None: + open_orders = [(a, o) for (a, o) in self._open_orders if o.order_book_id == order_book_id] + self._matcher.match(open_orders) final_orders = [(a, o) for a, o in self._open_orders if o.is_final()] self._open_orders = [(a, o) for a, o in self._open_orders if not o.is_final()]