def start_up(self, env, mod_config): env.set_price_board(StockLimitUpDownPriceBoard()) type_ = DataSourceType(mod_config.source) if type_ in [DataSourceType.MONGO, DataSourceType.REAL_TIME]: args = (env.config.base.data_bundle_path, mod_config.mongo_url) data_source_cls = MongoCacheDataSource if mod_config.enable_cache else MongoDataSource elif type_ == DataSourceType.BUNDLE: args = (env.config.base.data_bundle_path, mod_config.bundle_path) data_source_cls = BundleCacheDataSource if mod_config.enable_cache else BundleDataSource else: raise RuntimeError("data source type [%s] is not supported" % mod_config.source) if mod_config.enable_cache: if mod_config.cache_length: CacheMixin.set_cache_length(int(mod_config.cache_length)) if mod_config.max_cache_space: CacheMixin.set_cache_length(int(mod_config.cache_length)) data_source = data_source_cls(*args) mod_config.redis_uri = mod_config.redis_url # fit rqalpha if env.config.base.run_type is RUN_TYPE.BACKTEST and env.config.base.persist_mode == PERSIST_MODE.ON_NORMAL_EXIT: # generate user context using backtest persist_provider = DiskPersistProvider(mod_config.persist_path) env.set_persist_provider(persist_provider) is_real_time = env.config.base.run_type in (RUN_TYPE.PAPER_TRADING, RUN_TYPE.LIVE_TRADING) if is_real_time or type_ == DataSourceType.REAL_TIME: user_system_log.warn( _("[Warning] When you use this version of RealtimeTradeMod, history_bars can only " "get data from yesterday.")) if mod_config.redis_url: data_source = RedisDataSource(env.config.base.data_bundle_path, mod_config.redis_url, datasource=data_source) system_log.info(_("RealtimeTradeMod using market from redis")) else: data_source = DirectDataSource( env.config.base.data_bundle_path) system_log.info( _("RealtimeTradeMod using market from network")) if is_real_time: event_source = RealTimeEventSource(mod_config.fps, mod_config) # add persist persist_provider = DiskPersistProvider(mod_config.persist_path) env.set_persist_provider(persist_provider) env.config.base.persist = True env.config.base.persist_mode = PERSIST_MODE.REAL_TIME else: event_source = IntervalEventSource(env) env.set_data_source(data_source) # a patch to start_date since it's real time mod if env.config.base.start_date == datetime.now().date(): trading_dates = data_source.get_trading_calendar() pos = trading_dates.searchsorted(env.config.base.start_date) if trading_dates[pos].to_pydatetime().date( ) != env.config.base.start_date: env.config.base.start_date = trading_dates[max( 0, pos - 1)].to_pydatetime().date() env.set_event_source(event_source)
def start_up(self, env, mod_config): if env.config.base.run_type in (RUN_TYPE.PAPER_TRADING, RUN_TYPE.LIVE_TRADING): user_system_log.warn( _("[Warning] When you use this version of RealtimeTradeMod, history_bars can only get data from yesterday." )) if mod_config.redis_uri: env.set_data_source( RedisDataSource(env.config.base.data_bundle_path, mod_config.redis_uri)) system_log.info(_("RealtimeTradeMod using market from redis")) else: env.set_data_source( DirectDataSource(env.config.base.data_bundle_path)) system_log.info( _("RealtimeTradeMod using market from network")) env.set_event_source( RealtimeEventSource(mod_config.fps, mod_config)) # add persist persist_provider = DiskPersistProvider(mod_config.persist_path) env.set_persist_provider(persist_provider) env.config.base.persist = True env.config.base.persist_mode = PERSIST_MODE.REAL_TIME
def start_up(self, env, mod_config): if env.config.base.run_type == RUN_TYPE.PAPER_TRADING: env.set_data_source(DataSource(env.config.base.data_bundle_path)) env.set_event_source(RealtimeEventSource(mod_config.fps)) persist_provider = DiskPersistProvider(mod_config.persist_path) env.set_persist_provider(persist_provider) env.config.base.persist = True env.config.base.persist_mode = PERSIST_MODE.REAL_TIME
def _init(self, event): env = self._env mod_config = self._mod_config system_log.info("use recorder {}", mod_config.recorder) if mod_config.recorder == "CsvRecorder": if not mod_config.persist_folder: raise RuntimeError( _(u"You need to set persist_folder to use CsvRecorder")) persist_provider = DiskPersistProvider( os.path.join(mod_config.persist_folder, "persist")) self._recorder = recorders.CsvRecorder(mod_config.persist_folder) elif mod_config.recorder == "MongodbRecorder": if mod_config.strategy_id is None: raise RuntimeError(_(u"You need to set strategy_id")) persist_provider = persist_providers.MongodbPersistProvider( mod_config.strategy_id, mod_config.mongo_url, mod_config.mongo_dbname) self._recorder = recorders.MongodbRecorder(mod_config.strategy_id, mod_config.mongo_url, mod_config.mongo_dbname) else: raise RuntimeError( _(u"unknown recorder {}").format(mod_config.recorder)) if env.persist_provider is None: env.set_persist_provider(persist_provider) self._meta = { "strategy_id": mod_config.strategy_id, "origin_start_date": self._env.config.base.start_date.strftime("%Y-%m-%d"), "start_date": self._env.config.base.start_date.strftime("%Y-%m-%d"), "end_date": self._env.config.base.end_date.strftime("%Y-%m-%d"), "last_run_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), } persist_meta = self._recorder.load_meta() if persist_meta: if persist_meta["end_date"] >= self._meta["start_date"]: raise RuntimeError( _(u"current start_date {} is before last end_date {}"). format(self._meta["start_date"], persist_meta["end_date"])) else: self._meta["origin_start_date"] = persist_meta[ "origin_start_date"] env.event_bus.add_listener(EVENT.TRADE, self.on_trade) env.event_bus.add_listener(EVENT.POST_SETTLEMENT, self.on_settlement)
def _set_env_and_data_source(self): env = self._env mod_config = self._mod_config system_log.info("use recorder {}", mod_config.recorder) if mod_config.recorder == "CsvRecorder": if not mod_config.persist_folder: raise RuntimeError( _(u"You need to set persist_folder to use CsvRecorder")) persist_provider = DiskPersistProvider( os.path.join(mod_config.persist_folder, "persist")) self._recorder = recorders.CsvRecorder(mod_config.persist_folder) elif mod_config.recorder == "MongodbRecorder": if mod_config.strategy_id is None: raise RuntimeError(_(u"You need to set strategy_id")) persist_provider = persist_providers.MongodbPersistProvider( mod_config.strategy_id, mod_config.mongo_url, mod_config.mongo_dbname) self._recorder = recorders.MongodbRecorder(mod_config.strategy_id, mod_config.mongo_url, mod_config.mongo_dbname) else: raise RuntimeError( _(u"unknown recorder {}").format(mod_config.recorder)) if env.persist_provider is None: env.set_persist_provider(persist_provider) self._meta = { "strategy_id": mod_config.strategy_id, "origin_start_date": self._env.config.base.start_date.strftime("%Y-%m-%d"), "start_date": self._env.config.base.start_date.strftime("%Y-%m-%d"), "end_date": self._env.config.base.end_date.strftime("%Y-%m-%d"), "last_end_time": self._env.config.base.end_date.strftime("%Y-%m-%d"), "last_run_time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), } event_start_time = self._env.config.base.start_date persist_meta = self._recorder.load_meta() if persist_meta: # 不修改回测开始时间 self._env.config.base.start_date = persist_meta['start_date'] event_start_time = datetime.datetime.strptime( persist_meta['last_end_time'], '%Y-%m-%d').date() + datetime.timedelta(days=1) # 代表历史有运行过,根据历史上次运行的end_date下一天设为事件发送的start_time self._meta["origin_start_date"] = persist_meta["origin_start_date"] self._meta["start_date"] = persist_meta["start_date"] self._meta[ "last_end_time"] = self._env.config.base.end_date.strftime( "%Y-%m-%d") env.set_data_source( IncrementcalDataSource( self._env.config.base.data_bundle_path, getattr(self._env.config.base, "future_info", {}), self._env.config.base.start_date)) event_source = IncrementalEventSource(env, event_start_time, self._env.config.base.end_date) env.set_event_source(event_source)