예제 #1
0
    def __init__(self,
                 starting_cash,
                 asset_finder,
                 period_open=None,
                 period_close=None,
                 keep_transactions=True,
                 keep_orders=False,
                 serialize_positions=True):

        self.asset_finder = asset_finder

        self.period_open = period_open
        self.period_close = period_close

        self.ending_value = 0.0
        self.ending_exposure = 0.0
        self.period_cash_flow = 0.0
        self.pnl = 0.0

        self.ending_cash = starting_cash
        # rollover initializes a number of self's attributes:
        self.rollover()
        self.keep_transactions = keep_transactions
        self.keep_orders = keep_orders

        # An object to recycle via assigning new values
        # when returning portfolio information.
        # So as not to avoid creating a new object for each event
        self._portfolio_store = zp.Portfolio()
        self._account_store = zp.Account()
        self.serialize_positions = serialize_positions

        # This dict contains the known cash flow multipliers for sids and is
        # keyed on sid
        self._execution_cash_flow_multipliers = {}
예제 #2
0
파일: period.py 프로젝트: syneric/zipline
    def __init__(self,
                 starting_cash,
                 period_open=None,
                 period_close=None,
                 keep_transactions=True,
                 keep_orders=False,
                 serialize_positions=True):

        self.period_open = period_open
        self.period_close = period_close

        self.ending_value = 0.0
        self.period_cash_flow = 0.0
        self.pnl = 0.0

        self.ending_cash = starting_cash
        # rollover initializes a number of self's attributes:
        self.rollover()
        self.keep_transactions = keep_transactions
        self.keep_orders = keep_orders

        # An object to recycle via assigning new values
        # when returning portfolio information.
        # So as not to avoid creating a new object for each event
        self._portfolio_store = zp.Portfolio()
        self._account_store = zp.Account()
        self.serialize_positions = serialize_positions
예제 #3
0
    def __init__(
            self,
            starting_cash,
            period_open=None,
            period_close=None,
            keep_transactions=True,
            keep_orders=False,
            serialize_positions=True):

        self.period_open = period_open
        self.period_close = period_close

        self.ending_value = 0.0
        self.period_cash_flow = 0.0
        self.pnl = 0.0
        # sid => position object
        self.positions = positiondict()
        self.ending_cash = starting_cash
        # rollover initializes a number of self's attributes:
        self.rollover()
        self.keep_transactions = keep_transactions
        self.keep_orders = keep_orders

        # Arrays for quick calculations of positions value
        self._position_amounts = pd.Series()
        self._position_last_sale_prices = pd.Series()

        self.calculate_performance()

        # An object to recycle via assigning new values
        # when returning portfolio information.
        # So as not to avoid creating a new object for each event
        self._portfolio_store = zp.Portfolio()
        self._positions_store = zp.Positions()
        self.serialize_positions = serialize_positions
예제 #4
0
 def portfolio(self):
     account = self._api.get_account()
     z_portfolio = zp.Portfolio()
     z_portfolio.cash = float(account.cash)
     z_portfolio.positions = self.positions
     z_portfolio.positions_value = float(
         account.portfolio_value) - float(account.cash)
     return z_portfolio
예제 #5
0
 def portfolio(self):
     account = self.api_client.Position.Position_get().result()
     z_portfolio = zp.Portfolio()
     z_portfolio.cash = float(account['cash'])
     z_portfolio.positions = self.positions
     z_portfolio.positions_value = float(account.portfolio_value) - float(
         account.cash)
     z_portfolio.portfolio_value = float(account.portfolio_value)
     return z_portfolio
예제 #6
0
    def __init__(self, trading_sessions, capital_base, data_frequency):
        if len(trading_sessions):
            start = trading_sessions[0]
        else:
            start = None

        # Have some fields of the portfolio changed? This should be accessed
        # through ``self._dirty_portfolio``
        self.__dirty_portfolio = False
        self._immutable_portfolio = zp.Portfolio(start, capital_base)
        self._portfolio = zp.MutableView(self._immutable_portfolio)

        self.daily_returns_series = pd.Series(
            np.nan,
            index=trading_sessions,
        )
        # Get a view into the storage of the returns series. Metrics
        # can access this directly in minute mode for performance reasons.
        self.daily_returns_array = self.daily_returns_series.values

        self._previous_total_returns = 0

        # this is a component of the cache key for the account
        self._position_stats = None

        # Have some fields of the account changed?
        self._dirty_account = True
        self._immutable_account = zp.Account()
        self._account = zp.MutableView(self._immutable_account)

        # The broker blotter can override some fields on the account. This is
        # way to tangled up at the moment but we aren't fixing it today.
        self._account_overrides = {}

        self.position_tracker = PositionTracker(data_frequency)

        self._processed_transactions = {}

        self._orders_by_modified = {}
        self._orders_by_id = OrderedDict()

        # Keyed by asset, the previous last sale price of positions with
        # payouts on price differences, e.g. Futures.
        #
        # This dt is not the previous minute to the minute for which the
        # calculation is done, but the last sale price either before the period
        # start, or when the price at execution.
        self._payout_last_sale_prices = {}

        self.fees = {
            'interest': 0.,
            'manager_fees': 0.,
        }
예제 #7
0
파일: period.py 프로젝트: StratiFi/zipline
    def __init__(
            self,
            starting_cash,
            asset_finder,
            data_frequency,
            period_open=None,
            period_close=None,
            keep_transactions=True,
            keep_orders=False,
            serialize_positions=True,
            name=None):

        self.asset_finder = asset_finder
        self.data_frequency = data_frequency

        # Start and end of the entire period
        self.period_open = period_open
        self.period_close = period_close

        self.initialize(starting_cash=starting_cash,
                        starting_value=0.0,
                        starting_exposure=0.0)

        self.ending_value = 0.0
        self.ending_exposure = 0.0
        self.ending_cash = starting_cash

        self.subperiod_divider = None

        # Keyed by asset, the previous last sale price of positions with
        # payouts on price differences, e.g. Futures.
        #
        # This dt is not the previous minute to the minute for which the
        # calculation is done, but the last sale price either before the period
        # start, or when the price at execution.
        self._payout_last_sale_prices = {}

        self.keep_transactions = keep_transactions
        self.keep_orders = keep_orders

        self.name = name

        # An object to recycle via assigning new values
        # when returning portfolio information.
        # So as not to avoid creating a new object for each event
        self._portfolio_store = zp.Portfolio()
        self._account_store = zp.Account()
        self.serialize_positions = serialize_positions

        # This dict contains the known cash flow multipliers for sids and is
        # keyed on sid
        self._execution_cash_flow_multipliers = {}
예제 #8
0
    def portfolio(self):
        z_portfolio = protocol.Portfolio()
        data = self._client.query_data(BALANCE)
        z_portfolio.capital_used = None  # TODO
        z_portfolio.starting_cash = None
        z_portfolio.portfolio_value = data[" 总资产"].values[0]
        z_portfolio.pnl = None
        z_portfolio.returns = None
        z_portfolio.cash = data["可用资金"].values[0]
        z_portfolio.start_date = None
        z_portfolio.positions = self.positions
        z_portfolio.positions_value = data["最新市值"].values[0]
        z_portfolio.position_exposure = z_portfolio.positions_value / (z_portfolio.positions_value + z_portfolio.cash)

        return z_portfolio
예제 #9
0
    def portfolio(self):
        z_portfolio = protocol.Portfolio()
        pfo = self._shipane_client.portfolio()
        if isinstance(pfo, list):
            pfo = TdxPortfolio(*pfo)
        z_portfolio.capital_used = None  # TODO
        z_portfolio.starting_cash = None
        z_portfolio.portfolio_value = pfo.portfolio_value
        z_portfolio.pnl = None
        z_portfolio.returns = None
        z_portfolio.cash = pfo.cash
        z_portfolio.start_date = None
        z_portfolio.positions = self.positions
        z_portfolio.positions_value = pfo.positions_value
        z_portfolio.position_exposure = z_portfolio.positions_value / (z_portfolio.positions_value + z_portfolio.cash)

        return z_portfolio
예제 #10
0
    def portfolio(self):
        ib_account = self._tws.accounts[self.account_id][self.currency]

        z_portfolio = zp.Portfolio()
        z_portfolio.capital_used = None  # TODO(tibor)
        z_portfolio.starting_cash = None  # TODO(tibor): Fill from state
        z_portfolio.portfolio_value = float(ib_account['EquityWithLoanValue'])
        z_portfolio.pnl = (float(ib_account['RealizedPnL']) +
                           float(ib_account['UnrealizedPnL']))
        z_portfolio.returns = None  # TODO(tibor): pnl / total_at_start
        z_portfolio.cash = float(ib_account['TotalCashValue'])
        z_portfolio.start_date = None  # TODO(tibor)
        z_portfolio.positions = self.positions
        z_portfolio.positions_value = None  # TODO(tibor)
        z_portfolio.positions_exposure = None  # TODO(tibor)

        return z_portfolio
예제 #11
0
    def __init__(self,
                 starting_cash,
                 period_open=None,
                 period_close=None,
                 keep_transactions=True,
                 keep_orders=False,
                 serialize_positions=True):

        self.period_open = period_open
        self.period_close = period_close

        self.ending_value = 0.0
        self.period_cash_flow = 0.0
        self.pnl = 0.0
        # sid => position object
        self.positions = positiondict()
        self.starting_value = 0.0
        # cash balance at start of period
        self.starting_cash = starting_cash
        self.ending_cash = starting_cash
        self.keep_transactions = keep_transactions
        self.processed_transactions = defaultdict(list)
        self.keep_orders = keep_orders
        self.orders_by_modified = defaultdict(list)
        self.orders_by_id = OrderedDict()
        self.cumulative_capital_used = 0.0
        self.max_capital_used = 0.0
        self.max_leverage = 0.0

        # Maps position to following array indexes
        self._position_index_map = {}
        # Arrays for quick calculations of positions value
        self._position_amounts = np.array([])
        self._position_last_sale_prices = np.array([])

        self.calculate_performance()

        # An object to recycle via assigning new values
        # when returning portfolio information.
        # So as not to avoid creating a new object for each event
        self._portfolio_store = zp.Portfolio()
        self._positions_store = zp.Positions()
        self.serialize_positions = serialize_positions
예제 #12
0
    def portfolio(self):
        latest_portfolio = self.get_latest_portfolio_info()

        z_portfolio = zp.Portfolio()
        z_portfolio.starting_cash = latest_portfolio['starting_cash']
        z_portfolio.portfolio_value = latest_portfolio['portfolio_value']
        z_portfolio.pnl = latest_portfolio['pnl']
        z_portfolio.returns = latest_portfolio['returns']
        z_portfolio.cash = latest_portfolio['cash']
        z_portfolio.start_date = latest_portfolio['start_date']
        z_portfolio.positions = self.positions
        # TODO: calculate position value
        z_portfolio.positions_value = get_latest_position_value(
            z_portfolio.positions)
        if z_portfolio.positions_value == 0:
            z_portfolio.positions_exposure = 0
        else:
            z_portfolio.positions_exposure \
                = (z_portfolio.positions_value / (z_portfolio.positions_value + z_portfolio.cash))

        return z_portfolio