def generate(self): if self.bar is not None: self.bar.interval = 1 event = Event(type=EVENT_BAR, data=self.bar) common_signals.bar_signal.send(event) for x in self.XMIN: if self.bar is not None: bar = getattr(self, "min_{}_bar".format(x)) bar.interval = x event = Event(type=EVENT_BAR, data=bar) common_signals.bar_signal.send(event) self.bar = None [setattr(self, "min_{}_bar".format(x), None) for x in self.XMIN]
def update_bar(self, xmin, bar: BarData): """ Update 1 minute bar into generator """ xmin_bar = getattr(self, f"min_{xmin}_bar", None) if not xmin_bar: xmin_bar = BarData(symbol=bar.symbol, exchange=bar.exchange, datetime=bar.datetime, gateway_name=bar.gateway_name, open_price=bar.open_price, high_price=bar.high_price, low_price=bar.low_price) setattr(self, f"min_{xmin}_bar", xmin_bar) else: xmin_bar.high_price = max(xmin_bar.high_price, bar.high_price) xmin_bar.low_price = min(xmin_bar.low_price, bar.low_price) xmin_bar.close_price = bar.close_price xmin_bar.volume += int(bar.volume) if not (bar.datetime.minute + 1) % xmin: xmin_bar.datetime = xmin_bar.datetime.replace(second=0, microsecond=0) xmin_bar.interval = xmin event = Event(type=EVENT_BAR, data=xmin_bar) common_signals.bar_signal.send(event) setattr(self, f"min_{xmin}_bar", None)
def _start_looper(self): """ 基于现有的数据进行回测数据, 此API用户不需要关注 """ d = VessData(*self.data) if self.basic_info is not None: self.trader.account.basic_info = self.basic_info """ trader初始化参数""" self.trader.init_params(params=self.config) flag = False while True: try: if flag: p = next(d) self.trader(p) else: print("===> 发送初始化信号") from ctpbee.constant import EVENT_INIT_FINISHED self.app_signal.init_signal.send( Event(type=EVENT_INIT_FINISHED, data=None)) flag = True except StopIteration: self.logger.info("回测结束,正在生成结果") break except ValueError: raise ValueError("数据存在问题, 请检查") return self.trader
def on_event(self, type, data): event = Event(type=type, data=data) if type == EVENT_BAR or type == EVENT_TICK: import ctpbee.signals as signals signal = getattr(signals.common_signals, f"{type}_signal") else: signal = getattr(self.app_signal, f"{type}_signal") signal.send(event)
def update_tick(self, tick: TickData): """ Update new tick data into generator and new_shared time data. """ new_minute = False """ 更新价位 """ self.last_price = tick.last_price self.open_interest = tick.open_interest self.volume = tick.volume if self.last_volume is None: self.last_volume = tick.volume if self.local_symbol is None: self.local_symbol = tick.local_symbol if not self.bar: new_minute = True elif self.bar.datetime.minute != tick.datetime.minute: self.bar.datetime = self.bar.datetime.replace(second=0, microsecond=0) self.bar.interval = 1 event = Event(type=EVENT_BAR, data=self.bar) common_signals.bar_signal.send(event) [self.update_bar(x, self.bar) for x in self.XMIN] new_minute = True if new_minute: self.last_volume = tick.volume gateway_name = tick.gateway_name if getattr( tick, "gateway_name", None) else "looper" self.bar = BarData( symbol=tick.symbol, exchange=tick.exchange, datetime=tick.datetime, gateway_name=gateway_name, open_price=tick.last_price, high_price=tick.last_price, low_price=tick.last_price, close_price=tick.last_price, ) else: self.bar.high_price = max(self.bar.high_price, tick.last_price) self.bar.low_price = min(self.bar.low_price, tick.last_price) self.bar.close_price = tick.last_price self.bar.datetime = tick.datetime if self.last_tick: """ 更新volume的改变 """ volume_change = tick.volume - self.last_tick.volume self.bar.volume += max(volume_change, 0) self.last_tick = tick
def update_tick(self, tick: TickData): bar = self.resample(tick) for x in bar: event = Event(type=EVENT_BAR, data=x) common_signals.bar_signal.send(event)
def __del__(self): for x in self.last_entity.values(): event = Event(type=EVENT_BAR, data=x) common_signals.bar_signal.send(event) self.last_entity.clear()
def on_event(self, type, data): event = Event(type=type, data=data) signal = getattr(self.app_signal, f"{type}_signal") signal.send(event)
def on_event(self, type, data): if type == EVENT_TICK: event = Event(type=type, data=data) signal = getattr(common_signals, f"{type}_signal") signal.send(event)
def running_timer(common_signal): while True: event = Event(type=EVENT_TIMER) common_signal.timer_signal.send(event) sleep(self.config['TIMER_INTERVAL'])