def handle_bar(self, event): if self._force_run_before_trading: self.before_trading(event) else: bar_dict = event.bar_dict with ExecutionContext(EXECUTION_PHASE.ON_BAR): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._handle_bar(self._user_context, bar_dict)
def before_trading_(self, event): with ExecutionContext(EXECUTION_PHASE.BEFORE_TRADING): self._stage = 'before_trading' for day_rule, time_rule, func in self._registry: if day_rule() and time_rule(): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): func(self._ucontext, None) self._stage = None
def handle_tick(self, event): if self._force_run_before_trading: self.before_trading(event) else: tick = event.tick with ExecutionContext(EXECUTION_PHASE.ON_TICK): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._handle_tick(self._user_context, tick)
def init(self): if self._init: with ExecutionContext(EXECUTION_PHASE.ON_INIT): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._init(self._user_context) Environment.get_instance().event_bus.publish_event( Event(EVENT.POST_USER_INIT))
def next_bar_(self, event): bars = event.bar_dict with ExecutionContext(EXECUTION_PHASE.SCHEDULED): self._current_minute = self._minutes_since_midnight(self._ucontext.now.hour, self._ucontext.now.minute) for day_rule, time_rule, func in self._registry: if day_rule() and time_rule(): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): func(self._ucontext, bars) self._last_minute = self._current_minute
def end(self): if not self._end: return with ExecutionContext(EXECUTION_PHASE.FINALIZED): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._end(self._user_context) Environment.get_instance().event_bus.publish_event( Event(EVENT.POST_USER_END))
def before_trading(self, event): self._force_run_before_trading = False with ExecutionContext(EXECUTION_PHASE.BEFORE_TRADING): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._before_trading(self._user_context)
def after_trading(self, event): with ExecutionContext(EXECUTION_PHASE.AFTER_TRADING): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._after_trading(self._user_context)
def handle_bar(self, event): bar_dict = event.bar_dict with ExecutionContext(EXECUTION_PHASE.ON_BAR): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._handle_bar(self._user_context, bar_dict)
def wrapped_handler(event): with ExecutionContext(EXECUTION_PHASE.GLOBAL): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): return handler(self._user_context, event)
def handle_tick(self, event): tick = event.tick with ExecutionContext(EXECUTION_PHASE.ON_TICK): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._handle_tick(self._user_context, tick)
def open_auction(self, event): bar_dict = event.bar_dict with ExecutionContext(EXECUTION_PHASE.OPEN_AUCTION): with ModifyExceptionFromType(EXC_TYPE.USER_EXC): self._open_auction(self._user_context, bar_dict)