Example #1
0
def test_update_sets_correct_time():
    """
    Tests that the update method sets the current
    time correctly.
    """
    start_dt = pd.Timestamp('2017-10-05 08:00:00', tz=pytz.UTC)
    new_dt = pd.Timestamp('2017-10-07 08:00:00', tz=pytz.UTC)
    exchange = ExchangeMock()
    data_handler = DataHandlerMock()

    sb = SimulatedBroker(start_dt, exchange, data_handler)
    sb.update(new_dt)
    assert sb.current_dt == new_dt
Example #2
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