How to use the qstrader.broker.portfolio.portfolio_event.PortfolioEvent function in qstrader

To help you get started, we’ve selected a few qstrader examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
with pytest.raises(ValueError):
        port_raise.withdraw_funds(start_dt, neg_cash)

    # Test withdraw_funds raises for not enough cash
    port_broke = Portfolio(start_dt)
    port_broke.subscribe_funds(later_dt, 1000.0)

    with pytest.raises(ValueError):
        port_broke.withdraw_funds(later_dt, 2000.0)

    # Test withdraw_funds correctly subtracts positive
    # amount, generates correct event and modifies time
    # Initial subscribe
    port_cor = Portfolio(start_dt)
    port_cor.subscribe_funds(later_dt, pos_cash)
    pe_sub = PortfolioEvent(
        dt=later_dt, type='subscription',
        description="SUBSCRIPTION", debit=0.0,
        credit=1000.0, balance=1000.0
    )
    assert port_cor.total_cash == 1000.0
    assert port_cor.total_non_cash_equity == 0.0
    assert port_cor.total_equity == 1000.0
    assert port_cor.history == [pe_sub]
    assert port_cor.current_dt == later_dt

    # Now withdraw
    port_cor.withdraw_funds(even_later_dt, 468.0)
    pe_wdr = PortfolioEvent(
        dt=even_later_dt, type='withdrawal',
        description="WITHDRAWAL", debit=468.0,
        credit=0.0, balance=532.0
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
tn_even_later = Transaction(
        asset=asset.symbol,
        quantity=100,
        dt=even_later_dt,
        price=567.0,
        order_id=1,
        commission=15.78
    )
    port.transact_asset(tn_even_later)

    assert port.total_cash == 43284.22
    assert port.total_non_cash_equity == 56700.00
    assert port.total_equity == 99984.22

    description = "LONG 100 EQ:AAA 567.00 07/10/2017"
    pe_tn = PortfolioEvent(
        dt=even_later_dt, type="asset_transaction",
        description=description, debit=56715.78,
        credit=0.0, balance=43284.22
    )

    assert port.history == [pe_sub1, pe_sub2, pe_tn]
    assert port.current_dt == even_later_dt
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
with pytest.raises(ValueError):
        port.subscribe_funds(earlier_dt, pos_cash)

    # Test subscribe_funds raises for negative amount
    with pytest.raises(ValueError):
        port.subscribe_funds(start_dt, neg_cash)

    # Test subscribe_funds correctly adds positive
    # amount, generates correct event and modifies time
    port.subscribe_funds(later_dt, pos_cash)

    assert port.total_cash == 3000.0
    assert port.total_non_cash_equity == 0.0
    assert port.total_equity == 3000.0

    pe1 = PortfolioEvent(
        dt=start_dt, type='subscription',
        description="SUBSCRIPTION", debit=0.0,
        credit=2000.0, balance=2000.0
    )
    pe2 = PortfolioEvent(
        dt=later_dt, type='subscription',
        description="SUBSCRIPTION", debit=0.0,
        credit=1000.0, balance=3000.0
    )

    assert port.history == [pe1, pe2]
    assert port.current_dt == later_dt
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
port.subscribe_funds(start_dt, neg_cash)

    # Test subscribe_funds correctly adds positive
    # amount, generates correct event and modifies time
    port.subscribe_funds(later_dt, pos_cash)

    assert port.total_cash == 3000.0
    assert port.total_non_cash_equity == 0.0
    assert port.total_equity == 3000.0

    pe1 = PortfolioEvent(
        dt=start_dt, type='subscription',
        description="SUBSCRIPTION", debit=0.0,
        credit=2000.0, balance=2000.0
    )
    pe2 = PortfolioEvent(
        dt=later_dt, type='subscription',
        description="SUBSCRIPTION", debit=0.0,
        credit=1000.0, balance=3000.0
    )

    assert port.history == [pe1, pe2]
    assert port.current_dt == later_dt
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
port_cor = Portfolio(start_dt)
    port_cor.subscribe_funds(later_dt, pos_cash)
    pe_sub = PortfolioEvent(
        dt=later_dt, type='subscription',
        description="SUBSCRIPTION", debit=0.0,
        credit=1000.0, balance=1000.0
    )
    assert port_cor.total_cash == 1000.0
    assert port_cor.total_non_cash_equity == 0.0
    assert port_cor.total_equity == 1000.0
    assert port_cor.history == [pe_sub]
    assert port_cor.current_dt == later_dt

    # Now withdraw
    port_cor.withdraw_funds(even_later_dt, 468.0)
    pe_wdr = PortfolioEvent(
        dt=even_later_dt, type='withdrawal',
        description="WITHDRAWAL", debit=468.0,
        credit=0.0, balance=532.0
    )
    assert port_cor.total_cash == 532.0
    assert port_cor.total_non_cash_equity == 0.0
    assert port_cor.total_equity == 532.0
    assert port_cor.history == [pe_sub, pe_wdr]
    assert port_cor.current_dt == even_later_dt
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
price=567.0,
        order_id=1,
        commission=0.0
    )
    with pytest.raises(ValueError):
        port.transact_asset(tn_early)

    # Test transact_asset raises for transaction total
    # cost exceeding total cash
    port.subscribe_funds(later_dt, 1000.0)

    assert port.total_cash == 1000.0
    assert port.total_non_cash_equity == 0.0
    assert port.total_equity == 1000.0

    pe_sub1 = PortfolioEvent(
        dt=later_dt, type='subscription',
        description="SUBSCRIPTION", debit=0.0,
        credit=1000.0, balance=1000.0
    )
    tn_large = Transaction(
        asset=asset.symbol,
        quantity=100,
        dt=later_dt,
        price=567.0,
        order_id=1,
        commission=15.78
    )
    with pytest.raises(ValueError):
        port.transact_asset(tn_large)

    # Test correct total_cash and total_securities_value
github mhallsmoore / qstrader / qstrader / broker / portfolio / portfolio.py View on Github external
"""
        Initialise the portfolio with a (default) currency Cash Asset
        with quantity equal to 'starting_cash'.
        """
        cash_position = Position(
            self.cash_position_key,
            self.starting_cash,
            book_cost_pu=1.0,
            current_price=1.0,
            current_dt=self.current_dt
        )
        self.pos_handler.positions[self.cash_position_key] = cash_position

        if self.starting_cash > 0.0:
            self.history.append(
                PortfolioEvent.create_subscription(
                    self.current_dt, self.starting_cash, self.starting_cash
                )
            )

        self.logger.info(
            '(%s) Funds subscribed to portfolio "%s" '
            '- Credit: %0.2f, Balance: %0.2f' % (
                self.current_dt.strftime(settings.LOGGING["DATE_FORMAT"]),
                self.portfolio_id,
                round(self.starting_cash, 2),
                round(self.starting_cash, 2)
            )
github mhallsmoore / qstrader / qstrader / broker / portfolio / portfolio.py View on Github external
new_cash_quantity = cash_position.quantity - txn_total_cost
        self.pos_handler.update_position(
            self.cash_position_key,
            quantity=new_cash_quantity,
            current_dt=self.current_dt
        )

        # Form Portfolio history details
        direction = "LONG" if txn.direction > 0 else "SHORT"
        description = "%s %s %s %0.2f %s" % (
            direction, txn.quantity, txn.asset.upper(),
            txn.price, datetime.datetime.strftime(txn.dt, "%d/%m/%Y")
        )
        if direction == "LONG":
            pe = PortfolioEvent(
                dt=txn.dt, type='asset_transaction',
                description=description,
                debit=round(txn_total_cost, 2), credit=0.0,
                balance=round(self.total_cash, 2)
            )
            self.logger.info(
                '(%s) Asset "%s" transacted LONG in portfolio "%s" '
                '- Debit: %0.2f, Balance: %0.2f' % (
                    txn.dt.strftime(settings.LOGGING["DATE_FORMAT"]),
                    txn.asset, self.portfolio_id,
                    round(txn_total_cost, 2), round(self.total_cash, 2)
                )
            )
        else:
            pe = PortfolioEvent(
                dt=txn.dt, type='asset_transaction',
github mhallsmoore / qstrader / qstrader / broker / portfolio / portfolio.py View on Github external
pe = PortfolioEvent(
                dt=txn.dt, type='asset_transaction',
                description=description,
                debit=round(txn_total_cost, 2), credit=0.0,
                balance=round(self.total_cash, 2)
            )
            self.logger.info(
                '(%s) Asset "%s" transacted LONG in portfolio "%s" '
                '- Debit: %0.2f, Balance: %0.2f' % (
                    txn.dt.strftime(settings.LOGGING["DATE_FORMAT"]),
                    txn.asset, self.portfolio_id,
                    round(txn_total_cost, 2), round(self.total_cash, 2)
                )
            )
        else:
            pe = PortfolioEvent(
                dt=txn.dt, type='asset_transaction',
                description=description,
                debit=0.0, credit=-1.0 * round(txn_total_cost, 2),
                balance=round(self.total_cash, 2)
            )
            self.logger.info(
                '(%s) Asset "%s" transacted SHORT in portfolio "%s" '
                '- Credit: %0.2f, Balance: %0.2f' % (
                    txn.dt.strftime(settings.LOGGING["DATE_FORMAT"]),
                    txn.asset, self.portfolio_id,
                    -1.0 * round(txn_total_cost, 2), round(self.total_cash, 2)
                )
            )
        self.history.append(pe)