Ejemplo n.º 1
0
def test_update_fails_if_price_is_nan_and_position_open():
    c1 = SecurityBase('c1')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1'], data=100)
    data['c1'][dts[1]] = np.nan

    c1.setup(data)

    i = 0
    # mock in position
    c1._position = 100
    c1.update(dts[i], data.ix[dts[i]])

    # test normal case - position & non-nan price
    assert c1._value == 100 * 100

    i = 1
    # this should fail, because we have non-zero position, and price is nan, so
    # bt has no way of updating the _value
    try:
        c1.update(dts[i], data.ix[dts[i]])
        assert False
    except Exception as e:
        assert str(e).startswith('Position is open')

    # on the other hand, if position was 0, this should be fine, and update
    # value to 0
    c1._position = 0
    c1.update(dts[i], data.ix[dts[i]])
    assert c1._value == 0
Ejemplo n.º 2
0
def test_update_fails_if_price_is_nan_and_position_open():
    c1 = SecurityBase('c1')

    dts = pd.date_range('2010-01-01', periods=3)
    data = pd.DataFrame(index=dts, columns=['c1'], data=100)
    data['c1'][dts[1]] = np.nan

    c1.setup(data)

    i = 0
    # mock in position
    c1._position = 100
    c1.update(dts[i], data.ix[dts[i]])

    # test normal case - position & non-nan price
    assert c1._value == 100 * 100

    i = 1
    # this should fail, because we have non-zero position, and price is nan, so
    # bt has no way of updating the _value
    try:
        c1.update(dts[i], data.ix[dts[i]])
        assert False
    except Exception as e:
        assert e.message.startswith('Position is open')

    # on the other hand, if position was 0, this should be fine, and update
    # value to 0
    c1._position = 0
    c1.update(dts[i], data.ix[dts[i]])
    assert c1._value == 0