Esempio n. 1
0
def test_get_account_total_market_value():
    """
    Tests get_account_total_market_value method for:
    * The correct market values after cash is subscribed.
    """
    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)

    # Subscribe all necessary funds and create portfolios
    sb.subscribe_funds_to_account(300000.0)
    sb.create_portfolio(portfolio_id="1", name="My Portfolio #1")
    sb.create_portfolio(portfolio_id="2", name="My Portfolio #1")
    sb.create_portfolio(portfolio_id="3", name="My Portfolio #1")
    sb.subscribe_funds_to_portfolio("1", 100000.0)
    sb.subscribe_funds_to_portfolio("2", 100000.0)
    sb.subscribe_funds_to_portfolio("3", 100000.0)

    # Check that the market value is correct
    res_equity = sb.get_account_total_equity()
    test_equity = {
        "1": 100000.0,
        "2": 100000.0,
        "3": 100000.0,
        "master": 300000.0
    }
    assert res_equity == test_equity
Esempio n. 2
0
def test_withdraw_funds_from_portfolio():
    """
    Tests withdraw_funds_from_portfolio method for:
    * Raising ValueError with negative amount
    * Raising ValueError if portfolio does not exist
    * Raising ValueError for a lack of cash
    * Correctly setting cash_balances for a positive amount
    """
    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)

    # Raising ValueError with negative amount
    with pytest.raises(ValueError):
        sb.withdraw_funds_from_portfolio("1234", -4306.23)

    # Raising KeyError if portfolio doesn't exist
    with pytest.raises(KeyError):
        sb.withdraw_funds_from_portfolio("1234", 5432.12)

    # Add in cash balance to the account
    sb.create_portfolio(portfolio_id=1234, name="My Portfolio #1")
    sb.subscribe_funds_to_account(165303.23)
    sb.subscribe_funds_to_portfolio("1234", 100000.00)

    # Raising ValueError if not enough cash
    with pytest.raises(ValueError):
        sb.withdraw_funds_from_portfolio("1234", 200000.00)

    # If everything else worked, check balances are correct
    sb.withdraw_funds_from_portfolio("1234", 50000.00)
    assert sb.cash_balances[sb.base_currency] == 115303.23000000001
    assert sb.portfolios["1234"].total_cash == 50000.00
Esempio n. 3
0
def test_subscribe_funds_to_account():
    """
    Tests subscribe_funds_to_account method for:
    * Raising ValueError with negative amount
    * Correctly setting cash_balances for a positive amount
    """
    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)

    # Raising ValueError with negative amount
    with pytest.raises(ValueError):
        sb.subscribe_funds_to_account(-4306.23)

    # Correctly setting cash_balances for a positive amount
    sb.subscribe_funds_to_account(165303.23)
    assert sb.cash_balances[sb.base_currency] == 165303.23
Esempio n. 4
0
def test_get_portfolio_non_cash_equity():
    """
    Tests get_portfolio_non_cash_equity method for:
    * Raising ValueError if portfolio_id not in keys
    * Correctly obtaining the market value after cash transfers
    """
    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)

    # Raising KeyError if portfolio_id not in keys
    with pytest.raises(KeyError):
        sb.get_portfolio_non_cash_equity("5678")

    # Create fund transfers and portfolio
    sb.create_portfolio(portfolio_id=1234, name="My Portfolio #1")
    sb.subscribe_funds_to_account(175000.0)
    sb.subscribe_funds_to_portfolio("1234", 100000.00)

    # Check correct values obtained after cash transfers
    assert sb.get_portfolio_total_equity("1234") == 100000.0
Esempio n. 5
0
def test_submit_order():
    """
    Tests the execute_order method for:
    * Raises ValueError if no portfolio_id
    * Raises ValueError if bid/ask is (np.NaN, np.NaN)
    * Checks that bid/ask are correctly set dependent
    upon order direction
    * Checks that portfolio values are correct after
    carrying out a transaction
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)

    # Raising KeyError if portfolio_id not in keys
    exchange = ExchangeMock()
    data_handler = DataHandlerMock()

    sb = SimulatedBroker(start_dt, exchange, data_handler)
    asset = AssetMock("Royal Dutch Shell Class B", "EQ:RDSB")
    quantity = 100
    order = OrderMock(asset.symbol, quantity)
    with pytest.raises(KeyError):
        sb.submit_order("1234", order)

    # Raises ValueError if bid/ask is (np.NaN, np.NaN)
    exchange_exception = ExchangeMockException()
    sbnp = SimulatedBroker(start_dt, exchange_exception, data_handler)
    sbnp.create_portfolio(portfolio_id=1234, name="My Portfolio #1")
    quantity = 100
    order = OrderMock(asset.symbol, quantity)
    with pytest.raises(ValueError):
        sbnp._execute_order(start_dt, "1234", order)

    # Checks that bid/ask are correctly set dependent on
    # order direction

    # Positive direction
    exchange_price = ExchangeMockPrice()
    data_handler_price = DataHandlerMockPrice()

    sbwp = SimulatedBroker(start_dt, exchange_price, data_handler_price)
    sbwp.create_portfolio(portfolio_id=1234, name="My Portfolio #1")
    sbwp.subscribe_funds_to_account(175000.0)
    sbwp.subscribe_funds_to_portfolio("1234", 100000.00)
    quantity = 1000
    order = OrderMock(asset.symbol, quantity)
    sbwp.submit_order("1234", order)
    sbwp.update(start_dt)

    port = sbwp.portfolios["1234"]
    assert port.total_cash == 46530.0
    assert port.total_non_cash_equity == 53470.0
    assert port.total_equity == 100000.0
    assert port.pos_handler.positions[asset.symbol].book_cost == 53470.0
    assert port.pos_handler.positions[asset.symbol].unrealised_gain == 0.0
    assert port.pos_handler.positions[asset.symbol].market_value == 53470.0
    assert port.pos_handler.positions[asset.symbol].unrealised_percentage_gain == 0.0
    assert port.pos_handler.positions[asset.symbol].quantity == 1000

    # Negative direction
    exchange_price = ExchangeMockPrice()
    sbwp = SimulatedBroker(start_dt, exchange_price, data_handler_price)
    sbwp.create_portfolio(portfolio_id=1234, name="My Portfolio #1")
    sbwp.subscribe_funds_to_account(175000.0)
    sbwp.subscribe_funds_to_portfolio("1234", 100000.00)
    quantity = -1000
    order = OrderMock(asset.symbol, quantity)
    sbwp.submit_order("1234", order)
    sbwp.update(start_dt)

    port = sbwp.portfolios["1234"]
    assert port.total_cash == 153450.0
    assert port.total_non_cash_equity == -53450.0
    assert port.total_equity == 100000.0
    assert port.pos_handler.positions[asset.symbol].book_cost == -53450.0
    assert port.pos_handler.positions[asset.symbol].unrealised_gain == 0.0
    assert port.pos_handler.positions[asset.symbol].market_value == -53450.0
    assert port.pos_handler.positions[asset.symbol].unrealised_percentage_gain == 0.0
    assert port.pos_handler.positions[asset.symbol].quantity == -1000