'Initialize sids and other state variables.
:Arguments:
:Optional:
initialize : function
Function that is called with a single
argument at the begninning of the simulation.
handle_data : function
Function that is called with 2 arguments
(context and data) on every bar.
script : str
Algoscript that contains initialize and
handle_data function definition.
data_frequency : {\'daily\', \'minute\'}
The duration of the bars.
capital_base : float <default: 1.0e5>
How much capital to start with.
asset_finder : An AssetFinder object
A new AssetFinder object to be used in this TradingEnvironment
equities_metadata : can be either:
- dict
- pandas.DataFrame
- object with \'read\' property
If dict is provided, it must have the following structure:
* keys are the identifiers
* values are dicts containing the metadata, with the metadata
field name as the key
If pandas.DataFrame is provided, it must have the
following structure:
* column names must be the metadata fields
* index must be the different asset identifiers
* array contents should be the metadata value
If an object with a \'read\' property is provided, \'read\' must
return rows containing at least one of \'sid\' or \'symbol\' along
with the other metadata fields.
identifiers : List
Any asset identifiers that are not provided in the
equities_metadata, but will be traded by this TradingAlgorithm'

def __init__(self, *args, **kwargs):

    self.sources = []
    self.trading_controls = []
    self.account_controls = []
    self._recorded_vars = {}
    self.namespace = kwargs.pop('namespace', {})
    self._platform = kwargs.pop('platform', 'zipline')
    self.logger = None
    self.data_portal = kwargs.pop('data_portal', None)
    self.trading_environment = kwargs.pop('env', None)
    if (self.trading_environment is None):
        self.trading_environment = TradingEnvironment()
    if (('equities_metadata' in kwargs) or ('futures_metadata' in kwargs)):
        warnings.warn('passing    metadata    to    TradingAlgorithm    is    deprecated;    please    write    this    data    into    the    asset    db    before    passing    it    to    the    trading    environment', DeprecationWarning, stacklevel=1)
        self.trading_environment.write_data(equities=kwargs.pop('equities_metadata', None), futures=kwargs.pop('futures_metadata', None))
    self.trading_calendar = kwargs.pop('trading_calendar', get_calendar('NYSE'))
    self.sim_params = kwargs.pop('sim_params', None)
    if (self.sim_params is None):
        self.sim_params = create_simulation_parameters(start=kwargs.pop('start', None), end=kwargs.pop('end', None), trading_calendar=self.trading_calendar)
    self.perf_tracker = None
    self.asset_finder = self.trading_environment.asset_finder
    self.init_engine(kwargs.pop('get_pipeline_loader', None))
    self._pipelines = {}
    self._pipeline_cache = CachedObject.expired()
    self.blotter = kwargs.pop('blotter', None)
    self.cancel_policy = kwargs.pop('cancel_policy', NeverCancel())
    if (not self.blotter):
        self.blotter = Blotter(data_frequency=self.data_frequency, cancel_policy=self.cancel_policy)
    self._symbol_lookup_date = None
    self.portfolio_needs_update = True
    self.account_needs_update = True
    self.performance_needs_update = True
    self._portfolio = None
    self._account = None
    self.algoscript = kwargs.pop('script', None)
    self._initialize = None
    self._before_trading_start = None
    self._analyze = None
    self._in_before_trading_start = False
    self.event_manager = EventManager(create_context=kwargs.pop('create_event_context', None))
    self._handle_data = None
    def noop(*args, **kwargs):
        pass
    if (self.algoscript is not None):
        api_methods = {'initialize', 'handle_data', 'before_trading_start', 'analyze'}
        unexpected_api_methods = (viewkeys(kwargs) & api_methods)
        if unexpected_api_methods:
            raise ValueError('TradingAlgorithm    received    a    script    and    the    following    API    methods    as    functions:\n{funcs}'.format(funcs=unexpected_api_methods))
        filename = kwargs.pop('algo_filename', None)
        if (filename is None):
            filename = '<string>'
        code = compile(self.algoscript, filename, 'exec')
        exec_(code, self.namespace)
        self._initialize = self.namespace.get('initialize', noop)
        self._handle_data = self.namespace.get('handle_data', noop)
        self._before_trading_start = self.namespace.get('before_trading_start')
        self._analyze = self.namespace.get('analyze')
    else:
        self._initialize = kwargs.pop('initialize', noop)
        self._handle_data = kwargs.pop('handle_data', noop)
        self._before_trading_start = kwargs.pop('before_trading_start', None)
        self._analyze = kwargs.pop('analyze', None)
    self.event_manager.add_event(zipline.utils.events.Event(zipline.utils.events.Always(), self.handle_data.__func__), prepend=True)
    if ('data_frequency' in kwargs):
        self.data_frequency = kwargs.pop('data_frequency')
    self.initialized = False
    self.initialize_args = args
    self.initialize_kwargs = kwargs
    self.benchmark_sid = kwargs.pop('benchmark_sid', None)
    self.capital_changes = kwargs.pop('capital_changes', {})
    self.capital_change_deltas = {}
    self.restrictions = NoRestrictions()
