Example #1
0
    def _get_positions_from_broker(self):
        """
        get the positions from the broker and update zipline objects ( the ledger )
        should be used once at startup and once every time we want to refresh the positions array
        """
        cur_pos_in_tracker = self.metrics_tracker.positions
        positions = self._api.list_positions()
        for ap_position in positions:
            # ap_position = positions[symbol]
            try:
                z_position = zp.Position(
                    zp.InnerPosition(symbol_lookup(ap_position.symbol)))
                editable_position = zp.MutableView(z_position)
            except SymbolNotFound:
                # The symbol might not have been ingested to the db therefore
                # it needs to be skipped.
                log.warning(
                    'Wanted to subscribe to %s, but this asset is probably not ingested'
                    % ap_position.symbol)
                continue
            if int(ap_position.qty) == 0:
                continue
            editable_position._underlying_position.amount = int(
                ap_position.qty)
            editable_position._underlying_position.cost_basis = float(
                ap_position.avg_entry_price)
            editable_position._underlying_position.last_sale_price = float(
                ap_position.current_price)
            editable_position._underlying_position.last_sale_date = self._api.get_last_trade(
                ap_position.symbol).timestamp

            self.metrics_tracker.update_position(
                z_position.asset,
                amount=z_position.amount,
                last_sale_price=z_position.last_sale_price,
                last_sale_date=z_position.last_sale_date,
                cost_basis=z_position.cost_basis)

        # now let's sync the positions in the internal zipline objects
        position_names = [p.symbol for p in positions]
        assets_to_update = [
        ]  # separate list to not change list while iterating
        for asset in cur_pos_in_tracker:
            if asset.symbol not in position_names:
                assets_to_update.append(asset)
        for asset in assets_to_update:
            # deleting object from the metrics_tracker as its not in the portfolio
            self.metrics_tracker.update_position(asset, amount=0)
        # for some reason, the metrics tracker has self.positions AND self.portfolio.positions. let's make sure
        # these objects are consistent
        self.metrics_tracker._ledger._portfolio.positions = self.metrics_tracker.positions
Example #2
0
 def __init__(
     self, asset, amount=0, cost_basis=0.0, last_sale_price=0.0, last_sale_date=None
 ):
     inner = zp.InnerPosition(
         asset=asset,
         amount=amount,
         cost_basis=cost_basis,
         last_sale_price=last_sale_price,
         last_sale_date=last_sale_date,
         pnl_realized=PnlRealized(),
         lots=set(),
     )
     object.__setattr__(self, "inner_position", inner)
     object.__setattr__(self, "protocol_position", zp.Position(inner))
Example #3
0
 def __init__(self,
              asset,
              amount=0,
              cost_basis=0.0,
              last_sale_price=0.0,
              last_sale_date=None):
     inner = zp.InnerPosition(
         asset=asset,
         amount=amount,
         cost_basis=cost_basis,
         last_sale_price=last_sale_price,
         last_sale_date=last_sale_date,
     )
     object.__setattr__(self, 'inner_position', inner)
     object.__setattr__(self, 'protocol_position', zp.Position(inner))
Example #4
0
 def _get_positions_from_broker(self):
     """
     get the positions from the broker and update zipline objects ( the ledger )
     should be used once at startup and once every time we want to refresh the positions array
     """
     cur_pos_in_tracker = self.metrics_tracker.positions
     for symbol in self._tws.positions:
         ib_position = self._tws.positions[symbol]
         try:
             z_position = zp.Position(
                 zp.InnerPosition(symbol_lookup(symbol)))
             editable_position = MutableView(z_position)
         except SymbolNotFound:
             # The symbol might not have been ingested to the db therefore
             # it needs to be skipped.
             log.warning(
                 'Wanted to subscribe to %s, but this asset is probably not ingested'
                 % symbol)
             continue
         editable_position._underlying_position.amount = int(
             ib_position.position)
         editable_position._underlying_position.cost_basis = float(
             ib_position.average_cost)
         # Check if symbol exists in bars df
         if symbol in self._tws.bars:
             editable_position._underlying_position.last_sale_price = \
                 float(self._tws.bars[symbol].last_trade_price.iloc[-1])
             editable_position._underlying_position.last_sale_date = \
                 self._tws.bars[symbol].index.values[-1]
         else:
             # editable_position._underlying_position.last_sale_price = None  # this cannot be set to None. only numbers.
             editable_position._underlying_position.last_sale_date = None
         self.metrics_tracker.update_position(
             z_position.asset,
             amount=z_position.amount,
             last_sale_price=z_position.last_sale_price,
             last_sale_date=z_position.last_sale_date,
             cost_basis=z_position.cost_basis)
     for asset in cur_pos_in_tracker:
         if asset.symbol not in self._tws.positions:
             # deleting object from the metrcs_tracker as its not in the portfolio
             self.metrics_tracker.update_position(asset, amount=0)
     # for some reason, the metrics tracker has self.positions AND self.portfolio.positions. let's make sure
     # these objects are consistent
     # (self.portfolio.positions is self.metrics_tracker._ledger._portfolio.positions)
     # (self.metrics_tracker.positions is self.metrics_tracker._ledger.position_tracker.positions)
     self.metrics_tracker._ledger._portfolio.positions = self.metrics_tracker.positions
    def _get_positions_from_broker(self):
        """
        get the positions from the broker and update zipline objects ( the ledger )
        should be used once at startup and once every time we want to refresh the positions array
        """
        cur_pos_in_tracker = self.metrics_tracker.positions
        for ib_symbol in self._tws.ib_positions:
            ib_position = self._tws.ib_positions[ib_symbol]

            equity = self._safe_symbol_lookup(ib_symbol)
            if not equity:
                log.warning(
                    'Wanted to subscribe to %s, but this asset is probably not ingested'
                    % ib_symbol)
                continue

            zp_position = zp.Position(zp.InnerPosition(equity))
            editable_position = MutableView(zp_position)

            editable_position._underlying_position.amount = int(
                ib_position.position)
            editable_position._underlying_position.cost_basis = float(
                ib_position.average_cost)
            editable_position._underlying_position.last_sale_price = ib_position.market_price
            last_close = self.metrics_tracker._trading_calendar.session_close(
                self.metrics_tracker._last_session)
            editable_position._underlying_position.last_sale_date = last_close

            self.metrics_tracker.update_position(
                zp_position.asset,
                amount=zp_position.amount,
                last_sale_price=zp_position.last_sale_price,
                last_sale_date=zp_position.last_sale_date,
                cost_basis=zp_position.cost_basis)
        for asset in cur_pos_in_tracker:
            ib_symbol = self._asset_symbol(asset)
            if ib_symbol not in self._tws.ib_positions:
                # deleting object from the metrcs_tracker as its not in the portfolio
                self.metrics_tracker.update_position(asset, amount=0)

        self.metrics_tracker._ledger._portfolio.positions = zp.Positions(
            self.metrics_tracker.positions)