def query( self, cls: type, filter_expr=None, instrument_ids=None, as_nautilus=False, sort_columns: Optional[List[str]] = None, as_type: Optional[Dict] = None, **kwargs, ): if not is_nautilus_class(cls=cls): # Special handling for generic data return self.generic_data( cls=cls, filter_expr=filter_expr, instrument_ids=instrument_ids, as_nautilus=as_nautilus, **kwargs, ) return self._query( cls=cls, filter_expr=filter_expr, instrument_ids=instrument_ids, sort_columns=sort_columns, as_type=as_type, as_dataframe=not as_nautilus, **kwargs, )
def _create_engine( self, config: BacktestEngineConfig, venue_configs: List[BacktestVenueConfig], data_configs: List[BacktestDataConfig], ): # Build the backtest engine engine = BacktestEngine(config=config) # Add instruments for config in data_configs: if is_nautilus_class(config.data_type): instruments = config.catalog().instruments( instrument_ids=config.instrument_id, as_nautilus=True) for instrument in instruments or []: engine.add_instrument(instrument) # Add venues for config in venue_configs: engine.add_venue( venue=Venue(config.name), venue_type=VenueType[config.venue_type], oms_type=OMSType[config.oms_type], account_type=AccountType[config.account_type], base_currency=Currency.from_str(config.base_currency), starting_balances=[ Money.from_str(m) for m in config.starting_balances ], book_type=BookTypeParser.from_str_py(config.book_type), ) return engine
def class_to_filename(cls: type) -> str: """ Convert the given class to a filename. """ name = f"{camel_to_snake_case(cls.__name__)}" if not is_nautilus_class(cls): name = f"{GENERIC_DATA_PREFIX}{camel_to_snake_case(cls.__name__)}" return name
def _create_writers(self): for cls in self._schemas: table_name = get_cls_table(cls).__name__ if table_name in self._writers: continue prefix = "genericdata_" if not is_nautilus_class(cls) else "" schema = self._schemas[cls] full_path = f"{self.path}/{prefix}{table_name}.feather" f = self.fs.open(str(full_path), "wb") self._files[cls] = f self._writers[table_name] = pa.ipc.new_stream(f, schema)
def _load_engine_data(engine: BacktestEngine, data): if data["type"] in (QuoteTick, TradeTick): engine.add_ticks(data=data["data"]) elif data["type"] == Bar: engine.add_bars(data=data["data"]) elif data["type"] in (OrderBookDelta, OrderBookData): engine.add_order_book_data(data=data["data"]) elif data["type"] in (InstrumentStatusUpdate, ): engine.add_data(data=data["data"]) elif not is_nautilus_class(data["type"]): engine.add_generic_data(client_id=data["client_id"], data=data["data"]) else: raise ValueError( f"Data type {data['type']} not setup for loading into backtest engine" )
def test_is_nautilus_class(cls, is_nautilus): # Arrange, Act, Assert assert is_nautilus_class(cls) is is_nautilus