Esempio n. 1
0
    def _get_history_window(self,
                            assets,
                            end_dt,
                            bar_count,
                            frequency,
                            field,
                            data_frequency,
                            ffill=True):
        exchange_assets = group_assets_by_exchange(assets)
        if len(exchange_assets) > 1:
            df_list = []
            for exchange_name in exchange_assets:
                assets = exchange_assets[exchange_name]

                df_exchange = self.get_exchange_history_window(
                    exchange_name, assets, end_dt, bar_count, frequency, field,
                    data_frequency, ffill)

                df_list.append(df_exchange)

            # Merging the values values of each exchange
            return pd.concat(df_list)

        else:
            exchange_name = list(exchange_assets.keys())[0]
            return self.get_exchange_history_window(exchange_name, assets,
                                                    end_dt, bar_count,
                                                    frequency, field,
                                                    data_frequency, ffill)
Esempio n. 2
0
    def synchronize_portfolio(self):
        """
        Synchronizes the portfolio tracked by the algorithm to refresh
        its current value.

        This includes updating the last_sale_price of all tracked
        positions, returning the available cash, and raising error
        if the data goes out of sync.

        Returns
        -------
        float
            The amount of base currency available for trading.

        float
            The total value of all tracked positions.

        """
        check_balances = (not self.simulate_orders)
        quote_currency = None
        tracker = self.perf_tracker.position_tracker
        total_cash = 0.0
        total_positions_value = 0.0

        # Position keys correspond to assets
        positions = self.portfolio.positions
        assets = list(positions)
        exchange_assets = group_assets_by_exchange(assets)
        for exchange_name in self.exchanges:
            assets = exchange_assets[exchange_name] \
                if exchange_name in exchange_assets else []

            exchange_positions = copy.deepcopy(
                [positions[asset] for asset in assets if asset in positions])

            exchange = self.exchanges[exchange_name]  # Type: Exchange

            if quote_currency is None:
                quote_currency = exchange.quote_currency

            orders = []
            for asset in self.blotter.open_orders:
                asset_orders = self.blotter.open_orders[asset]
                if asset_orders:
                    orders += asset_orders

            required_cash = self.portfolio.cash if not orders else None
            cash, positions_value = exchange.sync_positions(
                positions=exchange_positions,
                check_balances=check_balances,
                cash=required_cash,
            )
            total_cash += cash
            total_positions_value += positions_value

            # Applying modifications to the original positions
            for position in exchange_positions:
                tracker.update_position(
                    asset=position.asset,
                    amount=position.amount,
                    last_sale_date=position.last_sale_date,
                    last_sale_price=position.last_sale_price,
                )

        if not check_balances:
            total_cash = self.portfolio.cash

        return total_cash, total_positions_value