def __init__(self, *args, **kwargs): ''' data_frequency: 'minute' or 'daily' algoname: str, defaults to 'algo' backend: str or Backend instance, defaults to 'alpaca' (str is either backend module name under 'pylivetrader.backend', or global import path) trading_calendar: pd.DateIndex for trading calendar initialize: initialize function handle_data: handle_data function before_trading_start: before_trading_start function ''' self._recorded_vars = {} self.data_frequency = kwargs.pop('data_frequency', 'minute') assert self.data_frequency in ('minute', 'daily') self._algoname = kwargs.pop('algoname', 'algo') self._state_store = StateStore( kwargs.pop('statefile', None) or '{}-state.pkl'.format(self._algoname)) self._pipelines = {} backend_param = kwargs.pop('backend', 'alpaca') if not isinstance(backend_param, str): self._backend = backend_param self._backend_name = backend_param.__class__.__name__ else: self._backend_name = backend_param try: # First, tries to import official backend packages backendmod = importlib.import_module( 'pylivetrader.backend.{}'.format(self._backend_name)) except ImportError: # Then if failes, tries to find pkg in global package # namespace. try: backendmod = importlib.import_module(self._backend_name) except ImportError: raise RuntimeError( "Could not find backend package `{}`.".format( self._backend_name)) backend_options = kwargs.pop('backend_options', None) or {} self._backend = backendmod.Backend(**backend_options) self.asset_finder = AssetFinder(self._backend) self.trading_calendar = kwargs.pop('trading_calendar', get_calendar('NYSE')) self.data_portal = DataPortal(self._backend, self.asset_finder, self.trading_calendar) self.event_manager = EventManager() self.trading_controls = [] self.account_controls = [] self.restrictions = NoRestrictions() self._initialize = kwargs.pop('initialize', noop) self._handle_data = kwargs.pop('handle_data', noop) self._before_trading_start = kwargs.pop('before_trading_start', noop) self.event_manager.add_event( events.Event( events.Always(), # We pass handle_data.__func__ to get the unbound method. self.handle_data.__func__, ), prepend=True, ) self._account_needs_update = True self._portfolio_needs_update = True self._in_before_trading_start = False self._assets_from_source = [] self._context_persistence_excludes = [] self._max_shares = int(1e+11) self.initialized = False
def __init__(self, *args, **kwargs): ''' data_frequency: 'minute' or 'daily' algoname: str, defaults to 'algo' backend: str or Backend instance, defaults to 'alpaca' (str is either backend module name under 'pylivetrader.backend', or global import path) trading_calendar: pd.DateIndex for trading calendar initialize: initialize function handle_data: handle_data function before_trading_start: before_trading_start function log_level: 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL' storage_engine: 'file', 'redis' pipeline_hook: pipeline_output hook function to enable smoke like functionality. it is not meant to be used by the CLI ''' log.level = lookup_level(kwargs.pop('log_level', 'INFO')) self._recorded_vars = {} self.data_frequency = kwargs.pop('data_frequency', 'minute') assert self.data_frequency in ('minute', 'daily') self._algoname = kwargs.pop('algoname', 'algo') self.quantopian_compatible = kwargs.pop('quantopian_compatible', True) storage_engine = kwargs.pop('storage_engine', 'file') if storage_engine == 'redis': storage_engine = RedisStore() else: storage_engine = FileStore( kwargs.pop('statefile', None) or '{}-state.pkl'.format(self._algoname)) self._state_store = StateStore(storage_engine=storage_engine) self._pipelines = {} backend_param = kwargs.pop('backend', 'alpaca') if not isinstance(backend_param, str): self._backend = backend_param self._backend_name = backend_param.__class__.__name__ else: self._backend_name = backend_param try: # First, tries to import official backend packages backendmod = importlib.import_module( 'pylivetrader.backend.{}'.format(self._backend_name)) except ImportError: # Then if failes, tries to find pkg in global package # namespace. try: backendmod = importlib.import_module(self._backend_name) except ImportError: raise RuntimeError( "Could not find backend package `{}`.".format( self._backend_name)) self.backend_options = kwargs.pop('backend_options', None) or {} self._backend = backendmod.Backend(**self.backend_options) self.asset_finder = AssetFinder(self._backend) self.trading_calendar = kwargs.pop('trading_calendar', get_calendar('NYSE')) self.data_portal = DataPortal(self._backend, self.asset_finder, self.trading_calendar, self.quantopian_compatible) self.event_manager = EventManager() self.trading_controls = [] self.account_controls = [] self.restrictions = NoRestrictions() self._initialize = kwargs.pop('initialize', noop) self._handle_data = kwargs.pop('handle_data', noop) self._before_trading_start = kwargs.pop('before_trading_start', noop) self._pipeline_hook = kwargs.get('pipeline_hook') self.event_manager.add_event( events.Event( events.Always(), # We pass handle_data.__func__ to get the unbound method. self.handle_data.__func__, ), prepend=True, ) self._account_needs_update = True self._portfolio_needs_update = True self._in_before_trading_start = False self._assets_from_source = [] self._context_persistence_excludes = [] self._max_shares = int(1e+11) self.initialized = False self.api_methods = [ func for func in dir(Algorithm) if callable(getattr(Algorithm, func)) ]