How to use the qstrader.broker.portfolio.portfolio.Portfolio 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
later_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    even_later_dt = pd.Timestamp('2017-10-07 08:00:00', tz=pytz.UTC)
    pos_cash = 1000.0
    neg_cash = -1000.0
    port_raise = Portfolio(start_dt)

    # Test withdraw_funds raises for incorrect datetime
    with pytest.raises(ValueError):
        port_raise.withdraw_funds(earlier_dt, pos_cash)

    # Test withdraw_funds raises for negative amount
    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
github mhallsmoore / qstrader / tests / unit / broker / test_simulated_broker.py View on Github external
* If portfolio_id already in the dictionary keys,
    raise ValueError
    * If it isn't, check that they portfolio and open
    orders dictionary was created correctly.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    exchange = ExchangeMock()
    data_handler = DataHandlerMock()

    sb = SimulatedBroker(start_dt, exchange, data_handler)

    # If portfolio_id isn't in the dictionary, then check it
    # was created correctly, along with the orders dictionary
    sb.create_portfolio(portfolio_id=1234, name="My Portfolio")
    assert "1234" in sb.portfolios
    assert isinstance(sb.portfolios["1234"], Portfolio)
    assert "1234" in sb.open_orders
    assert isinstance(sb.open_orders["1234"], queue.Queue)

    # If portfolio is already in the dictionary
    # then raise ValueError
    with pytest.raises(ValueError):
        sb.create_portfolio(
            portfolio_id=1234, name="My Portfolio"
        )
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
def test_portfolio_to_dict_for_two_holdings():
    """
    Test portfolio_to_dict for two holdings.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    asset1_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    asset2_dt = pd.Timestamp('2017-10-07 08:00:00', tz=pytz.UTC)
    update_dt = pd.Timestamp('2017-10-08 08:00:00', tz=pytz.UTC)
    asset1 = Equity("AAA Inc.", "EQ:AAA", tax_exempt=False)
    asset2 = Equity("BBB Inc.", "EQ:BBB", tax_exempt=False)

    port = Portfolio(start_dt, portfolio_id='1234')
    port.subscribe_funds(start_dt, 100000.0)
    tn_asset1 = Transaction(
        asset=asset1.symbol, quantity=100, dt=asset1_dt,
        price=567.0, order_id=1, commission=15.78
    )
    port.transact_asset(tn_asset1)

    tn_asset2 = Transaction(
        asset=asset2.symbol, quantity=100, dt=asset2_dt,
        price=123.0, order_id=2, commission=7.64
    )
    port.transact_asset(tn_asset2)

    port.update_market_value_of_asset(asset2.symbol, 134.0, update_dt)
    test_holdings = {
        asset1.symbol: {
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
def test_subscribe_funds_behaviour():
    """
    Test subscribe_funds raises for incorrect datetime
    Test subscribe_funds raises for negative amount
    Test subscribe_funds correctly adds positive
    amount, generates correct event and modifies time
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    earlier_dt = pd.Timestamp('2017-10-04 08:00:00', tz=pytz.UTC)
    later_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    pos_cash = 1000.0
    neg_cash = -1000.0
    port = Portfolio(start_dt, starting_cash=2000.0)

    # Test subscribe_funds raises for incorrect datetime
    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
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
def test_update_market_value_of_asset_negative_price():
    """
    Test update_market_value_of_asset for
    asset with negative price.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    later_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    port = Portfolio(start_dt)

    asset = Equity("Acme Inc.", "AAA", tax_exempt=False)
    port.subscribe_funds(later_dt, 100000.0)
    tn_asset = Transaction(
        asset=asset.symbol,
        quantity=100,
        dt=later_dt,
        price=567.0,
        order_id=1,
        commission=15.78
    )
    port.transact_asset(tn_asset)
    with pytest.raises(ValueError):
        port.update_market_value_of_asset(
            asset.symbol, -54.34, later_dt
        )
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
def test_update_market_value_of_asset_not_in_list():
    """
    Test update_market_value_of_asset for asset not in list.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    later_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    port = Portfolio(start_dt)
    asset = Equity("Acme Inc.", "AAA", tax_exempt=False)
    update = port.update_market_value_of_asset(
        asset, 54.34, later_dt
    )
    assert update is None
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
def test_initial_settings_for_default_portfolio():
    """
    Test that the initial settings are as they should be
    for two specified portfolios.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)

    # Test a default Portfolio
    port1 = Portfolio(start_dt)
    assert port1.start_dt == start_dt
    assert port1.current_dt == start_dt
    assert port1.currency == "USD"
    assert port1.starting_cash == 0.0
    assert port1.portfolio_id is None
    assert port1.name is None
    assert port1.total_non_cash_equity == 0.0
    assert port1.total_cash == 0.0
    assert port1.total_equity == 0.0

    # Test a Portfolio with keyword arguments
    port2 = Portfolio(
        start_dt, starting_cash=1234567.56, currency="USD",
        portfolio_id=12345, name="My Second Test Portfolio"
    )
    assert port2.start_dt == start_dt
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
def test_transact_asset_behaviour():
    """
    Test transact_asset raises for incorrect time
    Test transact_asset raises for transaction total
    cost exceeding total cash
    Test correct total_cash and total_securities_value
    for correct transaction (commission etc), correct
    portfolio event and correct time update
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    earlier_dt = pd.Timestamp('2017-10-04 08:00:00', tz=pytz.UTC)
    later_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    even_later_dt = pd.Timestamp('2017-10-07 08:00:00', tz=pytz.UTC)
    port = Portfolio(start_dt)
    asset = Equity("Acme Inc.", "EQ:AAA", tax_exempt=False)

    # Test transact_asset raises for incorrect time
    tn_early = Transaction(
        asset=asset.symbol,
        quantity=100,
        dt=earlier_dt,
        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
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
def test_update_market_value_of_asset_earlier_date():
    """
    Test update_market_value_of_asset for asset
    with current_trade_date in past
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    earlier_dt = pd.Timestamp('2017-10-04 08:00:00', tz=pytz.UTC)
    later_dt = pd.Timestamp('2017-10-06 08:00:00', tz=pytz.UTC)
    port = Portfolio(start_dt, portfolio_id='1234')

    asset = Equity("Acme Inc.", "EQ:AAA", tax_exempt=False)
    port.subscribe_funds(later_dt, 100000.0)
    tn_asset = Transaction(
        asset=asset.symbol,
        quantity=100,
        dt=later_dt,
        price=567.0,
        order_id=1,
        commission=15.78
    )
    port.transact_asset(tn_asset)
    with pytest.raises(ValueError):
        port.update_market_value_of_asset(
            asset.symbol, 50.23, earlier_dt
        )
github mhallsmoore / qstrader / tests / unit / broker / portfolio / test_portfolio.py View on Github external
start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)

    # Test a default Portfolio
    port1 = Portfolio(start_dt)
    assert port1.start_dt == start_dt
    assert port1.current_dt == start_dt
    assert port1.currency == "USD"
    assert port1.starting_cash == 0.0
    assert port1.portfolio_id is None
    assert port1.name is None
    assert port1.total_non_cash_equity == 0.0
    assert port1.total_cash == 0.0
    assert port1.total_equity == 0.0

    # Test a Portfolio with keyword arguments
    port2 = Portfolio(
        start_dt, starting_cash=1234567.56, currency="USD",
        portfolio_id=12345, name="My Second Test Portfolio"
    )
    assert port2.start_dt == start_dt
    assert port2.current_dt == start_dt
    assert port2.currency == "USD"
    assert port2.starting_cash == 1234567.56
    assert port2.portfolio_id == 12345
    assert port2.name == "My Second Test Portfolio"
    assert port2.total_equity == 1234567.56
    assert port2.total_non_cash_equity == 0.0
    assert port2.total_cash == 1234567.56