text: #
  # Copyright 2013 Quantopian, Inc.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  from copy import copy
  from datetime import datetime
  from itertools import groupby, ifilter
  from operator import attrgetter
  
  import pytz
  import pandas as pd
  import numpy as np
  
  from alephnull.errors import (
      UnsupportedSlippageModel,
      OverrideSlippagePostInit,
      UnsupportedCommissionModel,
      OverrideCommissionPostInit
  )
  from alephnull.finance.performance import PerformanceTracker, FuturesPerformanceTracker
  from alephnull.sources import DataFrameSource, DataPanelSource
  from alephnull.utils.factory import create_simulation_parameters
  from alephnull.transforms.utils import StatefulTransform
  from alephnull.finance.slippage import (
      VolumeShareSlippage,
      SlippageModel,
      transact_partial
  )
  from alephnull.finance.commission import PerShare, PerTrade
  from alephnull.finance.blotter import Blotter
  from alephnull.finance.constants import ANNUALIZER
  import alephnull.finance.trading as trading
  import alephnull.protocol
  from alephnull.protocol import Event
  from alephnull.gens.composites import (
      date_sorted_sources,
      sequential_transforms,
      alias_dt
  )
  from alephnull.gens.tradesimulation import AlgorithmSimulator
  
  
  DEFAULT_CAPITAL_BASE = float("1.0e5")
  
  
  class AssetTypeEnum(object):
      def __init__(self):
          self.EQUITY = 0
          self.FUTURES = 1
  
  
  class TradingAlgorithm(object):
      """
      Base class for trading algorithms. Inherit and overload
      initialize() and handle_data(data).
  
      A new algorithm could look like this:
      ```
      class MyAlgo(TradingAlgorithm):
          def initialize(self, sids, amount):
              self.sids = sids
              self.amount = amount
  
          def handle_data(self, data):
              sid = self.sids[0]
              amount = self.amount
              self.order(sid, amount)
      ```
      To then to run this algorithm:
  
      my_algo = MyAlgo([0], 100) # first argument has to be list of sids
      stats = my_algo.run(data)
  
      """
      asset_types = AssetTypeEnum()
  
      def __init__(self, *args, **kwargs):
          """Initialize sids and other state variables.
  
          :Arguments:
              data_frequency : str (daily, hourly or minutely)
                 The duration of the bars.
              annualizer : int <optional>
                 Which constant to use for annualizing risk metrics.
                 If not provided, will extract from data_frequency.
              capital_base : float <default: 1.0e5>
                 How much capital to start with.
          """
  
          self._portfolio = None
          self.datetime = None
  
          # can be reset in a subclass's initialize() method
          self.asset_type = self.asset_types.EQUITY
  
          self.registered_transforms = {}
          self.transforms = []
          self.sources = []
  
          self._recorded_vars = {}
  
          self.logger = None
  
          self.benchmark_return_source = None
  
          # default components for transact
          self.slippage = VolumeShareSlippage()
          self.commission = PerShare()
  
          if 'data_frequency' in kwargs:
              self.set_data_frequency(kwargs.pop('data_frequency'))
          else:
              self.data_frequency = None
  
          self.instant_fill = kwargs.pop('instant_fill', False)
  
          # Override annualizer if set
          if 'annualizer' in kwargs:
              self.annualizer = kwargs['annualizer']
  
          # set the capital base
          self.capital_base = kwargs.pop('capital_base', DEFAULT_CAPITAL_BASE)
  
          self.sim_params = kwargs.pop('sim_params', None)
          if self.sim_params:
              self.sim_params.data_frequency = self.data_frequency
  
          self.live_execution = kwargs.pop('live_execution', False)
  
          if self.live_execution:
              # Only import and instantiate an IB Execution instance
              # If it is explicitly requested in kwargs
              # Todo: [BUG FIX] object instantiates on import and
              # therefore connects to the IB API
  
              from alephnull.live.broker import LiveExecution
  
              self.live_execution = LiveExecution(call_msg=False)
  
              # reconcile algo with InteractiveBrokers
              self.capital_base = self.live_execution.total_cash()
              self._portfolio = self.live_execution.ib_portfolio()
              self._portfolio.cash = self._portfolio.starting_cash = self.capital_base
              self._portfolio.portfolio_value = self._portfolio.cash + \
                                                self._portfolio.positions_value
  
              kwargs['blotter'] = self.live_execution.blotter
  
          self.blotter = kwargs.pop('blotter', None)
          if not self.blotter:
              self.blotter = Blotter()
  
  
  
  
          # an algorithm subclass needs to set initialized to True when
          # it is fully initialized.
          self.initialized = False
  
          # call to user-defined constructor method
          self.initialize(*args, **kwargs)
  
      def __repr__(self):
          """
          N.B. this does not yet represent a string that can be used
          to instantiate an exact copy of an algorithm.
  
          However, it is getting close, and provides some value as something
          that can be inspected interactively.
          """
          return """
  {class_name}(
      capital_base={capital_base}
      sim_params={sim_params},
      initialized={initialized},
      slippage={slippage},
      commission={commission},
      blotter={blotter},
      recorded_vars={recorded_vars})
  """.strip().format(class_name=self.__class__.__name__,
                     capital_base=self.capital_base,
                     sim_params=repr(self.sim_params),
                     initialized=self.initialized,
                     slippage=repr(self.slippage),
                     commission=repr(self.commission),
                     blotter=repr(self.blotter),
                     recorded_vars=repr(self.recorded_vars))
  
      def _init_positions(self):
          for sid, pos in self._portfolio.positions.iteritems():
              for perf_period in self.perf_tracker.perf_periods:
                  perf_period.update_position(
                      sid=sid,
                      contract=pos.contract if hasattr(pos, 'contract') else None,
                      amount=pos.amount,
                      last_sale_price=pos.last_sale_price,
                      last_sale_date=None,
                      cost_basis=pos.cost_basis)
  
  
      def _create_data_generator(self, source_filter, sim_params):
          """
          Create a merged data generator using the sources and
          transforms attached to this algorithm.
  
          ::source_filter:: is a method that receives events in date
          sorted order, and returns True for those events that should be
          processed by the zipline, and False for those that should be
          skipped.
          """
          if self.benchmark_return_source is None:
              benchmark_return_source = [
                  Event({'dt': dt,
                         'returns': ret,
                         'type': alephnull.protocol.DATASOURCE_TYPE.BENCHMARK,
                         'source_id': 'benchmarks'})
                  for dt, ret in trading.environment.benchmark_returns.iterkv()
                  if dt.date() >= sim_params.period_start.date()
                  and dt.date() <= sim_params.period_end.date()
              ]
          else:
              benchmark_return_source = self.benchmark_return_source
  
          date_sorted = date_sorted_sources(*self.sources)
  
          if source_filter:
              date_sorted = ifilter(source_filter, date_sorted)
  
          with_tnfms = sequential_transforms(date_sorted,
                                             *self.transforms)
          with_alias_dt = alias_dt(with_tnfms)
  
          with_benchmarks = date_sorted_sources(benchmark_return_source,
                                                with_alias_dt)
  
          # Group together events with the same dt field. This depends on the
          # events already being sorted.
          return groupby(with_benchmarks, attrgetter('dt'))
  
      def _create_generator(self, sim_params, source_filter=None):
          """
          Create a basic generator setup using the sources and
          transforms attached to this algorithm.
  
          ::source_filter:: is a method that receives events in date
          sorted order, and returns True for those events that should be
          processed by the zipline, and False for those that should be
          skipped.
          """
          sim_params.data_frequency = self.data_frequency
          self.data_gen = self._create_data_generator(source_filter,
                                                      sim_params)
  
          # if live execution is active instantiate perf_tracker with
          # the portfolio downloaded from IB
  
          if self.asset_type == self.asset_types.EQUITY:
              self.perf_tracker = PerformanceTracker(sim_params)
  
          elif self.asset_type == self.asset_types.FUTURES:
              self.perf_tracker = FuturesPerformanceTracker(sim_params)
  
          else:
              self.perf_tracker = PerformanceTracker(sim_params)
  
          if self.live_execution:
              self._init_positions()
  
          self.trading_client = AlgorithmSimulator(self, sim_params)
  
          transact_method = transact_partial(self.slippage, self.commission)
          self.set_transact(transact_method)
          return self.trading_client.transform(self.data_gen)
  
      def get_generator(self):
          """
          Override this method to add new logic to the construction
          of the generator. Overrides can use the _create_generator
          method to get a standard construction generator.
          """
          return self._create_generator(self.sim_params)
  
      def initialize(self, *args, **kwargs):
          pass
  
      # TODO: make a new subclass, e.g. BatchAlgorithm, and move
      # the run method to the subclass, and refactor to put the
      # generator creation logic into get_generator.
      def run(self, source, sim_params=None, benchmark_return_source=None):
          """Run the algorithm.
  
          :Arguments:
              source : can be either:
                       - pandas.DataFrame
                       - zipline source
                       - list of zipline sources
  
                 If pandas.DataFrame is provided, it must have the
                 following structure:
                 * column names must consist of ints representing the
                   different sids
                 * index must be DatetimeIndex
                 * array contents should be price info.
  
          :Returns:
              daily_stats : pandas.DataFrame
                Daily performance metrics such as returns, alpha etc.
  
          """
          if isinstance(source, (list, tuple)):
              assert self.sim_params is not None or sim_params is not None, \
                  """When providing a list of sources, \
                  sim_params have to be specified as a parameter
                  or in the constructor."""
          elif isinstance(source, pd.DataFrame):
              # if DataFrame provided, wrap in DataFrameSource
              source = DataFrameSource(source)
          elif isinstance(source, pd.Panel):
              source = DataPanelSource(source)
  
          if not isinstance(source, (list, tuple)):
              self.sources = [source]
          else:
              self.sources = source
  
          # Check for override of sim_params.
          # If it isn't passed to this function,
          # use the default params set with the algorithm.
          # Else, we create simulation parameters using the start and end of the
          # source provided.
          if not sim_params:
              if not self.sim_params:
                  start = source.start
                  end = source.end
  
                  sim_params = create_simulation_parameters(
                      start=start,
                      end=end,
                      capital_base=self.capital_base
                  )
              else:
                  sim_params = self.sim_params
  
          # Create transforms by wrapping them into StatefulTransforms
          self.transforms = []
          for namestring, trans_descr in self.registered_transforms.iteritems():
              sf = StatefulTransform(
                  trans_descr['class'],
                  *trans_descr['args'],
                  **trans_descr['kwargs']
              )
              sf.namestring = namestring
  
              self.transforms.append(sf)
  
          # create transforms and zipline
          self.gen = self._create_generator(sim_params)
  
          # loop through simulated_trading, each iteration returns a
          # perf dictionary
          perfs = []
          for perf in self.gen:
              perfs.append(perf)
  
          # convert perf dict to pandas dataframe
          daily_stats = self._create_daily_stats(perfs)
  
          return daily_stats
  
      def _create_daily_stats(self, perfs):
          # create daily and cumulative stats dataframe
          daily_perfs = []
          # TODO: the loop here could overwrite expected properties
          # of daily_perf. Could potentially raise or log a
          # warning.
          for perf in perfs:
              if 'daily_perf' in perf:
  
                  perf['daily_perf'].update(
                      perf['daily_perf'].pop('recorded_vars')
                  )
                  daily_perfs.append(perf['daily_perf'])
              else:
                  self.risk_report = perf
  
          daily_dts = [np.datetime64(perf['period_close'], utc=True)
                       for perf in daily_perfs]
          daily_stats = pd.DataFrame(daily_perfs, index=daily_dts)
  
          return daily_stats
  
      def add_transform(self, transform_class, tag, *args, **kwargs):
          """Add a single-sid, sequential transform to the model.
  
          :Arguments:
              transform_class : class
                  Which transform to use. E.g. mavg.
              tag : str
                  How to name the transform. Can later be access via:
                  data[sid].tag()
  
          Extra args and kwargs will be forwarded to the transform
          instantiation.
  
          """
          self.registered_transforms[tag] = {'class': transform_class,
                                             'args': args,
                                             'kwargs': kwargs}
  
      def record(self, **kwargs):
          """
          Track and record local variable (i.e. attributes) each day.
          """
          for name, value in kwargs.items():
              self._recorded_vars[name] = value
  
      def order(self, sid, amount, limit_price=None, stop_price=None):
          self.blotter.update_account(self.portfolio)
          return self.blotter.order(sid, amount, limit_price, stop_price)
  
  
      def order_value(self, sid, value, limit_price=None, stop_price=None):
          last_price = self.trading_client.current_data[sid].price
          return self.blotter.order_value(sid, value, last_price,
                                          limit_price=limit_price,
                                          stop_price=stop_price)
  
      def get_open_orders(self, sid=None):
          """
          Return open order events
          """
  
          if sid is None:
              return {key: [order.to_api_obj() for order in orders]
                      for key, orders
                      in self.blotter.open_orders.iteritems()}
          if sid in self.blotter.open_orders:
              orders = self.blotter.open_orders[sid]
              return [order.to_api_obj() for order in orders]
          return []
  
      def get_orders(self, sid=None):
          """
          Return order events
          """
          orders = {id_: {(self.blotter.orders[id_].__dict__['sid'],
                           self.blotter.orders[id_].__dict__['contract']):
                              self.blotter.orders[id_].__dict__} for id_ in
                    self.blotter.orders.keys()}
          orders = [{sym: {key: v} for sym, v in orders[key].iteritems()} for key in orders.keys()]
          orders_flat = {}
          for d in orders:
              orders_flat.update(d)
          if sid:
              return orders_flat[sid]
          else:
              return orders_flat
  
  
      @property
      def recorded_vars(self):
          return copy(self._recorded_vars)
  
      @property
      def portfolio(self):
          return self._portfolio
  
      def set_portfolio(self, portfolio):
          self._portfolio = portfolio
  
      def set_logger(self, logger):
          self.logger = logger
  
      def set_datetime(self, dt):
          assert isinstance(dt, datetime), \
              "Attempt to set algorithm's current time with non-datetime"
          assert dt.tzinfo == pytz.utc, \
              "Algorithm expects a utc datetime"
          self.datetime = dt
  
      def get_datetime(self):
          """
          Returns a copy of the datetime.
          """
          date_copy = copy(self.datetime)
          assert date_copy.tzinfo == pytz.utc, \
              "Algorithm should have a utc datetime"
          return date_copy
  
      def set_transact(self, transact):
          """
          Set the method that will be called to create a
          transaction from open orders and trade events.
          """
          self.blotter.transact = transact
  
      def set_slippage(self, slippage):
          if not isinstance(slippage, SlippageModel):
              raise UnsupportedSlippageModel()
          if self.initialized:
              raise OverrideSlippagePostInit()
          self.slippage = slippage
  
      def set_commission(self, commission):
          if not isinstance(commission, (PerShare, PerTrade)):
              raise UnsupportedCommissionModel()
  
          if self.initialized:
              raise OverrideCommissionPostInit()
          self.commission = commission
  
      def set_sources(self, sources):
          assert isinstance(sources, list)
          self.sources = sources
  
      def set_transforms(self, transforms):
          assert isinstance(transforms, list)
          self.transforms = transforms
  
      def set_data_frequency(self, data_frequency):
          assert data_frequency in ('daily', 'minute')
          self.data_frequency = data_frequency
          self.annualizer = ANNUALIZER[self.data_frequency]
  
      def order_percent(self, sid, percent, limit_price=None, stop_price=None):
          """
          Place an order in the specified security corresponding to the given
          percent of the current portfolio value.
  
          Note that percent must expressed as a decimal (0.50 means 50\%).
          """
          value = self.portfolio.portfolio_value * percent
          return self.order_value(sid, value, limit_price, stop_price)
  
      def target(self, sid, target, limit_price=None, stop_price=None):
          """
          Place an order to adjust a position to a target number of shares. If
          the position doesn't already exist, this is equivalent to placing a new
          order. If the position does exist, this is equivalent to placing an
          order for the difference between the target number of shares and the
          current number of shares.
          """
          if sid in self.portfolio.positions:
              current_position = self.portfolio.positions[sid].amount
              req_shares = target - current_position
              return self.order(sid, req_shares, limit_price, stop_price)
          else:
              return self.order(sid, target, limit_price, stop_price)
  
      def target_value(self, sid, target, limit_price=None, stop_price=None):
          """
          Place an order to adjust a position to a target value. If
          the position doesn't already exist, this is equivalent to placing a new
          order. If the position does exist, this is equivalent to placing an
          order for the difference between the target value and the
          current value.
          """
          if sid in self.portfolio.positions:
              current_position = self.portfolio.positions[sid].amount
              current_price = self.portfolio.positions[sid].last_sale_price
              current_value = current_position * current_price
              req_value = target - current_value
              return self.order_value(sid, req_value, limit_price, stop_price)
          else:
              return self.order_value(sid, target, limit_price, stop_price)
  
      def target_percent(self, sid, target, limit_price=None, stop_price=None):
          """
          Place an order to adjust a position to a target percent of the
          current portfolio value. If the position doesn't already exist, this is
          equivalent to placing a new order. If the position does exist, this is
          equivalent to placing an order for the difference between the target
          percent and the current percent.
  
          Note that target must expressed as a decimal (0.50 means 50\%).
          """
          if sid in self.portfolio.positions:
              current_position = self.portfolio.positions[sid].amount
              current_price = self.portfolio.positions[sid].last_sale_price
              current_value = current_position * current_price
          else:
              current_value = 0
          target_value = self.portfolio.portfolio_value * target
  
          req_value = target_value - current_value
          return self.order_value(sid, req_value, limit_price, stop_price)
  