Exemple #1
0
    def do_initialize(self, engine: Engine):

        engine.register_event(
            EventDefinition(ed_type=EventDefinitionType.TIME,
                            time_rule=MarketOpen()), self.market_open)
        engine.register_event(
            EventDefinition(ed_type=EventDefinitionType.TIME,
                            time_rule=MarketClose(offset=-1)),
            self.market_close)
    def do_initialize(self, engine: Engine, data_portal: DataPortal):
        if engine.is_backtest:
            market_close = EventDefinition(ed_type=EventDefinitionType.TIME, time_rule=MarketClose())
            market_open = EventDefinition(ed_type=EventDefinitionType.TIME, time_rule=MarketOpen())
        else:
            market_open = EventDefinition(ed_type=EventDefinitionType.TIME, time_rule=MarketOpen(second_offset=5))
            market_close = EventDefinition(ed_type=EventDefinitionType.TIME, time_rule=MarketClose(second_offset=-60))
            market_close_set_price = EventDefinition(ed_type=EventDefinitionType.TIME,
                                                     time_rule=MarketClose())
            engine.register_event(market_close_set_price, self.set_close_price)

        engine.register_event(market_open, self.market_open)
        engine.register_event(market_close, self.market_close)

        # 初始化昨日开盘价和收盘价
        self.last_open = None
        self.last_close = None
        if not engine.is_backtest:
            command = HistoryDataQueryCommand(None, None, self.scope.codes, window=1)
            command.with_calendar(trading_calendar=self.scope.trading_calendar)
            df = data_portal.history_data("ibAdjustedDailyBar", command)
            if len(df) >= 1:
                self.last_open = df.iloc[-1]['open']
                self.last_close = df.iloc[-1]['close']
                logging.info("初始化数据成功,昨日开盘价:{}, 昨日收盘价:{}, bar的开始时间:{}"
                             .format(self.last_open, self.last_close, df.iloc[-1]['start_time']))
            else:
                raise RuntimeError("没有获取到昨日开盘价和收盘价")
            # self.last_open = 35.17
            # self.last_close = 33.77

        if len(self.scope.codes) != 1:
            raise RuntimeError("wrong codes")
        self.code = self.scope.codes[0]
Exemple #3
0
    def do_initialize(self, engine: Engine, data_portal: DataPortal):
        if engine.is_backtest:
            raise RuntimeError("不支持回测")
        engine.register_event(EventDefinition(ed_type=EventDefinitionType.DATA, ts_type_name='ibTick',
                                              event_data_type=EventDataType.TICK), self.on_tick)
        engine.register_event(event_definition=EventDefinition(ed_type=EventDefinitionType.TIME, time_rule=MarketOpen()),
                              callback=self.market_open)
        engine.register_event(
            event_definition=EventDefinition(ed_type=EventDefinitionType.TIME, time_rule=MarketOpen()),
            callback=self.market_close)

        self.daily_ticks: DataFrame = DataFrame()
        self.time_span = Timedelta(minutes=1)
        self.threshold = 0.005
        self.code = self.scope.codes[0]
        self.market_is_open = self.scope.trading_calendar.is_open_on_minute(Timestamp.now(tz='Asia/Shanghai'))